From 58f4af3f92e9571ae499bb3ba5ee5c96ee6b01fe Mon Sep 17 00:00:00 2001 From: nishi Date: Thu, 23 May 2024 10:23:19 +0000 Subject: [PATCH] free parser token git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@88 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Client/main.c | 33 +++++++++++++------------- Library/dw_parser.h | 1 + Library/parser.c | 58 ++++++++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/Client/main.c b/Client/main.c index 80a2c4e..0e003b7 100644 --- a/Client/main.c +++ b/Client/main.c @@ -51,11 +51,11 @@ void padleft(int leftpad, const char* str) { free(spaces); } -void print_recursive(struct __dw_token* token, int depth){ +void print_recursive(struct __dw_token* token, int depth) { int i; for(i = 0; i < depth; i++) printf(" "); printf("%d:%s\n", token->type, token->name == NULL ? "(null)" : token->name); - if(token->token != NULL){ + if(token->token != NULL) { for(i = 0; token->token[i] != NULL; i++) print_recursive(token->token[i], depth + 1); } } @@ -84,7 +84,7 @@ int main(int argc, char** argv) { banner = false; } else if(strcmp(argv[i], "--nolog") == 0 || strcmp(argv[i], "-NL") == 0) { log = false; - } else if(strcmp(argv[i], "-f") == 0){ + } else if(strcmp(argv[i], "-f") == 0) { fprog = argv[i + 1]; i++; } else { @@ -115,7 +115,7 @@ int main(int argc, char** argv) { printf("\x1b[2J\x1b[1;1H"); fflush(stdout); } - if(banner){ + if(banner) { printf("DataWorks version %s %s %s\n", dataworks_get_version(), dataworks_get_compile_date(), dataworks_get_platform()); if(dataworks_get_endian() == 'L') { printf("This system is little-endian.\n"); @@ -127,7 +127,7 @@ int main(int argc, char** argv) { printf("All rights reserved.\n"); } if(create) { - if(log){ + if(log) { printf("\n"); printf("Creating the database: %s\n", fname); } @@ -139,7 +139,7 @@ int main(int argc, char** argv) { if(log) printf("Created successfully.\n"); } } - if(log){ + if(log) { printf("\n"); printf("Opening the database: %s\n", fname); } @@ -149,14 +149,14 @@ int main(int argc, char** argv) { dataworks_database_close(db); return 1; } - if(log){ + if(log) { time_t mtime = (time_t)dataworks_database_get_mtime(db); struct tm* tm = localtime(&mtime); char mtimestr[256]; strftime(mtimestr, 255, "%a %b %d %H:%M:%S %Z %Y", tm); printf("Opened the database (Version %d, Modified at %s).\n", dataworks_database_get_version(db), mtimestr); printf("\n"); - if(fprog == NULL){ + if(fprog == NULL) { printf("Type a command (.help) for the help.\n"); printf("\n"); } @@ -167,14 +167,14 @@ int main(int argc, char** argv) { char ch; char prompt = '.'; FILE* fp = stdin; - if(fprog != NULL){ + if(fprog != NULL) { fp = fopen(fprog, "r"); - if(fp == NULL){ + if(fp == NULL) { printf("Could not open the program file.\n"); return 1; } } - if(fprog == NULL){ + if(fprog == NULL) { printf("%c ", prompt); fflush(stdout); } @@ -226,9 +226,9 @@ int main(int argc, char** argv) { linebuf = __dw_strcat(tmp, buf); free(tmp); int i; - while(true){ - for(i = 0; linebuf[i] != 0; i++){ - if(linebuf[i] == ';'){ + while(true) { + for(i = 0; linebuf[i] != 0; i++) { + if(linebuf[i] == ';') { char* line = malloc(i + 1); line[i] = 0; memcpy(line, linebuf, i); @@ -239,11 +239,12 @@ int main(int argc, char** argv) { } else { print_recursive(token, 0); } + __dw_parser_free(token); } else { printf("Parser returned NULL. Help!\n"); } free(line); - if(strlen(linebuf) > 0){ + if(strlen(linebuf) > 0) { char* newbuf = malloc(strlen(linebuf) - i); newbuf[strlen(linebuf) - i - 1] = 0; memcpy(newbuf, linebuf + i + 1, strlen(linebuf) - i - 1); @@ -256,7 +257,7 @@ int main(int argc, char** argv) { break; } } - if(fprog == NULL){ + if(fprog == NULL) { printf("%c ", prompt); fflush(stdout); } diff --git a/Library/dw_parser.h b/Library/dw_parser.h index 9f6b38c..bca3f57 100644 --- a/Library/dw_parser.h +++ b/Library/dw_parser.h @@ -53,6 +53,7 @@ struct __dw_token { }; struct __dw_token* __dw_parser_parse(const char* str); +void __dw_parser_free(struct __dw_token* token); #ifdef __cplusplus } diff --git a/Library/parser.c b/Library/parser.c index 57ffac6..1bb468c 100644 --- a/Library/parser.c +++ b/Library/parser.c @@ -59,9 +59,10 @@ struct __dw_token* __dw_parser_parse(const char* str) { if(str[i] == '(') brace++; if(str[i] == ')') brace--; if(brace > 0) { - if(str[i] == ','){ + if(str[i] == ',') { i++; - for(; str[i] != 0 && (str[i] == '\t' || str[i] == ' '); i++); + for(; str[i] != 0 && (str[i] == '\t' || str[i] == ' '); i++) + ; i--; } char* tmp = br; @@ -69,35 +70,35 @@ struct __dw_token* __dw_parser_parse(const char* str) { free(tmp); } else { int j; - for(j = 0; buf[j] != 0; j++){ - if(buf[j] != ' ' && buf[j] != '\t'){ + for(j = 0; buf[j] != 0; j++) { + if(buf[j] != ' ' && buf[j] != '\t') { char* newbuf = __dw_strdup(buf + j); free(buf); buf = newbuf; break; } } - for(j = 0; br[j] != 0; j++){ - if(br[j] != ' ' && br[j] != '\t'){ + for(j = 0; br[j] != 0; j++) { + if(br[j] != ' ' && br[j] != '\t') { char* newbr = __dw_strdup(br + j); free(br); br = newbr; break; } } - for(j = strlen(buf) - 1; j >= 0; j--){ - if(buf[j] != ' ' && buf[j] != '\t'){ + for(j = strlen(buf) - 1; j >= 0; j--) { + if(buf[j] != ' ' && buf[j] != '\t') { buf[j + 1] = 0; break; } } - for(j = strlen(br) - 1; j >= 0; j--){ - if(br[j] != ' ' && br[j] != '\t'){ + for(j = strlen(br) - 1; j >= 0; j--) { + if(br[j] != ' ' && br[j] != '\t') { br[j + 1] = 0; break; } } - if(buf[0] == ','){ + if(buf[0] == ',') { char* newbuf = __dw_strdup(buf + 1); free(buf); buf = newbuf; @@ -107,23 +108,24 @@ struct __dw_token* __dw_parser_parse(const char* str) { char* comma = malloc(1); comma[0] = 0; int intbrace = 0; - for(k = 0;; k++){ + for(k = 0;; k++) { char c = br[k]; - if(c == '(' || c == ')' || intbrace > 0){ + if(c == '(' || c == ')' || intbrace > 0) { if(c == '(') intbrace++; if(c == ')') intbrace--; cbuf[0] = c; char* tmp = comma; comma = __dw_strcat(tmp, cbuf); free(tmp); - }else if(c == 0 || c == ','){ + } else if(c == 0 || c == ',') { j = 0; - if(token->token != NULL){ - for(j = 0; token->token[j] != NULL; j++); + if(token->token != NULL) { + for(j = 0; token->token[j] != NULL; j++) + ; } struct __dw_token** newtokens = malloc(sizeof(*newtokens) * (j + 2)); - if(token->token != NULL){ - for(j = 0; token->token[j] != NULL; j++){ + if(token->token != NULL) { + for(j = 0; token->token[j] != NULL; j++) { newtokens[j] = token->token[j]; } free(token->token); @@ -135,7 +137,7 @@ struct __dw_token* __dw_parser_parse(const char* str) { comma = malloc(1); comma[0] = 0; if(c == 0) break; - }else{ + } else { cbuf[0] = c; char* tmp = comma; comma = __dw_strcat(tmp, cbuf); @@ -151,9 +153,9 @@ struct __dw_token* __dw_parser_parse(const char* str) { buf[0] = 0; } } else if(dq) { - if(str[i] == '"'){ + if(str[i] == '"') { dq = !dq; - }else{ + } else { char* tmp = buf; buf = __dw_strcat(tmp, cbuf); free(tmp); @@ -175,7 +177,7 @@ struct __dw_token* __dw_parser_parse(const char* str) { free(tmp); } } - if(!has_brace){ + if(!has_brace) { token->type = __DW_VALUE; token->name = __dw_strdup(buf); } @@ -183,3 +185,15 @@ struct __dw_token* __dw_parser_parse(const char* str) { free(buf); return token; } + +void __dw_parser_free(struct __dw_token* token) { + free(token->name); + if(token->type == __DW_METHOD) { + if(token->token != NULL) { + int i; + for(i = 0; token->token[i] != NULL; i++) __dw_parser_free(token->token[i]); + free(token->token); + } + } + free(token); +} -- 2.43.0