Exemplo n.º 1
0
int main(int argc, char *argv[])
{
	if(argc < 4) {
		printf("need input\n");
		exit(1);
	}

	FILE *fr = fopen(argv[1], "r");
	FILE *fw = fopen(argv[2], "w");
	char *line = NULL;
	ssize_t read = 0;
	size_t len = 0;
	int hash = atoi(argv[3]);

	unsigned long user_id, pos_id = 0;
	double x = 0, y = 0;
	char time[32] = {0};
	char hash_id[64] = {0};

	array_needsize(false, POOL, id_pool, pool_num, 10240, array_zero_init);

	while((read = getline(&line, &len, fr)) != -1) {
		if(hash == 0) {
			sscanf(line, "%ld %s %lf %lf %ld", &user_id, time, &x, &y, &pos_id);
			if(expect_false(x == 0 || y == 0 || pos_id == 0))
				goto RESET;

			pos_id = id_get(pos_id);
		}
		else {
			sscanf(line, "%ld %s %lf %lf %s", &user_id, time, &x, &y, hash_id);
			if(expect_false(x == 0 || y == 0 || hash_id[0] == 0))
				goto RESET;

			pos_id = id_get(BKDRHash(hash_id));
		}
		
		fprintf(fw, "%ld,%lf,%lf,%s,%ld\r\n", user_id, x, y, time, pos_id);
RESET:
		x = 0;
		y = 0;
		time[0] = 0;
		hash_id[0] = 0;
		pos_id = 0;
	}

	fclose(fr);
	fclose(fw);
	free(id_pool);
	id_pool = NULL;

	return 0;
}
Exemplo n.º 2
0
char* iupClassObjectGetAttributeId(Ihandle* ih, const char* name, int id)
{
  IattribFunc* afunc;

  if (ih->iclass->has_attrib_id==0)
    return NULL;

  if (name[0]==0)
    name = "IDVALUE";  /* pure numbers are used as attributes in IupList and IupMatrix, 
                          translate them into IDVALUE. */
  afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, name);
  if (afunc && afunc->flags & IUPAF_HAS_ID)
  {
    if (afunc->flags & IUPAF_WRITEONLY)
      return NULL;

    if (afunc->get && 
        !(afunc->flags & IUPAF_HAS_ID2) &&
        (ih->handle || afunc->flags & IUPAF_NOT_MAPPED))
    {
      IattribGetIdFunc id_get = (IattribGetIdFunc)afunc->get;
      return id_get(ih, id);
    }
  }

  return NULL;
}
Exemplo n.º 3
0
Arquivo: initrd.c Projeto: borlox/kos
static struct inode *parse_file(struct id_entry *file, void *disk)
{
    dbg_vprintf(DBG_FS, "creating file...\n");
    struct inode *ino = new_inode(file, disk);
    ino->flags = FS_FILE;
    ino->impl  = (dword)id_get(file, content, disk);
    return ino;
}
Exemplo n.º 4
0
Arquivo: initrd.c Projeto: borlox/kos
static struct inode *parse_dir(struct id_entry *dir, void *disk)
{
    if (dir->type != ID_TYPE_DIR) {
        panic("Tried to parse %s as a dir, but it's a file.\n", (char*)id_get(dir, name, disk));
    }

    dbg_vprintf(DBG_FS, "creating dir %s\n", (char*)id_get(dir, name, disk));

    struct inode *ino = new_inode(dir, disk);
    ino->flags = FS_DIR;
    list_t *entries = list_create();
    ino->impl = (dword)entries;

    if (id_get(dir, count, disk) > 0) {
        struct id_entry *entry = id_get(dir, content, disk);

        while (entry) {
            struct inode *e = NULL;
            if (entry->type == ID_TYPE_FILE) {
                e = parse_file(entry, disk);
            }
            else if (entry->type == ID_TYPE_DIR) {
                e = parse_dir(entry, disk);
            }
            else {
                panic("Invalid type: %d\n", entry->type);
            }

            list_add_back(entries, e);

            entry = id_get(entry, next, disk);
        }
    }

    dbg_vprintf(DBG_FS, "dir done!\n");

    return ino;
}
Exemplo n.º 5
0
int buzzvstig_put(buzzvm_t vm) {
   buzzvm_lnum_assert(vm, 2);
   /* Get vstig id */
   id_get();
   /* Get key */
   buzzvm_lload(vm, 1);
   buzzobj_t k = buzzvm_stack_at(vm, 1);
   /* Get value */
   buzzvm_lload(vm, 2);
   buzzobj_t v = buzzvm_stack_at(vm, 1);
   /* Look for virtual stigmergy */
   const buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t);
   if(vs) {
      /* Look for the element */
      const buzzvstig_elem_t* x = buzzvstig_fetch(*vs, &k);
      if(x) {
         /* Element found */
         if(v->o.type != BUZZTYPE_NIL) {
            /* New value is not nil, update the existing element */
            (*x)->data = v;
            ++((*x)->timestamp);
            (*x)->robot = vm->robot;
            /* Append a PUT message to the out message queue */
            buzzoutmsg_queue_append_vstig(vm, BUZZMSG_VSTIG_PUT, id, k, *x);
         }
         else {
            /* New value is nil, must delete the existing element */
            /* Make a new element with nil as value to update neighbors */
            buzzvstig_elem_t y = buzzvstig_elem_new(
               buzzobj_new(BUZZTYPE_NIL), // nil value
               (*x)->timestamp + 1,       // new timestamp
               vm->robot);                // robot id
            /* Append a PUT message to the out message queue with nil in it */
            buzzoutmsg_queue_append_vstig(vm, BUZZMSG_VSTIG_PUT, id, k, y);
            /* Delete the existing element */
            buzzvstig_remove(*vs, &k);
         }
      }
      else if(v->o.type != BUZZTYPE_NIL) {
         /* Element not found and new value is not nil, store it */
         buzzvstig_elem_t y = buzzvstig_elem_new(v, 1, vm->robot);
         buzzvstig_store(*vs, &k, &y);
         /* Append a PUT message to the out message queue */
         buzzoutmsg_queue_append_vstig(vm, BUZZMSG_VSTIG_PUT, id, k, y);
      }
   }
   /* Return */
   return buzzvm_ret0(vm);
}
Exemplo n.º 6
0
int buzzvstig_size(buzzvm_t vm) {
   buzzvm_lnum_assert(vm, 0);
   /* Get vstig id */
   id_get();
   /* Look for virtual stigmergy */
   const buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t);
   if(vs) {
      /* Virtual stigmergy found, return its size */
      buzzvm_pushi(vm, buzzdict_size((*vs)->data));
   }
   else {
      /* Virtual stigmergy not found, return 0 */
      buzzvm_pushi(vm, 0);
   }
   /* Return */
   return buzzvm_ret1(vm);
}
Exemplo n.º 7
0
Arquivo: initrd.c Projeto: borlox/kos
static struct inode *new_inode(struct id_entry *e, void *disk)
{
    struct inode *ino = kmalloc(sizeof(*ino));
    memset(ino, 0, sizeof(*ino));

    char *n = (char*)id_get(e, name, disk);
    if (!n)
        n = "<no name>";
    ino->name = n;
    ino->length = e->count;

    ino->sb = &super;
    ino->ops = &iops;

    dbg_vprintf(DBG_FS, " created new inode: %s\n", n);

    return ino;
}
Exemplo n.º 8
0
int buzzvstig_foreach(struct buzzvm_s* vm) {
   /* Make sure you got one argument */
   buzzvm_lnum_assert(vm, 1);
   /* Get vstig id */
   id_get();
   /* Look for virtual stigmergy */
   const buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t);
   if(vs) {
      /* Virtual stigmergy found */
      /* Get closure */
      buzzvm_lload(vm, 1);
      buzzvm_type_assert(vm, 1, BUZZTYPE_CLOSURE);
      buzzobj_t c = buzzvm_stack_at(vm, 1);
      /* Go through the elements and apply the closure */
      struct buzzvstig_foreach_params p = { .vm = vm, .fun = c };
      buzzdict_foreach((*vs)->data, buzzvstig_foreach_entry, &p);
   }
   /* Return */
   return buzzvm_ret0(vm);
}
Exemplo n.º 9
0
int buzzvstig_get(buzzvm_t vm) {
   buzzvm_lnum_assert(vm, 1);
   /* Get vstig id */
   id_get();
   /* Get key */
   buzzvm_lload(vm, 1);
   buzzobj_t k = buzzvm_stack_at(vm, 1);
   /* Look for virtual stigmergy */
   const buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t);
   if(vs) {
      /* Virtual stigmergy found */
      /* Look for key and push result */
      const buzzvstig_elem_t* e = buzzvstig_fetch(*vs, &k);
      if(e) {
         /* Key found */
         buzzvm_push(vm, (*e)->data);
         /* Append the message to the out message queue */
         buzzoutmsg_queue_append_vstig(vm, BUZZMSG_VSTIG_QUERY, id, k, *e);
      }
      else {
         /* Key not found, make a new one containing nil */
         buzzvm_pushnil(vm);
         buzzvstig_elem_t x =
            buzzvstig_elem_new(buzzvm_stack_at(vm, 1), // nil value
                               0,                      // timestamp
                               vm->robot);             // robot id
         /* Append the message to the out message queue */
         buzzoutmsg_queue_append_vstig(vm, BUZZMSG_VSTIG_QUERY, id, k, x);
         free(x);
      }
   }
   else {
      /* No virtual stigmergy found, just push false */
      /* If this happens, its a bug */
      buzzvm_pushnil(vm);
   }
   /* Return the value found */
   return buzzvm_ret1(vm);
}
Exemplo n.º 10
0
int buzzvstig_onconflictlost(struct buzzvm_s* vm) {
   buzzvm_lnum_assert(vm, 1);
   /* Get vstig id */
   id_get();
   /* Look for virtual stigmergy */
   const buzzvstig_t* vs = buzzdict_get(vm->vstigs, &id, buzzvstig_t);
   if(vs) {
      /* Virtual stigmergy found */
      /* Get closure */
      buzzvm_lload(vm, 1);
      buzzvm_type_assert(vm, 1, BUZZTYPE_CLOSURE);
      /* Clone the closure */
      if((*vs)->onconflictlost) free((*vs)->onconflictlost);
      (*vs)->onconflictlost = buzzheap_clone(vm, buzzvm_stack_at(vm, 1));
   }
   else {
      /* No virtual stigmergy found, just push false */
      /* If this happens, its a bug */
      buzzvm_pushnil(vm);
      fprintf(stderr, "[BUG] [ROBOT %u] Can't find virtual stigmergy %u\n", vm->robot, id);
   }
   /* Return the value found */
   return buzzvm_ret0(vm);
}
Exemplo n.º 11
0
/**
 * Build the flowgraph for the given channel descriptor. A new
 * graph may be built, or an existing flowgraph may be used. A new
 * graph is built per channel unless SSRC multiplexing is in use. 
 * The flow of data through the graph is started, once the graph
 * has been built within the context of this call. The identifier of
 * the flowgraph which contains the channel is returned in *id.
 */
vqec_dp_error_t
vqec_dp_graph_create (vqec_dp_chan_desc_t *chan,
                      vqec_dp_tunerid_t tid, 
                      vqec_dp_encap_type_t tuner_encap, 
                      vqec_dp_graphinfo_t *graphinfo)
{
    vqec_dp_graph_t *graph;
    vqec_dp_graphid_t id;
    vqec_dp_error_t ret = VQEC_DP_ERR_OK;

    if (!chan) {
        return VQEC_DP_ERR_INVALIDARGS;
    }

    /* if the graph doesn't already exist, create a new one */
    graph = (vqec_dp_graph_t *) zone_acquire(s_vqec_graph_pool);
    if (!graph || (graph_index == s_graphs_max)) {
        id = VQEC_DP_GRAPHID_INVALID;
        ret = VQEC_DP_ERR_NOMEM;
        goto bail;
    }
    s_graph_cache[graph_index] = graph;
    graph_index++;
    memset(graph, 0, sizeof(vqec_dp_graph_t));

    /* 
     * Register graph in id_mgr - must have a channel id at time of
     * channel init.
     */ 
    id = id_get(graph, vqec_dp_graph_id_table_key);
    if (id == VQEC_DP_GRAPHID_INVALID) {
        ret = VQEC_DP_ERR_NOMEM;
        goto bail;
    }
    graph->id = id;

    /* initialize graph and build all components */
    if (vqec_dp_graph_init(graph, chan, graphinfo) != VQEC_DP_ERR_OK) {
        VQEC_DP_SYSLOG_PRINT(ERROR, "graph initialization failed");
        ret = VQEC_DP_ERR_NOMEM;
        goto bail;
    }

    /* add_tuner() to the graph */
    if (vqec_dp_graph_add_tuner(graph->id, tid) != VQEC_DP_ERR_OK) {
        VQEC_DP_SYSLOG_PRINT(ERROR, "graph add_tuner failed");
        ret = VQEC_DP_ERR_NOMEM;
        goto bail;
    }

    /* cache channel info inside the tuner */
    vqec_dp_output_shim_tuner_cache_channel(tid, chan);

    /* make IS-OS connections */
    if (vqec_dp_graph_connect(graph, chan->fallback) != VQEC_DP_ERR_OK) {
        VQEC_DP_SYSLOG_PRINT(ERROR, "graph connection failed");
        ret = VQEC_DP_ERR_NOMEM;
        goto bail;
    }

    return VQEC_DP_ERR_OK;

bail:
    if (id != VQEC_DP_GRAPHID_INVALID) {
        vqec_dp_graph_destroy(id);
        id = VQEC_DP_GRAPHID_INVALID;
    } else if (graph) {
        zone_release(s_vqec_graph_pool, graph);
        s_graph_cache[graph_index] = NULL;
        graph_index--;
    }

    return ret;
}
Exemplo n.º 12
0
char* iupClassObjectGetAttribute(Ihandle* ih, const char* name, char* *def_value, int *inherit)
{
  IattribFunc* afunc;

  if (ih->iclass->has_attrib_id!=0)
  {
    const char* name_id = iClassFindId(name);
    if (name_id)
    {
      const char* partial_name = iClassCutNameId(name, name_id);
      if (!partial_name)
        partial_name = "IDVALUE";  /* pure numbers are used as attributes in IupList and IupMatrix, 
                                      translate them into IDVALUE. */
      afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, partial_name);
      if (afunc && afunc->flags & IUPAF_HAS_ID)
      {
        *def_value = NULL;  /* id numbered attributes have default value NULL always */
        *inherit = 0;       /* id numbered attributes are NON inheritable always */

        if (afunc->flags & IUPAF_WRITEONLY)
          return NULL;

        if (afunc->get && (ih->handle || afunc->flags & IUPAF_NOT_MAPPED))
        {
          if (afunc->flags & IUPAF_HAS_ID2)
          {
            IattribGetId2Func id2_get = (IattribGetId2Func)afunc->get;
            int id1=IUP_INVALID_ID, id2=IUP_INVALID_ID;
            iupStrToIntInt(name_id, &id1, &id2, ':');
            return id2_get(ih, id1, id2);
          }
          else
          {
            IattribGetIdFunc id_get = (IattribGetIdFunc)afunc->get;
            int id=IUP_INVALID_ID;
            if (iupStrToInt(name_id, &id))
              return id_get(ih, id);
          }
        }
        else
          return NULL;      /* if the function exists, then must return here */
      }
    }
  }

  /* if not has_attrib_id, or not found an ID, or not found the partial name, check using the full name */

  afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, name);
  *def_value = NULL;
  *inherit = 1; /* default is inheritable */
  if (afunc)
  {
    *def_value = iClassGetDefaultValue(afunc);
    *inherit = !(afunc->flags & IUPAF_NO_INHERIT) &&   /* is inheritable */
               !(afunc->flags & IUPAF_NO_STRING);      /* is a string */

    if (afunc->flags & IUPAF_WRITEONLY)
      return NULL;

    if (afunc->get && (ih->handle || afunc->flags & IUPAF_NOT_MAPPED))
    {
      if (afunc->flags & IUPAF_HAS_ID2)
      {
        IattribGetId2Func id2_get = (IattribGetId2Func)afunc->get;
        return id2_get(ih, IUP_INVALID_ID, IUP_INVALID_ID);  /* empty Id */
      }
      else if (afunc->flags & IUPAF_HAS_ID)
      {
        IattribGetIdFunc id_get = (IattribGetIdFunc)afunc->get;
        return id_get(ih, IUP_INVALID_ID);  /* empty Id */
      }
      else
        return afunc->get(ih);
    }
  }
  return NULL;
}
Exemplo n.º 13
0
int csp_can_tx(csp_iface_t * interface, csp_packet_t *packet, uint32_t timeout) {

	uint8_t bytes, overhead, avail, dest;
	uint8_t frame_buf[8];

	/* Get CFP identification number */
	int ident = id_get();
	if (ident < 0) {
		csp_log_warn("Failed to get CFP identification number\r\n");
		return CSP_ERR_INVAL;
	}

	/* Calculate overhead */
	overhead = sizeof(csp_id_t) + sizeof(uint16_t);

	/* Insert destination node mac address into the CFP destination field */
	dest = csp_route_get_nexthop_mac(packet->id.dst);
	if (dest == CSP_NODE_MAC)
		dest = packet->id.dst;

	/* Create CAN identifier */
	can_id_t id = 0;
	id |= CFP_MAKE_SRC(packet->id.src);
	id |= CFP_MAKE_DST(dest);
	id |= CFP_MAKE_ID(ident);
	id |= CFP_MAKE_TYPE(CFP_BEGIN);
	id |= CFP_MAKE_REMAIN((packet->length + overhead - 1) / 8);

	/* Get packet buffer */
	pbuf_element_t *buf = pbuf_new(id, NULL);

	if (buf == NULL) {
		csp_log_warn("Failed to get packet buffer for CAN\r\n");
		return CSP_ERR_NOMEM;
	}

	/* Set packet */
	buf->packet = packet;

	/* Calculate first frame data bytes */
	avail = 8 - overhead;
	bytes = (packet->length <= avail) ? packet->length : avail;

	/* Copy CSP headers and data */
	uint32_t csp_id_be = csp_hton32(packet->id.ext);
	uint16_t csp_length_be = csp_hton16(packet->length);

	memcpy(frame_buf, &csp_id_be, sizeof(csp_id_be));
	memcpy(frame_buf + sizeof(csp_id_be), &csp_length_be, sizeof(csp_length_be));
	memcpy(frame_buf + overhead, packet->data, bytes);

	/* Increment tx counter */
	buf->tx_count += bytes;

	/* Take semaphore so driver can post it later */
	csp_bin_sem_wait(&buf->tx_sem, 0);

	/* Send frame */
	if (can_send(id, frame_buf, overhead + bytes, NULL) != 0) {
		csp_log_info("Failed to send CAN frame in csp_tx_can\r\n");
		return CSP_ERR_DRIVER;
	}

	/* Non blocking mode */
	if (timeout == 0)
		return CSP_ERR_NONE;

	/* Blocking mode */
	if (csp_bin_sem_wait(&buf->tx_sem, timeout) != CSP_SEMAPHORE_OK) {
		csp_bin_sem_post(&buf->tx_sem);
		return CSP_ERR_TIMEDOUT;
	} else {
		csp_bin_sem_post(&buf->tx_sem);
		return CSP_ERR_NONE;
	}

}
Exemplo n.º 14
0
int csp_can_tx(csp_iface_t * interface, csp_packet_t *packet, uint32_t timeout) {

	uint8_t bytes, overhead, avail, dest;
	uint8_t frame_buf[8];

	/* Get CFP identification number */
	int ident = id_get();
	if (ident < 0) {
		csp_log_warn("Failed to get CFP identification number");
		return CSP_ERR_INVAL;
	}
	
	/* Calculate overhead */
	overhead = sizeof(csp_id_t) + sizeof(uint16_t);

	/* Insert destination node mac address into the CFP destination field */
	dest = csp_rtable_find_mac(packet->id.dst);
	if (dest == CSP_NODE_MAC)
		dest = packet->id.dst;

	/* Create CAN identifier */
	can_id_t id = 0;
	id |= CFP_MAKE_SRC(packet->id.src);
	id |= CFP_MAKE_DST(dest);
	id |= CFP_MAKE_ID(ident);
	id |= CFP_MAKE_TYPE(CFP_BEGIN);
	id |= CFP_MAKE_REMAIN((packet->length + overhead - 1) / 8);

	/* Get packet buffer */
	pbuf_element_t *buf = pbuf_new(id, NULL);

	if (buf == NULL) {
		csp_log_warn("Failed to get packet buffer for CAN");
		return CSP_ERR_NOMEM;
	}

	/* Set packet */
	buf->packet = packet;

	/* Calculate first frame data bytes */
	avail = 8 - overhead;
	bytes = (packet->length <= avail) ? packet->length : avail;

	/* Copy CSP headers and data */
	uint32_t csp_id_be = csp_hton32(packet->id.ext);
	uint16_t csp_length_be = csp_hton16(packet->length);

	memcpy(frame_buf, &csp_id_be, sizeof(csp_id_be));
	memcpy(frame_buf + sizeof(csp_id_be), &csp_length_be, sizeof(csp_length_be));
	memcpy(frame_buf + overhead, packet->data, bytes);

	/* Increment tx counter */
	buf->tx_count += bytes;

	/* Take semaphore so driver can post it later */
	if (csp_bin_sem_wait(&buf->tx_sem, 0) != CSP_SEMAPHORE_OK) {
		csp_log_error("Failed to take CAN pbuf TX sem!");
		pbuf_free(buf, NULL, false);
		return CSP_ERR_DRIVER;
	}

	/* Send frame. We must free packet buffer is this fails,
	 * but the packet itself should be freed by the caller */
	if (can_send(id, frame_buf, overhead + bytes, NULL) != 0) {
		csp_log_warn("Failed to send CAN frame in csp_tx_can");
		csp_bin_sem_post(&buf->tx_sem);
		pbuf_free(buf, NULL, false);
		return CSP_ERR_DRIVER;
	}

	/* NOTE: The transmit packet is now owned by the transmission MOB and
	 * must NOT be freed by the calling thread. */

	/* Non blocking mode */
	if (timeout == 0)
		return CSP_ERR_NONE;

	/* Blocking mode */
	if (csp_bin_sem_wait(&buf->tx_sem, timeout) != CSP_SEMAPHORE_OK) {
		/* tx_sem is posted by transmission callback. The packet
		 * could still be in use by the transmission MOB, so
		 * we can not return CSP_ERR_TIMEOUT and risk that the
		 * calling thread frees the packet. */
		return CSP_ERR_NONE;
	} else {
		csp_bin_sem_post(&buf->tx_sem);
		return CSP_ERR_NONE;
	}

}