Example #1
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);
}
Example #2
0
SEXP ParseArray(yajl_val node, int bigint){
  int len = YAJL_GET_ARRAY(node)->len;
  SEXP vec = PROTECT(allocVector(VECSXP, len));
  for (int i = 0; i < len; ++i) {
    SET_VECTOR_ELT(vec, i, ParseValue(YAJL_GET_ARRAY(node)->values[i], bigint));
  }
  UNPROTECT(1);
  return vec;
}
Example #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]);
}
Example #4
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;
}
Example #5
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));
}
Example #6
0
void yajl_tree_free (yajl_val v)
{
    if (v == NULL) return;

    if (YAJL_IS_STRING(v))
    {
        free(v->u.string);
        free(v);
    }
    else if (YAJL_IS_NUMBER(v))
    {
        free(v->u.number.r);
        free(v);
    }
    else if (YAJL_GET_OBJECT(v))
    {
        yajl_object_free(v);
    }
    else if (YAJL_GET_ARRAY(v))
    {
        yajl_array_free(v);
    }
    else /* if (yajl_t_true or yajl_t_false or yajl_t_null) */
    {
        free(v);
    }
}
Example #7
0
Analogsignal*
parseJSON(char* responseGnode){
	yajl_val node;
    char errbuf[1024];

    node = yajl_tree_parse((const char *) responseGnode, errbuf, sizeof(errbuf));

    const char * path[] = { "selected", 0 };
    yajl_val v = yajl_tree_get(node, path, yajl_t_array);
    yajl_val node2 = YAJL_GET_ARRAY(v)->values[0];

    const char * path2[] = { "permalink", 0 };
    yajl_val v2 = yajl_tree_get(node2, path2, yajl_t_string);
    // if (v2) printf("%s: %s\n", path2[0], YAJL_GET_STRING(v2));

    yajl_val node3 = YAJL_GET_OBJECT(v2);
    const char * path3[] = { "fields", 0 };
    yajl_val v3 = yajl_tree_get(node2, path3, yajl_t_object);

    const char * path4[] = { "description", 0 };
    yajl_val v4 = yajl_tree_get(v3, path4, yajl_t_string);

    Analogsignal *analog = (Analogsignal *)malloc(sizeof(Analogsignal));
    
    strcpy (analog->permalink, YAJL_GET_STRING(v2));
    strcpy (analog->description, YAJL_GET_STRING(v4));

    yajl_tree_free(node);

	return analog;
}
Example #8
0
/* OVS DB table update event handler.
 * This callback is called by POLL thread if OVS DB
 * table update callback is received from the DB
 * server. Once registered callback found, it's called
 * by this handler. */
static int ovs_db_table_update_cb(ovs_db_t *pdb, yajl_val jnode) {
  ovs_callback_t *cb = NULL;
  yajl_val jvalue;
  yajl_val jparams;
  yajl_val jtable_updates;
  const char *params_path[] = {"params", NULL};
  const char *id_path[] = {"id", NULL};

  /* check & get request attributes */
  if ((jparams = yajl_tree_get(jnode, params_path, yajl_t_array)) == NULL ||
      (yajl_tree_get(jnode, id_path, yajl_t_null) == NULL)) {
    OVS_ERROR("invalid OVS DB request received");
    return -1;
  }

  /* check array length: [<json-value>, <table-updates>] */
  if ((YAJL_GET_ARRAY(jparams) == NULL) ||
      (YAJL_GET_ARRAY(jparams)->len != 2)) {
    OVS_ERROR("invalid OVS DB request received");
    return -1;
  }

  jvalue = YAJL_GET_ARRAY(jparams)->values[0];
  jtable_updates = YAJL_GET_ARRAY(jparams)->values[1];
  if ((!YAJL_IS_OBJECT(jtable_updates)) || (!YAJL_IS_STRING(jvalue))) {
    OVS_ERROR("invalid OVS DB request id or table update received");
    return -1;
  }

  /* find registered callback based on <json-value> */
  pthread_mutex_lock(&pdb->mutex);
  cb = ovs_db_table_callback_get(pdb, jvalue);
  if (cb == NULL || cb->table.call == NULL) {
    OVS_ERROR("No OVS DB table update callback found");
    pthread_mutex_unlock(&pdb->mutex);
    return -1;
  }

  /* call registered callback */
  cb->table.call(jtable_updates);
  pthread_mutex_unlock(&pdb->mutex);
  return 0;
}
Example #9
0
void message_bus_write_json(yajl_gen g, void *context) {
    state *state = context;

    yajl_val output_values = state_get_json(state, STATE_OUTPUT_VALUES);

    assert(YAJL_IS_ARRAY(output_values));

    yajl_gen_map_open(g);

    int len = YAJL_GET_ARRAY(output_values)->len;
    for (int i = 0; i < len; i++) {
        char *k = YAJL_GET_STRING(YAJL_GET_ARRAY(output_values)->values[i]);

        state_value_type type = state_get_value_type(state, k);

        yajl_gen_string(g, (unsigned char *)k, strlen(k));

        float float_value;
        int int_value;
        yajl_val json_value;
        switch (type) {
            case state_value_type_float:
                float_value = state_get_float(state, k);
                yajl_gen_double(g, (double)float_value);
                break;
            case state_value_type_int:
                int_value = state_get_int(state, k);
                yajl_gen_integer(g, int_value);
                break;
            case state_value_type_json:
                json_value = state_get_json(state, k);
                json_write_value(g, json_value);
                break;
            default:
                assert(0);
                break;
        }
    }

    yajl_gen_map_close(g);
}
Example #10
0
/* Get OVS DB map value by given map key
 *
 * FROM RFC7047:
 *
 *   <pair>
 *     A 2-element JSON array that represents a pair within a database
 *     map.  The first element is an <atom> that represents the key, and
 *     the second element is an <atom> that represents the value.
 *
 *   <map>
 *     A 2-element JSON array that represents a database map value.  The
 *     first element of the array must be the string "map", and the
 *     second element must be an array of zero or more <pair>s giving the
 *     values in the map.  All of the <pair>s must have the same key and
 *     value types.
 *
 * EXAMPLE:
 *  [
 *    "map", [
 *             [ "key_a", <YAJL value>], [ "key_b", <YAJL value>], ...
 *           ]
 *  ]
 */
yajl_val ovs_utils_get_map_value(yajl_val jval, const char *key) {
  size_t map_len = 0;
  size_t array_len = 0;
  yajl_val *map_values = NULL;
  yajl_val *array_values = NULL;
  const char *str_val = NULL;

  /* check YAJL array */
  if (!YAJL_IS_ARRAY(jval) || (key == NULL))
    return NULL;

  /* check a database map value (2-element, first one should be a string */
  array_len = YAJL_GET_ARRAY(jval)->len;
  array_values = YAJL_GET_ARRAY(jval)->values;
  if ((array_len != 2) || (!YAJL_IS_STRING(array_values[0])) ||
      (!YAJL_IS_ARRAY(array_values[1])))
    return NULL;

  /* check first element of the array */
  str_val = YAJL_GET_STRING(array_values[0]);
  if (str_val == NULL || strcmp("map", str_val) != 0)
    return NULL;

  /* try to find map value by map key */
  if (YAJL_GET_ARRAY(array_values[1]) == NULL)
    return NULL;

  map_len = YAJL_GET_ARRAY(array_values[1])->len;
  map_values = YAJL_GET_ARRAY(array_values[1])->values;
  for (size_t i = 0; i < map_len; i++) {
    /* check YAJL array */
    if (!YAJL_IS_ARRAY(map_values[i]) || YAJL_GET_ARRAY(map_values[i]) == NULL)
      break;

    /* check a database pair value (2-element, first one represents a key
     * and it should be a string in our case */
    array_len = YAJL_GET_ARRAY(map_values[i])->len;
    array_values = YAJL_GET_ARRAY(map_values[i])->values;
    if ((array_len != 2) || (!YAJL_IS_STRING(array_values[0])))
      break;

    /* return map value if given key equals map key */
    str_val = YAJL_GET_STRING(array_values[0]);
    if (str_val != NULL && strcmp(key, str_val) == 0)
      return array_values[1];
  }
  return NULL;
}
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;

}
Example #12
0
struct github *fetch_github_commits(yajl_val *root, const char *repo, int *commit_count) {

	CURL *curl;
	CURLcode code;
	yajl_val val;
	struct github *commits = NULL;
	struct mem_buffer mem = {NULL, 0};
	char API_URL[URLLEN], errbuf[1024];

	// Use per_page field to limit json reply to the amount of commits specified
	snprintf(API_URL, URLLEN, "https://api.github.com/repos/%s/commits?per_page=%d", repo, *commit_count);
	*commit_count = 0;

	curl = curl_easy_init();
	if (!curl)
		goto cleanup;

#ifdef TEST
	curl_easy_setopt(curl, CURLOPT_URL, getenv("IRCBOT_TESTFILE"));
#else
	curl_easy_setopt(curl, CURLOPT_URL, API_URL);
#endif
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "irc-bot"); // Github requires a user-agent
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 8L);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_memory);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem);

	code = curl_easy_perform(curl);
	if (code != CURLE_OK) {
		fprintf(stderr, "Error: %s\n", curl_easy_strerror(code));
		goto cleanup;
	}
	if (!mem.buffer) {
		fprintf(stderr, "Error: Body was empty");
		goto cleanup;
	}
	*root = yajl_tree_parse(mem.buffer, errbuf, sizeof(errbuf));
	if (!*root) {
		fprintf(stderr, "%s\n", errbuf);
		goto cleanup;
	}
	if (YAJL_IS_ARRAY(*root)) {
		*commit_count = YAJL_GET_ARRAY(*root)->len;
		commits = malloc_w(*commit_count * sizeof(*commits));
	}
	// Find the field we are interested in the json reply, save a reference to it & null terminate
	for (int i = 0; i < *commit_count; i++) {
		val = yajl_tree_get(YAJL_GET_ARRAY(*root)->values[i], CFG("sha"),                      yajl_t_string);
		if (!val) break;
		commits[i].sha  = YAJL_GET_STRING(val);

		val = yajl_tree_get(YAJL_GET_ARRAY(*root)->values[i], CFG("commit", "author", "name"), yajl_t_string);
		if (!val) break;
		commits[i].name = YAJL_GET_STRING(val);

		val = yajl_tree_get(YAJL_GET_ARRAY(*root)->values[i], CFG("commit", "message"),        yajl_t_string);
		if (!val) break;
		commits[i].msg  = YAJL_GET_STRING(val);
		null_terminate(commits[i].msg, '\n'); // Cut commit message at newline character if present

		val = yajl_tree_get(YAJL_GET_ARRAY(*root)->values[i], CFG("html_url"),                 yajl_t_string);
		if (!val) break;
		commits[i].url  = YAJL_GET_STRING(val);
	}
cleanup:
	free(mem.buffer);
	curl_easy_cleanup(curl);
	return commits;
}