Exemplo n.º 1
0
static int translate_configs_(yajl_val js_configs)
{
  yajl_val js_connect_timeout = wsn_yajl_tree_get(js_configs, "connect-timeout", yajl_t_number);
  if (js_connect_timeout) {
    all_configs_.connect_timeout = YAJL_GET_INTEGER(js_connect_timeout);
  }
  yajl_val js_idle_timeout = wsn_yajl_tree_get(js_configs, "idle-timeout", yajl_t_number);
  if (js_idle_timeout) {
    all_configs_.idle_timeout = YAJL_GET_INTEGER(js_idle_timeout);
  }
  all_configs_.servers_conf = wsn_nodes_conf_find(WSN_NODE_TYPE_SERVER, 
                                                  &all_configs_.server_count, js_configs);
  if (all_configs_.servers_conf == NULL && wsn_last_err() != WSN_ERR_MISSING_CONF_ITEM) {
    return wsn_last_err();
  }
  all_configs_.clients_conf = wsn_nodes_conf_find(WSN_NODE_TYPE_CLIENT,
                                                  &all_configs_.client_count, js_configs);
  if (all_configs_.servers_conf == NULL && wsn_last_err() != WSN_ERR_MISSING_CONF_ITEM) {
    return wsn_last_err();
  }
  if (all_configs_.servers_conf == NULL && all_configs_.clients_conf == NULL) {
    wsn_report_err(WSN_ERR_MISSING_CONF_ITEM,
                   "Invalid config file, no node config was found");
    return WSN_ERR_MISSING_CONF_ITEM;
  }
  return 0;
}
Exemplo n.º 2
0
void FontLoadFromJSON(Font *f, const char *imgPath, const char *jsonPath)
{
	yajl_val node = YAJLReadFile(jsonPath);
	if (node == NULL)
	{
		fprintf(stderr, "Error parsing font JSON '%s'\n", jsonPath);
		goto bail;
	}

	memset(f, 0, sizeof *f);
	// Load definitions from JSON data
	YAJLVec2i(&f->Size, node, "Size");
	YAJLInt(&f->Stride, node, "Stride");

	// Padding order is: left/top/right/bottom
	const yajl_val paddingNode = YAJLFindNode(node, "Padding");
	f->Padding.Left = (int)YAJL_GET_INTEGER(YAJL_GET_ARRAY(paddingNode)->values[0]);
	f->Padding.Top = (int)YAJL_GET_INTEGER(YAJL_GET_ARRAY(paddingNode)->values[1]);
	f->Padding.Right = (int)YAJL_GET_INTEGER(YAJL_GET_ARRAY(paddingNode)->values[2]);
	f->Padding.Bottom = (int)YAJL_GET_INTEGER(YAJL_GET_ARRAY(paddingNode)->values[3]);

	YAJLVec2i(&f->Gap, node, "Gap");
	bool proportional = false;
	YAJLBool(&proportional, node, "Proportional");

	FontLoad(f, imgPath, proportional);

bail:
	yajl_tree_free(node);
}
Exemplo n.º 3
0
void YAJLVec2i(Vec2i *value, yajl_val node, const char *name)
{
	if (!YAJLTryLoadValue(&node, name) || !YAJL_IS_ARRAY(node))
	{
		return;
	}
	value->x = (int)YAJL_GET_INTEGER(YAJL_GET_ARRAY(node)->values[0]);
	value->y = (int)YAJL_GET_INTEGER(YAJL_GET_ARRAY(node)->values[1]);
}
Exemplo n.º 4
0
const webhdfs_fstat_t *webhdfs_dir_read (webhdfs_dir_t *dir) {
    const char *path[] = {"pathSuffix", NULL};
    const char *replication[] = {"replication", NULL};
    const char *permission[] = {"permission", NULL};
    const char *length[] = {"length", NULL};
    const char *group[] = {"group", NULL};
    const char *owner[] = {"owner", NULL};
    const char *type[] = {"type", NULL};
    const char *mtime[] = {"modificationTime", NULL};
    const char *block[] = {"blockSize", NULL};
    const char *atime[] = {"accessTime", NULL};
    yajl_val node, v;

    if (dir->current >= YAJL_GET_ARRAY(dir->statuses)->len)
        return(NULL);

    node = YAJL_GET_ARRAY(dir->statuses)->values[dir->current];
    dir->current++;

    if ((v = yajl_tree_get(node, atime, yajl_t_number)))
        dir->stat.atime = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, mtime, yajl_t_number)))
        dir->stat.mtime = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, length, yajl_t_number)))
        dir->stat.length = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, block, yajl_t_number)))
        dir->stat.block = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, replication, yajl_t_number)))
        dir->stat.replication = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, permission, yajl_t_string)))
        dir->stat.permission = strtol(YAJL_GET_STRING(v), NULL, 8);

    if ((v = yajl_tree_get(node, path, yajl_t_string)))
        dir->stat.path = YAJL_GET_STRING(v);

    if ((v = yajl_tree_get(node, group, yajl_t_string)))
        dir->stat.group = YAJL_GET_STRING(v);

    if ((v = yajl_tree_get(node, owner, yajl_t_string)))
        dir->stat.owner = YAJL_GET_STRING(v);

    if ((v = yajl_tree_get(node, type, yajl_t_string)))
        dir->stat.type = YAJL_GET_STRING(v);

    return(&(dir->stat));
}
Exemplo n.º 5
0
static rc_ReturnCode_t send_reg_payload(const char *payload, size_t payloadLen, int *regId)
{
  rc_ReturnCode_t res;
  char *rpayload = NULL, *respPayload = NULL;
  uint32_t respPayloadLen;
  yajl_val yval = NULL;

  res = emp_send_and_wait_response(EMP_REGISTERSMSLISTENER, 0, payload, payloadLen, &respPayload, &respPayloadLen);

  if (res != RC_OK)
    goto quit;

  rpayload = strndup(respPayload, respPayloadLen);
  if (rpayload == NULL)
  {
    res = RC_NO_MEMORY;
    goto quit;
  }

  YAJL_TREE_PARSE(yval, rpayload);
  if (yval->type != yajl_t_number)
  {
    SWI_LOG("SMS", ERROR, "Invalid regId type received from RA, got type=%u, expected %u\n", __FUNCTION__, yval->type, yajl_t_number);
    res = RC_BAD_PARAMETER;
    goto quit;
  }
  *regId = YAJL_GET_INTEGER(yval);
quit:
  yajl_tree_free(yval);
  free(respPayload);
  free(rpayload);
  return res;
}
Exemplo n.º 6
0
void YAJLInt(int *value, yajl_val node, const char *name)
{
	if (!YAJLTryLoadValue(&node, name) || !YAJL_IS_INTEGER(node))
	{
		return;
	}
	*value = (int)YAJL_GET_INTEGER(node);
}
Exemplo n.º 7
0
yajl_gen_status GenVal(yajl_gen g, yajl_val v) {
	yajl_gen_status status;
	switch (v->type) {
	case yajl_t_string:	return yajl_gen_string(g, (unsigned char*)v->u.string, strlen(v->u.string));

	case yajl_t_number: 
		{
			char buffer[100];
			char *num = buffer;
			size_t len;
			//if (YAJL_IS_INTEGER(v)) // buggy
			if (v->u.number.flags & YAJL_NUMBER_INT_VALID)
				len = sprintf(num, "%lld", YAJL_GET_INTEGER(v));
			//else if (YAJL_IS_DOUBLE(v))	// buggy
			else if (v->u.number.flags & YAJL_NUMBER_DOUBLE_VALID)
				len = sprintf(num, "%g", YAJL_GET_DOUBLE(v));
			else {
				num = YAJL_GET_NUMBER(v);
				len = strlen(buffer);
			}
			return yajl_gen_number(g, num, len);
		}

	case yajl_t_object:
		status = yajl_gen_map_open(g);
		if (status != yajl_gen_status_ok)
			return status;
		
		for (size_t i = 0; i < v->u.object.len; i++) {
			status = yajl_gen_string(g, (unsigned char *)v->u.object.keys[i], strlen(v->u.object.keys[i]));
			if (status != yajl_gen_status_ok)
				return status;
			status = GenVal(g, v->u.object.values[i]);
			if (status != yajl_gen_status_ok)
				return status;
		}
		return yajl_gen_map_close(g);

	case yajl_t_array:
		status = yajl_gen_array_open(g);
		if (status != yajl_gen_status_ok)
			return status;
		
		for (size_t i = 0; i < v->u.array.len; i++) {
			status = GenVal(g, v->u.array.values[i]);
			if (status != yajl_gen_status_ok)
				return status;
		}

		return yajl_gen_array_close(g);

	case yajl_t_true: return yajl_gen_bool(g, 1);
	case yajl_t_false: return yajl_gen_bool(g, 0);
	case yajl_t_null: return yajl_gen_null(g);
	case yajl_t_any: break;
	}
	return yajl_gen_in_error_state;
}
Exemplo n.º 8
0
static rc_ReturnCode_t newSmsHdlr(uint32_t payloadsize, char* payload)
{
  int regId;
  rc_ReturnCode_t res = RC_OK;
  cb_list_t *entry;
  char *sender, *message, *jsonPayload;
  yajl_val yval;

  jsonPayload = malloc(payloadsize + 1);
  memcpy(jsonPayload, payload, payloadsize);
  jsonPayload[payloadsize] = '\0';

  YAJL_TREE_PARSE(yval, jsonPayload);
  if (yval->u.array.values[0]->type != yajl_t_string)
  {
    SWI_LOG("SMS", ERROR, "%s: sender is not an string, got type=%d\n", __FUNCTION__, yval->u.array.values[0]->type);
    res = RC_BAD_PARAMETER;
    goto quit;
  }
  sender = YAJL_GET_STRING(yval->u.array.values[0]);

  if (yval->u.array.values[1]->type != yajl_t_string)
  {
    SWI_LOG("SMS", ERROR, "%s: message is not an string, got type=%d\n", __FUNCTION__, yval->u.array.values[1]->type);
    res = RC_BAD_PARAMETER;
    goto quit;
  }
  message = YAJL_GET_STRING(yval->u.array.values[1]);

  if (yval->u.array.values[2]->type != yajl_t_number)
  {
    SWI_LOG("SMS", ERROR, "%s: regId is not an number, got type=%d\n", __FUNCTION__, yval->u.array.values[2]->type);
    res = RC_BAD_PARAMETER;
    goto quit;
  }
  regId = YAJL_GET_INTEGER(yval->u.array.values[2]);

  pthread_mutex_lock(&handler_lock);
  for (entry = cb_list; entry; entry = entry->next)
  {
    if (entry->regId == regId)
    {
      entry->cb(sender, message);
      break;
    }
  }
  pthread_mutex_unlock(&handler_lock);
quit:
  free(jsonPayload);
  emp_freemessage(payload);
  yajl_tree_free(yval);

  return res;
}
Exemplo n.º 9
0
/* Add YAJL value into YAJL generator handle (JSON object)
 *
 * jgen - YAJL generator handle allocated by yajl_gen_alloc()
 * jval - YAJL value usually returned by yajl_tree_get()
 */
static yajl_gen_status ovs_yajl_gen_val(yajl_gen jgen, yajl_val jval) {
  size_t array_len = 0;
  yajl_val *jvalues = NULL;
  yajl_val jobj_value = NULL;
  const char *obj_key = NULL;
  size_t obj_len = 0;
  yajl_gen_status yajl_gen_ret = yajl_gen_status_ok;

  if (jval == NULL)
    return yajl_gen_generation_complete;

  if (YAJL_IS_STRING(jval))
    OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, YAJL_GET_STRING(jval));
  else if (YAJL_IS_DOUBLE(jval))
    OVS_YAJL_CALL(yajl_gen_double, jgen, YAJL_GET_DOUBLE(jval));
  else if (YAJL_IS_INTEGER(jval))
    OVS_YAJL_CALL(yajl_gen_double, jgen, YAJL_GET_INTEGER(jval));
  else if (YAJL_IS_TRUE(jval))
    OVS_YAJL_CALL(yajl_gen_bool, jgen, 1);
  else if (YAJL_IS_FALSE(jval))
    OVS_YAJL_CALL(yajl_gen_bool, jgen, 0);
  else if (YAJL_IS_NULL(jval))
    OVS_YAJL_CALL(yajl_gen_null, jgen);
  else if (YAJL_IS_ARRAY(jval)) {
    /* create new array and add all elements into the array */
    array_len = YAJL_GET_ARRAY(jval)->len;
    jvalues = YAJL_GET_ARRAY(jval)->values;
    OVS_YAJL_CALL(yajl_gen_array_open, jgen);
    for (size_t i = 0; i < array_len; i++)
      OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jvalues[i]);
    OVS_YAJL_CALL(yajl_gen_array_close, jgen);
  } else if (YAJL_IS_OBJECT(jval)) {
    /* create new object and add all elements into the object */
    OVS_YAJL_CALL(yajl_gen_map_open, jgen);
    obj_len = YAJL_GET_OBJECT(jval)->len;
    for (size_t i = 0; i < obj_len; i++) {
      obj_key = YAJL_GET_OBJECT(jval)->keys[i];
      jobj_value = YAJL_GET_OBJECT(jval)->values[i];
      OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, obj_key);
      OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jobj_value);
    }
    OVS_YAJL_CALL(yajl_gen_map_close, jgen);
  } else {
    OVS_ERROR("%s() unsupported value type %d (skip)", __FUNCTION__,
              (int)(jval)->type);
    goto yajl_gen_failure;
  }
  return yajl_gen_status_ok;

yajl_gen_failure:
  OVS_ERROR("%s() error to generate value", __FUNCTION__);
  return yajl_gen_ret;
}
Exemplo n.º 10
0
void bson_from_json_type(bson *b, yajl_val value, const char* key)
{
    if( YAJL_IS_STRING( value ) )
    {
        char* string = YAJL_GET_STRING( value );
        bson_append_string( b, key, string );
    }
    else if( YAJL_IS_NUMBER( value ) )
    {
        char* string = value->u.number.r;
        size_t len = strlen( string );

        // Hack to detect a double, since 'flags' is always set to double.
        if( memchr( string, '.', len ) ||
                memchr( string, 'e', len ) ||
                memchr( string, 'E', len ) )
        {
            double number = YAJL_GET_DOUBLE( value );
            bson_append_double( b, key, number );
        }
        else
        {
            uint64_t number = YAJL_GET_INTEGER( value );

            if( number <= INT_MIN && number <= INT_MAX)
                bson_append_int( b, key, (int)number );
            else
                bson_append_long( b, key, number);
        }
    }
    else if ( YAJL_IS_FALSE( value ) )
    {
        bson_append_bool( b, key, 0 );
    }
    else if ( YAJL_IS_TRUE( value ) )
    {
        bson_append_bool( b, key, 1 );
    }
    else if ( YAJL_IS_ARRAY( value ) )
    {
        bson_append_start_array( b, key );
        bson_from_json_array( b, value );
        bson_append_finish_array( b );
    }
    else if ( YAJL_IS_OBJECT( value ) )
    {
        bson_append_start_object( b, key );
        bson_from_json_object( b, value );
        bson_append_finish_object( b );
    }
}
Exemplo n.º 11
0
SEXP ParseValue(yajl_val node, int bigint){
  if(YAJL_IS_NULL(node)){
    return R_NilValue;
  }
  if(YAJL_IS_STRING(node)){
    SEXP tmp = PROTECT(allocVector(STRSXP, 1));
    SET_STRING_ELT(tmp, 0, mkCharCE(YAJL_GET_STRING(node),  CE_UTF8));
    UNPROTECT(1);
    return tmp;
  }
  if(YAJL_IS_INTEGER(node)){
    long long int val = YAJL_GET_INTEGER(node);
    /* 2^53 is highest int stored as double without loss */
    if(bigint && (val > 9007199254740992 || val < -9007199254740992)){
      char buf[32];
      #ifdef _WIN32
      snprintf(buf, 32, "%I64d", val);
      #else
      snprintf(buf, 32, "%lld", val);
      #endif
      return mkString(buf);
    /* see .Machine$integer.max in R */
    } else if(val > 2147483647 || val < -2147483647){
      return ScalarReal(val);
    } else {
      return ScalarInteger(val);
    }
  }
  if(YAJL_IS_DOUBLE(node)){
    return(ScalarReal(YAJL_GET_DOUBLE(node)));
  }
  if(YAJL_IS_NUMBER(node)){
    /* A number that is not int or double (very rare) */
    /* This seems to correctly round to Inf/0/-Inf */
    return(ScalarReal(YAJL_GET_DOUBLE(node)));
  }
  if(YAJL_IS_TRUE(node)){
    return(ScalarLogical(1));
  }
  if(YAJL_IS_FALSE(node)){
    return(ScalarLogical(0));
  }
  if(YAJL_IS_OBJECT(node)){
    return(ParseObject(node, bigint));
  }
  if(YAJL_IS_ARRAY(node)){
    return(ParseArray(node, bigint));
  }
  error("Invalid YAJL node type.");
}
Exemplo n.º 12
0
bool
JSONParser::find(const std::string& key, int& value)
{
  VALIDATE_STATE();
  bool result = false;

  if (mState->mTree) {
    const char* path[2] = { key.c_str(), NULL };
    yajl_val num = yajl_tree_get(mState->mTree, path, yajl_t_number);
    if (YAJL_IS_INTEGER(num)) {
      result = true;
      value = (int)YAJL_GET_INTEGER(num);
    }
  }
  return result;
}
Exemplo n.º 13
0
void message_json_handler_function(yajl_val j, void *context) {
    state *s = context;

    // LLog("[message_json_handler_function] will read value type\n");
    // LLog("[message_json_handler_function] value is %d\n", (unsigned int)j);

    // LLog("[message_json_handler_function] value type is %d\n", j->type);

    if (!YAJL_IS_OBJECT(j)) {
        LLog("[message_json_handler_function] value was not an object, bailing out\n");
        return;
    }

    int num_vals = YAJL_GET_OBJECT(j)->len;
    for (int i = 0; i < num_vals; i++) 
    {
        const char *name = YAJL_GET_OBJECT(j)->keys[i];
        //LLog("[message_json_handler_function] did get name = %s\n", name);

        yajl_val value = YAJL_GET_OBJECT(j)->values[i];

        if (YAJL_IS_INTEGER(value)) {
            //LLog("[message_json_handler_function] value is integer\n");
            state_set_int(s, name, YAJL_GET_INTEGER(value));
        }
        else if (YAJL_IS_DOUBLE(value)) {
            //LLog("[message_json_handler_function] value is double\n");
            state_set_float(s, name, (float)YAJL_GET_DOUBLE(value));
        }
        else if (YAJL_IS_TRUE(value)) {
            //LLog("[message_json_handler_function] value is true\n");
            state_set_int(s, name, 1);
        }
        else if (YAJL_IS_FALSE(value)) {
            //LLog("[message_json_handler_function] value is false\n");
            state_set_int(s, name, 0);
        }
        else {
            //LLog("[message_json_handler_function] value is complex\n");
            state_set_json(s, name, value);
        }
    }
}
Exemplo n.º 14
0
webhdfs_fstat_t *webhdfs_stat (webhdfs_t *fs,
                               const char *path,
                               char **error) {
    const char *pathSuffix[] = {"pathSuffix", NULL};
    const char *replication[] = {"replication", NULL};
    const char *permission[] = {"permission", NULL};
    const char *length[] = {"length", NULL};
    const char *group[] = {"group", NULL};
    const char *owner[] = {"owner", NULL};
    const char *type[] = {"type", NULL};
    const char *mtime[] = {"modificationTime", NULL};
    const char *block[] = {"blockSize", NULL};
    const char *atime[] = {"accessTime", NULL};
    yajl_val root, node, v;
    webhdfs_fstat_t *stat;
    webhdfs_req_t req;

    webhdfs_req_open(&req, fs, path);
    webhdfs_req_set_args(&req, "op=GETFILESTATUS");
    char *error2 = NULL;
    int ret = -1;
    if ((ret = webhdfs_req_exec(&req, WEBHDFS_REQ_GET, &error2)) != 0) {
        *error = __strdup(error2);
        free(error2);
        webhdfs_req_close(&req);
        return(NULL);
    }
    root = webhdfs_req_json_response(&req);
    webhdfs_req_close(&req);

    if ((v = webhdfs_response_exception(root)) != NULL) {
//        const char *exceptionNode[] = {"exception", NULL};
//        yajl_val exception = yajl_tree_get(v, exceptionNode , yajl_t_string);
//        YAJL_GET_STRING(exception);
        const char *messageNode[] = {"message", NULL};
        yajl_val message = yajl_tree_get(v, messageNode, yajl_t_string);
        *error = __strdup(YAJL_GET_STRING(message));

        yajl_tree_free(root);
        return(NULL);
    }

    if ((node = webhdfs_response_file_status(root)) == NULL) {
        yajl_tree_free(root);
        return(NULL);
    }

    if ((stat = (webhdfs_fstat_t *) malloc(sizeof(webhdfs_fstat_t))) == NULL) {
        yajl_tree_free(root);
        return(NULL);
    }

    memset(stat, 0, sizeof(webhdfs_fstat_t));

    if ((v = yajl_tree_get(node, atime, yajl_t_number)))
        stat->atime = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, mtime, yajl_t_number)))
        stat->mtime = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, length, yajl_t_number)))
        stat->length = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, block, yajl_t_number)))
        stat->block = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, replication, yajl_t_number)))
        stat->replication = YAJL_GET_INTEGER(v);

    if ((v = yajl_tree_get(node, permission, yajl_t_string)))
        stat->permission = strtol(YAJL_GET_STRING(v), NULL, 8);

    if ((v = yajl_tree_get(node, pathSuffix, yajl_t_string)))
        stat->path = __strdup(YAJL_GET_STRING(v));

    if ((v = yajl_tree_get(node, group, yajl_t_string)))
        stat->group = __strdup(YAJL_GET_STRING(v));

    if ((v = yajl_tree_get(node, owner, yajl_t_string)))
        stat->owner = __strdup(YAJL_GET_STRING(v));

    if ((v = yajl_tree_get(node, type, yajl_t_string)))
        stat->type = __strdup(YAJL_GET_STRING(v));

    yajl_tree_free(root);
    return(stat);
}
Exemplo n.º 15
0
Map * LoadMap(char * map_info){

	yajl_val node;
	char errbuf[1024];
	short x,y;
	short i;

	Map * map = (Map *) malloc(sizeof(Map));


	//logger("Parsing packet from JSON message");

	node = yajl_tree_parse(( char *) map_info, errbuf, sizeof(errbuf));

	 if (node == NULL) {
	        fprintf(stderr,"parse_error: ");
	        if (strlen(errbuf)){
	        	fprintf(stderr," %s", errbuf);
	        }else{
	        	fprintf(stderr,"unknown error");
			}
	        fprintf(stderr,"\n");
	        return false;
	    }

	 const char * path[] = { "map_id", ( char *) 0 };

	 map->area_id = YAJL_GET_INTEGER(yajl_tree_get(node, path, yajl_t_number));

	 path[0] = "cell_size";
	 map->cell_size = YAJL_GET_INTEGER(yajl_tree_get(node, path, yajl_t_number));

	 path[0] = "x_cells";
	 map->x_cells = YAJL_GET_INTEGER(yajl_tree_get(node, path, yajl_t_number));

	 path[0] = "y_cells";
	 map->y_cells = YAJL_GET_INTEGER(yajl_tree_get(node, path, yajl_t_number));

	 map->cells = (Cell **) malloc(map->x_cells * sizeof(Cell *));
	 for (x = 0; x < map->x_cells; x++){
		 map->cells[x] = (Cell *) malloc(map->y_cells * sizeof(Cell));
	 }

	 path[0] = "cells";

	 for (x = 0; x < map->x_cells; x++)
		 for (y = 0; y < map->y_cells; y++){

			 map->cells[x][y].prob_location = 1;
			 map->cells[x][y].transition_cell = true;

			 yajl_val cell = YAJL_GET_ARRAY(YAJL_GET_ARRAY((yajl_tree_get(node,path, yajl_t_array)))->values[x])->values[y];

			 if (YAJL_IS_NUMBER(cell)){
				 map->cells[x][y].prob_location = YAJL_GET_INTEGER(YAJL_GET_ARRAY(YAJL_GET_ARRAY((yajl_tree_get(node,path, yajl_t_array)))->values[x])->values[y]);
			 	 map->cells[x][y].transition_cell = false;
			 }else if (YAJL_IS_NULL(cell)){
				 map->cells[x][y].transp.area_id = 0;
				 map->cells[x][y].transp.x_cell = 0;
				 map->cells[x][y].transp.y_cell = 0;
			 }else{
				 for (i = 0; i < YAJL_GET_OBJECT(cell)->len; i++){
					 if (!strcmp(YAJL_GET_OBJECT(cell)->keys[i], "area_id"))
						 map->cells[x][y].transp.area_id = YAJL_GET_INTEGER(YAJL_GET_OBJECT(cell)->values[i]);
					 else if (!strcmp(YAJL_GET_OBJECT(cell)->keys[i], "x_cell"))
						 map->cells[x][y].transp.x_cell = YAJL_GET_INTEGER(YAJL_GET_OBJECT(cell)->values[i]);
					 else if (!strcmp(YAJL_GET_OBJECT(cell)->keys[i], "y_cell"))
						 map->cells[x][y].transp.y_cell = YAJL_GET_INTEGER(YAJL_GET_OBJECT(cell)->values[i]);
				 }
			 }
		 }

	 return map;

}
Exemplo n.º 16
0
void* events_thread(void* args)
{
    char input[2048] = {0};
    char errbuf[1024] = {0};
    char* walker;

    yajl_val node;
    yajl_val val;
    char name[512], instance[512];
    int button, x, y;

    while(fgets(input, sizeof(input), stdin)) {
        walker = input;
        if(*walker == '[')
            walker++;
        if(*walker == '\0')
            continue;
        if(*walker == ',')
            walker++;

        node = yajl_tree_parse(walker, errbuf, sizeof(errbuf));
        if (node == NULL) {
            fprintf(stderr, "parse_error: ");
            if (strlen(errbuf)) fprintf(stderr, " %s", errbuf);
            else fprintf(stderr, "unknown error");
            fprintf(stderr, "\n");
            continue;
        }

        val = yajl_tree_get(node, events_paths[0], yajl_t_string);
        if(val && YAJL_IS_STRING(val))
            strncpy(name, val->u.string, 512);
        else
            name[0] = '\0';

        val = yajl_tree_get(node, events_paths[1], yajl_t_string);
        if(val && YAJL_IS_STRING(val))
            strncpy(instance, val->u.string, 512);
        else
            instance[0] = '\0';

        val = yajl_tree_get(node, events_paths[2], yajl_t_number);
        button = (val ? YAJL_GET_INTEGER(val) : -1);

        val = yajl_tree_get(node, events_paths[3], yajl_t_number);
        x = (val ? YAJL_GET_INTEGER(val) : -1);

        val = yajl_tree_get(node, events_paths[4], yajl_t_number);
        y = (val ? YAJL_GET_INTEGER(val) : -1);

        yajl_tree_free(node);

        if(strcmp(name, "volume") == 0) {
            char* mixer = strchr(instance, '.');
            if(mixer == NULL)
                continue;
            *(mixer++) = '\0';
            char* mixer_id = strchr(mixer, '.');
            if(mixer_id == NULL)
                continue;
            *(mixer_id++) = '\0';
            mouse_volume(button, instance, mixer, atoi(mixer_id));
        }
    }
    return NULL;
}
Exemplo n.º 17
0
int maina(void)
{
    size_t rd;
    yajl_val node;
    char errbuf[1024];

    /* null plug buffers */
    fileData[0] = errbuf[0] = 0;

    /* read the entire config file */
    rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);

    /* file read error handling */
    if (rd == 0 && !feof(stdin)) {
        fprintf(stderr, "error encountered on file read\n");
        return 1;
    } else if (rd >= sizeof(fileData) - 1) {
        fprintf(stderr, "config file too big\n");
        return 1;
    }

    /* we have the whole config file in memory.  let's parse it ... */
    node = yajl_tree_parse((const char *) fileData, errbuf, sizeof(errbuf));

    /* parse error handling */
    if (node == NULL) {
        fprintf(stderr, "parse_error: ");
        if (strlen(errbuf)) fprintf(stderr, " %s", errbuf);
        else fprintf(stderr, "unknown error");
        fprintf(stderr, "\n");
        return 1;
    }

    /* ... and extract a nested value from the config file */
    {
	const char * path[] = { "wallet", (const char *) 0 };
        yajl_val v = yajl_tree_get(node, path, yajl_t_array);


	if(v) {
		yajl_val *objs = v->u.array.values;
		int i = 0;
		for(i; i < v->u.array.len; i++)
		{
			if(objs[i]) { /*A complete JSON object*/
				char **keys = objs[i]->u.object.keys; /*keys for JSON obj*/
				yajl_val *vals = objs[i]->u.object.values; /*values for JSON obj*/

				int j=0;
				for (j; j< objs[i]->u.object.len; j++) {
					printf("Object key : %s\n", keys[j]);
					if(YAJL_IS_INTEGER(vals[j])) {
						printf("Object value : %lli\n", YAJL_GET_INTEGER(vals[j]));
					}else if(YAJL_IS_DOUBLE(vals[j])) {
						printf("Object value : %lf\n", YAJL_GET_DOUBLE(vals[j]));
					}
					else {
						printf("Object value : %s\n", YAJL_GET_STRING(vals[j]));
					}
				}		
				
			}
		}
	}       
    }

    yajl_tree_free(node);

    return 0;
}