diff --git a/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcuserdata/huyibing.xcuserdatad/xcdebugger/Expressions.xcexplist b/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcuserdata/huyibing.xcuserdatad/xcdebugger/Expressions.xcexplist index d8ca57a..1d0c1c5 100644 --- a/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcuserdata/huyibing.xcuserdatad/xcdebugger/Expressions.xcexplist +++ b/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcuserdata/huyibing.xcuserdatad/xcdebugger/Expressions.xcexplist @@ -3,7 +3,62 @@ version = "1.0"> + contextName = "init_node:list.h"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -22,6 +77,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -33,44 +127,22 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + diff --git a/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index e74a180..c3ec7ec 100644 --- a/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -154,7 +154,7 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "test.c" - timestampString = "554986718.90595" + timestampString = "555307651.589517" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "64" @@ -186,7 +186,7 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "id/id.c" - timestampString = "554986718.906109" + timestampString = "555307651.589684" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "19" @@ -202,12 +202,12 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "id/id.c" - timestampString = "554986718.906213" + timestampString = "555307651.589751" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "99" endingLineNumber = "99" - landmarkName = "simFitS_id" + landmarkName = "s_idToASCIIString" landmarkType = "9"> @@ -218,13 +218,13 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "id/id.c" - timestampString = "554986718.906307" + timestampString = "555307651.589806" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "102" endingLineNumber = "102" - landmarkName = "unknown" - landmarkType = "0"> + landmarkName = "s_idToASCIIString" + landmarkType = "9"> + + + + @@ -397,23 +413,7 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "id/id.c" - timestampString = "554986718.90746" - startingColumnNumber = "9223372036854775807" - endingColumnNumber = "9223372036854775807" - startingLineNumber = "184" - endingLineNumber = "184" - landmarkName = "s_idToASCIIString" - landmarkType = "9"> - - - - @@ -509,7 +509,7 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "id/id.c" - timestampString = "554986718.908048" + timestampString = "555307651.590819" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "217" @@ -518,5 +518,629 @@ landmarkType = "9"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/communicate/communicate.c b/communicate/communicate.c index 32dc9ac..73a86a5 100644 --- a/communicate/communicate.c +++ b/communicate/communicate.c @@ -1 +1,266 @@ #include "communicate.h" + +STD_BLOCKS *initStandardDBlocks(SID *p_sid, unsigned long long data_size){ + STD_BLOCKS *p_stdb = (STD_BLOCKS *)malloc(sizeof(STD_BLOCKS)); + p_stdb->sid = s_idToASCIIString(p_sid); + p_stdb->if_data = 0; + unsigned int blocks_num = (unsigned int)(data_size/sizeof(char)); + p_stdb->blocks_num = blocks_num; + p_stdb->buff = (char *)malloc(sizeof(char) * blocks_num); + return p_stdb; +} + +int dataForStandardDBlock(STD_BLOCKS *p_stdb,void *data){ + char *t_data = (char *)data; + /*unsigned int data_size = sizeof(data);*/ + + for(int i = 0; i < p_stdb->blocks_num; i++){ + p_stdb->buff[i] = t_data[i]; + } + p_stdb->if_data = 1; + return 0; +} + +STD_CTN *initStandardDConnection(SID *f_sid, SID *s_sid){ + STD_CTN *p_stdc = (STD_CTN *)malloc(sizeof(STD_CTN)); + p_stdc->f_sid = s_idToASCIIString(f_sid); + p_stdc->s_sid = s_idToASCIIString(s_sid); + return p_stdc; +} + +STD_DATA *initStandardData(unsigned int type){ + STD_DATA *p_std = (STD_DATA *)malloc(sizeof(STD_DATA)); + p_std->pd_blocklst = initList(); + p_std->pd_ctnlst = initList(); + p_std->lock = 0; + p_std->type = type; + p_std->s_id = getS_id(STANDARD_DATA, 2); + return p_std; +} + +int standardDataAddBlock(STD_DATA *p_std, SID *p_sid ,void *data, unsigned long long data_size){ + if (p_std->lock) return -1; + STD_BLOCKS *p_stdb = initStandardDBlocks(p_sid, data_size); + dataForStandardDBlock(p_stdb, data); + insertInTail(p_std->pd_blocklst, nodeWithPointer(p_stdb)); + return 0; +} + +int standardDataAddConnection(STD_DATA *p_std, SID *f_sid, SID *s_sid){ + if (p_std->lock) return -1; + STD_CTN *p_stdb = initStandardDConnection(f_sid, s_sid); + insertInTail(p_std->pd_ctnlst, nodeWithPointer(p_stdb)); + return 0; +} + +D_FILE *initDataFileForWrite(char *route){ + D_FILE *p_dfile = (D_FILE *)malloc(sizeof(D_FILE)); + p_dfile->fp = fopen(route, "wb"); + p_dfile->pf_head = (F_HEAD *)malloc(sizeof(F_HEAD)); + strcpy(p_dfile->pf_head->head_test,"ZESTDLIB_STDDFILE"); + p_dfile->pf_head->data_num = 0; + p_dfile->pf_stdlst = initList(); + return p_dfile; +} + +D_FILE *initDataFileForRead(char *route){ + D_FILE *p_dfile = (D_FILE *)malloc(sizeof(D_FILE)); + p_dfile->fp = fopen(route, "rb"); + p_dfile->pf_head = (F_HEAD *)malloc(sizeof(F_HEAD)); + p_dfile->pf_head->data_num = 0; + p_dfile->pf_stdlst = initList(); + return p_dfile; +} + +int dataFileAddStandardData(D_FILE *p_dfile, STD_DATA *p_std){ + insertInTail(p_dfile->pf_stdlst, nodeWithPointer(p_std)); + p_dfile->pf_head->data_num = p_dfile->pf_stdlst->length; + return 0; +} + +int dataFileWriteIn(D_FILE *p_dfile){ + fwrite(p_dfile->pf_head->head_test, sizeof(char), 18, p_dfile->fp); + fwrite(&p_dfile->pf_head->data_num, sizeof(unsigned long long), 1, p_dfile->fp); + fwrite("HEAD_END", sizeof(char), 9, p_dfile->fp); + List *er_list = initList(); + insertInTail(er_list, nodeWithPointer(p_dfile->fp)); + /*fwrite("STDINFO", sizeof(char), 8, p_dfile->fp); + listThrough(p_dfile->pf_stdlst, _doStandardDataInfoWrite, er_list);*/ + /*fwrite("STDLST", sizeof(char), 7, p_dfile->fp);*/ + listThrough(p_dfile->pf_stdlst, _doStandardDataWrite, er_list); + releaseList(er_list); + return 0; +} +List *_doStandardDataInfoWrite(unsigned int type, void *value, List *er_list){ + List *p_rtnlst = initList(); + FILE *fp = getByPointerForNode(findByIndexForNode(er_list, 0)); + STD_DATA *p_std = value; + insertInTail(p_rtnlst, nodeWithInt(0)); + char *string_sid = s_idToASCIIString(p_std->s_id); + fwrite(string_sid, sizeof(char), DEEPER_LEN, fp); + free(string_sid); + fwrite(&p_std->type, sizeof(unsigned long), 1, fp); + fwrite(&p_std->pd_ctnlst->length, sizeof(unsigned long long), 1, fp); + fwrite(&p_std->pd_blocklst->length, sizeof(unsigned long long), 1, fp); + return p_rtnlst; +} + +List *_doStandardDataWrite(unsigned int type, void *value, List *er_list){ + List *p_rtnlst = initList(); + FILE *fp = getByPointerForNode(findByIndexForNode(er_list, 0)); + insertInTail(p_rtnlst, nodeWithInt(0)); + STD_DATA *p_std = value; + List *erc_list = initList(); + insertInTail(erc_list, nodeWithPointer(fp)); + fwrite("STD", sizeof(char), 4, fp); + char *string_sid = s_idToASCIIString(p_std->s_id); + unsigned long sid_len = strlen(string_sid) + 1; + fwrite(&sid_len, sizeof(unsigned long), 1, fp); + char *sid_w = s_idToASCIIString(p_std->s_id); + fwrite(sid_w, sizeof(char), sid_len, fp); + fwrite(&p_std->type, sizeof(unsigned int), 1, fp); + fwrite(&p_std->pd_ctnlst->length, sizeof(unsigned long long), 1, fp); + fwrite(&p_std->pd_blocklst->length, sizeof(unsigned long long), 1, fp); + listThrough(p_std->pd_ctnlst, _doStandardDConnectionWrite, erc_list); + listThrough(p_std->pd_blocklst, _doStandardDBlockWrite, erc_list); + releaseList(erc_list); + free(sid_w); + free(string_sid); + return p_rtnlst; +} + +List *_doStandardDConnectionWrite(unsigned int type, void *value, List *er_list){ + List *p_rtnlst = initList(); + insertInTail(p_rtnlst, nodeWithInt(0)); + FILE *fp = getByPointerForNode(findByIndexForNode(er_list, 0)); + STD_CTN *p_stdc = value; + unsigned long fsid_len = strlen(p_stdc->f_sid) + 1, ssid_len = strlen(p_stdc->s_sid)+1; + fwrite(&fsid_len, sizeof(unsigned long), 1, fp); + fwrite(p_stdc->f_sid, sizeof(char), fsid_len, fp); + fwrite(&ssid_len, sizeof(unsigned long), 1, fp); + fwrite(p_stdc->s_sid, sizeof(char), ssid_len, fp); + return p_rtnlst; +} + +List *_doStandardDBlockWrite(unsigned int type, void *value, List *er_list){ + List *p_rtnlst = initList(); + insertInTail(p_rtnlst, nodeWithInt(0)); + STD_BLOCKS *p_stdb = value; + FILE *fp = getByPointerForNode(findByIndexForNode(er_list, 0)); + unsigned long sid_len = strlen(p_stdb->sid)+1, blocks_num = p_stdb->blocks_num; + fwrite(&sid_len, sizeof(unsigned long), 1, fp); + fwrite(p_stdb->sid, sizeof(char), sid_len, fp); + fwrite(&blocks_num, sizeof(unsigned long), 1, fp); + fwrite(p_stdb->buff, sizeof(char), p_stdb->blocks_num, fp); + return p_rtnlst; +} + +STD_DATA *listToSTD(List *p_list){ + STD_DATA *p_std = initStandardData(LIST); + Node *p_node = p_list->head; + while (p_node != NULL) { + unsigned long long data_size = 0; + if(p_node->type == INT) data_size = sizeof(int); + else if (p_node->type == DOUBLE) data_size = sizeof(double); + else if (p_node->type == STRING) data_size = strlen((char *)p_node->value) + 1; + else data_size = sizeof(void *); + standardDataAddBlock(p_std, p_node->s_id, p_node->value, data_size); + p_node = p_node->next; + } + return p_std; +} + +int dataFileReadOut(D_FILE *p_dfile){ + char test_info[FILE_TSET_LEN],head_test_info[HEAD_TEST_LEN]; + fread(test_info, sizeof(char), FILE_TSET_LEN, p_dfile->fp); + strcpy(p_dfile->pf_head->head_test, test_info); + if(!strcmp(test_info, "ZESTDLIB_STDDFILE")){ + unsigned long long std_num = 0; + fread(&std_num, sizeof(unsigned long long), 1, p_dfile->fp); + p_dfile->pf_head->data_num = std_num; + fread(head_test_info, sizeof(char), 9, p_dfile->fp); + if (!strcmp(head_test_info, "HEAD_END")) { + for(int i = 0; i < std_num; i++){ + char std_test_info[4]; + fread(std_test_info, sizeof(char), 4, p_dfile->fp); + if(!strcmp(std_test_info, "STD")){ + unsigned long long sid_len = 0, ctn_num = 0, blk_num = 0; + unsigned int type = 0; + fread(&sid_len, sizeof(unsigned long), 1, p_dfile->fp); + char *string_sid = (char *)malloc(sizeof(char) * sid_len); + fread(string_sid, sizeof(char), sid_len, p_dfile->fp); + fread(&type, sizeof(unsigned int), 1, p_dfile->fp); + STD_DATA *p_std = initStandardData(type); + freeS_id(p_std->s_id); + p_std->s_id = asciiStringToS_id(string_sid); + dataFileAddStandardData(p_dfile, p_std); + free(string_sid); + fread(&ctn_num, sizeof(unsigned long long), 1, p_dfile->fp); + fread(&blk_num, sizeof(unsigned long long), 1, p_dfile->fp); + for(int j = 0; j < ctn_num; j++){ + fread(&sid_len, sizeof(unsigned long), 1, p_dfile->fp); + char *fstring_sid = (char *)malloc(sizeof(char) * sid_len); + fread(fstring_sid, sizeof(char), sid_len, p_dfile->fp); + fread(&sid_len, sizeof(unsigned long), 1, p_dfile->fp); + char *sstring_sid = (char *)malloc(sizeof(char) * sid_len); + fread(sstring_sid, sizeof(char), sid_len, p_dfile->fp); + SID *fs_id = asciiStringToS_id(fstring_sid), *ss_id = asciiStringToS_id(sstring_sid); + standardDataAddConnection(p_std, fs_id,ss_id); + freeS_id(fs_id); + freeS_id(ss_id); + free(fstring_sid); + free(sstring_sid); + } + for(int k = 0; k < blk_num; k++){ + fread(&sid_len, sizeof(unsigned long), 1, p_dfile->fp); + char *string_sid = (char *)malloc(sizeof(char) * sid_len); + fread(string_sid, sizeof(char), sid_len, p_dfile->fp); + unsigned long blk_len = 0; + fread(&blk_len, sizeof(unsigned long), 1, p_dfile->fp); + char *content = (char *)malloc(sizeof(char) * blk_len); + fread(content, sizeof(char), blk_len, p_dfile->fp); + SID *s_id = asciiStringToS_id(string_sid); + standardDataAddBlock(p_std, s_id, content, blk_len); + freeS_id(s_id); + free(string_sid); + free(content); + } + return 0; + } + else break; + } + } + } + showError(pushError(DATA_FILE, STANDARD, initInfo("dataFileReadOut()", "Datafile not complete."))); + return -1; +} + +int releaseSTDConnection(STD_CTN *p_stdc){ + free(p_stdc->f_sid); + free(p_stdc->s_sid); + free(p_stdc); + return 0; +} + +int releaseSTDBlocks(STD_BLOCKS *p_stdb){ + free(p_stdb->sid); + free(p_stdb->buff); + free(p_stdb); + return 0; +} + +int releaseStandardData(STD_DATA *p_std){ + freeS_id(p_std->s_id); + releaseListForCustom(p_std->pd_blocklst, (int (*)(void *))releaseSTDBlocks); + releaseListForCustom(p_std->pd_ctnlst, (int (*)(void *))releaseSTDConnection); + free(p_std); + return 0; +} + +int releaseDFile(D_FILE *p_dfile){ + releaseListForCustom(p_dfile->pf_stdlst, (int (*)(void *))releaseStandardData); + fclose(p_dfile->fp); + free(p_dfile->pf_head); + free(p_dfile); + return 0; +} diff --git a/communicate/communicate.h b/communicate/communicate.h index 4798a3f..bb15f73 100644 --- a/communicate/communicate.h +++ b/communicate/communicate.h @@ -1,6 +1,77 @@ #ifndef communicate_h #define communicate_h +#include "../type/type.h" +#include "../list/list_expand.h" +typedef struct message{ + SID *p_sid; + char titile[16]; + char content[0]; +}MSG; + +typedef struct file_head{ + char head_test[18]; + unsigned long long data_num; +}F_HEAD; + +typedef struct data_file{ + FILE *fp; + F_HEAD *pf_head; + List *pf_stdlst; +}D_FILE; + +typedef struct standard_data_blocks{ + unsigned int type; + char *sid; + _Bool if_data; + unsigned int blocks_num; + char *buff; +}STD_BLOCKS; + +typedef struct standard_data_connection{ + char *f_sid; + char *s_sid; +}STD_CTN; + +typedef struct standard_data_head{ + unsigned long long data_blk_num; + unsigned long long data_ctn_num; +}STD_HEAD; + +typedef struct standard_data{ + SID *s_id; + unsigned int type; + _Bool lock; + List *pd_blocklst; + List *pd_ctnlst; +}STD_DATA; + +D_FILE *initDataFileForWrite(char *route); +D_FILE *initDataFileForRead(char *route); +STD_BLOCKS *initStandardDBlocks(SID *p_sid, unsigned long long data_size); +STD_CTN *initStandardDConnection(SID *f_sid, SID *s_sid); +STD_DATA *initStandardData(unsigned int type); + +int dataForStandardDBlock(STD_BLOCKS *p_stdb,void *data); +int dataFileAddStandardData(D_FILE *p_dfile, STD_DATA *p_std); +int standardDataAddBlock(STD_DATA *p_std, SID *p_sid ,void *data, unsigned long long data_size); +int standardDataAddConnection(STD_DATA *p_std, SID *f_sid, SID *s_sid); +int dataFileWriteIn(D_FILE *p_dfile); +int dataFileReadOut(D_FILE *p_dfile); + +int releaseSTDBlocks(STD_BLOCKS *p_stdb); +int releaseStandardData(STD_DATA *p_std); +int releaseSTDConnection(STD_CTN *p_stdc); +int releaseDFile(D_FILE *p_file); + +List *_doStandardDataInfoWrite(unsigned int type, void *value, List *er_list); +List *_doStandardDataWrite(unsigned int type, void *value, List *er_list); +List *_doStandardDConnectionWrite(unsigned int type, void *value, List *er_list); +List *_doStandardDBlockWrite(unsigned int type, void *value, List *er_list); + +STD_DATA *listToSTD(List *); +STD_DATA *stackToSTD(Stack *); +STD_DATA *treeToSTD(Tree *); #endif /* communicate_h */ diff --git a/list/list.c b/list/list.c index b4c3abd..2c86746 100644 --- a/list/list.c +++ b/list/list.c @@ -9,12 +9,12 @@ int safeModeForNode(int ifon) { if (node_list == NULL && list_list == NULL) { node_list = (List *)malloc(sizeof(List)); if(node_list == NULL){ - showError(pushError(LIST, HIGH, initInfo("safeModeForNode()", "Error in get the memory of node_list."))); + showError(pushError(LIST, HIGH, initInfo("safeModeForNode()", "Error in getting the memory of node_list."))); return -1; } list_list = (List *)malloc(sizeof(List)); if(list_list == NULL){ - showError(pushError(LIST, HIGH, initInfo("safeModeForNode()", "Error in get the memory of list_list."))); + showError(pushError(LIST, HIGH, initInfo("safeModeForNode()", "Error in getting the memory of list_list."))); return -1; } list_list->head = NULL; @@ -89,7 +89,7 @@ int releaseAllForNode(void) { Node *initNode(void) { Node *p_node = (Node *)malloc(sizeof(Node)); if(p_node == NULL){ - showError(pushError(LIST_NODE, STANDARD, initInfo("initNode()", "Error in get the memory of node."))); + showError(pushError(LIST_NODE, STANDARD, initInfo("initNode()", "Error in getting the memory of node."))); return NULL; } Node *prec_node = NULL; @@ -113,7 +113,7 @@ List *initList(void) { Node *p_node = NULL; List *p_list = (List *)malloc(sizeof(List)); if(p_list == NULL){ - showError(pushError(LIST_NODE, STANDARD, initInfo("initList()", "Error in get the memory of list."))); + showError(pushError(LIST_NODE, STANDARD, initInfo("initList()", "Error in getting the memory of list."))); return NULL; } p_list->s_id = getS_id(LIST, 1); @@ -131,10 +131,10 @@ List *initList(void) { return p_list; } -int initMallocValueForNode(Node *p_node, unsigned int type, void *p_value) { +int initMallocValueForNode(Node *p_node, unsigned int type, const void *p_value) { p_node->if_malloc = 1; p_node->type = type; - p_node->value = p_value; + p_node->value = (void *)p_value; return 0; } @@ -479,7 +479,7 @@ int releaseNodeForCustom(Node *p_node, int (*func)(void *)){ removeByNode(node_list, p_node); } if (p_node->if_malloc == 1) { - if (!func(p_node->value)) + if (func(p_node->value)) showError(pushError(LIST_NODE, STANDARD, initInfo("releaseNodeForCustom()", "Error in using custom freeing value function."))); p_node->value = NULL; } diff --git a/list/list.h b/list/list.h index 349b525..9124f4b 100644 --- a/list/list.h +++ b/list/list.h @@ -11,7 +11,7 @@ int releaseAllForNode(void); List *initList(void); Node *initNode(void); -int initMallocValueForNode(Node *,unsigned int,void *); +int initMallocValueForNode(Node *,unsigned int,const void *); int insertInHead(List *p_list, Node *p_node); int insertInTail(List *p_list, Node *p_node); diff --git a/list/list_expand.c b/list/list_expand.c index e0cf001..411163d 100644 --- a/list/list_expand.c +++ b/list/list_expand.c @@ -2,16 +2,37 @@ Node *nodeWithInt(int m_int) { Node *p_node; - int *p_int = (int *)malloc(sizeof(int)); + int *p_int = (int *)malloc(sizeof(int)); + if(p_int == NULL){ + showError(pushError(INT, STANDARD, initInfo("nodeWithInt()", "Error in getting the memory of int."))); + return NULL; + } *p_int = m_int; p_node = initNode(); initMallocValueForNode(p_node, INT, (void *)p_int); return p_node; +} + +Node *nodeWithUInt(unsigned int m_uint){ + Node *p_node; + unsigned int *pu_int = (unsigned int *)malloc(sizeof(unsigned int)); + if(pu_int == NULL){ + showError(pushError(INT, STANDARD, initInfo("nodeWithUInt()", "Error in getting the memory of int."))); + return NULL; + } + *pu_int = m_uint; + p_node = initNode(); + initMallocValueForNode(p_node, INT, (void *)pu_int); + return p_node; } Node *nodeWithDouble(double m_double) { Node *p_node; - double *p_double = (double *)malloc(sizeof(double)); + double *p_double = (double *)malloc(sizeof(double)); + if(p_double == NULL){ + showError(pushError(DOUBLE, STANDARD, initInfo("nodeWithDouble()", "Error in getting the memory of double."))); + return NULL; + } *p_double = m_double; p_node = initNode(); initMallocValueForNode(p_node, DOUBLE, (void *)p_double); @@ -20,14 +41,18 @@ Node *nodeWithDouble(double m_double) { Node *nodeWithString(const char *m_string) { Node *p_node; - char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1)); + char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1)); + if(p_string == NULL){ + showError(pushError(STRING, STANDARD, initInfo("initWithString()", "Error in getting the memory of string."))); + return NULL; + } strcpy(p_string, m_string); p_node = initNode(); initMallocValueForNode(p_node, STRING, (void *)p_string); return p_node; } -Node *nodeWithPointer(void *m_pointer) { +Node *nodeWithPointer(const void *m_pointer) { Node *p_node = initNode(); initMallocValueForNode(p_node, POINTER, m_pointer); return p_node; @@ -50,28 +75,38 @@ Node *findByIndexForNode(List *p_list, unsigned long long m_index) { return p_node; } -int listThrough(List *p_list, int(*p_func)(unsigned int, void *)) { - Node *p_node = p_list->head; +List *listThrough(List *p_list, List *(*p_func)(unsigned int, void *, List *), List *expand_resources) { + Node *p_node = p_list->head; + List *m_rtnlst = NULL; while (p_node != NULL) { if (p_node->if_malloc == 1) { - int m_return = (*p_func)(p_node->type, p_node->value); - if (m_return == -1) break; - else if (m_return == 1) { - p_node = p_node->last; + List *m_rtnlst = (*p_func)(p_node->type, p_node->value, expand_resources); + int status = getByIntForNode(findByIndexForNode(m_rtnlst, 0)); + if (status == -1) break; + else if (status == 1) { + p_node = p_node->last; + releaseList(m_rtnlst); + m_rtnlst = NULL; continue; } else { - + releaseList(m_rtnlst); + m_rtnlst = NULL; } } p_node = p_node->next; } - return 0; + return m_rtnlst; } int getByIntForNode(Node *p_node) { if (p_node->type == INT) return *(int *)(p_node->value); else return -1; +} + +unsigned int getByUIntForNode(Node *p_node){ + if (p_node->type == UINT) return *(unsigned int *)(p_node->value); + else return -1; } char *getByStringForNode(Node *p_node) { @@ -249,7 +284,11 @@ int addValueForComplex(Node * p_node, int type, void *value) { int addIntForComplex(Node *p_node, int temp) { if (p_node->type == LIST) { - int *p_temp = (int *)malloc(sizeof(int)); + int *p_temp = (int *)malloc(sizeof(int)); + if(p_temp == NULL){ + showError(pushError(INT, STANDARD, initInfo("addIntForComplex()", "Error in getting the memory of int."))); + return -1; + } *p_temp = temp; addValueForComplex(p_node, INT, p_temp); return 0; @@ -259,7 +298,11 @@ int addIntForComplex(Node *p_node, int temp) { int addDoubleForComplex(Node *p_node, double temp) { if (p_node->type == LIST) { - double *p_temp = (double *)malloc(sizeof(double)); + double *p_temp = (double *)malloc(sizeof(double)); + if(p_temp == NULL){ + showError(pushError(DOUBLE, STANDARD, initInfo("addDoubleForComplex()", "Error in getting the memory of double."))); + return -1; + } *p_temp = temp; addValueForComplex(p_node, DOUBLE, p_temp); return 0; @@ -269,7 +312,11 @@ int addDoubleForComplex(Node *p_node, double temp) { int addStringForComplex(Node *p_node, char *temp) { if (p_node->type == LIST) { - char *p_temp = (char *)malloc(sizeof(strlen(temp) + 1)); + char *p_temp = (char *)malloc(sizeof(strlen(temp) + 1)); + if(p_temp == NULL){ + showError(pushError(STRING, STANDARD, initInfo("addStringForComplex()", "Error in getting the memory of string."))); + return -1; + } strcpy(p_temp, temp); addValueForComplex(p_node, STRING, p_temp); return 0; @@ -286,7 +333,11 @@ int addPointerForComplex(Node *p_node, void *temp) { } List *m_findByInt(List* p_list, int temp) { - int *p_temp = (int *)malloc(sizeof(int)); + int *p_temp = (int *)malloc(sizeof(int)); + if(p_temp == NULL){ + showError(pushError(INT, STANDARD, initInfo("m_findByInt()", "Error in getting the memory of int."))); + return NULL; + } List *t_list; *p_temp = temp; t_list = mply_findByValue(p_list, INT, (void *)p_temp); @@ -296,7 +347,11 @@ List *m_findByInt(List* p_list, int temp) { List *m_findByDouble(List* p_list, double temp) { List *t_list; - double *p_temp = (double *)malloc(sizeof(double)); + double *p_temp = (double *)malloc(sizeof(double)); + if(p_temp == NULL){ + showError(pushError(DOUBLE, STANDARD, initInfo("m_findByDouble()", "Error in getting the memory of double."))); + return NULL; + } *p_temp = temp; t_list = mply_findByValue(p_list, DOUBLE, (void *)p_temp); free(p_temp); @@ -305,7 +360,11 @@ List *m_findByDouble(List* p_list, double temp) { List *m_findByString(List* p_list, char *temp) { List *t_list; - char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); + char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); + if(p_temp == NULL){ + showError(pushError(STRING, STANDARD, initInfo("m_findByString()", "Error in getting the memory of string."))); + return NULL; + } strcpy(p_temp, temp); t_list = mply_findByValue(p_list, STRING, (void *)p_temp); free(p_temp); @@ -329,19 +388,31 @@ unsigned long long getIndexByNode(List *p_list, Node *p_node) { } List *m_findByIntForNode(List* p_list, int temp) { - int *p_temp = (int *)malloc(sizeof(int)); + int *p_temp = (int *)malloc(sizeof(int)); + if(p_temp == NULL){ + showError(pushError(INT, STANDARD, initInfo("m_findByIntForNode()", "Error in getting the memory of int."))); + return NULL; + } *p_temp = temp; return mply_findByValue(p_list, INT, (void *)p_temp); } List *m_findByDoubleForNode(List* p_list, double temp) { - double *p_temp = (double *)malloc(sizeof(double)); + double *p_temp = (double *)malloc(sizeof(double)); + if(p_temp == NULL){ + showError(pushError(DOUBLE, STANDARD, initInfo("m_findByDoubleForNode()", "Error in getting the memory of double."))); + return NULL; + } *p_temp = temp; return mply_findByValue(p_list, DOUBLE, (void *)p_temp); } List *m_findByStringForNode(List* p_list, char *temp) { - char *p_temp = (char *)malloc(sizeof(char) * (strlen(temp) + 1)); + char *p_temp = (char *)malloc(sizeof(char) * (strlen(temp) + 1)); + if(p_temp == NULL){ + showError(pushError(STRING, STANDARD, initInfo("m_findByStringForNode()", "Error in getting the memory of string."))); + return NULL; + } strcpy(p_temp, temp); return mply_findByValue(p_list, STRING, (void *)p_temp); } @@ -359,4 +430,46 @@ unsigned long long calListMemory(List * p_list){ p_node = p_node->next; } return list_size + nodes_size; +} + +int updateValueWithInt(Node *p_node,int value){ + int *p_value = (int *)malloc(sizeof(int)); + if(p_value == NULL){ + showError(pushError(INT, STANDARD, initInfo("updateValueWithInt()", "Error in getting the memory of int."))); + return -1; + } + *p_value = value; + free(p_node->value); + p_node->value = p_value; + return 0; } + +int updateValueWithDouble(Node *p_node, double value){ + double *p_value = (double *)malloc(sizeof(double)); + if(p_value == NULL){ + showError(pushError(DOUBLE, STANDARD, initInfo("updateValueWithDouble()", "Error in getting the memory of double."))); + return -1; + } + *p_value = value; + free(p_node->value); + p_node->value = p_value; + return 0; +} + +int updateValueWithString(Node *p_node, char *string){ + char *p_value = (char *)malloc(sizeof(strlen(string)) + 1); + if(p_value == NULL){ + showError(pushError(STRING, STANDARD, initInfo("updateValueWithString()", "Error in getting the memory of string."))); + return -1; + } + strcpy(p_value, string); + free(p_node->value); + p_node->value = p_value; + return 0; +} + +int updateValueWithPointer(Node *p_node, void *pointer){ + free(p_node->value); + p_node->value = pointer; + return 0; +} diff --git a/list/list_expand.h b/list/list_expand.h index 1d8d97e..dff20b7 100644 --- a/list/list_expand.h +++ b/list/list_expand.h @@ -3,17 +3,24 @@ #include "list.h" -Node *nodeWithInt(int); +Node *nodeWithInt(int); +Node *nodeWithUInt(unsigned int); Node *nodeWithDouble(double); Node *nodeWithString(const char *); -Node *nodeWithPointer(void *); +Node *nodeWithPointer(const void *); Node *nodeWithComplex(void); int addValueForComplex(Node *, int type, void *value); int addIntForComplex(Node *, int); int addDoubleForComplex(Node *, double); int addStringForComplex(Node *, char *); -int addPointerForComplex(Node *, void *); +int addPointerForComplex(Node *, void *); + +int updateValueWithInt(Node *,int); +int updateValueWithDouble(Node *,double); +int updateValueWithString(Node *,char *); +int updateValueWithPointer(Node *,void *); + Node *findByIndexForNode(List *, unsigned long long); Node *findByIntForNode(List *, int); @@ -31,12 +38,13 @@ void printNodeInfo(Node *p_node,int priority); void printList(List *); void printNode(Node *p_node); -int getByIntForNode(Node *); +int getByIntForNode(Node *); +unsigned int getByUIntForNode(Node *); double getByDoubleForNode(Node *); char *getByStringForNode(Node *); void *getByPointerForNode(Node *); unsigned long long getIndexByNode(List *p_list,Node *p_node); -int listThrough(List *p_list, int (*p_func)(unsigned int type, void *value)); +List *listThrough(List *p_list, List *(*p_func)(unsigned int type, void *value, List *), List *expand_resources); unsigned long long calListMemory(List *); #endif diff --git a/stack/stack.c b/stack/stack.c index acaee3f..f4b8ae1 100644 --- a/stack/stack.c +++ b/stack/stack.c @@ -3,7 +3,7 @@ Stack *initStack(void) { Stack *p_stack = (Stack *)malloc(sizeof(Stack)); if(p_stack == NULL){ - showError(pushError(STACK, STANDARD, initInfo("initStack()", "Error in get the memory of stack."))); + showError(pushError(STACK, STANDARD, initInfo("initStack()", "Error in getting the memory of stack."))); return NULL; } p_stack->s_id = getS_id(STACK, 1); @@ -15,7 +15,7 @@ Stack *initStack(void) { SNode *initSNode(void) { SNode *p_snode = (SNode *)malloc(sizeof(SNode)); if(p_snode == NULL){ - showError(pushError(STACK_NODE, STANDARD, initInfo("initSNode()", "Error in get the memory of snode."))); + showError(pushError(STACK_NODE, STANDARD, initInfo("initSNode()", "Error in getting the memory of snode."))); return NULL; } p_snode->s_id = getS_id(STACK_NODE, 2); diff --git a/stack/stack.h b/stack/stack.h index d20f752..ef36892 100644 --- a/stack/stack.h +++ b/stack/stack.h @@ -1,23 +1,8 @@ #ifndef STACK_H #define STACK_H +#include "../type/type.h" #include "../list/list_expand.h" -typedef struct stack_node{ - SID *s_id; - _Bool if_malloc; - _Bool if_sid; - unsigned int type; - void *value; - struct stack_node *next; -} SNode; - -typedef struct stack{ - SID *s_id; - unsigned long long length; - SNode *top; - _Bool if_sid; -} Stack; - Stack *initStack(void); SNode *initSNode(void); int initMallocValueForSNode(SNode *p_snode, unsigned int type, void *value); diff --git a/stack/stack_expand.c b/stack/stack_expand.c index f48ad09..b0b3aa1 100644 --- a/stack/stack_expand.c +++ b/stack/stack_expand.c @@ -2,7 +2,11 @@ SNode *snodeWithInt(int temp) { SNode *p_snode = initSNode(); - int *p_temp = (int *)malloc(sizeof(int)); + int *p_temp = (int *)malloc(sizeof(int)); + if(p_temp == NULL){ + showError(pushError(INT, STANDARD, initInfo("snodeWithInt()", "Error in getting the memory of int."))); + return NULL; + } *p_temp = temp; initMallocValueForSNode(p_snode, INT, p_temp); return p_snode; @@ -10,7 +14,11 @@ SNode *snodeWithInt(int temp) { SNode *snodeWithDouble(double temp) { SNode *p_snode = initSNode(); - double *p_temp = (double *)malloc(sizeof(double)); + double *p_temp = (double *)malloc(sizeof(double)); + if(p_temp == NULL){ + showError(pushError(DOUBLE, STANDARD, initInfo("snodeWithDouble()", "Error in getting the memory of double."))); + return NULL; + } *p_temp = temp; initMallocValueForSNode(p_snode, DOUBLE, p_temp); return p_snode; @@ -18,7 +26,11 @@ SNode *snodeWithDouble(double temp) { SNode *snodeWithString(char *temp) { SNode *p_snode = initSNode(); - char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); + char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); + if(p_temp == NULL){ + showError(pushError(STRING, STANDARD, initInfo("snodeWithString()", "Error in getting the memory of string."))); + return NULL; + } strcpy(p_temp, temp); initMallocValueForSNode(p_snode, STRING, p_temp); return p_snode; diff --git a/test.c b/test.c index bd9d494..7de1db3 100644 --- a/test.c +++ b/test.c @@ -81,8 +81,25 @@ int stack(void) { } -int main(int argc, char **argv) { - //tree(); - init_rand(); +int main(int argc, char **argv) { + init_rand(); + for (int j = 0; j < 65535; j++) { + List *t_list = initList(); + for (int i = 0; i < 12; i++) { + insertInHead(t_list, nodeWithInt(i)); + } + STD_DATA *p_std = listToSTD(t_list); + D_FILE *p_dfilew = initDataFileForWrite("data.df"); + dataFileAddStandardData(p_dfilew, p_std); + dataFileWriteIn(p_dfilew); + releaseDFile(p_dfilew); + D_FILE *p_dfiler = initDataFileForRead("data.df"); + dataFileReadOut(p_dfiler); + releaseDFile(p_dfiler); + releaseList(t_list); + + printf("%d\n",j); + usleep(2000); + } return 0; } diff --git a/test.h b/test.h index dfa8642..6dcdf20 100644 --- a/test.h +++ b/test.h @@ -4,7 +4,8 @@ #include "type/type.h" #include "list/list_expand.h" #include "stack/stack_expand.h" -#include "tree/tree_expand.h" +#include "tree/tree_expand.h" +#include "communicate/communicate.h" int stack(void); int list(void); diff --git a/tree/tree.c b/tree/tree.c index 276304d..af965d8 100644 --- a/tree/tree.c +++ b/tree/tree.c @@ -1,7 +1,5 @@ #include"tree.h" -static SID *target_sid = NULL; -static TNode *target_value_sid = NULL; static int if_safeModeForTree = 0; int safeModeForTree(int ifon) { @@ -39,7 +37,7 @@ TNode *initTNode(void) { Node *s_node; TNode *p_tnode = (TNode *)malloc(sizeof(TNode)); if(p_tnode == NULL){ - showError(pushError(TREE_NODE, STANDARD, initInfo("initTNode()", "Error in get the memory of tnode."))); + showError(pushError(TREE_NODE, STANDARD, initInfo("initTNode()", "Error in getting the memory of tnode."))); return NULL; } p_tnode->s_id = getS_id(TREE_NODE, 2); @@ -73,7 +71,7 @@ Tree *initTree(void) { Node *s_node; Tree *p_tree = (Tree *)malloc(sizeof(Tree)); if(p_tree == NULL){ - showError(pushError(TREE, STANDARD, initInfo("initTree()", "Error in get the memory of tree."))); + showError(pushError(TREE, STANDARD, initInfo("initTree()", "Error in getting the memory of tree."))); return NULL; } p_tree->s_id = getS_id(TREE, 1); @@ -163,79 +161,100 @@ int removeChildInRight(TNode *p_tnode) { TNode *getChildById(TNode *p_tnode, const SID *s_id) { List *p_home = p_tnode->home; - target_sid = NULL; - target_value_sid = NULL; - listThrough(p_home, _dogetChildById); - if (target_value_sid != NULL) { - return target_value_sid; + target_value_sid = NULL; + List *er_list = initList(); + insertInTail(er_list, nodeWithPointer(s_id)); + List *rtnc_list = listThrough(p_home, _dogetChildById, er_list); + free(er_list); + Node *p_node= findByIndexForNode(rtnc_list, 1); + if (p_node != NULL) { + TNode *p_tnode = getByPointerForNode(p_node); + free(rtnc_list); + return p_tnode; } return NULL; } -int _dogetChildById(unsigned int type, void *value) { +List *_dogetChildById(unsigned int type, void *value, List *er_list) { + SID *target_sid = getByPointerForNode(findByIndexForNode(er_list, 0)); + List *rtn_list = initList(); if (type == POINTER) { TNode *p_tode = (TNode *)value; if (simFitS_id(p_tode->s_id, target_sid)) { - target_value_sid = p_tode; - return -1; + target_value_sid = p_tode; + insertInTail(rtn_list, nodeWithInt(-1)); + insertInTail(rtn_list, nodeWithPointer(target_value_sid)); + return rtn_list; } - } - return 0; + } + insertInTail(rtn_list, nodeWithInt(0)); + return rtn_list; } -static int target_type = VOID; -static void *target_value = NULL; -static TNode *target_value_value = NULL; - TNode *getChildByValue(TNode *p_tnode, unsigned int type, void *value) { List *p_home = p_tnode->home; target_value = value; target_type = type; - target_value_value = NULL; - listThrough(p_home, _dogetChildByValue); - if (target_value_value != NULL) { - return target_value_value; + target_value_value = NULL; + List *er_list = initList(); + insertInTail(er_list, nodeWithUInt(type)); + insertInTail(er_list, nodeWithPointer(value)); + List *rtnc_list = listThrough(p_home, _dogetChildByValue,er_list); + free(er_list); + Node *p_node = NULL; + if ((p_node = findByIndexForNode(rtnc_list, 1)) != NULL) { + TNode *p_tnode = getByPointerForNode(p_node); + free(rtnc_list); + return p_tnode; } return NULL; } -int _dogetChildByValue(unsigned int type, void *value) { +List *_dogetChildByValue(unsigned int type, void *value, List *er_list) { + List *rtn_list = initList(); + unsigned int target_type = getByUIntForNode(findByIndexForNode(rtn_list, 0)); + void *target_value = getByPointerForNode(findByIndexForNode(rtn_list, 1)); if (type == target_type) { TNode *p_tode = (TNode *)value; if (target_type == INT) { if (*(int *)p_tode->value == *(int *)target_value) - { - target_value_value = p_tode; - return -1; + { + insertInTail(rtn_list, nodeWithInt(-1)); + insertInTail(rtn_list, nodeWithPointer(p_tode)); + return rtn_list; } } else if (target_type == DOUBLE) { if (*(double *)p_tode->value == *(double *)target_value) - { - target_value_value = p_tode; - return -1; + { + insertInTail(rtn_list, nodeWithInt(-1)); + insertInTail(rtn_list, nodeWithPointer(p_tode)); + return rtn_list; } } else if (target_type == STRING) { if (!strcmp((char *)p_tode->value, (char *)target_value)) - { - target_value_value = p_tode; - return -1; + { + insertInTail(rtn_list, nodeWithInt(-1)); + insertInTail(rtn_list, nodeWithPointer(p_tode)); + return rtn_list; } } else if (target_type == POINTER) { if (p_tode->value == target_value) - { - target_value_value = p_tode; - return -1; + { + insertInTail(rtn_list, nodeWithInt(-1)); + insertInTail(rtn_list, nodeWithPointer(p_tode)); + return rtn_list; } } - } - return 0; + } + insertInTail(rtn_list, nodeWithInt(0)); + return rtn_list; } int removeChildById(TNode *p_tnode, const SID *s_id) { diff --git a/tree/tree.h b/tree/tree.h index 2504c6f..d4b8a3d 100644 --- a/tree/tree.h +++ b/tree/tree.h @@ -28,8 +28,8 @@ int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height) int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); -int _dogetChildById(unsigned int type, void *value); -int _dogetChildByValue(unsigned int type, void *value); +List *_dogetChildById(unsigned int type, void *value, List *er_list); +List *_dogetChildByValue(unsigned int type, void *value, List *er_list); int _doreleaseTree(TNode *p_tnode, unsigned long long height); int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)); int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)); diff --git a/tree/tree_expand.c b/tree/tree_expand.c index e188663..5d9992d 100644 --- a/tree/tree_expand.c +++ b/tree/tree_expand.c @@ -2,7 +2,11 @@ TNode *tnodeWithInt(int temp) { TNode *p_tnode = initTNode(); - int *p_temp = (int *)malloc(sizeof(int)); + int *p_temp = (int *)malloc(sizeof(int)); + if(p_temp == NULL){ + showError(pushError(INT, STANDARD, initInfo("tnodeWithInt()", "Error in getting the memory of int."))); + return NULL; + } *p_temp = temp; initMallocValueForTNode(p_tnode, INT, p_temp); return p_tnode; @@ -10,14 +14,22 @@ TNode *tnodeWithInt(int temp) { TNode *tnodeWithDouble(double temp) { TNode *p_tnode = initTNode(); - double *p_temp = (double *)malloc(sizeof(double)); + double *p_temp = (double *)malloc(sizeof(double)); + if(p_temp == NULL){ + showError(pushError(DOUBLE, STANDARD, initInfo("tnodeWithDouble()", "Error in getting the memory of double."))); + return NULL; + } *p_temp = temp; initMallocValueForTNode(p_tnode, DOUBLE, p_temp); return p_tnode; } TNode *tnodeWithString(char *temp) { TNode *p_tnode = initTNode(); - char *p_temp = (char *)malloc(sizeof(temp)); + char *p_temp = (char *)malloc(sizeof(temp)); + if(p_temp == NULL){ + showError(pushError(STRING, STANDARD, initInfo("tnodeWithString()", "Error in getting the memory of string."))); + return NULL; + } strcpy(p_temp, temp); initMallocValueForTNode(p_tnode, STRING, p_temp); return p_tnode; diff --git a/type/type.h b/type/type.h index 47f5cca..b93035b 100644 --- a/type/type.h +++ b/type/type.h @@ -5,6 +5,7 @@ #include #include #include +#include #define VOID 0 #define INT 1 @@ -18,6 +19,9 @@ #define TREE_NODE 9 #define STACK_NODE 10 #define T_SID 11 +#define UINT 12 +#define STANDARD_DATA 13 +#define DATA_FILE 14 #define DEEPC 1 #define DEEPB 2 @@ -27,6 +31,12 @@ #define DEEPB_LEN 8 #define DEEPA_LEN 32 #define DATA_BIT 5 +#define DEEP_LEN 25 +#define DEEPER_LEN 65 +#define DEEPEST_LEN 225 +#define FILE_TSET_LEN 18 +#define HEAD_TEST_LEN 9 + #define HIGH 0x3 #define STANDARD 0x2 @@ -83,6 +93,22 @@ typedef struct Log{ unsigned long int id; }Log; +typedef struct stack_node{ + SID *s_id; + _Bool if_malloc; + _Bool if_sid; + unsigned int type; + void *value; + struct stack_node *next; +} SNode; + +typedef struct stack{ + SID *s_id; + unsigned long long length; + SNode *top; + _Bool if_sid; +} Stack; + typedef struct tree_node { SID *s_id;