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
+14 -4
View File
@@ -1,3 +1,4 @@
#file: y_socket_t/Makefile
# lib: -lysocket # lib: -lysocket
PROJECT_LIB=libysocket.so PROJECT_LIB=libysocket.so
CC=gcc CC=gcc
@@ -7,9 +8,10 @@ SOCDIR=$(PWD)
YLISTDIR=$(PWD)/../list_t YLISTDIR=$(PWD)/../list_t
YWORKDIR=$(PWD)/../y_worker_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" CFLAGS=-g -lpthread -Wall -Werror -fpic $(INCLUDE) #"-D DEBUG=1"
#LDFLAGS= #LDFLAGS=
@@ -20,7 +22,10 @@ YSOCKSRC_O=$(YSOCKSRC:.c=.o)
YNODESRC=$(PWD)/src/y_socket_t/y_node_t.c YNODESRC=$(PWD)/src/y_socket_t/y_node_t.c
YNODESRC_O=$(YNODESRC:.c=.o) 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) YFILEHANDLERSRC_O=$(YFILEHANDLERSRC:.c=.o)
YLISTSRC=$(YLISTDIR)/src/list_t/list_t.c 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 WORKSRC_0=$(YWORKDIR)/src/y_worker_t/y_worker_t.o
YTASKSRC_0=$(YWORKDIR)/src/y_worker_t/y_task_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) all: $(PROJECT_LIB)
TOPTARGETS := all clean 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) $(TOPTARGETS): $(DEPS)
@@ -58,6 +65,9 @@ $(YSOCKSRC_O): $(YSOCKSRC) $(YNODESRC_O) $(YFILEHANDLERSRC_O)
$(YNODESRC_O): $(YNODESRC) $(YLISTSRC_O) $(YNODESRC_O): $(YNODESRC) $(YLISTSRC_O)
$(CC) -o $@ -c $< $(CFLAGS) $(CC) -o $@ -c $< $(CFLAGS)
$(YY_STRINGSRC_O): $(YY_STRINGSRC) $(YLISTSRC_O)
$(CC) -o $@ -c $< $(CFLAGS)
$(YFILEHANDLERSRC_O): $(YFILEHANDLERSRC) $(YSOCKSRC_O) $(YFILEHANDLERSRC_O): $(YFILEHANDLERSRC) $(YSOCKSRC_O)
$(CC) -o $@ -c $< $(CFLAGS) $(CC) -o $@ -c $< $(CFLAGS)
@@ -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 <string.h>
#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 */
+2
View File
@@ -1,3 +1,5 @@
/*file: include/y_socket_t/y_node_t.h */
#ifndef __Y_NODE_T_H__C #ifndef __Y_NODE_T_H__C
#define __Y_NODE_T_H__C #define __Y_NODE_T_H__C
+10 -2
View File
@@ -1,3 +1,5 @@
/*file: include/y_socket_t/y_socket_t.h*/
#ifndef Y_SOCKET_T_H__C #ifndef Y_SOCKET_T_H__C
#define Y_SOCKET_T_H__C #define Y_SOCKET_T_H__C
@@ -20,14 +22,16 @@
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <string.h> //#include "y_socket_t/y_socket_t.h"
#include "y_socket_t/y_node_t.h" #include "y_socket_t/y_node_t.h"
#include "y_socket_t/y_file_handler.h" #include "y_socket_t/y_file_handler.h"
#include "y_worker_t/y_worker_t.h" #include "y_worker_t/y_worker_t.h"
#include "y_worker_t/y_task_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 BUF_SIZE 500
#define TIMEOUT_MS 100 #define TIMEOUT_MS 100
@@ -41,6 +45,8 @@ enum ipVersions{
extern const int af_array[nbIpVersion];//={AF_INET, AF_INET6}; extern const int af_array[nbIpVersion];//={AF_INET, AF_INET6};
/* y_ptr_STRING */
#if 0
struct y_string{ struct y_string{
char * buf; char * buf;
size_t size; 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 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); 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 y_socket_t{
struct pollfd *fds; struct pollfd *fds;
size_t size_fds; size_t size_fds;
+13 -1
View File
@@ -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_file_handler.h"
//#include "y_socket_t/y_node_t.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); 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 ) //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); //memset(buf_send, 0, BUF_SIZE+1);
@@ -208,7 +210,17 @@ void* y_socket_send_file_for_all_nodes(void* arg){
return NULL; 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){ void receve_from_node(struct pollfd *fds, char *msg, size_t count){
printf("\ndebug: <<<< receve_from_node %s %ld\n\n",msg,count); printf("\ndebug: <<<< receve_from_node %s %ld\n\n",msg,count);
char filename[500]; char filename[500];
+63
View File
@@ -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; i<local_size; ++i){
cur_str[i]=local_current->value->buf[i];
}
count_size += local_size;
//printf("<<debug: countsize :%ld/local=%ld |%s|(%s)\n",count_size,local_size,cur_str,dst_str);
cur_str = dst_str + count_size;
}
*(dst_str + count_size - 1)='\0';
//printf("debug: |%s|\n", dst_str);
return count_size;
}
+2
View File
@@ -1,3 +1,5 @@
/*file: src/y_socket_t/y_node_t.c*/
#include "y_socket_t/y_node_t.h" #include "y_socket_t/y_node_t.h"
#include "y_socket_t/y_socket_t.h" #include "y_socket_t/y_socket_t.h"
+125 -29
View File
@@ -1,7 +1,13 @@
/*file: src/y_socket_t/y_socket_t.c */
#include "y_socket_t/y_socket_t.h" #include "y_socket_t/y_socket_t.h"
//#include "y_socket_t/y_list_string.h"
//#include "json_t/json_t.h"
const int af_array[nbIpVersion]={AF_INET, AF_INET6}; const int af_array[nbIpVersion]={AF_INET, AF_INET6};
/* y_ptr_STRING */
#if 0
struct y_string * create_y_ptr_STRING(const char *buf, size_t size){ struct y_string * create_y_ptr_STRING(const char *buf, size_t size){
struct y_string *string=malloc(sizeof(struct y_string)); struct y_string *string=malloc(sizeof(struct y_string));
string->buf=malloc(size+1); string->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 * y_socket_create(char *port, size_t size_fds, int nb_workers){
struct y_socket_t *sock_temp=malloc(sizeof(struct y_socket_t)); struct y_socket_t *sock_temp=malloc(sizeof(struct y_socket_t));
if(size_fds>=nbIpVersion+1) if(size_fds>=nbIpVersion+1)
@@ -382,6 +390,116 @@ void* y_socket_handler_(void *arg){
return NULL; 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){ void *y_socket_poll_fds(void *arg){
struct y_socket_t * argSock = (struct y_socket_t*)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); copy_list_y_ptr_STRING_to_one_string(&temp_all_buf , m_str);
push_back_list_TYPE_PTR(list_arg, temp_all_buf); 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); handle_buf_socket_rec(temp_all_buf, node, workers, argx, list_arg, arg);
struct y_task_t task_handl = {
.func=y_socket_handler_,
.arg=ptr_argHandl,
.status=TASK_PENDING,
};
push_tasQ(argx->tasQ, task_handl);
}
/// ///
//y_socket_handler_(temp_all_buf, fds, argSock); //y_socket_handler_(temp_all_buf, fds, argSock);
@@ -526,13 +616,18 @@ void *y_socket_poll_fds(void *arg){
// stdin poll // stdin poll
if(fds[2].revents){// && POLLIN if(fds[2].revents){// && POLLIN
//pollEventRec = fds[1].events; //pollEventRec = fds[1].events;
//handle_input_kbd(arg);
//printf("fd = %d\n event=%d\n\n",fds[1].fd,pollEventRec); //printf("fd = %d\n event=%d\n\n",fds[1].fd,pollEventRec);
//fds[1].events = 0; //fds[1].events = 0;
puts("Saisie du message : "); puts("Saisie du message : ");
memset(buf, 0, sizeof buf); memset(buf, 0, sizeof buf);
//scanf(" %"xstr(BUF_SIZE)"[^\n]%*c", buf); //scanf(" %"xstr(BUF_SIZE)"[^\n]%*c", buf);
buf_len = read(0,buf,BUF_SIZE); buf_len = read(0,buf,BUF_SIZE);
printf("message saisi : %s\n len = %ld\n",buf, buf_len); 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(buf_len>6){
#if 1 #if 1
char cmd[BUF_SIZE], dst_addr[BUF_SIZE];//, msg_buf[BUF_SIZE]; char cmd[BUF_SIZE], dst_addr[BUF_SIZE];//, msg_buf[BUF_SIZE];
@@ -587,6 +682,7 @@ void *y_socket_poll_fds(void *arg){
} }
} }
} }
#endif
} }
+10 -4
View File
@@ -1,4 +1,4 @@
#file: y_socket_t/test/Makefil
@@ -7,10 +7,11 @@ CC=gcc
YTESTDIR=$(PWD)/../../ytest_t YTESTDIR=$(PWD)/../../ytest_t
YLISTDIR=$(PWD)/../../list_t YLISTDIR=$(PWD)/../../list_t
YWORKDIR=$(PWD)/../../y_worker_t YWORKDIR=$(PWD)/../../y_worker_t
YJSONDIR=$(PWD)/../../yjson_t
ROOT_DIR=$(PWD)/.. ROOT_DIR=$(PWD)/..
INCLUDE_DIR=$(ROOT_DIR)/include 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 LDFLAGS=-L$(YTESTDIR) -lytest -lpthread -lm -lOpenCL
#SRC_DIR=$(ROOT_DIR)/src #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=$(ROOT_DIR)/src/y_socket_t/y_node_t.c
YNODESRC_O=$(YNODESRC:.c=.o) 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=$(YLISTDIR)/src/list_t/list_t.c
YLISTSRC_O=$(YLISTSRC:.c=.o) YLISTSRC_O=$(YLISTSRC:.c=.o)
YWORKSRC_0=$(YWORKDIR)/src/y_worker_t/y_worker_t.o YWORKSRC_0=$(YWORKDIR)/src/y_worker_t/y_worker_t.o
YTASKSRC_0=$(YWORKDIR)/src/y_worker_t/y_task_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 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 LIB_YTEST=$(YTESTDIR)/libytest.so
+10 -5
View File
@@ -2,8 +2,6 @@
CC=gcc CC=gcc
TOOLDIR=$(PWD)/../ytools_t TOOLDIR=$(PWD)/../ytools_t
INCLUDE_DIR=$(TOOLDIR)/include
CFLAGS=-I$(INCLUDE_DIR) -I./src
#SRC_DIR=$(ROOT_DIR)/src #SRC_DIR=$(ROOT_DIR)/src
#SRC=$(wildcard */*/*.c) #SRC=$(wildcard */*/*.c)
@@ -13,6 +11,10 @@ LISTDIR=$(PWD)/../list_t
LISTSRC=$(LISTDIR)/src/list_t/list_t.c LISTSRC=$(LISTDIR)/src/list_t/list_t.c
LISTSRC_O=$(LISTSRC:.c=.o) 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=src/set_theoric_t/set_theoric_t.c
#SETTSRC_O=$(SETTSRC:.c=.o) #SETTSRC_O=$(SETTSRC:.c=.o)
@@ -25,17 +27,20 @@ TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c
TOOLSRC_O=$(TOOLSRC:.c=.o) TOOLSRC_O=$(TOOLSRC:.c=.o)
SRC=$(wildcard **/**/*.c) 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 TOPTARGETS := all clean
DEP=$(TOOLDIR) $(LISTDIR) DEP=$(TOOLDIR) $(LISTDIR) $(YSOCKETDIR)
$(TOPTARGETS): $(DEP) $(TOPTARGETS): $(DEP)
all: $(JSONSRC_O) all: $(JSONSRC_O)
$(JSONSRC_O): $(JSONSRC) $(TOOLSRC_O) # $(LISTSRC_O) $(JSONSRC_O): $(JSONSRC) $(TOOLSRC_O) $(YSTRINGLISTSRC_O) # $(LISTSRC_O)
$(CC) -o $@ -c $< $(CFLAGS) $(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)){ if((tmpjs->type.object.iter != NULL) && (tmpjs->type.object.prev_object == NULL)){
free(tmpjs->type.object.iter); free(tmpjs->type.object.iter);
} }
if(tmpjs->type.object.key)
free(tmpjs->type.object.key);
free_js_value(tmpjs->type.object.value); free_js_value(tmpjs->type.object.value);
js = tmpjs->type.object.next_object; 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 * create_js_value_string(char * input, struct js_value * parent){
struct js_value * js = malloc(sizeof(struct js_value)); struct js_value * js = malloc(sizeof(struct js_value));
js->parent = parent; js->parent = parent;
/*
if(*input == '"') if(*input == '"')
js->str_value = input+1; js->str_value = input+1;
else else*/
js->str_value = input; js->str_value = input;
js->code_type = jstype_string; js->code_type = jstype_string;
char *cur = js->str_value; 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); // js->type.object.key = create_js_value_string(cur, js);
if(*cur == '"') ++cur; if(*cur == '"') ++cur;
js->type.object.key = cur; char *key = cur;
// /1/js->type.object.key = cur;
for(; *cur != '"'; ++cur); for(; *cur != '"'; ++cur);
//js->key = cur; //js->key = cur;
//for(; *cur != '"'; ++cur); //for(; *cur != '"'; ++cur);
//js->key_length = cur - js->key; //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); for(; *cur != ':'; ++cur);
++cur; ++cur;
for(; is_js_space(*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){ 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){ }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){ }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){ }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){ }else if(js->code_type == jstype_object){
struct js_value *tmpjs = js; struct js_value *tmpjs = js;
while(tmpjs){ while(tmpjs){
if(tmpjs->type.object.prev_object == NULL){ 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]; local_size = sprintf(buf,"%c%*c\"%s\" : ",sep, tab_depth*(tmpjs->type.object.depth),' ', tmpjs->type.object.key);
strncpy(key, tmpjs->type.object.key, tmpjs->type.object.key_length); UPDATE_RET_N_PUSH_Y_STRING(local_size, global_size, m_str, y_string_b_uf, buf)
key[tmpjs->type.object.key_length]='\0';
printf("\n%*c\"%s\" : ", 4*(tmpjs->type.object.depth),' ', key );
print_js_value(tmpjs->type.object.value);
if(tmpjs->type.object.next_object == NULL) global_size += sprint_js_value_to_y_string_list(m_str, tmpjs->type.object.value, sep, tab_depth );
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"); //printf("\n");
tmpjs = tmpjs->type.object.next_object; tmpjs = tmpjs->type.object.next_object;
} }
@@ -1014,20 +1051,54 @@ void __print_js_value(struct js_value *js){
struct js_value *tmpjs = js; struct js_value *tmpjs = js;
while(tmpjs){ while(tmpjs){
if(tmpjs->type.array.prev_element == NULL){ 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); 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){ void print_js_value(struct js_value *js){
if(js->code_type == jstype_number){ 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){ if(tmpjs->type.object.prev_object == NULL){
printf("\n%*c{ ",4*(tmpjs->type.object.depth),' '); printf("\n%*c{ ",4*(tmpjs->type.object.depth),' ');
} }
/*
char key[tmpjs->type.object.key_length + 1]; char key[tmpjs->type.object.key_length + 1];
strncpy(key, tmpjs->type.object.key, tmpjs->type.object.key_length); strncpy(key, tmpjs->type.object.key, tmpjs->type.object.key_length);
key[tmpjs->type.object.key_length]='\0'; 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); print_js_value(tmpjs->type.object.value);
if(tmpjs->type.object.next_object == NULL) 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){ struct js_value * value_of_(struct js_value * js){
if(js->code_type == jstype_object){ 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; js->type.object.iter->current = tmp;
return tmp; //tmp->type.object.value == value_of_(tmp); return tmp; //tmp->type.object.value == value_of_(tmp);
} }
tmp = next_(tmp); tmp = tmp->type.object.next_object;//next_(tmp);
} }
} }
return NULL; return NULL;
+3 -1
View File
@@ -7,6 +7,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "tools_t/tools_t.h" #include "tools_t/tools_t.h"
#include "y_socket_t/y_list_string.h"
#define ITERATOR__(type) \ #define ITERATOR__(type) \
struct 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 delete_key_js_value(char * key, struct js_value **js_org);
void print_js_value(struct js_value *js); 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); char * original_string_js_value(struct js_value *js);
+5 -4
View File
@@ -7,10 +7,11 @@ CC=gcc
ROOT_DIR=$(PWD) ROOT_DIR=$(PWD)
YTESTDIR=$(PWD)/../../ytest_t YTESTDIR=$(PWD)/../../ytest_t
LISTDIR=$(PWD)/../../list_t LISTDIR=$(PWD)/../../list_t
YSOCKETDIR=$(PWD)/../../y_socket_t
INCLUDE_LIST=$(LISTDIR)/src INCLUDE_LIST=$(LISTDIR)/src
INCLUDE_DIR=$(PWD)/../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 LDFLAGS=-L$(YTESTDIR) -lytest
#SRC_DIR=$(ROOT_DIR)/src #SRC_DIR=$(ROOT_DIR)/src
@@ -27,11 +28,11 @@ LISTSRC_O=$(LISTDIR)/src/list_t/list_t.o
JSONDIR=$(PWD)/.. JSONDIR=$(PWD)/..
JSONSRC=$(JSONDIR)/src/json_t/json_t.c JSONSRC=$(JSONDIR)/src/json_t/json_t.c
JSONSRC_O=$(JSONDIR)/src/json_t/json_t.o JSONSRC_O=$(JSONDIR)/src/json_t/json_t.o
YSTRINGLIST_O=$(YSOCKETDIR)/src/y_socket_t/y_list_string.o
TOPTARGETS := all clean TOPTARGETS := all clean
DEPS=$(LISTDIR) $(YTESTDIR) $(JSONDIR) DEPS=$(LISTDIR) $(YTESTDIR) $(JSONDIR) $(YSOCKETDIR)
$(TOPTARGETS): $(DEPS) $(TOPTARGETS): $(DEPS)
@@ -45,7 +46,7 @@ $(DEPS):
#TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c #TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c
#TOOLSRC_O=$(TOOLSRC:.c=.o) #TOOLSRC_O=$(TOOLSRC:.c=.o)
OBJ=$(SRC:.c=.o) $(JSONSRC_O) OBJ=$(SRC:.c=.o) $(JSONSRC_O) $(YSTRINGLIST_O)
#$(LISTSRC_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){ int main(int argc, char **argv){