debug json before use it in socket, split socket src

This commit is contained in:
2025-09-16 20:10:37 +02:00
parent 8a4268945c
commit 12242987f3
14 changed files with 406 additions and 83 deletions
+10 -5
View File
@@ -2,8 +2,6 @@
CC=gcc
TOOLDIR=$(PWD)/../ytools_t
INCLUDE_DIR=$(TOOLDIR)/include
CFLAGS=-I$(INCLUDE_DIR) -I./src
#SRC_DIR=$(ROOT_DIR)/src
#SRC=$(wildcard */*/*.c)
@@ -13,6 +11,10 @@ LISTDIR=$(PWD)/../list_t
LISTSRC=$(LISTDIR)/src/list_t/list_t.c
LISTSRC_O=$(LISTSRC:.c=.o)
YSOCKETDIR=$(PWD)/../y_socket_t/
YSTRINGLISTSRC=$(YSOCKETDIR)/src/y_socket_t/y_list_string.c
YSTRINGLISTSRC_O=$(YSOCKETSRC:.c=.o)
#SETTSRC=src/set_theoric_t/set_theoric_t.c
#SETTSRC_O=$(SETTSRC:.c=.o)
@@ -25,17 +27,20 @@ TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c
TOOLSRC_O=$(TOOLSRC:.c=.o)
SRC=$(wildcard **/**/*.c)
OBJ=$(SRC:.c=.o) $(TOOLSRC_O)
OBJ=$(SRC:.c=.o) $(TOOLSRC_O) $(LISTSRC_O) $(YSOCKETDIR) $(YSTRINGLISTSRC_O)
INCLUDE_DIR=$(TOOLDIR)/include
CFLAGS=-I$(INCLUDE_DIR) -I./src -I$(YSOCKETDIR)/include
TOPTARGETS := all clean
DEP=$(TOOLDIR) $(LISTDIR)
DEP=$(TOOLDIR) $(LISTDIR) $(YSOCKETDIR)
$(TOPTARGETS): $(DEP)
all: $(JSONSRC_O)
$(JSONSRC_O): $(JSONSRC) $(TOOLSRC_O) # $(LISTSRC_O)
$(JSONSRC_O): $(JSONSRC) $(TOOLSRC_O) $(YSTRINGLISTSRC_O) # $(LISTSRC_O)
$(CC) -o $@ -c $< $(CFLAGS)
+103 -30
View File
@@ -149,7 +149,8 @@ void free_js_value(struct js_value *js){
if((tmpjs->type.object.iter != NULL) && (tmpjs->type.object.prev_object == NULL)){
free(tmpjs->type.object.iter);
}
if(tmpjs->type.object.key)
free(tmpjs->type.object.key);
free_js_value(tmpjs->type.object.value);
js = tmpjs->type.object.next_object;
@@ -174,10 +175,10 @@ void free_js_value(struct js_value *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
else*/
js->str_value = input;
js->code_type = jstype_string;
char *cur = js->str_value;
@@ -498,12 +499,19 @@ struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*
// js->type.object.key = create_js_value_string(cur, js);
if(*cur == '"') ++cur;
js->type.object.key = cur;
char *key = cur;
// /1/js->type.object.key = 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;
// /1/js->type.object.key_length = cur - js->type.object.key;
js->type.object.key_length = cur - key ;
js->type.object.key = malloc(js->type.object.key_length+1);
memcpy(js->type.object.key,key,js->type.object.key_length);
*(js->type.object.key + js->type.object.key_length) ='\0';
//js->type.object.key = cur;
for(; *cur != ':'; ++cur);
++cur;
for(; is_js_space(*cur); ++cur);
@@ -980,33 +988,62 @@ char * original_string_js_value(struct js_value *js){
}
void __print_js_value(struct js_value *js){
//lret=strlen(buf);\
#define UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)\
(y_string_b_uf) = create_y_ptr_STRING((buf), (local_size));\
(global_size) += (local_size);\
push_back_list_y_ptr_STRING((m_str), (y_string_b_uf));\
// printf("debug:global_size=%ld, local_size=%ld, buf=%s / / %d / /\n",(global_size),(local_size),(buf), js->code_type);\
/* tab_depth = number of tabulationfor values, */
long int sprint_js_value_to_y_string_list(struct main_list_y_ptr_STRING *m_str, struct js_value *js, char sep, int tab_depth){
long int global_size = 0, local_size;
//char *buf = malloc(js->length);
//printf("debug: js->length = %ld\n",js->length);
char buf[500]; memset(buf,0,500);
y_ptr_STRING y_string_b_uf;
if(js->code_type == jstype_number){
printf("%Lf",js->type.number);
//memset(buf,0,499);
local_size = sprintf(buf,"%Lf",js->type.number);
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}else if(js->code_type == jstype_string){
printf("\"%s\"",js->type.string);
//memset(buf,0,499);
local_size = sprintf(buf,"\"%s\"",js->type.string);
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}else if(js->code_type == jstype_bool){
printf("%s",js->type.boolean ? "true" : "false");
//memset(buf,0,499);
local_size = sprintf(buf,"%s",js->type.boolean ? "true" : "false");
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}else if(js->code_type == jstype_null){
printf("%s","null");
//memset(buf,0,499);
local_size = sprintf(buf,"%s","null");
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}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),' ');
local_size = sprintf(buf,"%c%*c{ ",sep,tab_depth*(tmpjs->type.object.depth),' ');
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}
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_js_value(tmpjs->type.object.value);
local_size = sprintf(buf,"%c%*c\"%s\" : ",sep, tab_depth*(tmpjs->type.object.depth),' ', tmpjs->type.object.key);
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
global_size += sprint_js_value_to_y_string_list(m_str, tmpjs->type.object.value, sep, tab_depth );
if(tmpjs->type.object.next_object == NULL)
printf("\n%*c} ",4*(tmpjs->type.object.depth),' '); //printf(" }");
else printf(", ");
if(tmpjs->type.object.next_object == NULL){
local_size = sprintf(buf,"%c%*c} ",sep,tab_depth*(tmpjs->type.object.depth),' '); //printf(" }");
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}else{
//memset(buf,0,499);
local_size = sprintf(buf,", ");
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}
//printf("\n");
tmpjs = tmpjs->type.object.next_object;
}
@@ -1014,20 +1051,54 @@ void __print_js_value(struct js_value *js){
struct js_value *tmpjs = js;
while(tmpjs){
if(tmpjs->type.array.prev_element == NULL){
printf("\n%*c[ ",4*(tmpjs->type.array.depth),' ');
//memset(buf,0,499);
local_size = sprintf(buf, "%c%*c[ ",sep,tab_depth*(tmpjs->type.array.depth),' ');
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}
if(tmpjs->type.array.value->code_type < jstype_object ) {
//memset(buf,0,499);
local_size = sprintf(buf,"%c%*c ",sep,tab_depth*(tmpjs->type.array.depth),' ');
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}
global_size += sprint_js_value_to_y_string_list(m_str,tmpjs->type.array.value,sep,tab_depth);
if(tmpjs->type.array.next_element == NULL){ //printf(" ]");
//memset(buf,0,499);
local_size = sprintf(buf,"%c%*c] ",sep,tab_depth*(tmpjs->type.array.depth),' ');
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}else{
//memset(buf,0,499);
local_size = sprintf(buf,", ");
UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
}
if(tmpjs->type.array.value->code_type < jstype_object )
printf("\n%*c ",4*(tmpjs->type.array.depth),' ');
print_js_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);
}
}
//free(buf);
return global_size;
}
/* dstString can be NULL ! (it's better)*/
long int sprint_js_value(char **dstString, struct js_value *js, char sep, int tab_depth){
struct main_list_y_ptr_STRING *m_str=create_var_list_y_ptr_STRING();
// remove_all_ptr_type_list_y_ptr_STRING(m_str);
long int ret = sprint_js_value_to_y_string_list(m_str,js,sep, tab_depth);
copy_list_y_ptr_STRING_to_one_string(dstString , m_str);
//printf("debug:%s\n",*dstString);
purge_ptr_type_list_y_ptr_STRING(m_str);
return ret;
}
/* dstString can be NULL ! (it's better)*/
long int sprint_one_line_js_value(char **dstString, struct js_value *js){
struct main_list_y_ptr_STRING *m_str=create_var_list_y_ptr_STRING();
// remove_all_ptr_type_list_y_ptr_STRING(m_str);
long int ret = sprint_js_value_to_y_string_list(m_str,js,' ', 1);
copy_list_y_ptr_STRING_to_one_string(dstString , m_str);
//printf("debug:%s\n",*dstString);
purge_ptr_type_list_y_ptr_STRING(m_str);
return ret;
}
void print_js_value(struct js_value *js){
if(js->code_type == jstype_number){
@@ -1046,11 +1117,13 @@ void print_js_value(struct js_value *js){
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 );
printf("\n%*c%s : ", 4*(tmpjs->type.object.depth),' ', key );
*/
printf("\n%*c\"%s\" : ", 4*(tmpjs->type.object.depth),' ', tmpjs->type.object.key);
print_js_value(tmpjs->type.object.value);
if(tmpjs->type.object.next_object == NULL)
@@ -1079,7 +1152,7 @@ void print_js_value(struct js_value *js){
void print_js_value(struct js_value *js){
struct js_value * value_of_(struct js_value * js){
if(js->code_type == jstype_object){
@@ -1099,7 +1172,7 @@ struct js_value *get_js_value_of_key(char * key, struct js_value *js ){
js->type.object.iter->current = tmp;
return tmp; //tmp->type.object.value == value_of_(tmp);
}
tmp = next_(tmp);
tmp = tmp->type.object.next_object;//next_(tmp);
}
}
return NULL;
+3 -1
View File
@@ -7,6 +7,7 @@
#include <stdbool.h>
#include "tools_t/tools_t.h"
#include "y_socket_t/y_list_string.h"
#define ITERATOR__(type) \
struct iterator_##type {\
@@ -116,7 +117,8 @@ void add_js_value_index(size_t index, struct js_value *js_to_add, struct js_valu
void delete_key_js_value(char * key, struct js_value **js_org);
void print_js_value(struct js_value *js);
//char* sprint_js_value(struct js_value *js);
long int sprint_js_value(char **dstString, struct js_value *js, char sep, int tab_depth);
long int sprint_one_line_js_value(char **dstString, struct js_value *js);
char * original_string_js_value(struct js_value *js);
+5 -4
View File
@@ -7,10 +7,11 @@ CC=gcc
ROOT_DIR=$(PWD)
YTESTDIR=$(PWD)/../../ytest_t
LISTDIR=$(PWD)/../../list_t
YSOCKETDIR=$(PWD)/../../y_socket_t
INCLUDE_LIST=$(LISTDIR)/src
INCLUDE_DIR=$(PWD)/../src
CFLAGS=-I$(INCLUDE_DIR) -I$(INCLUDE_LIST) -I$(YTESTDIR)/include_ytest/include
CFLAGS=-I$(INCLUDE_DIR) -I$(INCLUDE_LIST) -I$(YTESTDIR)/include_ytest/include -I$(YSOCKETDIR)/include
LDFLAGS=-L$(YTESTDIR) -lytest
#SRC_DIR=$(ROOT_DIR)/src
@@ -27,11 +28,11 @@ 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
YSTRINGLIST_O=$(YSOCKETDIR)/src/y_socket_t/y_list_string.o
TOPTARGETS := all clean
DEPS=$(LISTDIR) $(YTESTDIR) $(JSONDIR)
DEPS=$(LISTDIR) $(YTESTDIR) $(JSONDIR) $(YSOCKETDIR)
$(TOPTARGETS): $(DEPS)
@@ -45,7 +46,7 @@ $(DEPS):
#TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c
#TOOLSRC_O=$(TOOLSRC:.c=.o)
OBJ=$(SRC:.c=.o) $(JSONSRC_O)
OBJ=$(SRC:.c=.o) $(JSONSRC_O) $(YSTRINGLIST_O)
#$(LISTSRC_O)
+18
View File
@@ -480,7 +480,25 @@ TEST(general_val_string_with_extra_value){
}
TEST(sprint_js_value__){
char *val = "{ \"-1 header\" : [ \"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\" : { }} here extra string ";
struct js_value *js = create_js_value(val, NULL);
printf("\n##############################################\n");
print_js_value(js);
printf("\n##############################################\n");
char *buf = NULL;
long int ret = sprint_one_line_js_value(&buf, js);
printf("\nret = %ld,\nbuf:\n%s\n",ret, buf);
printf("\n##############################################\n");
free(buf);
buf=NULL;
ret = sprint_js_value(&buf, js,'\n',8);
printf("\nret = %ld,\nbuf:\n%s\n",ret, buf);
free_js_value(js);
if(buf) free(buf);
}
int main(int argc, char **argv){