3 #include "../csm/csm_all.h" 21 "Converts JSON stream to Matlab file. \n" 22 "There are three usages: \n" 23 " 1) with only one parameter, \n" 24 " $ json2matlab dir/mydata.json \n" 25 " creates a Matlab function 'mydata' inside the file 'dir/mydata.m' \n" 26 " 2) with two parameters, \n" 27 " $ json2matlab dir/mydata.json dir/out.m \n" 28 " creates a Matlab function 'out' inside the file 'dir/out.m'. \n" 29 " 3) otherwise, use the options switches. \n" 31 " By default it creates a complete script of the kind:\n" 33 " function res = function_name()\n" 37 " If complete_script is set to 0, it just outputs the meat: \n" 43 int main(
int argc,
const char * argv[]) {
46 const char * input_filename;
47 const char * out_filename;
48 const char *
function;
55 options_string(ops,
"in", &input_filename,
"stdin",
"input file (JSON)");
56 options_string(ops,
"out", &out_filename,
"stdout",
"output file (MATLAB)");
57 options_string(ops,
"function", &
function,
"",
"Matlab function name (if empty, use basename of out)");
58 options_int(ops,
"complete_script", &complete_script, 1,
"Write complete script 'function res = ...'");
59 options_int(ops,
"debug", &debug, 1,
"Shows debug information");
61 if(argc == 2 && (argv[1][0] !=
'-')) {
63 input_filename = argv[1];
int len = strlen(input_filename) + 4;
64 char base[len], no_suffix[len], out[len];
66 sprintf(out,
"%s.m", no_suffix);
70 }
else if(argc == 3 && (argv[1][0] !=
'-') && (argv[2][0] !=
'-')) {
71 input_filename = argv[1];
72 out_filename = argv[2];
81 if(!strcmp(
function,
"")) {
82 int len = strlen(out_filename) + 4;
96 fprintf(out,
"function res = %s\n",
function);
97 fprintf(out,
" res = ... \n");
99 fprintf(out,
" { ... \n\t");
104 if(i>0) fprintf(out,
", ...\n\t");
110 fprintf(out,
"... \n }; \n");
116 if(!jo) { fprintf(out,
"NaN");
return; }
172 fprintf(out,
"struct(");
175 if(i) fprintf(out,
", ... \n\t ");
176 fprintf(out,
"'%s', ", iter.
key);
201 for(
int i=0;i<len;i++){
210 if(ncolumns==0)
return 0;
219 for(
int i=0;i<len;i++){
245 for(
int i=0;i<len;i++){
246 if(i>0) fprintf(out,
"; ");
249 for(
int j=0;j<n;j++) {
250 if(j>0) fprintf(out,
", ");
262 for(
int i=0;i<len;i++){
263 if(i>0) fprintf(out,
"; ");
278 for(
int i=0;i<len;i++){
279 if(i>0) fprintf(out,
", ");
char * json_object_get_string(struct json_object *this)
void options_banner(const char *message)
int json_object_array_length(struct json_object *this)
void jo_write_as_matlab_object(JO jo, FILE *out)
void jo_write_as_matrix(JO jo, FILE *out)
double json_object_get_double(struct json_object *this)
void sm_set_program_name(const char *name)
int jo_is_numeric_array(JO jo)
enum json_type json_object_get_type(struct json_object *this)
int json_object_get_int(struct json_object *this)
void my_basename_no_suffix(const char *file, char *dest)
FILE * open_file_for_writing(const char *filename)
void jo_write_as_cell_array(JO jo, FILE *out)
void jo_write_as_matlab(JO jo, FILE *out)
struct option * options_allocate(int n)
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
void my_no_suffix(const char *file, char *dest)
void sm_debug_write(int flag)
#define json_object_object_foreachC(obj, iter)
FILE * open_file_for_reading(const char *filename)
void jo_write_as_column_vector(JO jo, FILE *out)
int jo_is_numeric_matrix(JO jo)
int main(int argc, const char *argv[])
char * my_strdup(const char *s)
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
JO json_read_stream(FILE *f)
struct json_object * json_object_array_get_idx(struct json_object *this, int idx)
int options_parse_args(struct option *ops, int argc, const char *argv[])
boolean json_object_get_boolean(struct json_object *this)