From dc75e1f0e4dd1ae3e8efc3a2124473e8e56c853e Mon Sep 17 00:00:00 2001 From: nishi Date: Mon, 29 Jan 2024 02:43:08 +0000 Subject: [PATCH] working git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@129 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/w3b/w3b.c | 14 +++++++------- Library/Tag.c | 33 +++++++++++++++++++++++++++++++-- Library/W3Tag.h | 2 +- W3Version.h.p | 2 +- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Example/w3b/w3b.c b/Example/w3b/w3b.c index 4f14aaa..1afcf2f 100644 --- a/Example/w3b/w3b.c +++ b/Example/w3b/w3b.c @@ -94,17 +94,17 @@ void access_site(const char* url) { } } -void html_handler(char* tagname, char** attr){ - printf("<%s>\n", tagname); - if(attr != NULL){ - int i; - for(i = 0; attr[i] != NULL; i++) printf("%s\n", attr[i]); - } +void html_handler(char* tagname, char* attr){ + printf("<%s> %s\n", tagname, attr != NULL ? attr : "NULL"); +} + +void text_handler(char* data){ + printf(" %s\n", data); } void render_site(){ if(databuf != NULL){ - W3_Tag_Parse(databuf, datalen, html_handler); + W3_Tag_Parse(databuf, datalen, html_handler, text_handler); } } diff --git a/Library/Tag.c b/Library/Tag.c index 3c15a7e..761893c 100644 --- a/Library/Tag.c +++ b/Library/Tag.c @@ -5,8 +5,10 @@ #include #include +#include +#include -void W3_Tag_Parse(char* data, size_t size, void(*tagfunc)(char* tagname, char** attr)){ +void W3_Tag_Parse(char* data, size_t size, void(*tagfunc)(char* tagname, char* attr), void(*textfunc)(char* data)){ int i; int phase = 0; char* tagn = malloc(1); @@ -14,30 +16,57 @@ void W3_Tag_Parse(char* data, size_t size, void(*tagfunc)(char* tagname, char** char* cbuf = malloc(2); cbuf[1] = 0; char* attrlist = NULL; + char* attr = malloc(1); + attr[0] = 0; + char* text = malloc(1); + text[0] = 0; + bool dq = false; for(i = 0; i < size; i++){ if(phase == 1 && data[i] != ' ' && data[i] != '>'){ cbuf[0] = data[i]; char* tmp = tagn; tagn = __W3_Concat(tmp, cbuf); free(tmp); + }else if(phase == 2 && (dq || data[i] != '>')){ + if(data[i] == '"') dq = !dq; + cbuf[0] = data[i]; + char* tmp = attr; + attr = __W3_Concat(tmp, cbuf); + free(tmp); } if(phase == 0 && data[i] == '<'){ + if(strlen(text) > 0){ + textfunc(text); + } + free(text); + text = malloc(1); + text[0] = 0; phase = 1; free(tagn); tagn = malloc(1); tagn[0] = 0; + }else if(phase == 0){ + cbuf[0] = data[i]; + char* tmp = text; + text = __W3_Concat(tmp, cbuf); + free(tmp); }else if(phase == 1 && (data[i] == ' ' || data[i] == '>')){ if(data[i] == ' '){ phase = 2; + free(attr); + attr = malloc(1); + attr[0] = 0; }else if(data[i] == '>'){ tagfunc(tagn, NULL); phase = 0; } }else if(phase == 2 && data[i] == '>'){ - tagfunc(tagn, attrlist); + tagfunc(tagn, attr); phase = 0; } } + free(text); + free(attr); free(tagn); free(cbuf); } diff --git a/Library/W3Tag.h b/Library/W3Tag.h index 61fb34d..ac64673 100644 --- a/Library/W3Tag.h +++ b/Library/W3Tag.h @@ -4,6 +4,6 @@ #include -void W3_Tag_Parse(char* data, size_t size, void(*func)(char* tagname, char** attr)); +void W3_Tag_Parse(char* data, size_t size, void(*tagfunc)(char* tagname, char* attr), void(*textfunc)(char* data)); #endif diff --git a/W3Version.h.p b/W3Version.h.p index 8f9b50e..1d033d5 100644 --- a/W3Version.h.p +++ b/W3Version.h.p @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.1" \ +#define LIBW3_VERSION "2.1A" \ SUFFIX #ifdef __cplusplus -- 2.43.0