From d2a32ee517c5359a9b38909062bad28266cc63d4 Mon Sep 17 00:00:00 2001 From: nishi Date: Mon, 29 Jan 2024 00:35:10 +0000 Subject: [PATCH] tag parsing git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@128 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/w3b/w3b.c | 5 +++++ Library/Tag.c | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Example/w3b/w3b.c b/Example/w3b/w3b.c index 07f7c87..4f14aaa 100644 --- a/Example/w3b/w3b.c +++ b/Example/w3b/w3b.c @@ -95,6 +95,11 @@ 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 render_site(){ diff --git a/Library/Tag.c b/Library/Tag.c index 948a2b6..3c15a7e 100644 --- a/Library/Tag.c +++ b/Library/Tag.c @@ -1,7 +1,43 @@ /* $Id$ */ #include "W3Tag.h" +#include "W3Util.h" + #include +#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)){ + int i; + int phase = 0; + char* tagn = malloc(1); + tagn[0] = 0; + char* cbuf = malloc(2); + cbuf[1] = 0; + char* attrlist = NULL; + 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); + } + if(phase == 0 && data[i] == '<'){ + phase = 1; + free(tagn); + tagn = malloc(1); + tagn[0] = 0; + }else if(phase == 1 && (data[i] == ' ' || data[i] == '>')){ + if(data[i] == ' '){ + phase = 2; + }else if(data[i] == '>'){ + tagfunc(tagn, NULL); + phase = 0; + } + }else if(phase == 2 && data[i] == '>'){ + tagfunc(tagn, attrlist); + phase = 0; + } + } + free(tagn); + free(cbuf); } -- 2.43.0