]> Nishi Git Mirror - libw3.git/commitdiff
tag parsing
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Mon, 29 Jan 2024 00:35:10 +0000 (00:35 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Mon, 29 Jan 2024 00:35:10 +0000 (00:35 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@128 d27a3e52-49c5-7645-884c-6793ebffc270

Example/w3b/w3b.c
Library/Tag.c

index 07f7c87f2c9585cd97f7038e7b001acf98988705..4f14aaa222ae4099fb3f1804ca9218142ff991a3 100644 (file)
@@ -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(){
index 948a2b646322066707f3066d92665a537765779d..3c15a7ec74ee0f67ed13d84aa7c7be5cb1eb3f31 100644 (file)
@@ -1,7 +1,43 @@
 /* $Id$ */
 #include "W3Tag.h"
 
+#include "W3Util.h"
+
 #include <stddef.h>
+#include <stdlib.h>
 
-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);
 }