void process_file(FILE *input, avro_file_writer_t out, avro_schema_t schema, int verbose, int memstat, int errabort, int strjson, size_t max_str_sz) { json_error_t err; json_t *json; int n = 0; json = json_loadf(input, JSON_DISABLE_EOF_CHECK, &err); while (!feof(input)) { n++; if (verbose && !(n % 1000)) printf("Processing record %d\n", n); if (!json) { if (errabort) { fprintf(stderr, "JSON error on line %d, column %d, pos %d: %s, aborting.\n", n, err.column, err.position, err.text); return; } fprintf(stderr, "JSON error on line %d, column %d, pos %d: %s, skipping to EOL\n", n, err.column, err.position, err.text); while (getc(input) != '\n' && !feof(input)) {}; json = json_loadf(input, JSON_DISABLE_EOF_CHECK, &err); continue; } avro_value_t record; avro_value_iface_t *iface = avro_generic_class_from_schema(schema); avro_generic_value_new(iface, &record); if (!schema_traverse(schema, json, NULL, &record, 0, strjson, max_str_sz)) { if (avro_file_writer_append_value(out, &record)) { fprintf(stderr, "ERROR: avro_file_writer_append_value() FAILED: %s\n", avro_strerror()); exit(EXIT_FAILURE); } } else fprintf(stderr, "Error processing record %d, skipping...\n", n); avro_value_iface_decref(iface); avro_value_decref(&record); json_decref(json); if (memstat && !(n % 1000)) memory_status(); json = json_loadf(input, JSON_DISABLE_EOF_CHECK, &err); } if (memstat) memory_status(); avro_schema_decref(schema); }
static void load_wrong_args() { json_t *json; json_error_t error; json = json_loads(NULL, 0, &error); if (json) fail("json_loads should return NULL if the first argument is NULL"); json = json_loadb(NULL, 0, 0, &error); if (json) fail("json_loadb should return NULL if the first argument is NULL"); json = json_loadf(NULL, 0, &error); if (json) fail("json_loadf should return NULL if the first argument is NULL"); json = json_loadfd(-1, 0, &error); if (json) fail("json_loadfd should return NULL if the first argument is < 0"); json = json_load_file(NULL, 0, &error); if (json) fail("json_load_file should return NULL if the first argument is NULL"); }
static json_t *build_command(int argc, char **argv) { json_t *cmd; int i; // Read blob from stdin if (json_input_arg) { json_error_t err; cmd = json_loadf(stdin, 0, &err); if (cmd == NULL) { fprintf(stderr, "failed to parse JSON from stdin: %s\n", err.text); exit(1); } return cmd; } // Special case: no arguments means that we just want // to verify that the service is up, starting it if // needed if (argc == 0) { return NULL; } cmd = json_array(); for (i = 0; i < argc; i++) { json_array_append_new(cmd, json_string(argv[i])); } return cmd; }
json_t *json_load_file(const char *path, size_t flags, json_error_t *error) { json_t *result; FILE *fp; jsonp_error_init(error, path); if (path == NULL) { error_set(error, NULL, "wrong arguments"); return NULL; } fp = fopen(path, "rb"); if(!fp) { error_set(error, NULL, "unable to open %s: %s", path, strerror(errno)); return NULL; } result = json_loadf(fp, flags, error); fclose(fp); return result; }
void net_read_json_raw(const char* name) { json_t *root; json_error_t err; FILE* f = fopen(name, "r"); root = json_loadf(f, 0, &err); fclose(f); json_t* scene = json_object_get(root, "scene"); json_t* vers = json_object_get(scene, "beesVersion"); int min = json_integer_value(json_object_get(vers, "min")); printf("\r\n got minor version number from json: %d", min); if(min == 3) { // if(sceneData->desc.moduleVersion.min == 3) { printf(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! converting from 0.3"); net_json_convert_min3(root); } net_read_json_scene(json_object_get(root, "scene")); net_read_json_ops(json_object_get(root, "ops")); net_read_json_ins(json_object_get(root, "ins")); net_read_json_outs(json_object_get(root, "outs")); net_read_json_params(json_object_get(root, "params")); net_read_json_presets(json_object_get(root, "presets")); }
static proto_t watchman_read_with_timeout(struct watchman_connection *conn, struct timeval *timeout, struct watchman_error *error) { proto_t result; json_error_t jerror; int ret = 1; if (!timeout || timeout->tv_sec || timeout->tv_usec) ret = block_on_read(fileno(conn->fp), timeout); if (ret == -1) { watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN, "Error encountered blocking on watchman"); return proto_null(); } if (ret != 1) { watchman_err(error, WATCHMAN_ERR_TIMEOUT, "timed out waiting for watchman"); return proto_null(); } if (use_bser_encoding) { bser_t* bser = bser_parse_from_file(conn->fp, NULL); result = proto_from_bser(bser); } else { json_t* json = json_loadf(conn->fp, JSON_DISABLE_EOF_CHECK, &jerror); result = proto_from_json(json); if (fgetc(conn->fp) != '\n') { if (errno == EAGAIN || errno == EWOULDBLOCK) { watchman_err(error, WATCHMAN_ERR_TIMEOUT, "Timeout reading EOL from watchman"); } else { watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN, "No newline at end of reply"); } json_decref(json); return proto_null(); } } if (proto_is_null(result)) { if (errno == EAGAIN) { watchman_err(error, WATCHMAN_ERR_TIMEOUT, "Timeout:EAGAIN reading from watchman."); } else if (errno == EWOULDBLOCK) { watchman_err(error, WATCHMAN_ERR_TIMEOUT, "Timeout:EWOULDBLOCK reading from watchman"); } else { watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN, "Can't parse result from watchman: %s", jerror.text); } return proto_null(); } return result; }
int createRevenueMovementList(FILE * revenue) { json_error_t * error; size_t index; json_t *value; json_t * json_revenue = json_loadf(revenue, 0, error); json_revenue = json_object_get(json_revenue, "revenue"); if (json_revenue == NULL) { return 0; } json_array_foreach(json_revenue, index, value) { struct revenue * newRevenue; newRevenue = calloc(1, sizeof(struct revenue)); json_t * json_title = json_object_get(value, "title"); json_t * json_value = json_object_get(value, "value"); json_t * json_date = json_object_get(value, "date"); json_t * json_desc = json_object_get(value, "description"); char * title = strdup(json_string_value(json_title)); char * description = strdup(json_string_value(json_desc)); float value = json_real_value(json_value); time_t date = json_integer_value(json_date); newRevenue->title = malloc(sizeof(title)); newRevenue->title = title; newRevenue->description = malloc(sizeof(description)); newRevenue->description = description; newRevenue->value = value; newRevenue->date = date; newRevenue->next = firstRevenueMovement; firstRevenueMovement = newRevenue; } return 1; }
struct ast_json *ast_json_load_file(FILE *input, struct ast_json_error *error) { json_error_t jansson_error = {}; struct ast_json *r = NULL; if (input != NULL) { r = (struct ast_json *)json_loadf(input, 0, &jansson_error); copy_error(error, &jansson_error); } else { parse_error(error, "NULL input file", "<null>"); } return r; }
int main(int argc, char* argv[]) { json_error_t error; #if JANSSON_VERSION_HEX >= 0x020600 json_object_seed(1); /* make test results repeatable */ #endif json_t* root = json_loadf(stdin, 0, &error); if (root == NULL) { fprintf(stderr, "Parse error: %s\n at %s line %d col %d\n", error.text, error.source, error.line, error.column); } else { int ret = bser_write_to_file(root, stdout); fprintf(stderr, "Wrote %d bytes\n", ret); } }
void getTotal(char * revenue) { json_error_t * error; FILE * revenueFile = fopen(strcat(revenue, "/revenue.json"), "r"); json_t * json_total = json_loadf(revenueFile, 0, error); json_total = json_object_get(json_total, "total"); total = json_real_value(json_total); free(json_total); fclose(revenueFile); }
int main(int argc, char *argv[]) { int indent = 0; size_t flags = 0; json_t *json; json_error_t error; if(argc != 1) { fprintf(stderr, "usage: %s\n", argv[0]); return 2; } indent = getenv_int("JSON_INDENT"); if(indent < 0 || indent > 255) { fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent); return 2; } if(indent > 0) flags |= JSON_INDENT(indent); if(getenv_int("JSON_COMPACT") > 0) flags |= JSON_COMPACT; if(getenv_int("JSON_ENSURE_ASCII")) flags |= JSON_ENSURE_ASCII; if(getenv_int("JSON_PRESERVE_ORDER")) flags |= JSON_PRESERVE_ORDER; if(getenv_int("JSON_SORT_KEYS")) flags |= JSON_SORT_KEYS; json = json_loadf(stdin, 0, &error); if(!json) { fprintf(stderr, "%d\n%s\n", error.line, error.text); return 1; } json_dumpf(json, stdout, flags); json_decref(json); return 0; }
int load_policy_config(dnssec_kasp_policy_t *policy, const char *filename) { assert(policy); assert(filename); _cleanup_fclose_ FILE *file = fopen(filename, "r"); if (!file) { return DNSSEC_NOT_FOUND; } json_error_t error = { 0 }; _json_cleanup_ json_t *config = json_loadf(file, JSON_LOAD_OPTIONS, &error); if (!config) { return DNSSEC_CONFIG_MALFORMED; } return decode_object(POLICY_ATTRS, config, policy); }
static json_t * watchman_read(struct watchman_connection *conn, struct watchman_error *error) { json_error_t jerror; int flags = JSON_DISABLE_EOF_CHECK; json_t *result = json_loadf(conn->fp, flags, &jerror); if (!result) { watchman_err(error, "Can't parse result from watchman: %s", jerror.text); return NULL; } if (fgetc(conn->fp) != '\n') { watchman_err(error, "No newline at end of reply"); json_decref(result); return NULL; } return result; }
void Config::loadConfig(){ FILE *fp=NULL; json_error_t err; fp = fopen(filename.c_str(), "r"); if (fp != NULL){ json=json_loadf(fp,0,&err); // Newer version of jansoon lib fclose(fp); } else { fprintf(stderr, "Failed to open %s\n",filename.c_str()); } if (json!=NULL){ // Do Stuff } else { if (fp!=NULL){ printf("%d %d %d\n%s\n", err.line, err.column, err.position, err.text); } } }
struct watchman_connection * watchman_connect(struct watchman_error *error) { struct watchman_connection *conn = NULL; FILE *fp = popen("watchman get-sockname 2>/dev/null", "r"); if (!fp) { watchman_err(error, "Could not run watchman get-sockname: %s", strerror(errno)); return NULL; } json_error_t jerror; json_t *json = json_loadf(fp, 0, &jerror); pclose(fp); if (!json) { watchman_err(error, "Got bad JSON from watchman get-sockname: %s", jerror.text); goto done; } if (!json_is_object(json)) { watchman_err(error, "Got bad JSON from watchman get-sockname:" " object expected"); goto bad_json; } json_t *sockname_obj = json_object_get(json, "sockname"); if (!sockname_obj) { watchman_err(error, "Got bad JSON from watchman get-sockname:" " sockname element expected"); goto bad_json; } if (!json_is_string(sockname_obj)) { watchman_err(error, "Got bad JSON from watchman get-sockname:" " sockname is not string"); goto bad_json; } const char *sockname = json_string_value(sockname_obj); conn = watchman_sock_connect(error, sockname); bad_json: json_decref(json); done: return conn; }
json_t * file2json (FILE *fp) { json_error_t jerr; int rc = -1; json_t *j = NULL; assert (fp != NULL); if (0 == (j = json_loadf (fp, 0, &jerr))) { fprintf(stderr, "Failed to parse JSON file: %s\n", jerr.text); } fclose (fp); return j; }
static json_t * watchman_read_with_timeout(struct watchman_connection *conn, struct timeval *timeout, struct watchman_error *error) { json_error_t jerror; int flags = JSON_DISABLE_EOF_CHECK; json_t *result; int ret = block_on_read(fileno(conn->fp), timeout); if (ret == -1) { watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN, "Error encountered blocking on watchman"); } result = json_loadf(conn->fp, flags, &jerror); if (!result) { if (errno == EAGAIN) { watchman_err(error, WATCHMAN_ERR_TIMEOUT, "Timeout:EAGAIN reading from watchman."); } else if (errno == EWOULDBLOCK) { watchman_err(error, WATCHMAN_ERR_TIMEOUT, "Timeout:EWOULDBLOCK reading from watchman"); } else { watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN, "Can't parse result from watchman: %s", jerror.text); } return NULL; } if (fgetc(conn->fp) != '\n') { if (errno == EAGAIN || errno == EWOULDBLOCK) { watchman_err(error, WATCHMAN_ERR_TIMEOUT, "Timeout reading EOL from watchman"); } else { watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN, "No newline at end of reply"); } json_decref(result); return NULL; } return result; }
json_t* TextureManager::loadJSON(char const *path) { json_t *root = NULL; if (FILE* p_inputFile = fopen(path, "r")) { printf("File exists\n"); json_t *jsonFile = NULL; json_error_t *error = NULL; jsonFile = json_loadf(p_inputFile, 0, error); root = json_object_get(jsonFile, "frames"); fclose(p_inputFile); } else { printf("Error loading json %s\n", path); return root; } return root; }
json_t *json_load_file(const char *path, json_error_t *error) { json_t *result; FILE *fp; error_init(error); fp = fopen(path, "r"); if(!fp) { error_set(error, NULL, "unable to open %s: %s", path, strerror(errno)); return NULL; } result = json_loadf(fp, error); fclose(fp); return result; }
json_ref json_load_file(const char *path, size_t flags, json_error_t *error) { FILE *fp; jsonp_error_init(error, path); if (path == nullptr) { error_set(error, nullptr, "wrong arguments"); return nullptr; } fp = fopen(path, "rb"); if(!fp) { error_set(error, nullptr, "unable to open %s: %s", path, strerror(errno)); return nullptr; } auto result = json_loadf(fp, flags, error); fclose(fp); return result; }
void dbload() { // If the file doesn't exist, there is no DB to load if(!FileExists(dbpath)) return; dprintf("Loading database..."); DWORD ticks = GetTickCount(); // Multi-byte (UTF8) file path converted to UTF16 WString databasePathW = StringUtils::Utf8ToUtf16(dbpath); // Decompress the file if compression was enabled bool useCompression = !settingboolget("Engine", "DisableCompression"); LZ4_STATUS lzmaStatus = LZ4_INVALID_ARCHIVE; { lzmaStatus = LZ4_decompress_fileW(databasePathW.c_str(), databasePathW.c_str()); // Check return code if(useCompression && lzmaStatus != LZ4_SUCCESS && lzmaStatus != LZ4_INVALID_ARCHIVE) { dputs("\nInvalid database file!"); return; } } // Open the file for reading by the JSON parser FILE* jsonFile = nullptr; long jsonFileSize = 0; if(_wfopen_s(&jsonFile, databasePathW.c_str(), L"rb")) { dputs("\nFailed to open database file!"); return; } // Get the current file size fseek(jsonFile, 0, SEEK_END); jsonFileSize = ftell(jsonFile); fseek(jsonFile, 0, SEEK_SET); // Verify that the file size is greater than 0. // This corrects a bug when a file exists, but there is no data inside. JSON root = nullptr; if(jsonFileSize > 0) root = json_loadf(jsonFile, 0, 0); // Release the file handle and re-compress fclose(jsonFile); if(lzmaStatus != LZ4_INVALID_ARCHIVE && useCompression) LZ4_compress_fileW(databasePathW.c_str(), databasePathW.c_str()); // Validate JSON load status if(!root) { dputs("\nInvalid database file (JSON)!"); return; } // Finally load all structures CommentCacheLoad(root); LabelCacheLoad(root); BookmarkCacheLoad(root); FunctionCacheLoad(root); LoopCacheLoad(root); BpCacheLoad(root); // Free root json_decref(root); dprintf("%ums\n", GetTickCount() - ticks); }
int do_modify_permissions(FILE *input, recursive_op recurse, option_flags oflags) { int item_count = 0; int error_count = 0; rodsEnv env; rcComm_t *conn = rods_login(&env); if (!conn) goto error; while (!feof(input)) { size_t flags = JSON_DISABLE_EOF_CHECK | JSON_REJECT_DUPLICATES; json_error_t load_error; json_t *target = json_loadf(input, flags, &load_error); if (!target) { if (!feof(input)) { logmsg(ERROR, "JSON error at line %d, column %d: %s", load_error.line, load_error.column, load_error.text); } continue; } item_count++; if (!json_is_object(target)) { logmsg(ERROR, "Item %d in stream was not a JSON object; skipping", item_count); error_count++; json_decref(target); continue; } baton_error_t path_error; char *path = json_to_path(target, &path_error); if (add_error_report(target, &path_error)) { error_count++; } else { json_t *perms = json_object_get(target, JSON_ACCESS_KEY); if (!json_is_array(perms)) { error_count++; set_baton_error(&path_error, -1, "Permissions data for %s is not in " "a JSON array", path); add_error_report(target, &path_error); } else { rodsPath_t rods_path; resolve_rods_path(conn, &env, &rods_path, path, oflags, &path_error); if (add_error_report(target, &path_error)) { error_count++; } else { for (size_t i = 0; i < json_array_size(perms); i++) { json_t *perm = json_array_get(perms, i); baton_error_t mod_error; modify_json_permissions(conn, &rods_path, recurse, perm, &mod_error); if (add_error_report(target, &mod_error)) { error_count++; } } if (rods_path.rodsObjStat) free(rods_path.rodsObjStat); } } } print_json(target); if (unbuffered_flag) fflush(stdout); json_decref(target); if (path) free(path); } // while rcDisconnect(conn); if (error_count > 0) { logmsg(WARN, "Processed %d items with %d errors", item_count, error_count); } else { logmsg(DEBUG, "Processed %d items with %d errors", item_count, error_count); } return error_count; error: if (conn) rcDisconnect(conn); logmsg(ERROR, "Processed %d items with %d errors", item_count, error_count); return 1; }
int do_search_metadata(FILE *input, char *zone_name, option_flags oflags) { int item_count = 0; int error_count = 0; rodsEnv env; rcComm_t *conn = rods_login(&env); if (!conn) goto error; while (!feof(input)) { size_t jflags = JSON_DISABLE_EOF_CHECK | JSON_REJECT_DUPLICATES; json_error_t load_error; json_t *target = json_loadf(input, jflags, &load_error); if (!target) { if (!feof(input)) { logmsg(ERROR, "JSON error at line %d, column %d: %s", load_error.line, load_error.column, load_error.text); } continue; } item_count++; if (!json_is_object(target)) { logmsg(ERROR, "Item %d in stream was not a JSON object; skipping", item_count); error_count++; json_decref(target); continue; } json_t *results = NULL; if (has_collection(target)) { baton_error_t resolve_error; resolve_collection(target, conn, &env, oflags, &resolve_error); if (add_error_report(target, &resolve_error)) { error_count++; } else { baton_error_t search_error; results = search_metadata(conn, target, zone_name, oflags, &search_error); if (add_error_report(target, &search_error)) { error_count++; print_json(target); } else { print_json(results); } } } else { baton_error_t search_error; results = search_metadata(conn, target, zone_name, oflags, &search_error); if (add_error_report(target, &search_error)) { error_count++; print_json(target); } else { print_json(results); } } if (unbuffered_flag) fflush(stdout); if (results) json_decref(results); json_decref(target); } // while rcDisconnect(conn); if (error_count > 0) { logmsg(WARN, "Processed %d items with %d errors", item_count, error_count); } else { logmsg(DEBUG, "Processed %d items with %d errors", item_count, error_count); } return error_count; error: if (conn) rcDisconnect(conn); logmsg(ERROR, "Processed %d items with %d errors", item_count, error_count); return 1; }
void MapFormat::loadFromFile(String filename) { this->filename = filename; FILE* file = fopen(filename.c_str(), "r"); if (file) { json_error_t* error = new json_error_t(); json_t* root = json_loadf(file, 0, error); delete error; int blockSize = blocks.size(); json_t* blocksJson = json_array(); // const char* charBuffer = json_string_value(json_object_get(root, "name")); // strcpy_s(name, strlen(charBuffer), charBuffer); // charBuffer = json_string_value(json_object_get(root, "description")); // strcpy_s(description, strlen(charBuffer), charBuffer); firstRespawnOffset = json_integer_value(json_object_get(root, "description")); json_t* blockArray = json_object_get(root, "blocks"); int blockCount = json_array_size(blockArray); for (int i = 0; i < blockCount; i++) { Block* block = new Block(); json_t* indBlock = json_array_get(blockArray, i); block->extraArgsBit = json_integer_value(json_object_get(indBlock, "extra-args"));//, json_integer(block->extraArgsBit)); block->origin.x = json_integer_value(json_object_get(indBlock, "origin-x")); block->origin.y = json_integer_value(json_object_get(indBlock, "origin-y")); block->origin.z = json_integer_value(json_object_get(indBlock, "origin-z")); block->materialIndex = (WorldBox::CustomMaterial)json_integer_value(json_object_get(indBlock, "material-index")); if ((json_object_get(indBlock, "is-point"))) { block->isPoint = true; } else if ((json_object_get(indBlock, "is-respawn"))) { block->isRespawn = true; } if (json_integer_value(json_object_get(indBlock, "rotation"))) { block->rotation = Ogre::Degree(json_integer_value(json_object_get(indBlock, "rotation"))); } if (json_t* movableArgs = json_object_get(indBlock, "movable-args")) { if (json_object_get(movableArgs, "linear-speed"))//> 0 && Math::IFloor(block->linearLocation.length()) > 0) { block->linearSpeed = json_integer_value(json_object_get(movableArgs, "linear-speed")); block->linearLocation.x = json_integer_value(json_object_get(movableArgs, "linear-location-x")); block->linearLocation.y = json_integer_value(json_object_get(movableArgs, "linear-location-y")); block->linearLocation.z = json_integer_value(json_object_get(movableArgs, "linear-location-z")); if (json_object_get(movableArgs, "rotation-movement") && json_integer_value(json_object_get(movableArgs, "rotation-movement"))) { block->isRightRotationMovement = true; block->isRotationMovement = json_integer_value(json_object_get(movableArgs, "is-rotation-right")); } } if (json_object_get(movableArgs, "rotation-speed")) { block->rotationSpeed = json_integer_value(json_object_get(movableArgs, "rotation-speed")); } } if (json_object_get(indBlock, "special-block")) { block->specialBlockId = json_integer_value(json_object_get(indBlock, "special-block")); } blocks.push_back(block); } json_delete(root); fclose(file); } }
int do_list_paths(FILE *input, print_flags pflags) { int path_count = 0; int error_count = 0; rodsEnv env; rcComm_t *conn = rods_login(&env); if (!conn) goto error; size_t jflags = JSON_DISABLE_EOF_CHECK | JSON_REJECT_DUPLICATES; while (!feof(input)) { json_error_t load_error; json_t *target = json_loadf(input, jflags, &load_error); if (!target) { if (!feof(input)) { log(ERROR, "JSON error at line %d, column %d: %s", load_error.line, load_error.column, load_error.text); } continue; } baton_error_t path_error; char *path = json_to_path(target, &path_error); path_count++; if (path_error.code != 0) { error_count++; add_error_value(target, &path_error); print_json(target); } else { rodsPath_t rods_path; int status = resolve_rods_path(conn, &env, &rods_path, path); if (status < 0) { error_count++; set_baton_error(&path_error, status, "Failed to resolve path '%s'", path); add_error_value(target, &path_error); print_json(target); } else { baton_error_t error; json_t *results = list_path(conn, &rods_path, pflags, &error); if (error.code != 0) { error_count++; add_error_value(target, &error); print_json(target); } else { print_json(results); json_decref(results); } } if (rods_path.rodsObjStat) free(rods_path.rodsObjStat); } if (unbuffered_flag) fflush(stdout); json_decref(target); free(path); } // while rcDisconnect(conn); log(DEBUG, "Processed %d paths with %d errors", path_count, error_count); return error_count; error: if (conn) rcDisconnect(conn); log(ERROR, "Processed %d paths with %d errors", path_count, error_count); return 1; }
int main(int argc, char *argv[]) { int indent = 0; size_t flags = 0; json_t *json; json_error_t error; FILE* file = stdin; if(argc > 2) { fprintf(stderr, "usage: %s\n", argv[0]); return 2; } if(argc == 2) { file = fopen(argv[1], "r"); } if( !file ) { perror("invalid file"); return 2; } indent = getenv_int("JSON_INDENT"); if(indent < 0 || indent > 255) { fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent); return 2; } if(indent > 0) flags |= JSON_INDENT(indent); if(getenv_int("JSON_COMPACT") > 0) flags |= JSON_COMPACT; if(getenv_int("JSON_ENSURE_ASCII")) flags |= JSON_ENSURE_ASCII; if(getenv_int("JSON_PRESERVE_ORDER")) flags |= JSON_PRESERVE_ORDER; if(getenv_int("JSON_SORT_KEYS")) flags |= JSON_SORT_KEYS; if(getenv_int("JSON_ASSUME_OBJECT")) flags |= JSON_ASSUME_OBJECT; if(getenv_int("JSON_ALLOW_EQUAL_SIGN")) flags |= JSON_ALLOW_EQUAL_SIGN; if(getenv_int("JSON_QUOTELESS_KEYS")) flags |= JSON_QUOTELESS_KEYS; if(getenv_int("STRIP")) { /* Load to memory, strip leading and trailing whitespace */ size_t size = 0, used = 0; char *buffer = NULL; while(1) { int count; size = (size == 0 ? 128 : size * 2); buffer = realloc(buffer, size); if(!buffer) { fprintf(stderr, "Unable to allocate %d bytes\n", (int)size); return 1; } count = fread(buffer + used, 1, size - used, stdin); if(count < size - used) { buffer[used + count] = '\0'; break; } used += count; } json = json_loads(strip(buffer), flags, &error); free(buffer); } else json = json_loadf(file, flags, &error); if(!json) { fprintf(stderr, "%d %d %d\n%s\n", error.line, error.column, error.position, error.text); return 1; } json_dumpf(json, stdout, flags); json_decref(json); return 0; }
int use_conf(char *test_path) { int ret; size_t flags = 0; char filename[1024], errstr[1024]; char *buffer; FILE *infile, *conffile; json_t *json; json_error_t error; sprintf(filename, "%s%cinput", test_path, dir_sep); if (!(infile = fopen(filename, "rb"))) { fprintf(stderr, "Could not open \"%s\"\n", filename); return 2; } sprintf(filename, "%s%cenv", test_path, dir_sep); conffile = fopen(filename, "rb"); if (conffile) { read_conf(conffile); fclose(conffile); } if (conf.indent < 0 || conf.indent > 255) { fprintf(stderr, "invalid value for JSON_INDENT: %d\n", conf.indent); return 2; } if (conf.indent) flags |= JSON_INDENT(conf.indent); if (conf.compact) flags |= JSON_COMPACT; if (conf.ensure_ascii) flags |= JSON_ENSURE_ASCII; if (conf.preserve_order) flags |= JSON_PRESERVE_ORDER; if (conf.sort_keys) flags |= JSON_SORT_KEYS; if (conf.strip) { /* Load to memory, strip leading and trailing whitespace */ buffer = loadfile(infile); json = json_loads(strip(buffer), 0, &error); free(buffer); } else json = json_loadf(infile, 0, &error); fclose(infile); if (!json) { sprintf(errstr, "%d %d %d\n%s\n", error.line, error.column, error.position, error.text); ret = cmpfile(errstr, test_path, "error"); return ret; } buffer = json_dumps(json, flags); ret = cmpfile(buffer, test_path, "output"); free(buffer); json_decref(json); return ret; }
int sqlite3_open(const char* filename, /* Database filename (UTF-8) */ sqlite3** ppDb /* OUT: SQLite db handle */ ) { MYSQL* con = mysql_init(NULL); const char* host = NULL; const char* user = NULL; const char* pass = NULL; const char* db = NULL; if (con == NULL) { fprintf(stderr, "sqlite3_open() -> mysql_init() failed\n"); return 0; } FILE* fh = fopen(filename, "r"); if (fh) { json_t* root = json_loadf(fh, JSON_DECODE_ANY, NULL); if (root) { // We got JSON host = json_string_value(json_object_get(root, "host")); user = json_string_value(json_object_get(root, "user")); pass = json_string_value(json_object_get(root, "pass")); db = json_string_value(json_object_get(root, "db")); if (mysql_real_connect(con, host, user, pass, NULL, 0, NULL, 0) == NULL) { finish_with_error(con); *ppDb = NULL; } else { if (mysql_select_db(con, db) != 0) { char* sql; int len = asprintf(&sql, "CREATE DATABASE %s", db); mysql_real_query(con, sql, len); free(sql); } if (mysql_select_db(con, db) != 0) { finish_with_error(con); *ppDb = NULL; } else { *ppDb = (sqlite3*)con; } } // Cleanup json_decref(root); } fclose(fh); } return 0; };
void emotions_Analyze(char* path, char* link){ //getting full path to .exe directory //requesting data from Emotion API using curl, saving JSON response to html char cmd[400]; char exportFileName[200]; sprintf(exportFileName, "%s\\emotions.json", path); char resultFileName[200]; sprintf(resultFileName, "%s\\emotions.txt", path); sprintf(cmd, "%s\\curl.exe -o %s -XPOST https://api.projectoxford.ai/emotion/v1.0/recognize?subscription-key=349f1c7ff1e642498d333cb2fe5fab25 -k --data-binary @%s -H \"Content-Type:application/octet-stream\"", path, exportFileName, link); system(cmd);// WARNING! NO UPDATE IF COMMENTED system("cls"); //parsing and structurizing JSON response, writing to stream FILE* fJSON = fopen(exportFileName, "r"); FILE* output = fopen(resultFileName, "w"); json_error_t error; json_t* root = json_loadf(fJSON, 0, &error); json_t* wrongURL = json_object_get(root, "error"); if (json_is_object(wrongURL)){ //failed to download photo by API fprintf(output, "%s", "@"); fprintf(stdout, "%s", "Download fail"); } else if (json_array_size(root) == 0){//no face detected fprintf(output, "%s", "#"); fprintf(stdout, "%s", "No faces on photo. Come back!"); } for (int i = 0; i < json_array_size(root); i++){ json_t* data = json_array_get(root, i); json_t* scores = json_object_get(data, "scores"); json_t* anger = json_object_get(scores, "anger"); json_t* contempt = json_object_get(scores, "contempt"); json_t* disgust = json_object_get(scores, "disgust"); json_t* fear = json_object_get(scores, "fear"); json_t* happiness = json_object_get(scores, "happiness"); json_t* neutral = json_object_get(scores, "neutral"); json_t* sadness = json_object_get(scores, "sadness"); json_t* surprise = json_object_get(scores, "surprise"); puts("Your emotions:\n"); fprintf(stdout, "Anger: %.3lf %%\n", json_real_value(anger)*100); fprintf(stdout, "Contempt: %.3lf %%\n", json_real_value(contempt) * 100); fprintf(stdout, "Disgust: %.3lf %%\n", json_real_value(disgust) * 100); fprintf(stdout, "Fear: %.3lf %%\n", json_real_value(fear) * 100); fprintf(stdout, "Happiness: %.3lf %%\n", json_real_value(happiness) * 100); fprintf(stdout, "Neutral: %.3lf %%\n", json_real_value(neutral) * 100); fprintf(stdout, "Sadness: %.3lf %%\n", json_real_value(sadness) * 100); fprintf(stdout, "Surprise: %.3lf %%\n", json_real_value(surprise)*100); fprintf(output, "%.15lf\n", json_real_value(anger)); fprintf(output, "%.15lf\n", json_real_value(contempt)); fprintf(output, "%.15lf\n", json_real_value(disgust)); fprintf(output, "%.15lf\n", json_real_value(fear)); fprintf(output, "%.15lf\n", json_real_value(happiness)); fprintf(output, "%.15lf\n", (json_real_value(neutral)>0.95) ? json_real_value(neutral):0.0); fprintf(output, "%.15lf\n", json_real_value(sadness)); fprintf(output, "%.15lf\n", json_real_value(surprise)); puts(""); } fclose(fJSON); fclose(output); }
int use_env() { int indent; size_t flags = 0; json_t *json; json_error_t error; #ifdef _WIN32 /* On Windows, set stdout and stderr to binary mode to avoid outputting DOS line terminators */ _setmode(_fileno(stdout), _O_BINARY); _setmode(_fileno(stderr), _O_BINARY); #endif indent = getenv_int("JSON_INDENT"); if(indent < 0 || indent > 255) { fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent); return 2; } if(indent > 0) flags |= JSON_INDENT(indent); if(getenv_int("JSON_COMPACT") > 0) flags |= JSON_COMPACT; if(getenv_int("JSON_ENSURE_ASCII")) flags |= JSON_ENSURE_ASCII; if(getenv_int("JSON_PRESERVE_ORDER")) flags |= JSON_PRESERVE_ORDER; if(getenv_int("JSON_SORT_KEYS")) flags |= JSON_SORT_KEYS; if(getenv_int("STRIP")) { /* Load to memory, strip leading and trailing whitespace */ size_t size = 0, used = 0; char *buffer = NULL; while(1) { size_t count; size = (size == 0 ? 128 : size * 2); buffer = realloc(buffer, size); if(!buffer) { fprintf(stderr, "Unable to allocate %d bytes\n", (int)size); return 1; } count = fread(buffer + used, 1, size - used, stdin); if(count < size - used) { buffer[used + count] = '\0'; break; } used += count; } json = json_loads(strip(buffer), 0, &error); free(buffer); } else json = json_loadf(stdin, 0, &error); if(!json) { fprintf(stderr, "%d %d %d\n%s\n", error.line, error.column, error.position, error.text); return 1; } json_dumpf(json, stdout, flags); json_decref(json); return 0; }