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; }
/* OVS DB result request handler. * This callback is called by POLL thread if OVS DB * result reply is received from the DB server. * Once registered callback found, it's called * by this handler. */ static int ovs_db_result_cb(ovs_db_t *pdb, yajl_val jnode) { ovs_callback_t *cb = NULL; yajl_val jresult; yajl_val jerror; yajl_val jid; const char *result_path[] = {"result", NULL}; const char *error_path[] = {"error", NULL}; const char *id_path[] = {"id", NULL}; jresult = yajl_tree_get(jnode, result_path, yajl_t_any); jerror = yajl_tree_get(jnode, error_path, yajl_t_any); jid = yajl_tree_get(jnode, id_path, yajl_t_string); /* check & get result attributes */ if (!jresult || !jerror || !jid) return -1; /* try to find registered callback */ pthread_mutex_lock(&pdb->mutex); cb = ovs_db_table_callback_get(pdb, jid); if (cb != NULL && cb->result.call != NULL) { /* call registered callback */ cb->result.call(jresult, jerror); /* unlock owner of the reply */ sem_post(&cb->result.sync); } pthread_mutex_unlock(&pdb->mutex); return 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; }
/* 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; }
/* OVS DB echo request handler. When OVS DB sends * "echo" request to the client, client should generate * "echo" replay with the same content received in the * request */ static int ovs_db_table_echo_cb(const ovs_db_t *pdb, yajl_val jnode) { yajl_val jparams; yajl_val jid; yajl_gen jgen; size_t resp_len = 0; const char *resp = NULL; const char *params_path[] = {"params", NULL}; const char *id_path[] = {"id", NULL}; yajl_gen_status yajl_gen_ret; if ((jgen = yajl_gen_alloc(NULL)) == NULL) return -1; /* check & get request attributes */ if ((jparams = yajl_tree_get(jnode, params_path, yajl_t_array)) == NULL || ((jid = yajl_tree_get(jnode, id_path, yajl_t_any)) == NULL)) { OVS_ERROR("parse echo request failed"); goto yajl_gen_failure; } /* generate JSON echo response */ OVS_YAJL_CALL(yajl_gen_map_open, jgen); OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "result"); OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jparams); OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "error"); OVS_YAJL_CALL(yajl_gen_null, jgen); OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "id"); OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jid); OVS_YAJL_CALL(yajl_gen_map_close, jgen); OVS_YAJL_CALL(yajl_gen_get_buf, jgen, (const unsigned char **)&resp, &resp_len); /* send the response */ OVS_DEBUG("response: %s", resp); if (ovs_db_data_send(pdb, resp, resp_len) < 0) { OVS_ERROR("send echo reply failed"); goto yajl_gen_failure; } /* clean up and return success */ yajl_gen_clear(jgen); return 0; yajl_gen_failure: /* release memory */ yajl_gen_clear(jgen); return -1; }
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)); }
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; }
/* void LoadSoundFromNode(Mix_Chunk **value, json_t *node, const char *name) { if (json_find_first_label(node, name) == NULL) { return; } if (!TryLoadValue(&node, name)) { return; } *value = StrSound(node->text); } void LoadPic(const Pic **value, json_t *node, const char *name) { if (json_find_first_label(node, name)) { char *tmp = GetString(node, name); *value = PicManagerGetPic(&gPicManager, tmp); CFREE(tmp); } } void LoadBulletGuns(CArray *guns, json_t *node, const char *name) { node = json_find_first_label(node, name); if (node == NULL || node->child == NULL) { return; } CArrayInit(guns, sizeof(const GunDescription *)); for (json_t *gun = node->child->child; gun; gun = gun->next) { const GunDescription *g = StrGunDescription(gun->text); CArrayPushBack(guns, &g); } } void LoadColor(color_t *c, json_t *node, const char *name) { if (json_find_first_label(node, name) == NULL) { return; } if (!TryLoadValue(&node, name)) { return; } *c = StrColor(node->text); } */ yajl_val YAJLFindNode(yajl_val node, const char *path) { // max 256 levels const char *pathSplit[256]; for (int i = 0; i < 256; i++) pathSplit[i] = NULL; char *pathCopy; CSTRDUP(pathCopy, path); char *pch = strtok(pathCopy, "/"); int i = 0; yajl_val out = NULL; while (pch != NULL) { if (i == 256) { fprintf(stderr, "JSON path too long: '%s'\n", path); goto bail; } pathSplit[i] = pch; i++; pch = strtok(NULL, "/"); } out = yajl_tree_get(node, pathSplit, yajl_t_any); bail: CFREE(pathCopy); return out; }
/* * process an assertion using the hosted verifier. * * TODO: local verification */ VerifyResult processAssertion(request_rec *r, const char *assertion) { VerifyResult res = apr_pcalloc(r->pool, sizeof(struct _VerifyResult)); yajl_val parsed_result = NULL; char *assertionResult = verifyAssertionRemote(r, (char*) assertion); if (assertionResult) { char errorBuffer[256]; parsed_result = yajl_tree_parse(assertionResult, errorBuffer, 255); if (!parsed_result) { res->errorResponse = apr_psprintf(r->pool, jsonErrorResponse, "malformed payload", errorBuffer); return res; } } else { // XXX: verifyAssertionRemote should return specific error message. res->errorResponse = apr_psprintf(r->pool, jsonErrorResponse, "communication error", "can't contact verification server"); return res; } char *parsePath[2]; parsePath[0] = "email"; parsePath[1] = NULL; yajl_val foundEmail = yajl_tree_get(parsed_result, (const char**)parsePath, yajl_t_string); if (!foundEmail) { res->errorResponse = apr_pstrdup(r->pool, assertionResult); return res; } parsePath[0] = "issuer"; parsePath[1] = NULL; yajl_val identityIssuer = yajl_tree_get(parsed_result, (const char**)parsePath, yajl_t_string); if (!identityIssuer) { res->errorResponse = apr_pstrdup(r->pool, assertionResult); return res; } res->verifiedEmail = apr_pstrdup(r->pool, foundEmail->u.string); res->identityIssuer = apr_pstrdup(r->pool, identityIssuer->u.string); return res; }
/* #include "config.h" #include "weapon.h" #include "pic_manager.h" #include "sys_config.h" void AddIntPair(json_t *parent, const char *name, int number) { char buf[32]; sprintf(buf, "%d", number); json_insert_pair_into_object(parent, name, json_new_number(buf)); } void AddBoolPair(json_t *parent, const char *name, int value) { json_insert_pair_into_object(parent, name, json_new_bool(value)); } void AddStringPair(json_t *parent, const char *name, const char *s) { if (!s) { json_insert_pair_into_object(parent, name, json_new_string("")); } else { json_insert_pair_into_object( parent, name, json_new_string(json_escape(s))); } } void AddColorPair(json_t *parent, const char *name, const color_t c) { char buf[8]; ColorStr(buf, c); AddStringPair(parent, name, buf); } */ bool YAJLTryLoadValue(yajl_val *node, const char *name) { if (*node == NULL || !YAJL_IS_OBJECT(*node)) { return false; } const char *path[] = { name, NULL }; *node = yajl_tree_get(*node, path, yajl_t_any); return *node != NULL; }
/* 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; }
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; }
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; }
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; }
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); }
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); }
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; }
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; }
int v1(struct http_request *http_req) { struct jsonrpc_request req; int ret; /* We only allow POST/PUT methods. */ if (http_req->method != HTTP_METHOD_POST && http_req->method != HTTP_METHOD_PUT) { http_response_header(http_req, "allow", "POST, PUT"); http_response(http_req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0); return (KORE_RESULT_OK); } /* Read JSON-RPC request. */ if ((ret = jsonrpc_read_request(http_req, &req)) != 0) return jsonrpc_error(&req, ret, NULL); /* Echo command takes and gives back params. */ if (strcmp(req.method, "echo") == 0) { if (!YAJL_IS_ARRAY(req.params)) { jsonrpc_log(&req, LOG_ERR, "Echo only accepts positional params"); return jsonrpc_error(&req, JSONRPC_INVALID_PARAMS, NULL); } for (size_t i = 0; i < req.params->u.array.len; i++) { yajl_val v = req.params->u.array.values[i]; if (!YAJL_IS_STRING(v)) { jsonrpc_log(&req, -3, "Echo only accepts strings"); return jsonrpc_error(&req, JSONRPC_INVALID_PARAMS, NULL); } } return jsonrpc_result(&req, write_string_array_params, NULL); } /* Date command displays date and time according to parameters. */ if (strcmp(req.method, "date") == 0) { time_t time_value; struct tm time_info; char timestamp[33]; struct tm *(*gettm)(const time_t *, struct tm *) = localtime_r; if (YAJL_IS_OBJECT(req.params)) { const char *path[] = {"local", NULL}; yajl_val bf; bf = yajl_tree_get(req.params, path, yajl_t_false); if (bf != NULL) gettm = gmtime_r; } else if (req.params != NULL) { jsonrpc_log(&req, LOG_ERR, "Date only accepts named params"); return jsonrpc_error(&req, JSONRPC_INVALID_PARAMS, NULL); } if ((time_value = time(NULL)) == -1) return jsonrpc_error(&req, -2, "Failed to get date time"); if (gettm(&time_value, &time_info) == NULL) return jsonrpc_error(&req, -3, "Failed to get date time info"); memset(timestamp, 0, sizeof(timestamp)); if (strftime_l(timestamp, sizeof(timestamp) - 1, "%c", &time_info, LC_GLOBAL_LOCALE) == 0) return jsonrpc_error(&req, -4, "Failed to get printable date time"); return jsonrpc_result(&req, write_string, timestamp); } return jsonrpc_error(&req, JSONRPC_METHOD_NOT_FOUND, NULL); }
* Get the window ID from the JSON response. * Returns a signed long integer. * */ long get_window_id(char *json) { const char *path[] = { "container", "window", (const char *) 0 }; yajl_val node; char errbuf[1024]; long window_id; if ((node = yajl_tree_parse((const char *) json, errbuf, sizeof(errbuf))) == NULL) { dbg(if (strlen(errbuf)) fprintf(stderr, "%s", errbuf)); die("JSON response parse error"); } yajl_val window = yajl_tree_get(node, path, yajl_t_number); if (window) // YAJL handles numbers as 64bit (long long), but we know // an X window ID will fit in long. window_id = (long) YAJL_GET_INTEGER(window); else die("Window node not found in the JSON response"); yajl_tree_free(node); return window_id; } int main(int argc, char *argv[]) { int nwin = 1; // for how many windows we wait. int ncmd = 1; // where do command argv starts.
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; }
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; }