]> Nishi Git Mirror - libw3.git/commitdiff
working
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Mon, 29 Jan 2024 02:43:08 +0000 (02:43 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Mon, 29 Jan 2024 02:43:08 +0000 (02:43 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@129 d27a3e52-49c5-7645-884c-6793ebffc270

Example/w3b/w3b.c
Library/Tag.c
Library/W3Tag.h
W3Version.h.p

index 4f14aaa222ae4099fb3f1804ca9218142ff991a3..1afcf2fbba5e8082f35edc4ef53d53f0c34f2082 100644 (file)
@@ -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("<TEXT> %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);
        }
 }
 
index 3c15a7ec74ee0f67ed13d84aa7c7be5cb1eb3f31..761893c4025e16ee9d9be054a1910ae28a23238a 100644 (file)
@@ -5,8 +5,10 @@
 
 #include <stddef.h>
 #include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
 
-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);
 }
index 61fb34d572b264ddeda73149100a2c00fbb2857b..ac646737aa786f4b1f300d96f885ce8f71514a64 100644 (file)
@@ -4,6 +4,6 @@
 
 #include <stddef.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), void(*textfunc)(char* data));
 
 #endif
index 8f9b50ecec0d1105883d61e038e4b041609996e4..1d033d58f0ef0fd1928c7489dfe95eb011b4354c 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "2.1" \
+#define LIBW3_VERSION "2.1A" \
 SUFFIX
 
 #ifdef __cplusplus