Exemplo n.º 1
0
bson* bson_from_json_str(const char* str)
{
    yajl_val root;
    char* errbuf;
    bson *b;

    errbuf = malloc( sizeof(char) * 1024 );
    root = yajl_tree_parse( str, errbuf, 1024 );

    if( !root )
    {
        puts( "Parse error: " );
        if( strlen( errbuf ) )
            puts( errbuf );
        else
            puts( "unknown error." );

        yajl_tree_free( root );
        free( errbuf );
        return NULL;
    }

    free( errbuf );

    assert( YAJL_IS_OBJECT( root ) );

    b = malloc( sizeof( bson ) );
    bson_init( b );
    bson_from_json_object( b, root );
    bson_finish( b );

    yajl_tree_free( root );
    return b;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
0
/* 
 * Parse the json package
 */
int parse_gps_json(const char * buffer, struct gps_package * data) {
    yajl_val node;
    char errbuf[1024];

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

    /* we have the whole config file in memory.  let's parse it ... */
    node = yajl_tree_parse((const char *) buffer, 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 0;
    }*/

    /* ... and extract a nested value from the json file */
    {
        const char * lat_path[] = { "lat", (const char *) 0 };
        const char * lon_path[] = { "lon", (const char *) 0 };
        yajl_val lat = yajl_tree_get(node, lat_path, yajl_t_number);
        yajl_val lon = yajl_tree_get(node, lon_path, yajl_t_number);
        if (lat) data->lat = YAJL_GET_DOUBLE(lat);
        else return 0;
        if (lon) data->lon = YAJL_GET_DOUBLE(lon);
        else return 0;
    }

    yajl_tree_free(node);

    return 1;
}
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
size_t
parse_get_response (void *ptr, size_t size, size_t nmemb, void *stream)
{
        yajl_val        node;
        yajl_val        value;

        node = yajl_tree_parse(ptr,NULL,0);
        if (node) {
                value = my_yajl_tree_get(node,value_path,yajl_t_string);
                if (value) {
                        /* 
                         * YAJL probably copied it once, now we're going to
                         * copy it again.  If anybody really cares for such
                         * small and infrequently used values, we'd have to do
                         * do something much more complicated (like using the
                         * stream interface) to avoid the copy.  Right now it's
                         * just not worth it.
                         */
                        *((char **)stream) = strdup(MY_YAJL_GET_STRING(value));
                }
                else {
                        /* Might as well try this. */
                        *((char **)stream) = parse_array_response(node);
                }
                yajl_tree_free(node);
        }

        return size*nmemb;
}
Exemplo n.º 7
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.º 8
0
TEST_F(Yajl, yajl_tree_parse) {
	for (size_t i = 0; i < kTrialCount; i++) {
		yajl_val root = yajl_tree_parse(json_, NULL, 0);
		ASSERT_TRUE(root != NULL);
		yajl_tree_free(root);
	}
}
Exemplo n.º 9
0
yajl_val yajl_tree_parse_options (const char *input,
                                   char *error_buffer, size_t error_buffer_size,
                                   yajl_tree_option options)
{
    static const yajl_callbacks callbacks =
        {
            /* null        = */ handle_null,
            /* boolean     = */ handle_boolean,
            /* integer     = */ NULL,
            /* double      = */ NULL,
            /* number      = */ handle_number,
            /* string      = */ handle_string,
            /* start map   = */ handle_start_map,
            /* map key     = */ handle_string,
            /* end map     = */ handle_end_map,
            /* start array = */ handle_start_array,
            /* end array   = */ handle_end_array
        };

    yajl_handle handle;
    yajl_status status;
    char * internal_err_str;
	context_t ctx = { NULL, NULL, NULL, 0 };

	ctx.errbuf = error_buffer;
	ctx.errbuf_size = error_buffer_size;

    if (error_buffer != NULL)
        memset (error_buffer, 0, error_buffer_size);

    handle = yajl_alloc (&callbacks, NULL, &ctx);
    yajl_config(handle, yajl_allow_comments,
            (options & yajl_tree_option_dont_allow_comments) ? 0 : 1);
    yajl_config(handle, yajl_allow_trailing_separator,
            (options & yajl_tree_option_allow_trailing_separator) ? 1 : 0);

    status = yajl_parse(handle,
                        (unsigned char *) input,
                        strlen (input));
    status = yajl_complete_parse (handle);
    if (status != yajl_status_ok) {
        if (error_buffer != NULL && error_buffer_size > 0) {
               internal_err_str = (char *) yajl_get_error(handle, 1,
                     (const unsigned char *) input,
                     strlen(input));
             snprintf(error_buffer, error_buffer_size, "%s", internal_err_str);
             YA_FREE(&(handle->alloc), internal_err_str);
        }
        while (ctx.stack) {
            yajl_tree_free(context_pop(&ctx));
        }
        yajl_free (handle);
        return NULL;
    }

    yajl_free (handle);
    return (ctx.root);
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
int webhdfs_set_times (webhdfs_t *fs, const char *path, int mtime, int atime) {
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, path);
    webhdfs_req_set_args(&req, "op=SETTIMES&modificationtime=%d&accesstime=%d",
                               mtime, atime);
    webhdfs_req_exec(&req, WEBHDFS_REQ_PUT);
    node = webhdfs_req_json_response(&req);
    webhdfs_req_close(&req);

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

    yajl_tree_free(node);
    return(0);
}
Exemplo n.º 12
0
webhdfs_dir_t *webhdfs_dir_open (webhdfs_t *fs,
                                 const char *path)
{
    const char *file_status[] = {"FileStatus", NULL};
    webhdfs_dir_t *dir;
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, path);
    webhdfs_req_set_args(&req, "op=LISTSTATUS");
    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_file_statuses(node)) == NULL) {
        yajl_tree_free(node);
        return(NULL);
    }

    if ((v = yajl_tree_get(v, file_status, yajl_t_array)) == NULL) {
        yajl_tree_free(node);
        return(NULL);
    }

    if ((dir = (webhdfs_dir_t *) malloc(sizeof(webhdfs_dir_t))) == NULL) {
        yajl_tree_free(node);
        return(NULL);
    }

    dir->root = node;
    dir->statuses = v;
    dir->current = 0;

    return(dir);
}
Exemplo n.º 13
0
int webhdfs_chmod (webhdfs_t *fs, const char *path, int permission) {
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, path);
    webhdfs_req_set_args(&req, "op=SETPERMISSION&permission=%o", permission);
    char *error = NULL;
    webhdfs_req_exec(&req, WEBHDFS_REQ_PUT, &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(1);
    }

    yajl_tree_free(node);
    return(0);
}
Exemplo n.º 14
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.º 15
0
int webhdfs_chmod (webhdfs_t *fs, const char *path, int permission) {
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, path);

    // the permission must be in radix-8 format and it's maximal value is 777
    int short_permission = permission % (8 * 8 * 8);
    webhdfs_req_set_args(&req, "op=SETPERMISSION&permission=%o", short_permission);
    webhdfs_req_exec(&req, WEBHDFS_REQ_PUT);
    node = webhdfs_req_json_response(&req);
    webhdfs_req_close(&req);

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

    yajl_tree_free(node);
    return(0);
}
Exemplo n.º 16
0
bool JSONParser::parse(const String& json)
{
    char errbuf[128];
    yajl_val value = yajl_tree_parse(json.constData(), errbuf, sizeof(errbuf));
    if (!value) {
        mRoot = Value(); // just in case
        return false;
    }
    mRoot = yajlValueToValue(value);
    yajl_tree_free(value);
    return isValid();
}
Exemplo n.º 17
0
int load_()
{
  int err = 0;
  yajl_val js_configs = parse_config_file_();
  if (js_configs == NULL) {
    err = wsn_last_err();
  } else {
    err = translate_configs_(js_configs);
    yajl_tree_free(js_configs);
  }
  return err;
}
Exemplo n.º 18
0
int webhdfs_chown (webhdfs_t *fs,
                   const char *path,
                   const char *user,
                   const char *group)
{
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, path);
    webhdfs_req_set_args(&req, "op=SETOWNER&user=%s&group=%s", user, group);
    webhdfs_req_exec(&req, WEBHDFS_REQ_PUT);
    node = webhdfs_req_json_response(&req);
    webhdfs_req_close(&req);

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

    yajl_tree_free(node);
    return(0);
}
Exemplo n.º 19
0
Arquivo: utils.c Projeto: 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;
}
Exemplo n.º 20
0
int webhdfs_mkdir (webhdfs_t *fs, const char *path, int permission) {
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, path);
    webhdfs_req_set_args(&req, "op=MKDIRS&permission=%o", permission);
    webhdfs_req_exec(&req, WEBHDFS_REQ_PUT);
    node = webhdfs_req_json_response(&req);
    webhdfs_req_close(&req);

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

    if ((v = webhdfs_response_boolean(node)) != NULL) {
        int failure = YAJL_IS_FALSE(v);
        yajl_tree_free(node);
        return(failure);
    }

    yajl_tree_free(node);
    return(2);
}
Exemplo n.º 21
0
int webhdfs_set_replication (webhdfs_t *fs, const char *path, int replication) {
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, path);
    webhdfs_req_set_args(&req, "op=SETREPLICATION&replication=%d", replication);
    webhdfs_req_exec(&req, WEBHDFS_REQ_PUT);
    node = webhdfs_req_json_response(&req);
    webhdfs_req_close(&req);

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

    if ((v = webhdfs_response_boolean(node)) != NULL) {
        int failure = YAJL_IS_FALSE(v);
        yajl_tree_free(node);
        return(failure);
    }

    yajl_tree_free(node);
    return(2);
}
Exemplo n.º 22
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");
    webhdfs_req_exec(&req, WEBHDFS_REQ_GET);
    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);
}
Exemplo n.º 23
0
int webhdfs_rename_snapshot (webhdfs_t *fs, const char *path, const char *oldname, const char *newname) {
    webhdfs_req_t req;
    yajl_val node, v;

    webhdfs_req_open(&req, fs, path);
    webhdfs_req_set_args(&req, "op=RENAMESNAPSHOT&oldsnapshotname=%s&snapshotname=%s", oldname, newname);
    webhdfs_req_exec(&req, WEBHDFS_REQ_PUT);
    node = webhdfs_req_json_response(&req);
    webhdfs_req_close(&req);

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

    if ((v = webhdfs_response_boolean(node)) != NULL) {
        int failure = YAJL_IS_FALSE(v);
        yajl_tree_free(node);
        return(failure);
    }

    yajl_tree_free(node);
    return(2);
}
Exemplo n.º 24
0
static void yajl_array_free (yajl_val v)
{
    size_t i;

    if (!YAJL_IS_ARRAY(v)) return;

    for (i = 0; i < v->u.array.len; i++)
    {
        yajl_tree_free (v->u.array.values[i]);
        v->u.array.values[i] = NULL;
    }

    free(v->u.array.values);
    free(v);
}
Exemplo n.º 25
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;
}
Exemplo n.º 26
0
static void yajl_object_free (yajl_val v)
{
    size_t i;

    if (!YAJL_IS_OBJECT(v)) return;

    for (i = 0; i < v->u.object.len; i++)
    {
        free((char *) v->u.object.keys[i]);
        v->u.object.keys[i] = NULL;
        yajl_tree_free (v->u.object.values[i]);
        v->u.object.values[i] = NULL;
    }

    free((void*) v->u.object.keys);
    free(v->u.object.values);
    free(v);
}
Exemplo n.º 27
0
SEXP R_parse(SEXP x, SEXP bigint_as_char) {
    /* get data from R */
    const char* json = translateCharUTF8(asChar(x));
    const int bigint = asLogical(bigint_as_char);

    /* ignore BOM as suggested by RFC */
    if(json[0] == '\xEF' && json[1] == '\xBB' && json[2] == '\xBF'){
      warning("JSON string contains (illegal) UTF8 byte-order-mark!");
      json = json + 3;
    }

    /* parse json */
    char errbuf[1024];
    yajl_val node = yajl_tree_parse(json, errbuf, sizeof(errbuf));

    /* parser error */
    if (!node) {
      error(errbuf);
    }
    SEXP out = ParseValue(node, bigint);
    yajl_tree_free(node);
    return(out);
}
Exemplo n.º 28
0
 ~State()
 {
   yajl_tree_free(mTree); mTree = NULL;
 }
Exemplo n.º 29
0
int ovs_db_send_request(ovs_db_t *pdb, const char *method, const char *params,
                        ovs_db_result_cb_t cb) {
  int ret = 0;
  yajl_gen_status yajl_gen_ret;
  yajl_val jparams;
  yajl_gen jgen;
  ovs_callback_t *new_cb = NULL;
  uint64_t uid;
  char uid_buff[OVS_UID_STR_SIZE];
  const char *req = NULL;
  size_t req_len = 0;
  struct timespec ts;

  /* sanity check */
  if (!pdb || !method || !params)
    return -1;

  if ((jgen = yajl_gen_alloc(NULL)) == NULL)
    return -1;

  /* try to parse params */
  if ((jparams = yajl_tree_parse(params, NULL, 0)) == NULL) {
    OVS_ERROR("params is not a JSON string");
    yajl_gen_clear(jgen);
    return -1;
  }

  /* generate method field */
  OVS_YAJL_CALL(yajl_gen_map_open, jgen);

  OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "method");
  OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, method);

  /* generate params field */
  OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "params");
  OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jparams);
  yajl_tree_free(jparams);

  /* generate id field */
  OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "id");
  uid = ovs_uid_generate();
  snprintf(uid_buff, sizeof(uid_buff), "%" PRIX64, uid);
  OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, uid_buff);

  OVS_YAJL_CALL(yajl_gen_map_close, jgen);

  if (cb) {
    /* register result callback */
    if ((new_cb = calloc(1, sizeof(*new_cb))) == NULL)
      goto yajl_gen_failure;

    /* add new callback to front */
    sem_init(&new_cb->result.sync, 0, 0);
    new_cb->result.call = cb;
    new_cb->uid = uid;
    ovs_db_callback_add(pdb, new_cb);
  }

  /* send the request */
  OVS_YAJL_CALL(yajl_gen_get_buf, jgen, (const unsigned char **)&req, &req_len);
  OVS_DEBUG("%s", req);
  if (!ovs_db_data_send(pdb, req, req_len)) {
    if (cb) {
      /* wait for result */
      clock_gettime(CLOCK_REALTIME, &ts);
      ts.tv_sec += OVS_DB_SEND_REQ_TIMEOUT;
      if (sem_timedwait(&new_cb->result.sync, &ts) < 0) {
        OVS_ERROR("%s() no replay received within %d sec", __FUNCTION__,
                  OVS_DB_SEND_REQ_TIMEOUT);
        ret = (-1);
      }
    }
  } else {
    OVS_ERROR("ovs_db_data_send() failed");
    ret = (-1);
  }

yajl_gen_failure:
  if (new_cb) {
    /* destroy callback */
    sem_destroy(&new_cb->result.sync);
    ovs_db_callback_remove(pdb, new_cb);
  }

  /* release memory */
  yajl_gen_clear(jgen);
  return (yajl_gen_ret != yajl_gen_status_ok) ? (-1) : ret;
}
Exemplo n.º 30
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);
}