Go to the documentation of this file.00001 #include <assert.h>
00002 #include "json_journal.h"
00003
00004
00005 #define MAX_STACK 1000
00006
00007 static JO jj_stack[MAX_STACK];
00008 static int jj_stack_index = -1;
00009 static FILE * jj_file = 0;
00010
00011
00012 int jj_enabled() {
00013 return jj_file != 0;
00014 }
00015
00016 JO jj_stack_top() {
00017 assert(jj_stack_index>=0);
00018 return jj_stack[jj_stack_index];
00019 }
00020
00021 void jj_stack_push(JO jo) {
00022 assert(jj_stack_index<MAX_STACK);
00023 jj_stack[++jj_stack_index] = jo;
00024 }
00025
00026 void jj_stack_pop() {
00027
00028 assert(jj_stack_index>=0);
00029 if(jj_stack_index == 0 && jj_file) {
00030 fprintf(jj_file, "%s\n", json_object_to_json_string(jj_stack_top()));
00031 jo_free(jj_stack_top());
00032 }
00033 jj_stack_index--;
00034 }
00035
00036 void jj_context_enter(const char*context_name) {
00037
00038
00039 JO jo = json_object_new_object();
00040 if(jj_stack_index>=0)
00041 jo_add(jj_stack_top(), context_name, jo);
00042
00043 jj_stack_push(jo);
00044 }
00045
00046
00047
00048 void jj_must_be_hash() {
00049 assert(json_object_is_type(jj_stack_top(), (enum json_type) json_type_object));
00050 }
00051
00052 void jj_must_be_array() {
00053 assert(json_object_is_type(jj_stack_top(), (enum json_type) json_type_array));
00054 }
00055
00056 void jj_context_exit() {
00057 jj_must_be_hash();
00058 jj_stack_pop();
00059 }
00060
00061 void jj_loop_enter(const char*loop_name) {
00062 jj_must_be_hash();
00063 JO jo = json_object_new_array();
00064 jo_add(jj_stack_top(), loop_name, jo);
00065 jj_stack_push(jo);
00066 }
00067
00068 void jj_loop_iteration() {
00069 JO this_iteration = json_object_new_object();
00070 if(!json_object_is_type(jj_stack_top(), (enum json_type) json_type_array)) {
00071 jj_stack_pop();
00072 jj_must_be_array();
00073 }
00074 json_object_array_add(jj_stack_top(), this_iteration);
00075 jj_stack_push(this_iteration);
00076 }
00077
00078 void jj_loop_exit() {
00079 if(!json_object_is_type(jj_stack_top(), (enum json_type) json_type_array))
00080 jj_stack_pop();
00081
00082 jj_must_be_array();
00083 jj_stack_pop();
00084 }
00085
00086 void jj_add_int(const char*name, int v) {
00087 jj_must_be_hash();
00088 jo_add(jj_stack_top(), name, jo_new_int(v));
00089 }
00090
00091 void jj_add_double(const char*name, double v) {
00092 jj_must_be_hash();
00093 jo_add(jj_stack_top(), name, jo_double_or_null(v));
00094 }
00095
00096 void jj_add_double_array(const char *name, double *v, int n) {
00097 jj_add(name, jo_new_double_array(v, n));
00098 }
00099
00100 void jj_add_int_array(const char*name, int* v, int n) {
00101 jj_add(name, jo_new_int_array(v, n));
00102 }
00103
00104 void jj_add(const char*name, JO jo) {
00105 jj_must_be_hash();
00106 jo_add(jj_stack_top(), name, jo);
00107 }
00108
00109 void jj_set_stream(FILE* f) {
00110 jj_file = f;
00111 }
00112
00113 FILE * jj_get_stream() {
00114 return jj_file;
00115 }