From 12242987f310762473cc5279b5951dbae87012ce Mon Sep 17 00:00:00 2001 From: fanasina Date: Tue, 16 Sep 2025 20:10:37 +0200 Subject: [PATCH] debug json before use it in socket, split socket src --- y_socket_t/Makefile | 18 +- y_socket_t/include/y_socket_t/y_list_string.h | 25 +++ y_socket_t/include/y_socket_t/y_node_t.h | 2 + y_socket_t/include/y_socket_t/y_socket_t.h | 12 +- y_socket_t/src/y_socket_t/y_file_handler.c | 14 +- y_socket_t/src/y_socket_t/y_list_string.c | 63 +++++++ y_socket_t/src/y_socket_t/y_node_t.c | 2 + y_socket_t/src/y_socket_t/y_socket_t.c | 160 ++++++++++++++---- y_socket_t/test/Makefile | 14 +- yjson_t/Makefile | 15 +- yjson_t/src/json_t/json_t.c | 133 +++++++++++---- yjson_t/src/json_t/json_t.h | 4 +- yjson_t/test/Makefile | 9 +- yjson_t/test/is_good.c | 18 ++ 14 files changed, 406 insertions(+), 83 deletions(-) create mode 100644 y_socket_t/include/y_socket_t/y_list_string.h create mode 100644 y_socket_t/src/y_socket_t/y_list_string.c diff --git a/y_socket_t/Makefile b/y_socket_t/Makefile index 50c15ac..8a21afd 100644 --- a/y_socket_t/Makefile +++ b/y_socket_t/Makefile @@ -1,3 +1,4 @@ +#file: y_socket_t/Makefile # lib: -lysocket PROJECT_LIB=libysocket.so CC=gcc @@ -7,9 +8,10 @@ SOCDIR=$(PWD) YLISTDIR=$(PWD)/../list_t YWORKDIR=$(PWD)/../y_worker_t +YJSONDIR=$(PWD)/../yjson_t -INCLUDE=-I$(INCLUDE_DIRS) -I$(YLISTDIR)/src -I$(YWORKDIR)/include +INCLUDE=-I$(INCLUDE_DIRS) -I$(YLISTDIR)/src -I$(YWORKDIR)/include -I$(YJSONDIR)/src CFLAGS=-g -lpthread -Wall -Werror -fpic $(INCLUDE) #"-D DEBUG=1" #LDFLAGS= @@ -20,7 +22,10 @@ YSOCKSRC_O=$(YSOCKSRC:.c=.o) YNODESRC=$(PWD)/src/y_socket_t/y_node_t.c YNODESRC_O=$(YNODESRC:.c=.o) -YFILEHANDLERSRC=$(PWD)/src/y_socket_t/y_file_handler.c +YY_STRINGSRC=$(PWD)/src/y_socket_t/y_list_string.c +YY_STRINGSRC_O=$(YY_STRINGSRC:.c=.o) + +FILEHANDLERSRC=$(PWD)/src/y_socket_t/y_file_handler.c YFILEHANDLERSRC_O=$(YFILEHANDLERSRC:.c=.o) YLISTSRC=$(YLISTDIR)/src/list_t/list_t.c @@ -29,14 +34,16 @@ YLISTSRC_O=$(YLISTSRC:.c=.o) WORKSRC_0=$(YWORKDIR)/src/y_worker_t/y_worker_t.o YTASKSRC_0=$(YWORKDIR)/src/y_worker_t/y_task_t.o +YJSONSRC=$(YJSONDIR)/src/json_t/json_t.c +YJSONSRC_O=$(YJSONSRC:.c=.o) all: $(PROJECT_LIB) TOPTARGETS := all clean -DEPS:=$(YLISTDIR) $(YWORKDIR) +DEPS:=$(YLISTDIR) $(YWORKDIR) $(YJSONDIR) -OBJ=$(YSOCKSRC_O) $(YNODESRC_O) $(YLISTSRC_O) $(WORKSRC_0) $(YTASKSRC_0) $(YFILEHANDLERSRC_O) +OBJ=$(YSOCKSRC_O) $(YNODESRC_O) $(YY_STRINGSRC_O) $(YLISTSRC_O) $(WORKSRC_0) $(YTASKSRC_0) $(YFILEHANDLERSRC_O) $(YJSONSRC_O) $(TOPTARGETS): $(DEPS) @@ -58,6 +65,9 @@ $(YSOCKSRC_O): $(YSOCKSRC) $(YNODESRC_O) $(YFILEHANDLERSRC_O) $(YNODESRC_O): $(YNODESRC) $(YLISTSRC_O) $(CC) -o $@ -c $< $(CFLAGS) +$(YY_STRINGSRC_O): $(YY_STRINGSRC) $(YLISTSRC_O) + $(CC) -o $@ -c $< $(CFLAGS) + $(YFILEHANDLERSRC_O): $(YFILEHANDLERSRC) $(YSOCKSRC_O) $(CC) -o $@ -c $< $(CFLAGS) diff --git a/y_socket_t/include/y_socket_t/y_list_string.h b/y_socket_t/include/y_socket_t/y_list_string.h new file mode 100644 index 0000000..aa79bba --- /dev/null +++ b/y_socket_t/include/y_socket_t/y_list_string.h @@ -0,0 +1,25 @@ +/*file: include/y_socket_t/y_list_string.h */ +#ifndef Y_PTR_STRING_T_H__C +#define Y_PTR_STRING_T_H__C + + +#include + +#include "list_t/list_t.h" + + +struct y_string{ + char * buf; + size_t size; +}; + +typedef struct y_string * y_ptr_STRING; + +struct y_string * create_y_ptr_STRING(const char *buf, size_t size); +GENERATE_LIST_ALL(y_ptr_STRING) +GEN_HEAD_PTR_LIST(y_ptr_STRING) + +size_t total_size_list_y_ptr_STRING(struct main_list_y_ptr_STRING *mstr); +size_t copy_list_y_ptr_STRING_to_one_string(char **p_dst_str, struct main_list_y_ptr_STRING *mstr); + +#endif /* Y_PTR_STRING_T_H__C */ diff --git a/y_socket_t/include/y_socket_t/y_node_t.h b/y_socket_t/include/y_socket_t/y_node_t.h index f481386..6f7cb39 100644 --- a/y_socket_t/include/y_socket_t/y_node_t.h +++ b/y_socket_t/include/y_socket_t/y_node_t.h @@ -1,3 +1,5 @@ +/*file: include/y_socket_t/y_node_t.h */ + #ifndef __Y_NODE_T_H__C #define __Y_NODE_T_H__C diff --git a/y_socket_t/include/y_socket_t/y_socket_t.h b/y_socket_t/include/y_socket_t/y_socket_t.h index a5f9203..33fa6ea 100644 --- a/y_socket_t/include/y_socket_t/y_socket_t.h +++ b/y_socket_t/include/y_socket_t/y_socket_t.h @@ -1,3 +1,5 @@ +/*file: include/y_socket_t/y_socket_t.h*/ + #ifndef Y_SOCKET_T_H__C #define Y_SOCKET_T_H__C @@ -20,14 +22,16 @@ #include #include -#include - +//#include "y_socket_t/y_socket_t.h" #include "y_socket_t/y_node_t.h" #include "y_socket_t/y_file_handler.h" #include "y_worker_t/y_worker_t.h" #include "y_worker_t/y_task_t.h" +#include "y_socket_t/y_list_string.h" + +#include "json_t/json_t.h" #define BUF_SIZE 500 #define TIMEOUT_MS 100 @@ -41,6 +45,8 @@ enum ipVersions{ extern const int af_array[nbIpVersion];//={AF_INET, AF_INET6}; +/* y_ptr_STRING */ +#if 0 struct y_string{ char * buf; size_t size; @@ -55,6 +61,8 @@ GEN_HEAD_PTR_LIST(y_ptr_STRING) size_t total_size_list_y_ptr_STRING(struct main_list_y_ptr_STRING *mstr); size_t copy_list_y_ptr_STRING_to_one_string(char **p_dst_str, struct main_list_y_ptr_STRING *mstr); +#endif /* y_ptr_STRING */ + struct y_socket_t{ struct pollfd *fds; size_t size_fds; diff --git a/y_socket_t/src/y_socket_t/y_file_handler.c b/y_socket_t/src/y_socket_t/y_file_handler.c index ca8c9a6..092a0dd 100644 --- a/y_socket_t/src/y_socket_t/y_file_handler.c +++ b/y_socket_t/src/y_socket_t/y_file_handler.c @@ -1,3 +1,5 @@ +/*file: src/y_socket_t/y_file_handler.c*/ + #include "y_socket_t/y_file_handler.h" //#include "y_socket_t/y_node_t.h" @@ -122,7 +124,7 @@ void* y_socket_send_file_for_all_nodes(void* arg){ } y_send_post_file_to_all_nodes(arg); - usleep(100); + usleep(1); //for(struct list_y_NODE_T *local_list_current = nodes->begin_list; local_list_current; local_list_current=local_list_current->next ) //memset(buf_send, 0, BUF_SIZE+1); @@ -208,7 +210,17 @@ void* y_socket_send_file_for_all_nodes(void* arg){ return NULL; } +/* +struct arg_record_to_file{ + int *is_file_to_record, + char * filename, + char *buf +}; +void record_buffer_to_file(void *arg){ + +} +*/ void receve_from_node(struct pollfd *fds, char *msg, size_t count){ printf("\ndebug: <<<< receve_from_node %s %ld\n\n",msg,count); char filename[500]; diff --git a/y_socket_t/src/y_socket_t/y_list_string.c b/y_socket_t/src/y_socket_t/y_list_string.c new file mode 100644 index 0000000..d80fc8f --- /dev/null +++ b/y_socket_t/src/y_socket_t/y_list_string.c @@ -0,0 +1,63 @@ +/*file: "src/y_socket_t/y_list_string.c" */ +#include "y_socket_t/y_list_string.h" + +struct y_string * create_y_ptr_STRING(const char *buf, size_t size){ + struct y_string *string=malloc(sizeof(struct y_string)); + string->buf=malloc(size+1); + if(buf){ + //strncpy(string->buf, buf, size); + //snprintf(string->buf, size, "%s", buf); + memcpy(string->buf, buf, size); + //if(strlen(buf)>=size) + if(buf[size]!='\0') + string->buf[size]='\0'; +// printf("\nDEBUG: ################\n%ld~~%s~~\n################\n",size,string->buf); + } + string->size=size; + return string; +} + +GEN_LIST_ALL(y_ptr_STRING) + +GEN_FUNC_PTR_LIST_FREE(y_ptr_STRING){ + free(arg->buf); + free(arg); +} + +size_t total_size_list_y_ptr_STRING(struct main_list_y_ptr_STRING *mstr){ + size_t total_size=0; + for(struct list_y_ptr_STRING * local_current = mstr->begin_list; local_current; local_current = local_current->next){ + total_size += local_current->value->size; + } + //printf("debug: totalsize :%ld\n",total_size); + return total_size; +} + +size_t copy_list_y_ptr_STRING_to_one_string(char **p_dst_str, struct main_list_y_ptr_STRING *mstr){ + if(*p_dst_str == NULL){ + *p_dst_str=malloc(total_size_list_y_ptr_STRING(mstr)); + } + char * dst_str = *p_dst_str; + char *cur_str = dst_str; + size_t local_size=0; + size_t count_size=0; + //for(move_current_to_begin_list_y_ptr_STRING(mstr); mstr->current_list; increment_list_y_ptr_STRING(mstr)) + for(struct list_y_ptr_STRING * local_current = mstr->begin_list; local_current; local_current = local_current->next){ + local_size = local_current->value->size; + //printf(">>debug: countsize :%ld/local=%ld |%s|(%s)\n",count_size,local_size,local_current->value->buf,dst_str); + //printf("debug: local_size :%ld\n",local_size); + for(size_t i=0; ivalue->buf[i]; + } + count_size += local_size; + //printf("<buf=malloc(size+1); @@ -54,6 +60,8 @@ size_t copy_list_y_ptr_STRING_to_one_string(char **p_dst_str, struct main_list_y } +#endif /* y_ptr_STRING */ + struct y_socket_t * y_socket_create(char *port, size_t size_fds, int nb_workers){ struct y_socket_t *sock_temp=malloc(sizeof(struct y_socket_t)); if(size_fds>=nbIpVersion+1) @@ -382,6 +390,116 @@ void* y_socket_handler_(void *arg){ return NULL; } +void handle_input_kbd(char *buf, ssize_t buf_len ,void *arg){ + struct y_socket_t * argSock = (struct y_socket_t*)arg; + struct pollfd *fds = argSock->fds; + y_NODE_T node; + int af, status; + /* + ssize_t nread, buf_len; + char buf[BUF_SIZE]; + //struct main_list_y_ptr_STRING *m_str=create_var_list_y_ptr_STRING(); + //printf("fd = %d\n event=%d\n\n",fds[1].fd,pollEventRec); + //fds[1].events = 0; + + puts("Saisie du message : "); + memset(buf, 0, sizeof buf); + //scanf(" %"xstr(BUF_SIZE)"[^\n]%*c", buf); + buf_len = read(0,buf,BUF_SIZE); + */ + // printf("message saisi : %s\n len = %ld\n",buf, buf_len); + if(buf_len>6){ +#if 1 + char cmd[BUF_SIZE], dst_addr[BUF_SIZE];//, msg_buf[BUF_SIZE]; + int index_buf=0, index_str=0; + for(; buf[index_buf]!=' '; ++index_buf){ + cmd[index_str++]=buf[index_buf]; + } + cmd[index_str]='\0'; +// printf("debug : index_str= %d; cmd=[%s]\n",index_str, cmd); + + index_str=0; + while(buf[index_buf]==' '){++index_buf;} + for(; buf[index_buf]!=' '; ++index_buf){ + dst_addr[index_str++]=buf[index_buf]; + } + dst_addr[index_str]='\0'; + while(buf[index_buf]==' '){++index_buf;} + /*index_str=0; + for(; buf[index_buf]!='\n'; ++index_buf) + msg_buf[index_str++]=buf[index_buf]; + msg_buf[index_str++]='\0';*/ + + // printf("debug : index_str=%d, dst_addr=[%s]\n", index_str, dst_addr); +#endif + + + if(strncmp(cmd, "sendto", 6)==0){ +// printf("debug : sendto match, dst_addr=[%s]\n", dst_addr); + if(set_addr_y_NODE_T(&node, dst_addr)){ +// printf("debug : set_addr_y_NODE_T done\n"); + set_str_port_y_NODE_T(&node, argSock->port); + update_nodes(node, argSock->nodes); + af=(node.addr.ss_family == AF_INET6); + + + printf("debug : af = AF_INET=%d, af = AF_INET6=%d, vs af=[%d]\n",AF_INET, AF_INET6, af); + + if(sendto(fds[af].fd, buf+index_buf , buf_len-index_buf, + // msg_buf, index_str, + 0, + (struct sockaddr*)(&(node.addr)), node.addr_len) == -1){ + printf("message erreur sendto : %s\n\n",buf); + perror("sendto:"); + close(fds[af].fd); + return ;//NULL; + } + char dddnn[56]; + put_y_NODE_T_in_string(&node, dddnn); + printf("debug: sendto : %s: msg :%s\n\n",dddnn, buf+index_buf); + + + } + } + } +} + +void handle_buf_socket_rec(char *temp_all_buf, y_NODE_T node, struct main_list_ptr_y_WORKER_T * workers, struct argExecTasQ *argx, struct main_list_TYPE_PTR * list_arg, void * arg){ + struct y_socket_t * argSock = (struct y_socket_t*)arg; + struct pollfd *fds = argSock->fds; + + if(strncmp(temp_all_buf,"update standby",14)==0){ + //pthread_mutex_lock(sock->mut_go_on); + //sock->go_on = 0; + //pthread_mutex_unlock(sock->mut_go_on); + standby_all_workers(workers->begin_list->value->arg); +// printf("debug: kill_all\n"); + } + else if(strncmp(temp_all_buf,"update wakeup",13)==0){ + //pthread_mutex_lock(sock->mut_go_on); + //sock->go_on = 0; + //pthread_mutex_unlock(sock->mut_go_on); + wakeup_all_workers(workers->begin_list->value->arg); +// printf("debug: kill_all\n"); + } + else{ + struct arg_handler_ *ptr_argHandl = malloc(sizeof(struct arg_handler_)); + ptr_argHandl->buf = temp_all_buf; + ptr_argHandl->fds=fds; + ptr_argHandl->sock=argSock; + ptr_argHandl->node=node; + ptr_argHandl->argw=workers->begin_list->value->arg; + + push_back_list_TYPE_PTR(list_arg, ptr_argHandl); + struct y_task_t task_handl = { + .func=y_socket_handler_, + .arg=ptr_argHandl, + .status=TASK_PENDING, + }; + push_tasQ(argx->tasQ, task_handl); + } +} + void *y_socket_poll_fds(void *arg){ struct y_socket_t * argSock = (struct y_socket_t*)arg; // // // @@ -487,36 +605,8 @@ void *y_socket_poll_fds(void *arg){ copy_list_y_ptr_STRING_to_one_string(&temp_all_buf , m_str); push_back_list_TYPE_PTR(list_arg, temp_all_buf); - if(strncmp(temp_all_buf,"update standby",14)==0){ - //pthread_mutex_lock(sock->mut_go_on); - //sock->go_on = 0; - //pthread_mutex_unlock(sock->mut_go_on); - standby_all_workers(workers->begin_list->value->arg); -// printf("debug: kill_all\n"); - } - else if(strncmp(temp_all_buf,"update wakeup",13)==0){ - //pthread_mutex_lock(sock->mut_go_on); - //sock->go_on = 0; - //pthread_mutex_unlock(sock->mut_go_on); - wakeup_all_workers(workers->begin_list->value->arg); -// printf("debug: kill_all\n"); - } - else{ - struct arg_handler_ *ptr_argHandl = malloc(sizeof(struct arg_handler_)); - ptr_argHandl->buf = temp_all_buf; - ptr_argHandl->fds=fds; - ptr_argHandl->sock=argSock; - ptr_argHandl->node=node; - ptr_argHandl->argw=workers->begin_list->value->arg; - - push_back_list_TYPE_PTR(list_arg, ptr_argHandl); - struct y_task_t task_handl = { - .func=y_socket_handler_, - .arg=ptr_argHandl, - .status=TASK_PENDING, - }; - push_tasQ(argx->tasQ, task_handl); - } + + handle_buf_socket_rec(temp_all_buf, node, workers, argx, list_arg, arg); /// //y_socket_handler_(temp_all_buf, fds, argSock); @@ -526,13 +616,18 @@ void *y_socket_poll_fds(void *arg){ // stdin poll if(fds[2].revents){// && POLLIN //pollEventRec = fds[1].events; - //printf("fd = %d\n event=%d\n\n",fds[1].fd,pollEventRec); + + //handle_input_kbd(arg); +//printf("fd = %d\n event=%d\n\n",fds[1].fd,pollEventRec); //fds[1].events = 0; + puts("Saisie du message : "); memset(buf, 0, sizeof buf); //scanf(" %"xstr(BUF_SIZE)"[^\n]%*c", buf); buf_len = read(0,buf,BUF_SIZE); printf("message saisi : %s\n len = %ld\n",buf, buf_len); +// handle_input_kbd(buf, buf_len ,arg); +#if 1 if(buf_len>6){ #if 1 char cmd[BUF_SIZE], dst_addr[BUF_SIZE];//, msg_buf[BUF_SIZE]; @@ -587,7 +682,8 @@ void *y_socket_poll_fds(void *arg){ } } } - +#endif + } diff --git a/y_socket_t/test/Makefile b/y_socket_t/test/Makefile index 070f104..823d2cb 100644 --- a/y_socket_t/test/Makefile +++ b/y_socket_t/test/Makefile @@ -1,4 +1,4 @@ - +#file: y_socket_t/test/Makefil @@ -7,10 +7,11 @@ CC=gcc YTESTDIR=$(PWD)/../../ytest_t YLISTDIR=$(PWD)/../../list_t YWORKDIR=$(PWD)/../../y_worker_t +YJSONDIR=$(PWD)/../../yjson_t ROOT_DIR=$(PWD)/.. INCLUDE_DIR=$(ROOT_DIR)/include -CFLAGS=-I$(INCLUDE_DIR) -I$(YTESTDIR)/include_ytest/include -I$(YLISTDIR)/src -I$(YWORKDIR)/include +CFLAGS=-I$(INCLUDE_DIR) -I$(YTESTDIR)/include_ytest/include -I$(YLISTDIR)/src -I$(YWORKDIR)/include -I$(YJSONDIR)/src LDFLAGS=-L$(YTESTDIR) -lytest -lpthread -lm -lOpenCL #SRC_DIR=$(ROOT_DIR)/src @@ -34,18 +35,23 @@ YFILEHANDLSRC_O=$(YFILEHANDLSRC:.c=.o) YNODESRC=$(ROOT_DIR)/src/y_socket_t/y_node_t.c YNODESRC_O=$(YNODESRC:.c=.o) +YY_STRINGSRC=$(ROOT_DIR)/src/y_socket_t/y_list_string.c +YY_STRINGSRC_O=$(YY_STRINGSRC:.c=.o) + YLISTSRC=$(YLISTDIR)/src/list_t/list_t.c YLISTSRC_O=$(YLISTSRC:.c=.o) YWORKSRC_0=$(YWORKDIR)/src/y_worker_t/y_worker_t.o YTASKSRC_0=$(YWORKDIR)/src/y_worker_t/y_task_t.o +YJSONSRC=$(YJSONDIR)/src/json_t/json_t.c +YJSONSRC_O=$(YJSONSRC:.c=.o) TOPTARGETS := all clean -DEPS := $(YTESTDIR) $(YLISTDIR) $(YWORKDIR) $(ROOT_DIR) +DEPS := $(YTESTDIR) $(YLISTDIR) $(YWORKDIR) $(ROOT_DIR) $(YJSONDIR) -OBJ=$(YSOCKSRC_O) $(YFILEHANDLSRC_O) $(YNODESRC_O) $(YLISTSRC_O) $(YWORKSRC_0) $(YTASKSRC_0) +OBJ=$(YSOCKSRC_O) $(YFILEHANDLSRC_O) $(YNODESRC_O) $(YY_STRINGSRC_O) $(YLISTSRC_O) $(YWORKSRC_0) $(YTASKSRC_0) $(YJSONSRC_O) LIB_YTEST=$(YTESTDIR)/libytest.so diff --git a/yjson_t/Makefile b/yjson_t/Makefile index 1d67f3d..db389f9 100644 --- a/yjson_t/Makefile +++ b/yjson_t/Makefile @@ -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) diff --git a/yjson_t/src/json_t/json_t.c b/yjson_t/src/json_t/json_t.c index 2a838aa..ec50399 100644 --- a/yjson_t/src/json_t/json_t.c +++ b/yjson_t/src/json_t/json_t.c @@ -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; diff --git a/yjson_t/src/json_t/json_t.h b/yjson_t/src/json_t/json_t.h index bdaadc3..15f92de 100644 --- a/yjson_t/src/json_t/json_t.h +++ b/yjson_t/src/json_t/json_t.h @@ -7,6 +7,7 @@ #include #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); diff --git a/yjson_t/test/Makefile b/yjson_t/test/Makefile index 47a6674..cc3f4af 100644 --- a/yjson_t/test/Makefile +++ b/yjson_t/test/Makefile @@ -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) diff --git a/yjson_t/test/is_good.c b/yjson_t/test/is_good.c index aa2c6ba..1c3e4fb 100644 --- a/yjson_t/test/is_good.c +++ b/yjson_t/test/is_good.c @@ -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){