diff --git a/yjson_t/Makefile b/yjson_t/Makefile new file mode 100644 index 0000000..c474160 --- /dev/null +++ b/yjson_t/Makefile @@ -0,0 +1,55 @@ + +CC=gcc +TOOLDIR=$(PWD)/../ytools_t + +INCLUDE_DIR=$(TOOLDIR)/include +CFLAGS=-I$(INCLUDE_DIR) -I./src + +#SRC_DIR=$(ROOT_DIR)/src +#SRC=$(wildcard */*/*.c) +#HEADS=$(OBJS:.o=.h) + +LISTDIR=$(PWD)/../list_t +LISTSRC=$(LISTDIR)/src/list_t/list_t.c +LISTSRC_O=$(LISTSRC:.c=.o) + +#SETTSRC=src/set_theoric_t/set_theoric_t.c +#SETTSRC_O=$(SETTSRC:.c=.o) + +JSONDIR=$(PWD) +JSONSRC=$(JSONDIR)/src/json_t/json_t.c +JSONSRC_O=$(JSONSRC:.c=.o) + +TOOLSRC_O=$(TOOLDIR)/src/tools_t/tools_t.o +#TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c +#TOOLSRC_O=$(TOOLSRC:.c=.o) + +SRC=$(wildcard **/**/*.c) +OBJ=$(SRC:.c=.o) #$(TOOLSRC_O) + +TOPTARGETS := all clean +DEP=$(TOOLDIR) $(LISTDIR) + +$(TOPTARGETS): $(DEP) + +all: $(JSONSRC_O) + + +$(JSONSRC_O): $(JSONSRC) $(LISTSRC_O) $(TOOLSRC_O) + $(CC) -o $@ -c $< $(CFLAGS) + + +$(DEP): + $(MAKE) -C $@ $(MAKECMDGOALS) + + +.PHONY: clean mrproper + +clean: + rm -f $(OBJ) + +mrproper: clean + rm -f $(EXEC) + +run: $(EXEC) + $(EXEC) -h diff --git a/yjson_t/src/json_t/json_t.c b/yjson_t/src/json_t/json_t.c new file mode 100644 index 0000000..a31b567 --- /dev/null +++ b/yjson_t/src/json_t/json_t.c @@ -0,0 +1,815 @@ +#include "json_t/json_t.h" + +bool is_js_space(char c){ + //printf("c=|%c|[%d]\n",c,c); + if(c == ' ') return true; + if(c == '\t') return true; + if(c == '\n') return true; + if(c == '\r') return true; + + return false; + +} + +void set_index_(struct js_value *js, size_t index){ + if(js->code_type == jstype_object) + js->type.object.index = index; + else if(js->code_type == jstype_array) + js->type.array.index = index; +} + +long get_index_(struct js_value *js){ + if(js->code_type == jstype_object) + return js->type.object.index; + if(js->code_type == jstype_array) + return js->type.array.index; + return -1; +} + + + +struct js_value * next_(struct js_value *js){ + if(js->code_type == jstype_object){ + return js->type.object.next_object; + } + if(js->code_type == jstype_array){ + return js->type.array.next_element; + } + return NULL; +} +struct js_value * prev_(struct js_value *js){ + if(js->code_type == jstype_object){ + return js->type.object.prev_object; + } + if(js->code_type == jstype_array){ + return js->type.array.prev_element; + } + return NULL; +} + + +void free_js_value(struct js_value *js){ + + if(js->code_type == jstype_string){ + free(js->type.string); + }else if(js->code_type == jstype_object){ + // free_js_value(js->type.object.key); + free_js_value(js->type.object.value); + struct js_value *tmpV = js->type.object.next_object; + js->type.object.next_object = NULL; + struct js_value *ttmp; + while(tmpV){ + ttmp = tmpV; + tmpV = ttmp->type.object.next_object ; //next_(ttmp); + ttmp->type.object.next_object = NULL; + //print_value(ttmp); + free_js_value(ttmp); + } + if(js->type.object.iter){ + free(js->type.object.iter); + js->type.object.iter = NULL; + } + }else if(js->code_type == jstype_array){ + free_js_value(js->type.array.value); + struct js_value *tmpV = js->type.array.next_element; + js->type.array.next_element = NULL; + struct js_value *ttmp; + while(tmpV){ + ttmp = tmpV; + tmpV = ttmp->type.array.next_element ; //next_(ttmp); + ttmp->type.array.next_element = NULL; + //print_value(ttmp); + free_js_value(ttmp); + } + if(js->type.array.iter){ + free(js->type.array.iter); + js->type.array.iter = NULL; + } + } + + + free(js); + +} + +struct js_value * create_js_value_string(char * input, struct js_value * parent){ + struct js_value * js = malloc(sizeof(struct js_value)); + js->parent = parent; + if(*input == '"') + js->str_value = input+1; + else + js->str_value = input; + js->code_type = jstype_string; + char *cur = js->str_value; + for(; *cur!='"'; ++cur); + js->length = cur - js->str_value; + js->type.string = malloc(js->length + 1); + strncpy(js->type.string, js->str_value, js->length); + js->type.string[js->length]='\0'; + return js; +} + +struct js_value * create_js_value_number(char * input, struct js_value * parent){ + struct js_value * js = malloc(sizeof(struct js_value)); + js->parent = parent; + js->str_value = input; + js->code_type = jstype_number; + char *end; + js->type.number = strtold(input, &end); + js->length = end - input; +// printf("\nend_num:%s, num:%s, lenN:%ld\n",end,input, js->length); + return js; +} + +struct js_value * create_js_value_null(char * input, struct js_value * parent){ + struct js_value * js = malloc(sizeof(struct js_value)); + js->parent = parent; + js->str_value = input; + js->code_type = jstype_null; + js->length = 4; + + return js; +} + + +struct js_value * create_js_value_true(char * input, struct js_value * parent){ + struct js_value * js = malloc(sizeof(struct js_value)); + js->parent = parent; + js->str_value = input; + js->code_type = jstype_bool; + js->type.boolean = true; + js->length = 4; + + return js; +} +struct js_value * create_js_value_false(char * input, struct js_value * parent){ + struct js_value * js = malloc(sizeof(struct js_value)); + js->parent = parent; + js->str_value = input; + js->code_type = jstype_bool; + js->type.boolean = false; + js->length = 5; + + return js; +} + + +struct js_value * create_js_value_array(char * _input, struct js_value * parent){ + struct js_value * js_parent = malloc(sizeof(struct js_value)); + struct js_value * js, *tmp_js; + js = js_parent; + char *input = _input; + if(*_input == '[') + input = _input + 1; + + char *cur = input; + for(long depth = 1; *cur != '\0' && depth != 0; ++cur){ + if(*cur == '[') ++depth; + if(*cur == ']') --depth; + } + js->length = cur - input; +// printf("\na]input ==%s, len:%ld\n",input,js->length); + + js->type.array.next_element = NULL; + js->type.array.prev_element = NULL; + js->type.array.index = 0; + + js->type.array.iter = malloc(sizeof(struct js_iterator)); + + js->type.array.iter->size = js->type.array.index + 1; + js->type.array.iter->begin = js; + js->type.array.iter->end = js; + js->type.array.iter->current = js; + + if(parent == NULL) + js->type.array.depth= 0; + else + js->type.array.depth = parent->type.array.depth + 1; +// js->index = 0; + + js->str_value = input; + js->code_type = jstype_array; + cur = input; + for(; is_js_space(*cur); ++cur); + if(*cur == ']'){ + + js->type.array.value = create_js_value_null(NULL, js); // NULL; + js->type.array.value->length = cur - input + 1; + js->type.array.next_element = NULL; + js->parent = parent; + + //js->type.object.key_length = cur - input; + //js->type.array.value = NULL; + +return js; + } + + while(cur < input + js_parent->length ) + { +// printf("aa::%c\n",*cur); + if(*cur == ','){ + ++cur; + tmp_js = malloc(sizeof(struct js_value)); + //for(; is_js_space(*cur); ++cur); + tmp_js->str_value = cur;// + 1; + tmp_js->code_type = jstype_array; + char *ccur = cur; + for(; *ccur != '\0' && *ccur != ',' ; ++ccur){ + if(*ccur == '{'){ // to ignore ',' in the object + ++ccur; + for(long depth = 1; *ccur != '\0' && depth != 0; ++ccur){ + if(*ccur == '{') ++depth; + if(*ccur == '}') --depth; + } + //break; + } + if(*ccur == ']') + break; + if(*cur == '['){// to ignore ',' in the list + ++ccur; + for(long depth = 1; *ccur != '\0' && depth != 0; ++ccur){ + if(*ccur == '[') ++depth; + if(*ccur == ']') --depth; + } + //break; + } + if(*cur == '"'){ // to ignore ',' in "string value" + for(; *ccur != '\0' || *ccur !='"'; ++ccur); + } + } + tmp_js->length = ccur - cur; + tmp_js->type.array.prev_element = js; + tmp_js->type.array.next_element = NULL; + tmp_js->type.array.iter = NULL; + tmp_js->type.array.index = js->type.array.index + 1; + //tmp_js->type.object.iter = NULL; + js->type.array.next_element = tmp_js; + js= tmp_js; + if(parent == NULL) + js->type.array.depth= 0; + else + js->type.array.depth = parent->type.array.depth + 1; + + + js_parent->type.array.iter->size = js->type.array.index + 1; + js_parent->type.array.iter->end = js; + js_parent->type.array.iter->current = js; + + }else{ + + } + + js->parent = parent; + + + for(; is_js_space(*cur); ++cur); + + // value section + char *str_value = cur; // + js->type.object.key->length; + + + js->type.array.value = create_js_value(str_value, js); + + cur = str_value + js->type.array.value->length + (js->type.array.value->code_type != jstype_number) ; //1 ; +// printf(">strVal:%s\n",cur); + + if(js->type.array.value->code_type == jstype_string) + for(; is_js_space(*cur) || *cur=='"' || *cur=='}' || *cur == ']'; ++cur); + else + for(; is_js_space(*cur) || *cur=='}' || *cur == ']'; ++cur); + + } + + return js_parent; + + +} + +struct js_value * create_js_value(char *_input, struct js_value *parent){ + char *input = _input; + for(; is_js_space(*input); ++input); + +// printf(">>strVal:%s\n",input); + if(*input == '"'){ + return create_js_value_string(input+1, parent); + }else if(strncmp(input,"true",4) == 0){ + return create_js_value_true(input, parent); + }else if(strncmp(input,"false",5) == 0){ + return create_js_value_false(input, parent); + }else if(strncmp(input,"null",4) == 0){ + return create_js_value_null(input, parent); + }else if(*input == '{'){ + return create_js_value_object(input + 1, parent); + }else if(*input == '['){ + return create_js_value_array(input + 1, parent); + }else /*if(isdigit(*input))*/{ + return create_js_value_number(input, parent); + } + + +} + +struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*/, struct js_value * parent){ + struct js_value * js_parent = malloc(sizeof(struct js_value)); + struct js_value * js, *tmp_js; + js = js_parent; + char *input = _input; + + if(*_input == '{') + input = _input + 1; + + char *cur = input; + for(long depth = 1; *cur != '\0' && depth != 0; ++cur){ + if(*cur == '{') ++depth; + if(*cur == '}') --depth; + } + js->length = cur - input; + + // printf("iin:%s\noo cur:%s\nlenObj:%ld\n",input,cur, js->length); + + js->type.object.next_object = NULL; + js->type.object.prev_object = NULL; + js->type.object.index = 0; + + js->type.object.iter = malloc(sizeof(struct js_iterator)); + + js->type.object.iter->size = js->type.object.index + 1; + js->type.object.iter->begin = js; + js->type.object.iter->end = js; + js->type.object.iter->current = js; + + if(parent == NULL) + js->type.object.depth = 0; + else + js->type.object.depth = parent->type.object.depth + 1; + + js->str_value = input; + js->code_type = jstype_object; + + cur = input; + for(; is_js_space(*cur); ++cur); +// printf("oocp::%s\n",cur); + if(*cur == '}'){ +// printf(">>>iin:%s\noo cur:%s\nlenObj:%ld\n",input,cur, js->length); + + js->type.object.value = create_js_value_null(NULL, js);// NULL; + js->type.object.value->length = cur - input; + js->type.object.key_length = 0; + js->type.object.key = NULL; + js->type.object.next_object = NULL; + js->parent = parent; + return js; + } + + +// printf("cooc::%s, diff:%ld\n",cur, input + js_parent->length - cur); + + while(cur < input + js_parent->length) + { +// printf("cc::%c\n",*cur); + if(*cur == ','){ + ++cur; + tmp_js = malloc(sizeof(struct js_value)); + //for(; is_js_space(*cur); ++cur); + tmp_js->str_value = cur ;//+ 1; + tmp_js->code_type = jstype_object; + char *ccur = cur; + for(; *ccur != '\0' && *ccur != ',' ; ++ccur){ + if(*ccur == '{'){ // to ignore ',' in the object + ++ccur; + for(long depth = 1; *ccur != '\0' && depth != 0; ++ccur){ + if(*ccur == '{') ++depth; + if(*ccur == '}') --depth; + } + //break; + } + if(*ccur == '}') + break; + if(*cur == '['){// to ignore ',' in the list + ++ccur; + for(long depth = 1; *ccur != '\0' && depth != 0; ++ccur){ + if(*ccur == '[') ++depth; + if(*ccur == ']') --depth; + } + //break; + } + if(*cur == '"'){ // to ignore ',' in "string value" + for(; *ccur != '\0' || *ccur !='"'; ++ccur); + } + } + tmp_js->length = ccur - cur; + tmp_js->type.object.prev_object = js; + tmp_js->type.object.next_object = NULL; + tmp_js->type.object.iter = NULL; + tmp_js->type.object.index = js->type.object.index + 1; + js->type.object.next_object = tmp_js; + js= tmp_js; + if(parent == NULL) + js->type.object.depth = 0; + else + js->type.object.depth = parent->type.object.depth + 1; + + js_parent->type.object.iter->size = js->type.object.index + 1; + js_parent->type.object.iter->end = js; + js_parent->type.object.iter->current = js; + + }else{ + + } + + js->parent = parent; + for(; is_js_space(*cur); ++cur); + // key section + // js->type.object.key = create_js_value_string(cur, js); + + if(*cur == '"') ++cur; + js->type.object.key = cur; + // printf("cur:%s\n",cur); + for(; *cur != '"'; ++cur); + //js->key = cur; + //for(; *cur != '"'; ++cur); + //js->key_length = cur - js->key; + js->type.object.key_length = cur - js->type.object.key; + for(; *cur != ':'; ++cur); + ++cur; + for(; is_js_space(*cur); ++cur); +// printf("cur=%s| lenKey :%ld \n",cur,js->type.object.key_length); + + + // value section + char *str_value = cur; // + js->type.object.key->length; + + js->type.object.value = create_js_value(str_value, js); + + cur = str_value + js->type.object.value->length + (js->type.object.value->code_type != jstype_number);// 1 ; +// printf("[>>>: cur:%s\n",cur); + + if(js->type.object.value->code_type == jstype_string) + for(; is_js_space(*cur) || *cur=='"' || *cur=='}' || *cur == ']'; ++cur); + else + for(; is_js_space(*cur) || *cur=='}' || *cur == ']'; ++cur); + + + } + + return js_parent; + +} +void create_copy_js_iterator(struct js_iterator **_dstit, struct js_iterator *srcit){ + if(*_dstit == NULL){ + *_dstit = malloc(sizeof(struct js_iterator)); + } + struct js_iterator * dstit = *_dstit; + dstit->size = srcit->size; +// dstit->next_ = srcit->next_; +// dstit->prev_ = srcit->prev_; +// dstit->set_index_ = srcit->set_index_; +// dstit->get_index_ = srcit->get_index_; + dstit->begin = srcit->begin; + dstit->end = srcit->end; + dstit->current = srcit->current; + +} + +void setup_js_iterator(struct js_iterator **_iter, struct js_value * js){ + + + if(js->code_type == jstype_object || js->code_type == jstype_array ) { + if(*_iter == NULL){ + *_iter = malloc(sizeof(struct js_iterator)); + } + struct js_iterator *iter = *_iter ; +// iter->next_ = next_; +// iter->prev_ = prev_; +// iter->set_index_ = set_index_; +// iter->get_index_ = get_index_; + + iter->begin = js; + struct js_value *tmpjs = js; + struct js_value *tmpit = iter->begin; + //size_t index = 0; + while(tmpjs){ + //iter->set_index_(tmpjs, index); + //++index; + + iter->size = get_index_(tmpjs); + + iter->end = tmpjs; + iter->current = tmpjs; + tmpjs = next_(tmpjs); + } + + //return iter; + + } + + //return NULL; + + +} + +void set_next_(struct js_value * dstjs, struct js_value * jsnxt){ + if(dstjs->code_type == jstype_object){ + struct js_value *tmpjs = dstjs->type.object.next_object, *ttmpjs; + ttmpjs = dstjs; + while(prev_(ttmpjs)) ttmpjs = prev_(ttmpjs); + struct js_iterator *iter = ttmpjs->type.object.iter; + dstjs->type.object.next_object = jsnxt; + jsnxt->type.object.prev_object = dstjs; + size_t last_index = dstjs->type.object.index; + struct js_value *ttmp = jsnxt; + struct js_value *end = jsnxt; + while(next_(ttmp)){ + end = ttmp; + ttmp->type.object.index = (++last_index); + ttmp = next_(ttmp); + } + ttmp->type.object.next_object = tmpjs; + if(tmpjs){ +// print_value(tmpjs); +// printf("%d line\n",__LINE__); + tmpjs->type.object.prev_object = ttmp; + while(tmpjs){ + end = tmpjs; + tmpjs->type.object.index = (++last_index); + tmpjs = next_(tmpjs); + } + } + //struct js_iterator *iter = get_iterator_(dstjs); + if(iter){ + iter->size = last_index + 1; + iter->end = end; + } + }else if(dstjs->code_type == jstype_array){ + struct js_value *tmpjs = dstjs->type.array.next_element, *ttmpjs; + ttmpjs = dstjs; + while(prev_(ttmpjs)) ttmpjs = prev_(ttmpjs); + struct js_iterator *iter = ttmpjs->type.array.iter; + dstjs->type.array.next_element = jsnxt; + jsnxt->type.array.prev_element = dstjs; + size_t last_index = dstjs->type.array.index; + struct js_value *ttmp = jsnxt; + struct js_value *end = jsnxt; + while(next_(ttmp)){ + end = ttmp; + ttmp->type.array.index = (++last_index); + ttmp = next_(ttmp); + } + ttmp->type.array.next_element = tmpjs; + if(tmpjs){ + tmpjs->type.array.prev_element = ttmp; + while(tmpjs){ + end = tmpjs; + tmpjs->type.array.index = (++last_index); + tmpjs = next_(tmpjs); + } + } + //struct js_iterator *iter = get_iterator_(dstjs); + if(iter){ + iter->size = last_index + 1; + iter->end = end; + } + } +} + +void set_prev_(struct js_value ** _dst, struct js_value * jsprv){ + struct js_value *dst = *_dst; + if(dst->code_type == jstype_object){ + struct js_value *tmpjs = dst->type.object.prev_object, *ttmpjs; + ttmpjs = dst; + while(prev_(ttmpjs)) ttmpjs = prev_(ttmpjs); + struct js_iterator *iter = ttmpjs->type.object.iter; + + dst->type.object.prev_object = jsprv; + jsprv->type.object.prev_object = tmpjs; + jsprv->type.object.index = dst->type.object.index; + jsprv->type.object.prev_object = tmpjs; + size_t last_index = dst->type.object.index; + struct js_value *ttmp = jsprv; + while(next_(ttmp)){ + ttmp->type.object.index = (++last_index); + ttmp = next_(ttmp); + if(iter) ++(iter->size);// = last_index + 1; + } + ttmp->type.object.next_object = dst; + if(tmpjs){ + tmpjs->type.object.next_object = jsprv; + }else{// dst prev null (top) + *_dst = jsprv; + (*_dst)->type.object.iter = malloc(sizeof(struct js_iterator)); + (*_dst)->type.object.iter->begin = jsprv; + if(iter){ + (*_dst)->type.object.iter->end = iter->end; + (*_dst)->type.object.iter->current = iter->current; + (*_dst)->type.object.iter->size = iter->size; // + (last_index + 1 - dst->type.object.index); + } + iter = (*_dst)->type.object.iter; + } + tmpjs = dst; + while(tmpjs){ + tmpjs->type.object.index = (++last_index); + tmpjs = next_(tmpjs); + //if(iter) { + // iter->size = last_index + 1; + // iter->end = tmpjs; + //} + } + }else if(dst->code_type == jstype_array){ + struct js_value *tmpjs = dst->type.array.prev_element, *ttmpjs; + ttmpjs = dst; + while(prev_(ttmpjs)) ttmpjs = prev_(ttmpjs); + struct js_iterator *iter = ttmpjs->type.array.iter; + dst->type.array.prev_element = jsprv; + jsprv->type.array.prev_element = tmpjs; + jsprv->type.array.index = dst->type.array.index; + jsprv->type.array.prev_element = tmpjs; + size_t last_index = dst->type.array.index; + struct js_value *ttmp = jsprv; + while(next_(ttmp)){ + ttmp->type.array.index = (++last_index); + ttmp = next_(ttmp); + if(iter) ++(iter->size);// = last_index + 1; + } + ttmp->type.array.next_element = dst; + if(tmpjs){ + tmpjs->type.array.next_element = jsprv; + }else{// dst prev null (top) + *_dst = jsprv; + (*_dst)->type.array.iter = malloc(sizeof(struct js_iterator)); + (*_dst)->type.array.iter->begin = jsprv; + if(iter){ + (*_dst)->type.array.iter->end = iter->end; + (*_dst)->type.array.iter->current = iter->current; + (*_dst)->type.array.iter->size = iter->size; // + (last_index + 1 - dst->type.array.index); + } + iter = (*_dst)->type.array.iter; + } + tmpjs = dst; + while(tmpjs){ + ++(tmpjs->type.array.index); + tmpjs = next_(tmpjs); + } + + } +} + + +void add_js_value_index(size_t index, struct js_value *js_to_add, struct js_value **js_org){ + if((*js_org)->code_type >= jstype_object && (*js_org)->code_type == js_to_add->code_type ){ + struct js_value *tmpjs = *js_org, *ttmp=NULL; + while(tmpjs){// TO DO : use iter to optimize + if(get_index_(tmpjs) == index){ +// printf("broken, index:%ld\n",index); + break; + } + ttmp = tmpjs; + tmpjs = next_(tmpjs); + } + if(ttmp){ +// printf("not NULL\n"); + set_next_(ttmp,js_to_add); + }else{ + set_prev_(js_org, js_to_add); + } + } +} + +struct js_iterator * get_iterator_(struct js_value *js){ + if(js->code_type == jstype_object) return js->type.object.iter; + if(js->code_type == jstype_array) return js->type.array.iter; + return NULL; +} + + +void append_js_value(struct js_value *dst, char *input){/* if object type: input = { "key" : value }, if key already exists value replace the old value! */ + if(dst->code_type < jstype_object || get_iterator_(dst) == NULL) return; + struct js_value *newval = create_js_value(input, dst); + if(dst->code_type == jstype_object && newval->code_type != jstype_object){ + printf(" input needs to be at the format : { \"key\" : value } \n"); + free_js_value(newval); + return; + }else{ + if(dst->code_type == jstype_object){ + char key[newval->type.object.key_length + 1]; + strncpy(key, newval->type.object.key, newval->type.object.key_length); + key[newval->type.object.key_length]='\0'; + struct js_value * valjs = get_js_value_of_key(key, dst); + if(valjs){ + struct js_value *ttmp = value_of_(valjs); + valjs->type.object.value = value_of_(newval); + free_js_value(ttmp); + if(newval->type.object.iter) free(newval->type.object.iter); + free(newval); // but keep value_of_(newval)! + return; + } + + } + struct js_iterator *iter = get_iterator_(dst); + set_next_(iter->end, newval); + struct js_value *tmpjs = newval; + while(next_(tmpjs)) tmpjs = next_(tmpjs); + iter->end = tmpjs; + iter->current = tmpjs; + iter->size = get_index_(tmpjs) + 1; + + } +} + +char * original_string_js_value(struct js_value *js){ + char *ret = malloc(js->length+1); + strncpy(ret,js->str_value,js->length); + ret[js->length] = '\0'; + + + + + return ret; + +} + +void print_value(struct js_value *js){ + if(js->code_type == jstype_number){ + printf("%Lf",js->type.number); + }else if(js->code_type == jstype_string){ + printf("\"%s\"",js->type.string); + }else if(js->code_type == jstype_bool){ + printf("%s",js->type.boolean ? "true" : "false"); + }else if(js->code_type == jstype_null){ + printf("%s","null"); + }else if(js->code_type == jstype_object){ + struct js_value *tmpjs = js; + + while(tmpjs){ + + if(tmpjs->type.object.prev_object == NULL){ + printf("\n%*c{ ",4*(tmpjs->type.object.depth),' '); + } + + char key[tmpjs->type.object.key_length + 1]; + strncpy(key, tmpjs->type.object.key, tmpjs->type.object.key_length); + key[tmpjs->type.object.key_length]='\0'; + printf("\n%*c\"%s\" : ", 4*(tmpjs->type.object.depth),' ', key ); + print_value(tmpjs->type.object.value); + + if(tmpjs->type.object.next_object == NULL) + printf("\n%*c} ",4*(tmpjs->type.object.depth),' '); //printf(" }"); + else printf(", "); + //printf("\n"); + tmpjs = tmpjs->type.object.next_object; + } + }else if(js->code_type == jstype_array){ + struct js_value *tmpjs = js; + while(tmpjs){ + if(tmpjs->type.array.prev_element == NULL){ + printf("\n%*c[ ",4*(tmpjs->type.array.depth),' '); + } + if(tmpjs->type.array.value->code_type < jstype_object ) + printf("\n%*c ",4*(tmpjs->type.array.depth),' '); + print_value(tmpjs->type.array.value); + if(tmpjs->type.array.next_element == NULL) //printf(" ]"); + printf("\n%*c] ",4*(tmpjs->type.array.depth),' '); + else printf(", "); + tmpjs = tmpjs->type.array.next_element; // next_(tmpjs); + } + } + +} + +struct js_value * value_of_(struct js_value * js){ + if(js->code_type == jstype_object){ + return js->type.object.value; + } + if(js->code_type == jstype_array){ + return js->type.array.value; + } + return NULL; +} + +struct js_value *get_js_value_of_key(char * key, struct js_value *js ){ + if(js->code_type == jstype_object){ + struct js_value *tmp = js; + while(tmp){ + if(strncmp(key,tmp->type.object.key, tmp->type.object.key_length) == 0){ + return tmp; //tmp->type.object.value == value_of_(tmp); + } + tmp = next_(tmp); + } + } + return NULL; +} + +/* +void set_value_of_key(char *key, char * value, struct js_value *js ){ + if(js->code_type != jstype_object) + return; + struct js_value *valjs = get_js_value_of_key(key, js); + if(valjs){ + struct js_value *tmp = valjs->type.object.value; + valjs->type.object.value = create_js_value(value, valjs); + + free_js_value(tmp); + }else{ + js->type.object. + } +} +*/ diff --git a/yjson_t/src/json_t/json_t.h b/yjson_t/src/json_t/json_t.h new file mode 100644 index 0000000..b148051 --- /dev/null +++ b/yjson_t/src/json_t/json_t.h @@ -0,0 +1,134 @@ +#ifndef __Y_JSON_T_H__C__ +#define __Y_JSON_T_H__C__ + +#include +#include +#include +#include + +#include "list_t/list_t.h" + +#define ITERATOR__(type) \ +struct iterator_##type {\ + size_t size;\ + type *begin;\ + type *end;\ + type *iterator;\ +};\ + + + + +enum code_jstype { + jstype_string, + jstype_bool, + jstype_null, + jstype_number, + jstype_object, + jstype_array, +}; + +struct js_value; + +struct js_iterator { + size_t size; + struct js_value *begin; + struct js_value *end; + struct js_value *current; +// struct js_value *child; +// struct js_value *parent; +// struct js_value * (*next_)(struct js_value * js); +// struct js_value * (*prev_)(struct js_value * js); +// void (*set_index_)(struct js_value *js, size_t index); +// long (*get_index_)(struct js_value *js); +}; + +void set_index_(struct js_value *js, size_t index); +long get_index_(struct js_value *js); +struct js_value * next_(struct js_value * js); +struct js_value * prev_(struct js_value * js); +void set_next_(struct js_value * dst, struct js_value * jsnxt); +void set_prev_(struct js_value ** dst, struct js_value * jsprv); + + +struct js_object { + size_t index; + int depth; + char *key; /* non zero terminal */ + size_t key_length; /* key string length */ +// struct js_value *key; + struct js_value *value; +// struct iterator *iter; + struct js_value *next_object; + struct js_value *prev_object; + struct js_iterator *iter; +}; + + + +struct js_array { + size_t index; + int depth; + struct js_value *value; + struct js_value *next_element; + struct js_value *prev_element; + struct js_iterator *iter; +}; + + +struct js_value { + char *str_value; /* non zero terminal */ + size_t length; /* value string lenght */ + enum code_jstype code_type; + union { + char *string; + long double number; + bool boolean; + struct js_object object; + struct js_array array; + } type; + struct js_value * parent; +// struct js_value * next; +// struct js_value * prev; +}; + + +void free_js_value(struct js_value *js); + +bool is_js_space(char c); + +struct js_value * create_js_value(char * input, struct js_value * parent); + +struct js_value * create_js_value_string(char * input, struct js_value * parent); + +struct js_value * create_js_value_number(char * input, struct js_value * parent); + +struct js_value * create_js_value_null(char * input, struct js_value * parent); + + +struct js_value * create_js_value_true(char * input, struct js_value * parent); +struct js_value * create_js_value_false(char * input, struct js_value * parent); + +struct js_value * create_js_value_object(char *input, struct js_value * parent); +struct js_value * create_js_value_array(char * input, struct js_value * parent); + +struct js_iterator * create_js_iterator(struct js_value * js); + +void add_js_value_index(size_t index, struct js_value *js_to_add, struct js_value **js_org); + +void print_value(struct js_value *js); + +char * original_string_js_value(struct js_value *js); + +struct js_value *get_js_value_of_key(char * key, struct js_value *js ); +struct js_iterator * get_iterator_(struct js_value *js); + +void append_js_value(struct js_value *dst, char *input); + +struct js_value * value_of_(struct js_value * js); + + +#endif /* __Y_JSON_T_H__C__ */ + + + diff --git a/yjson_t/test/Makefile b/yjson_t/test/Makefile new file mode 100644 index 0000000..47a6674 --- /dev/null +++ b/yjson_t/test/Makefile @@ -0,0 +1,68 @@ + + + + +NAME_TEST=is_good +CC=gcc +ROOT_DIR=$(PWD) +YTESTDIR=$(PWD)/../../ytest_t +LISTDIR=$(PWD)/../../list_t +INCLUDE_LIST=$(LISTDIR)/src + +INCLUDE_DIR=$(PWD)/../src +CFLAGS=-I$(INCLUDE_DIR) -I$(INCLUDE_LIST) -I$(YTESTDIR)/include_ytest/include +LDFLAGS=-L$(YTESTDIR) -lytest + +#SRC_DIR=$(ROOT_DIR)/src +#SRC=$(wildcard */*/*.c) +SRC=$(wildcard **/**/*.c) +#HEADS=$(OBJS:.o=.h) +TEST_DIR=$(PWD) +EXECSRC=$(NAME_TEST).c +EXEC=launch_$(NAME_TEST)_m + + +LISTSRC_O=$(LISTDIR)/src/list_t/list_t.o + +JSONDIR=$(PWD)/.. +JSONSRC=$(JSONDIR)/src/json_t/json_t.c +JSONSRC_O=$(JSONDIR)/src/json_t/json_t.o + + +TOPTARGETS := all clean + +DEPS=$(LISTDIR) $(YTESTDIR) $(JSONDIR) + +$(TOPTARGETS): $(DEPS) + +$(DEPS): + $(MAKE) -C $@ $(MAKECMDGOALS) + + +#LISTSRC_O=$(LISTSRC:.c=.o) +#SETTSRC_O=$(PWD)/../src/set_theoric_t/set_theoric_t.o +#SETTSRC_O=$(SETTSRC:.c=.o) +#TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c +#TOOLSRC_O=$(TOOLSRC:.c=.o) + +OBJ=$(SRC:.c=.o) $(JSONSRC_O) +#$(LISTSRC_O) + + +LIB_YTEST=$(YTESTDIR)/libytest.so + +all: $(EXEC) $(LIB_YTEST) + +$(EXEC): $(EXECSRC) $(OBJ) + $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) + +.PHONY: clean mrproper + +clean: + rm -f $(OBJ) + +mrproper: clean + rm -f $(EXEC) + +run: $(EXEC) + $(EXEC) -h diff --git a/yjson_t/test/compile.sh b/yjson_t/test/compile.sh new file mode 100644 index 0000000..91c319a --- /dev/null +++ b/yjson_t/test/compile.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +if [ "$#" -le 0 ] ; then + echo "Usage: $0 is_good.c" >&2 + echo "for example to compile: is_good.c" >&2 + exit 1 +fi +if [ "$#" -le 1 ] ; then + echo "Usage: $0 $1" >&2 + echo " we can add more option for example '-D DEBUG=1' to have debug print, '-D HK' to have gtest like prompt, od '-g' to gbd" >&2 + echo "for example: $0 $1 \"-D DEBUG=1 -D HK -g\"" +fi + +DIR_YTEST=$PWD/../../ytest_t +SRC=../src + +gcc -o launch_is_good_c $1 -L$DIR_YTEST $2 -lytest -I$DIR_YTEST/include_ytest/include $SRC/list_t/list_t.c -I$SRC +#gcc -o launch_is_good_c $1 $2 -lytest -I../include_ytest src/list_t/list_t.o src/set_theoric_t/set_theoric_t.o -I./src + +export LD_LIBRARY_PATH=$DIR_YTEST/:LD_LIBRARY_PATH + + +#gcc $1 src/ftest/ftest.c src/fmock/fmock.c src/tools_t/tools_t.c src/bar_progress/bar_progress.c src/list_t/list_t.c src/set_theoric_t/set_theoric_t.c -I./include $2 -o launch_is_good_c -lpthread diff --git a/yjson_t/test/is_good.c b/yjson_t/test/is_good.c new file mode 100644 index 0000000..b9a25ce --- /dev/null +++ b/yjson_t/test/is_good.c @@ -0,0 +1,211 @@ +#include "ftest/ftest.h" +#include "fmock/fmock.h" + + +#include "json_t/json_t.h" + +TEST(string_val){ + + char *val = "\"Hello! it's a string\""; + + struct js_value *js = create_js_value_string(val, NULL); + + LOG("js type = %d\n",js->code_type); + LOG("js len = %ld\n",js->length); + LOG(" js val = %s\n", js->type.string); + + free_js_value(js); +} +TEST(number){ + + char *val = "3.14"; + + struct js_value *js = create_js_value_number(val, NULL); + + LOG("js type = %d\n",js->code_type); + LOG("js len = %ld\n",js->length); + LOG(" js val = %Lf\n", js->type.number); + + free_js_value(js); +} +TEST(object_val){ + + char *val = "{ \"message\" : \"Hello! it's a string\" , \"age\" : 48 , \"speaker\" : \" the president \" }"; + struct js_value *js = create_js_value_object(val, NULL); + + LOG("key : %s\n lenKey: %ld \n",js->type.object.key, js->type.object.key_length); + +/* + LOG("value : %s\n",js->type.object.value->type.string); + LOG("value : %Lf\n",js->type.object.next_object->type.object.value->type.number); + LOG("value : %s\n",js->type.object.next_object->type.object.next_object->type.object.value->type.string); +*/ + + print_value(js); + + char *rrjson = original_string_js_value(js); + printf("original :%s\n",rrjson); + char *rrjson1 = original_string_js_value(js->type.object.next_object); + printf("original 1 :%s\n",rrjson1); + + free_js_value(js); + + free(rrjson); + free(rrjson1); + + LOG("line %d\n",__LINE__); +} +TEST(array_val){ + + char *val = "[ \"message\" , \"Hello! it's a string\" , { \"age\" : 48 , \"speaker\" : \" the president \" }, 45 , true ]"; + //char *val = "[ \"message\" , \"Hello! it's a string\" , { \"age\" : 48 } , {\"speaker\" : \" the president \" } , 25, \"tttt\" ]"; + struct js_value *js = create_js_value_array(val, NULL); + +// LOG("key : %s\n lenKey: %ld \n",js->type.object.key, js->type.object.key_length); + +/* + LOG("value : %s\n",js->type.object.value->type.string); + LOG("value : %Lf\n",js->type.object.next_object->type.object.value->type.number); + LOG("value : %s\n",js->type.object.next_object->type.object.next_object->type.object.value->type.string); +*/ + + print_value(js); + +/* + char *rrjson = original_string_js_value(js); + printf("original :%s\n",rrjson); + char *rrjson1 = original_string_js_value(js->type.array.next_element); + printf("original 1 :%s\n",rrjson1); +*/ + free_js_value(js); + +// free(rrjson); +// free(rrjson1); + //LOG("%s\n",__LINE__); + LOG("line %d\n",__LINE__); + +} +TEST(general_val){ + + char *val = "{ \"array\" : [ \"message\" , \"Hello! it's a string\" , { \"age\" : 48 , \"speaker\" : \" the president \" }, 45 , true ] }"; + //char *val = "[ \"message\" , \"Hello! it's a string\" , { \"age\" : 48 } , {\"speaker\" : \" the president \" } , 25, \"tttt\" ]"; + struct js_value *js = create_js_value(val, NULL); + +// LOG("key : %s\n lenKey: %ld \n",js->type.object.key, js->type.object.key_length); + +/* + LOG("value : %s\n",js->type.object.value->type.string); + LOG("value : %Lf\n",js->type.object.next_object->type.object.value->type.number); + LOG("value : %s\n",js->type.object.next_object->type.object.next_object->type.object.value->type.string); +*/ + + print_value(js); + +/* + char *rrjson = original_string_js_value(js); + printf("original :%s\n",rrjson); + char *rrjson1 = original_string_js_value(js->type.array.next_element); + printf("original 1 :%s\n",rrjson1); +*/ + free_js_value(js); + +// free(rrjson); +// free(rrjson1); + LOG("line %d\n",__LINE__); + +} + +TEST(general_val2){ + + char *val = "{ \"array\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } }"; + //char *val = "{ \"array\" : [ \"message\", \"Hello! it's a string\", { \"age\" : 48, \"speaker\" : \" the president \" }, 45, true ], \"second\" : [ 324.1, 1 , { \"math\" : 17.1 } ] }"; + //char *val = "[ \"message\" , \"Hello! it's a string\" , { \"age\" : 48 } , {\"speaker\" : \" the president \" } , 25, \"tttt\" ]"; + struct js_value *js = create_js_value(val, NULL); + +// LOG("key : %s\n lenKey: %ld \n",js->type.object.key, js->type.object.key_length); + +/* + LOG("value : %s\n",js->type.object.value->type.string); + LOG("value : %Lf\n",js->type.object.next_object->type.object.value->type.number); + LOG("value : %s\n",js->type.object.next_object->type.object.next_object->type.object.value->type.string); +*/ + + print_value(js); + +/* + char *rrjson = original_string_js_value(js); + printf("original :%s\n",rrjson); + char *rrjson1 = original_string_js_value(js->type.array.next_element); + printf("original 1 :%s\n",rrjson1); +*/ + free_js_value(js); + LOG("line %d\n",__LINE__); + +// free(rrjson); +// free(rrjson1); + +} + + +TEST(add_val){ + + char *val = "{ \"array\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } }"; + //char *val = "{ \"array\" : [ \"message\", \"Hello! it's a string\", { \"age\" : 48, \"speaker\" : \" the president \" }, 45, true ], \"second\" : [ 324.1, 1 , { \"math\" : 17.1 } ] }"; + //char *val = "[ \"message\" , \"Hello! it's a string\" , { \"age\" : 48 } , {\"speaker\" : \" the president \" } , 25, \"tttt\" ]"; + + struct js_value *js = create_js_value(val, NULL); + + char *add ="{ \"add0\" : 0.0 , \"addname\" : \"Fanasina\" }"; + + struct js_value *js_add = create_js_value(add, NULL); + + + print_value(js); + add_js_value_index(10, js_add, &js); + print_value(js); + free_js_value(js); + LOG("line %d\n",__LINE__); + +// free(rrjson); +// free(rrjson1); + +} +TEST(append_val){ + + char *val = "{ \"array\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } }"; + //char *val = "{ \"array\" : [ \"message\", \"Hello! it's a string\", { \"age\" : 48, \"speaker\" : \" the president \" }, 45, true ], \"second\" : [ 324.1, 1 , { \"math\" : 17.1 } ] }"; + //char *val = "[ \"message\" , \"Hello! it's a string\" , { \"age\" : 48 } , {\"speaker\" : \" the president \" } , 25, \"tttt\" ]"; + + struct js_value *js = create_js_value(val, NULL); + + char *add ="{ \"add0\" : 0.0 }"; + +// struct js_value *js_add = create_js_value(add, NULL); + + + print_value(js); +// add_js_value_index(10, js_add, &js); + append_js_value(js,add); + print_value(js); + + char *add_arr ="{ \"array\" : [ 0.0 ] }"; + append_js_value(js,add_arr); + print_value(js); + + + free_js_value(js); + LOG("line %d\n",__LINE__); + +// free(rrjson); +// free(rrjson1); + +} + + +int main(int argc, char **argv){ + + + run_all_tests_args(argc, argv); + + return 0; +}