int main(int argc, const char * argv[]) { sm_set_program_name(argv[0]); const char*input_filename; const char*output_pattern_op; struct option* ops = options_allocate(3); options_string(ops, "in", &input_filename, "stdin", "input file (JSON)"); options_string(ops, "out", &output_pattern_op, "./ld_split^02d.json", "output pattern; printf() pattern, but write '^' instead of '%'"); if(!options_parse_args(ops, argc, argv)) { fprintf(stderr, "%s : splits a JSON file into many files." "\n\nOptions:\n", argv[0]); options_print_help(ops, stderr); return -1; } /* Substitute "$" with "%" */ char output_pattern[256]; strcpy(output_pattern, output_pattern_op); char *f = output_pattern; while(*f) { if(*f=='^') *f='%'; f++; } fputs(output_pattern, stderr); FILE * input_stream = open_file_for_reading(input_filename); int count = 0; JO jo; while( (jo = json_read_stream(input_stream)) ) { char filename[1000]; sprintf(filename, output_pattern, count); if(!count) { } sm_debug("Writing to file (%s) %s\n", output_pattern, filename); FILE * f = open_file_for_writing(filename); if(!f) return -1; fputs(json_object_to_json_string(jo), f); jo_free(jo); fclose(f); count++; } return 0; }
int main(int argc, const char * argv[]) { sm_set_program_name(argv[0]); int period; int phase; const char*input_filename; const char*output_filename; struct csm_option* ops = csm_options_allocate(3); csm_options_int(ops, "period", &period, 1, "Period of objects to extract."); csm_options_int(ops, "phase", &phase, 0, "Phase (=0 starts with the first object)"); csm_options_string(ops, "in", &input_filename, "stdin", "input file (JSON)"); csm_options_string(ops, "out", &output_filename, "stdout", "output file (JSON)"); if(!csm_options_parse_args(ops, argc, argv)) { fprintf(stderr, "%s : decimates a JSON stream." "\n\ncsm_options:\n", argv[0]); csm_options_print_help(ops, stderr); return -1; } if(period < 1) { sm_error("Period must be >= 1.\n"); return 2; } FILE * input_stream = open_file_for_reading(input_filename); FILE *output_stream = open_file_for_writing(output_filename); if(!input_stream || !output_stream) return -1; int count = 0; while(1) { JO jo = json_read_stream(input_stream); if(!jo) { if(feof(input_stream)) break; sm_error("Malformed JSON\n"); return -1; } if( (count - phase) % period == 0) { const char * s = json_object_to_json_string(jo); fputs(s,output_stream); fputs("\n",output_stream); } jo_free(jo); count++; } return 0; }
int main(int argc, const char * argv[]) { sm_set_program_name(argv[0]); int nth; const char*input_filename; const char*output_filename; struct csm_option* ops = csm_options_allocate(3); csm_options_int(ops, "nth", &nth, 0, "Index of object to extract."); csm_options_string(ops, "in", &input_filename, "stdin", "input file (JSON)"); csm_options_string(ops, "out", &output_filename, "stdout", "output file (JSON)"); if(!csm_options_parse_args(ops, argc, argv)) { fprintf(stderr, "%s : extracts n-th JSON object from stream." "\n\ncsm_options:\n", argv[0]); csm_options_print_help(ops, stderr); return -1; } FILE * input_stream = open_file_for_reading(input_filename); FILE *output_stream = open_file_for_writing(output_filename); if(!input_stream || !output_stream) return -1; int i; for(i=0;i<nth;i++) { if(!json_stream_skip(input_stream)) { sm_error("Could not skip %d-th object\n", i); return -2; } } JO jo = json_read_stream(input_stream); if(!jo) { fprintf(stderr, "Could not read %d-th object (after skipping %d)\n", nth, i); return -2; } fputs(json_object_to_json_string(jo), output_stream); fputs("\n", output_stream); return 0; }
int main() { JO jo; /* the monkey */ LDP ld; while((jo = json_read_stream(stdin))) { if(!(ld = json_to_ld(jo))) { fprintf(stderr, "Could not transform to laser_data:\n\n"); fprintf(stderr, "-----\n"); fprintf(stderr, "%s", json_object_to_json_string(jo)); fprintf(stderr, "-----\n"); continue; } jo = ld_to_json(ld); printf("%s", json_object_to_json_string(jo)); printf("\n"); } return 0; }