void ParserImpl::handle(const std::string& json) { if (!_allowNullByte && json.find("\\u0000") != json.npos) throw JSONException("Null bytes in strings not allowed."); try { json_open_buffer(_pJSON, json.data(), json.size()); checkError(); ////////////////////////////////// // Underlying parser is capable of parsing multiple consecutive JSONs; // we do not currently support this feature; to force error on // excessive characters past valid JSON end, this MUST be called // AFTER opening the buffer - otherwise it is overwritten by // json_open*() call, which calls internal init() json_set_streaming(_pJSON, false); ///////////////////////////////// handle(); checkError(); if (JSON_DONE != json_next(_pJSON)) throw JSONException("Excess characters found after JSON end."); json_close(_pJSON); } catch (std::exception&) { json_close(_pJSON); throw; } }
void display_close(time_t now) { switch(DisplayMode) { case DisplayReport: report_close(); break; case DisplayTXT: txt_close(); break; case DisplayXML: xml_close(); break; case DisplayJSON: json_close(); break; case DisplayCSV: csv_close(now); break; case DisplayCurses: #ifdef IPINFO if (ipinfo_no >= 0) asn_close(); #endif mtr_curses_close(); break; case DisplaySplit: split_close(); break; case DisplayGTK: gtk_close(); break; } }
Scene *loadScene(char *aFilename) { Scene *t = new Scene; json_stream json; FILE * f = fopen(aFilename, "rb"); json_open_stream(&json, f); json_type type = json_next(&json); assert(type == JSON_OBJECT); while (json_peek(&json) != JSON_OBJECT_END && json_peek(&json) != JSON_ERROR) { type = json_next(&json); assert(type == JSON_STRING); const char *otype = json_get_string(&json, 0); if (strcmp(otype, "material") == 0) { const char *name = "[untitled]"; glm::vec3 diffuse(1); glm::vec3 specular(0); glm::vec3 ambient(0); float opacity = 1; float reflection = 0; type = json_next(&json); assert(type == JSON_OBJECT); while (json_peek(&json) != JSON_OBJECT_END) { type = json_next(&json); assert(type == JSON_STRING); otype = json_get_string(&json, 0); if (!getJSONString(&json, "name", otype, (char**)&name)) if (!getJSONVec3(&json, "diffuse", otype, diffuse)) if (!getJSONVec3(&json, "ambient", otype, ambient)) if (!getJSONVec3(&json, "specular", otype, specular)) if (!getJSONNumber(&json, "opacity", otype, opacity)) if (!getJSONNumber(&json, "reflection", otype, reflection)) assert(0 && "error parsing material"); } type = json_next(&json); assert(type == JSON_OBJECT_END); Material *m = new Material(); m->mName = (char*)name; m->mDiffuse = diffuse; m->mAmbient = ambient; m->mSpecular = specular; m->mOpacity = opacity; m->mReflection = reflection; m->mNext = t->mMaterial; t->mMaterial = m; } else if (strcmp(otype, "box") == 0) { const char *name = "[untitled]"; const char *material = "default"; glm::vec3 center; glm::vec3 size; float dynamic = 0; type = json_next(&json); assert(type == JSON_OBJECT); while (json_peek(&json) != JSON_OBJECT_END) { type = json_next(&json); assert(type == JSON_STRING); otype = json_get_string(&json, 0); if (!getJSONString(&json, "name", otype, (char**)&name)) if (!getJSONString(&json, "material", otype, (char**)&material)) if (!getJSONNumber(&json, "dynamic", otype, dynamic)) if (!getJSONVec3(&json, "position", otype, center)) if (!getJSONVec3(&json, "center", otype, center)) if (!getJSONVec3(&json, "size", otype, size)) assert(0 && "error parsing box"); } type = json_next(&json); assert(type == JSON_OBJECT_END); SceneObject *so; t->insert(so = new Box((char*)name, center, size, t->getMaterialByName((char*)material))); so->mDynamic = dynamic != 0; } else if (strcmp(otype, "plane") == 0) { const char *name = "[untitled]"; const char *material = "default"; glm::vec3 point; glm::vec3 normal; float dynamic = 0; type = json_next(&json); assert(type == JSON_OBJECT); while (json_peek(&json) != JSON_OBJECT_END) { type = json_next(&json); assert(type == JSON_STRING); otype = json_get_string(&json, 0); if (!getJSONString(&json, "name", otype, (char**)&name)) if (!getJSONString(&json, "material", otype, (char**)&material)) if (!getJSONNumber(&json, "dynamic", otype, dynamic)) if (!getJSONVec3(&json, "point", otype, point)) if (!getJSONVec3(&json, "normal", otype, normal)) assert(0 && "error parsing box"); } type = json_next(&json); assert(type == JSON_OBJECT_END); SceneObject *so; t->insert(so = new Plane((char*)name, point, normal, t->getMaterialByName((char*)material))); so->mDynamic = dynamic != 0; } else if (strcmp(otype, "sphere") == 0) { const char *name = "[untitled]"; const char *material = "default"; glm::vec3 center; float radius = 5; float dynamic = 0; type = json_next(&json); assert(type == JSON_OBJECT); while (json_peek(&json) != JSON_OBJECT_END) { type = json_next(&json); assert(type == JSON_STRING); otype = json_get_string(&json, 0); if (!getJSONString(&json, "name", otype, (char**)&name)) if (!getJSONString(&json, "material", otype, (char**)&material)) if (!getJSONNumber(&json, "dynamic", otype, dynamic)) if (!getJSONVec3(&json, "center", otype, center)) if (!getJSONVec3(&json, "position", otype, center)) if (!getJSONNumber(&json, "radius", otype, radius)) assert(0 && "error parsing sphere"); } type = json_next(&json); assert(type == JSON_OBJECT_END); SceneObject *so; t->insert(so = new Sphere((char*)name, center, radius, t->getMaterialByName((char*)material))); so->mDynamic = dynamic != 0; } else if (strcmp(otype, "light") == 0) { const char *name = "[untitled]"; const char *material = "default"; glm::vec3 position; type = json_next(&json); assert(type == JSON_OBJECT); while (json_peek(&json) != JSON_OBJECT_END) { type = json_next(&json); assert(type == JSON_STRING); otype = json_get_string(&json, 0); if (!getJSONString(&json, "name", otype, (char**)&name)) if (!getJSONString(&json, "material", otype, (char**)&material)) if (!getJSONVec3(&json, "position", otype, position)) assert(0 && "error parsing light"); } type = json_next(&json); assert(type == JSON_OBJECT_END); t->insert(new Light((char*)name, position, t->getMaterialByName((char*)material))); } else { assert(0); } } type = json_next(&json); if (type == JSON_ERROR) { const char * err = json_get_error(&json); err = err; } assert(type == JSON_OBJECT_END); type = json_next(&json); assert(type == JSON_DONE); json_close(&json); setupScene(t); t->optimize(); return t; };
int main (int argc, char* argv[]) { char filename1[200]; char filename2[200]; char pairs_file[200]; char fileout[200]; FILE* query_fp = 0; FILE* reference_fp = 0; FILE* fp_pairs = 0; FILE* fpout = stdout; int total_pairs = 0; pair_struct* pairs = 0; extern FILE *scaninfo_file; setup_match_types(); /* Set Default Parameter Values*/ length_5p_for_weighting = 8; /* The 5' sequence length to be weighted except for the last residue */ scale = 4.0; /* The 5' miRNA scaling parameter */ strict = 0; /* Strict seed model on/off*/ debug = 0; /* Debugging mode on/off*/ key_value_pairs = 0; gap_open = -9.0; /* Gap-open Penalty*/ gap_extend = -4.0; /* Gap-extend Penalty*/ score_threshold = 140.0; /* SW Score Threshold for reporting hits*/ score_ceiling = 0; /* Default upper limit to score. If zero, this is not used. */ energy_threshold = 1.0; /* Energy Threshold (DG) for reporting hits*/ verbosity = 1; /* Verbose mode on/off*/ brief_output = 0; /* Brief output off by default */ rusage_output = 0; /* rusage output off by default */ outfile = 0; /* Dump to file on/off*/ truncated = 0; /* Truncate sequences on/off*/ no_energy = 1; /* Turn off Energy Calcs - FASTER*/ restricted = 0; /* Perform restricted search space*/ parse_command_line(argc, argv, filename1, filename2, fileout, pairs_file); if (gap_open > 0.0 || gap_extend > 0.0) { fprintf(stderr, "Error: gap penalties may not be greater than 0\n"); return 1; } if (truncated < 0) { fprintf(stderr, "Error: negative value give for UTR truncation\n"); return 1; } if ((query_fp = fopen(filename1, "r")) == NULL) { fprintf(stderr, "Error: Cannot open file %s\n", filename1); return 1; } if ((reference_fp = fopen(filename2, "r")) == NULL) { fprintf(stderr, "Error: Cannot open file %s\n", filename2); return 1; } fclose(reference_fp); if ((outfile) && ((fpout = fopen(fileout, "w")) == NULL)) { fprintf(stderr, "Error: Cannot create output file %s\n", fileout); return 1; } if (restricted) { if ((fp_pairs = fopen(pairs_file, "r")) == NULL) { fprintf(stderr, "Error: Cannot open restrict pairs file %s\n", pairs_file); return 1; } /* Initialize the pairs list for restriced searches*/ total_pairs = load_pairs(fp_pairs, &pairs); fclose(fp_pairs); } fflush(fpout); initialize_globals(); if(!brief_output) print_parameters(filename1, filename2, fpout); if (restricted && verbosity) { printf("Performing Restricted Scan on:%d pairs\n", total_pairs); } find_targets(query_fp, fpout, pairs, total_pairs, filename2); #ifdef USE_RUSAGE if(rusage_output) { struct rusage ru; if(getrusage(RUSAGE_SELF,&ru) != 0) { fprintf(stderr,"Could not get rusage data\n"); if(errno) fprintf(stderr,"Reason: %s",strerror(errno)); } else { printf("User CPU time: %ld.%06ld\n",ru.ru_utime.tv_sec,ru.ru_utime.tv_usec); printf("Max RSS: %ld KB\n",ru.ru_maxrss); } } #endif // RUSAGE destroy_globals(); if (outfile) fclose(fpout); if (scaninfo_file != stdout && scaninfo_file != stderr && scaninfo_file != NULL) fclose(scaninfo_file); json_close(); fclose(query_fp); return 0; }
static void linda_store_handler(void *p1, const uuid *u, const void *_s, int len) { linda *l = (linda*)p1; const char *s = (const char*)_s; if (len > 0) // add { json *j = json_open(s); json *j1 = json_get_object(j); json *jid = json_find(j1, LINDA_ID); if (!jid) return; if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (l->sl && !l->is_int) { printf("linda_store_handler: expected integer id\n"); return; } if (!l->sl) { l->sl = sb_int_uuid_create2(); l->is_int = 1; } sb_int_uuid_set(l->sl, k, u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (l->sl && !l->is_string) { printf("linda_store_handler: expected string id\n"); return; } if (!l->sl) { l->sl = sb_string_uuid_create2(); l->is_string = 1; } sb_string_uuid_set(l->sl, k, u); } json_close(j); } else if (len < 0) // remove (with hint) { json *j = json_open(s); json *j1 = json_get_object(j); json *jid = json_find(j1, LINDA_ID); if (!jid) return; if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (l->sl && !l->is_int) { printf("linda_out: expected integer id\n"); return; } if (!l->sl) { l->sl = sb_int_uuid_create2(); l->is_int = 1; } sb_int_uuid_erase(l->sl, k, u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (l->sl && !l->is_string) { printf("linda_out: expected string id\n"); return; } if (!l->sl) { l->sl = sb_string_uuid_create2(); l->is_string = 1; } sb_string_uuid_erase(l->sl, k, u); } json_close(j); } else // remove (brute search) { sb_uuid_efface(l->sl, u); } }
int linda_rm(hlinda *h, const char *s) { json *j = json_open(s); json *j1 = json_get_object(j); int is_int = 0, is_string = 0; const char *string_id = NULL; long long int_id = 0; uuid u; json *joid = json_find(j1, LINDA_OID); if (!joid) { json_close(j); return 0; } uuid_from_string(json_get_string(joid), &u); json *jid = json_find(j1, LINDA_ID); if (jid) { if (h->l->is_int && !json_is_integer(jid)) { printf("linda_read: expected integer id\n"); json_close(j); return 0; } else if (h->l->is_string && !json_is_string(jid)) { printf("linda_read: expected string id\n"); json_close(j); return 0; } if (json_is_integer(jid)) { int_id = json_get_integer(jid); is_int = 1; } else if (json_is_string(jid)) { string_id = json_get_string(jid); json_close(h->jquery); is_string = 1; } else { json_close(j); return 0; } } json_close(j); if (is_int) { char tmpbuf[1024]; int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, int_id); store_hrem2(h->hst, &u, tmpbuf, tmplen); sb_int_uuid_erase(h->l->sl, int_id, &u); } else if (is_string) { char tmpbuf[1024], tmpbuf2[1024]; json_format_string(string_id, tmpbuf2, sizeof(tmpbuf2)); int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2); store_hrem2(h->hst, &u, tmpbuf, tmplen); sb_string_uuid_erase(h->l->sl, string_id, &u); } else { store_rem(h->l->st, &u); sb_uuid_efface(h->l->sl, &u); } return 1; }
int linda_out(hlinda *h, const char *s) { if (!h) return 0; json *j = json_open(s); json *j1 = json_get_object(j); json *joid = json_find(j1, LINDA_OID); uuid u; if (joid) { uuid_from_string(json_get_string(joid), &u); } else uuid_gen(&u); json *jid = json_find(j1, LINDA_ID); if (jid) { if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (h->l->sl && !h->l->is_int) { printf("linda_out: expected integer id\n"); return 0; } if (!h->l->sl) { h->l->sl = sb_int_uuid_create2(); h->l->is_int = 1; } sb_int_uuid_set(h->l->sl, k, &u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (h->l->sl && !h->l->is_string) { printf("linda_out: expected string id\n"); return 0; } if (!h->l->sl) { h->l->sl = sb_string_uuid_create2(); h->l->is_string = 1; } sb_string_uuid_set(h->l->sl, k, &u); } } store_hadd(h->hst, &u, s, strlen(s)); h->last_oid = u; json_close(j); return 0; }
static int linda_read(hlinda *h, const char *s, const char **buf, int rm, int nowait) { json *j = json_open(s); json *j1 = json_get_object(j); h->oid.u1 = h->oid.u2 = 0; int is_int = 0, is_string = 0; json *jid = json_find(j1, LINDA_ID); if (jid) { if (h->l->is_int && !json_is_integer(jid)) { printf("linda_read: expected integer id\n"); json_close(j); return 0; } else if (h->l->is_string && !json_is_string(jid)) { printf("linda_read: expected string id\n"); json_close(j); return 0; } if (json_is_integer(jid)) { h->int_id = json_get_integer(jid); h->jquery = json_open(s); sb_int_uuid_find(h->l->sl, h->int_id, &read_int_handler, h); json_close(h->jquery); is_int = 1; } else if (json_is_string(jid)) { h->string_id = json_get_string(jid); h->jquery = json_open(s); sb_string_uuid_find(h->l->sl, h->string_id, &read_string_handler, h); json_close(h->jquery); is_string = 1; } else { json_close(j); return 0; } } else { h->jquery = json_open(s); sb_iter(h->l->sl, &read_handler, h); json_close(h->jquery); } json_close(j); if (!h->oid.u1 && !h->oid.u2) return 0; if (rm) { if (is_int) { char tmpbuf[1024]; int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, h->int_id); store_hrem2(h->hst, &h->oid, tmpbuf, tmplen); sb_int_uuid_erase(h->l->sl, h->int_id, &h->oid); } else if (is_string) { char tmpbuf[1024], tmpbuf2[1024]; json_format_string(h->string_id, tmpbuf2, sizeof(tmpbuf2)); int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2); store_hrem2(h->hst, &h->oid, tmpbuf, tmplen); sb_string_uuid_erase(h->l->sl, h->string_id, &h->oid); } else { store_hrem(h->hst, &h->oid); sb_uuid_efface(h->l->sl, &h->oid); } } *buf = h->dst; return 1; }
static int read_handler(void *arg, void *k, void *v) { hlinda *h = (hlinda*)arg; uuid *u = (uuid*)v; if (!store_get(h->l->st, u, (void**)&h->dst, &h->len)) return 0; int match = 1; json *j1 = json_get_object(h->jquery); json *jdst = json_open(h->dst); json *j2 = json_get_object(jdst); size_t i, cnt = json_count(j1); for (i = 0; i < cnt; i++) { json *j1it = json_index(j1, i); const char *name = json_get_string(j1it); if (name[0] == '$') continue; json *j2it = json_find(j2, name); if (!j2it) { match = 0; continue; } if (json_is_integer(j1it)) { if (!json_is_integer(j2it)) { match = 0; break; } if (json_get_integer(j1it) != json_get_integer(j2it)) { match = 0; break; } } else if (json_is_real(j1it)) { if (!json_is_real(j2it)) { match = 0; break; } if (json_get_real(j1it) != json_get_real(j2it)) { match = 0; break; } } else if (json_is_string(j1it)) { if (!json_is_string(j2it)) { match = 0; break; } if (strcmp(json_get_string(j1it), json_get_string(j2it))) { match = 0; break; } } else if (json_is_true(j1it)) { if (!json_is_true(j2it)) { match = 0; break; } } else if (json_is_false(j1it)) { if (!json_is_false(j2it)) { match = 0; break; } } else if (json_is_null(j1it)) { if (!json_is_null(j2it)) { match = 0; break; } } } json_close(jdst); if (!match) return 1; h->oid.u1 = u->u1; h->oid.u2 = u->u2; return 0; }