Example #1
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 #2
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;
}
Example #3
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 #4
0
File: utils.c Project: lclc/mcu
void utils_decrypt_report(const char *report)
{
    int decrypt_len;
    char *dec;

    memset(decrypted_report, 0, sizeof(decrypted_report));

    yajl_val json_node = yajl_tree_parse(report, NULL, 0);

    if (!json_node) {
        strcpy(decrypted_report, "/* error: Failed to parse report. */");
        return;
    }

    size_t i, r = json_node->u.object.len;
    for (i = 0; i < r; i++) {
        const char *ciphertext_path[] = { cmd_str(CMD_ciphertext), (const char *) 0 };
        const char *echo_path[] = { "echo", (const char *) 0 };
        const char *ciphertext = YAJL_GET_STRING(yajl_tree_get(json_node, ciphertext_path,
                                 yajl_t_string));
        const char *echo = YAJL_GET_STRING(yajl_tree_get(json_node, echo_path, yajl_t_string));
        if (ciphertext) {
            dec = aes_cbc_b64_decrypt((const unsigned char *)ciphertext, strlens(ciphertext),
                                      &decrypt_len, PASSWORD_STAND);
            if (!dec) {
                strcpy(decrypted_report, "/* error: Failed to decrypt. */");
                goto exit;
            }

            sprintf(decrypted_report, "/* ciphertext */ %.*s", decrypt_len, dec);
            free(dec);
            goto exit;
        } else if (echo) {
            dec = aes_cbc_b64_decrypt((const unsigned char *)echo, strlens(echo), &decrypt_len,
                                      PASSWORD_VERIFY);
            if (!dec) {
                strcpy(decrypted_report, "/* error: Failed to decrypt echo. */");
                goto exit;
            }

            sprintf(decrypted_report, "/* echo */ %.*s", decrypt_len, dec);
            free(dec);
            goto exit;
        }
    }
    strcpy(decrypted_report, report);
exit:
    yajl_tree_free(json_node);
    return;
}
Example #5
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;
}
Example #6
0
char *YAJLGetStr(yajl_val node, const char *name)
{
	char *in = YAJL_GET_STRING(YAJLFindNode(node, name));
	char *out;
	CSTRDUP(out, in);
	return out;
}
Example #7
0
char *webhdfs_home_dir (webhdfs_t *fs) {
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, NULL);
    webhdfs_req_set_args(&req, "op=GETHOMEDIRECTORY");
    char *error = NULL;
    webhdfs_req_exec(&req, WEBHDFS_REQ_GET, &error);
    if (error) free(error);
    node = webhdfs_req_json_response(&req);
    webhdfs_req_close(&req);

    if ((v = webhdfs_response_exception(node)) != NULL) {
        yajl_tree_free(node);
        return(NULL);
    }

    if ((v = webhdfs_response_path(node)) != NULL) {
        char *path = strdup(YAJL_GET_STRING(v));
        yajl_tree_free(node);
        return(path);
    }

    yajl_tree_free(node);
    return(NULL);
}
Example #8
0
/*
 * We only call this in case where it should be safe, but gcc doesn't know
 * that so we use this to shut it up.
 */
char *
MY_YAJL_GET_STRING (yajl_val x)
{
        char *y = YAJL_GET_STRING(x);

        return y ? y : "bogus";
}
Example #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;
}
Example #10
0
/* Handle JSON data (one request) and call
 * appropriate event OVS DB handler. Currently,
 * update callback 'ovs_db_table_update_cb' and
 * result callback 'ovs_db_result_cb' is supported.
 */
static int ovs_db_json_data_process(ovs_db_t *pdb, const char *data,
                                    size_t len) {
  const char *method = NULL;
  char yajl_errbuf[OVS_YAJL_ERROR_BUFFER_SIZE];
  const char *method_path[] = {"method", NULL};
  const char *result_path[] = {"result", NULL};
  char *sjson = NULL;
  yajl_val jnode, jval;

  /* duplicate the data to make null-terminated string
   * required for yajl_tree_parse() */
  if ((sjson = calloc(1, len + 1)) == NULL)
    return -1;

  sstrncpy(sjson, data, len + 1);
  OVS_DEBUG("[len=%" PRIsz "] %s", len, sjson);

  /* parse json data */
  jnode = yajl_tree_parse(sjson, yajl_errbuf, sizeof(yajl_errbuf));
  if (jnode == NULL) {
    OVS_ERROR("yajl_tree_parse() %s", yajl_errbuf);
    sfree(sjson);
    return -1;
  }

  /* get method name */
  if ((jval = yajl_tree_get(jnode, method_path, yajl_t_string)) != NULL) {
    if ((method = YAJL_GET_STRING(jval)) == NULL) {
      yajl_tree_free(jnode);
      sfree(sjson);
      return -1;
    }
    if (strcmp("echo", method) == 0) {
      /* echo request from the server */
      if (ovs_db_table_echo_cb(pdb, jnode) < 0)
        OVS_ERROR("handle echo request failed");
    } else if (strcmp("update", method) == 0) {
      /* update notification */
      if (ovs_db_table_update_cb(pdb, jnode) < 0)
        OVS_ERROR("handle update notification failed");
    }
  } else if ((jval = yajl_tree_get(jnode, result_path, yajl_t_any)) != NULL) {
    /* result notification */
    if (ovs_db_result_cb(pdb, jnode) < 0)
      OVS_ERROR("handle result reply failed");
  } else
    OVS_ERROR("connot find method or result failed");

  /* release memory */
  yajl_tree_free(jnode);
  sfree(sjson);
  return 0;
}
Example #11
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 );
    }
}
Example #12
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.");
}
Example #13
0
/* Get OVS DB registered callback by YAJL val. The YAJL
 * value should be YAJL string (UID). Returns NULL if
 * callback hasn't been found. See also ovs_db_callback_get()
 * description for addition info.
 */
static ovs_callback_t *ovs_db_table_callback_get(ovs_db_t *pdb, yajl_val jid) {
  char *endptr = NULL;
  const char *suid = NULL;
  uint64_t uid;

  if (jid && YAJL_IS_STRING(jid)) {
    suid = YAJL_GET_STRING(jid);
    uid = (uint64_t)strtoul(suid, &endptr, 16);
    if (*endptr == '\0' && uid)
      return ovs_db_callback_get(pdb, uid);
  }

  return NULL;
}
Example #14
0
bool
JSONParser::find(const std::string& key, std::string& value)
{
  VALIDATE_STATE();
  bool result = false;

  if (mState->mTree) {
    const char* path[2] = { key.c_str(), NULL };
    yajl_val str = yajl_tree_get(mState->mTree, path, yajl_t_string);
    if (str) {
      result = true;
      value = YAJL_GET_STRING(str);
    }
  }
  return result;
}
Example #15
0
int
main(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[] = { "Logging", "timeFormat", (const char*)0 };
		yajl_val v = yajl_tree_get(node, path, yajl_t_string);
		if (v) printf("%s/%s: %s\n", path[0], path[1], YAJL_GET_STRING(v));
		else printf("no such node: %s/%s\n", path[0], path[1]);
	}

	yajl_tree_free(node);

	return 0;
}
Example #16
0
static char *
parse_json_get_object_string (const char *json, const char *key, int flags,
                              const char *func, const char *cmd)
{
  char *str, *ret;
  yajl_val tree = NULL, node;
  size_t i, len;

  tree = parse_json (json, func);
  if (tree == NULL)
    return NULL;

  if (! YAJL_IS_OBJECT (tree))
    goto bad_type;

  len = YAJL_GET_OBJECT (tree)->len;
  for (i = 0; i < len; ++i) {
    if (STREQ (YAJL_GET_OBJECT (tree)->keys[i], key)) {
      node = YAJL_GET_OBJECT (tree)->values[i];

      if ((flags & GET_STRING_NULL_TO_EMPTY) && YAJL_IS_NULL (node))
        ret = strdup ("");
      else {
        str = YAJL_GET_STRING (node);
        if (str == NULL)
          goto bad_type;
        ret = strdup (str);
      }
      if (ret == NULL)
        reply_with_perror ("strdup");

      yajl_tree_free (tree);

      return ret;
    }
  }

 bad_type:
  reply_with_error ("output of '%s' was not a JSON object "
                    "containing a key '%s' of type string", cmd, key);
  yajl_tree_free (tree);
  return NULL;
}
Example #17
0
File: v1.c Project: fahlgren/kore
static int
write_string_array_params(struct jsonrpc_request *req, void *ctx)
{
	int status = 0;

	if (!YAJL_GEN_KO(status = yajl_gen_array_open(req->gen))) {
		for (size_t i = 0; i < req->params->u.array.len; i++) {
			yajl_val yajl_str = req->params->u.array.values[i];
			char	 *str = YAJL_GET_STRING(yajl_str);

			if (YAJL_GEN_KO(status = yajl_gen_string(req->gen,
			    (unsigned char *)str, strlen(str))))
				break;
		}
		if (status == 0)
			status = yajl_gen_array_close(req->gen);
	}

	return status;
}
Example #18
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 #19
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;
}
Example #20
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);
}
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;
}