gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r26248 - monkey/trunk/pathologist/src/struct_parse


From: gnunet
Subject: [GNUnet-SVN] r26248 - monkey/trunk/pathologist/src/struct_parse
Date: Fri, 1 Mar 2013 14:00:32 +0100

Author: teichm
Date: 2013-03-01 14:00:31 +0100 (Fri, 01 Mar 2013)
New Revision: 26248

Added:
   monkey/trunk/pathologist/src/struct_parse/exmpl.c
Modified:
   monkey/trunk/pathologist/src/struct_parse/p.c
Log:
rewriting struct parser part1

Added: monkey/trunk/pathologist/src/struct_parse/exmpl.c
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/exmpl.c                           
(rev 0)
+++ monkey/trunk/pathologist/src/struct_parse/exmpl.c   2013-03-01 13:00:31 UTC 
(rev 26248)
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+struct a {
+       char c;
+       char c2;
+       char* s[2];
+       struct a* a;
+       char r[4];
+       int i[2];
+       float f;
+};
+
+int main(int argc, char* argv[]) {
+       struct a a;
+       struct a b;
+       a.c = '\'';
+       a.c2 = '"';
+       a.s[0] = "bliblablu\"";
+       a.s[1] = "trololo";
+       a.a = &b;
+       a.r[0] = '\n';
+       a.r[1] = 'a';
+       a.r[2] = '';
+       a.r[3] = '"';
+       a.i[0] = -5;
+       a.i[1] = 42;
+       a.f = 3.141f;
+
+       return 0;
+}

Modified: monkey/trunk/pathologist/src/struct_parse/p.c
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/p.c       2013-03-01 12:58:59 UTC 
(rev 26247)
+++ monkey/trunk/pathologist/src/struct_parse/p.c       2013-03-01 13:00:31 UTC 
(rev 26248)
@@ -1,7 +1,61 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-int parse(char* str, int indent) {
+char* parse(char* str, int indent);
+
+// parses one pair of curly braces recursively.
+// expects first char to be the opening '{'
+// returns pointer to the next char after the closing '}' or NULL on error
+char* parse(char* str, int indent) {
+       int is_struct, is_array, had_equals = 0;
+       char* first, second = NULL;
+
+       if(*str++ != '{') return NULL;
+       while(*str == ' ') str++;
+       if(*str == '\0') return NULL;
+       while(BLABLABLA) {
+               first = str;
+               while(*str != '\0') {
+                       switch(*str) {
+                               case '{':
+                                       if(is_struct && !had_equals) return 
NULL;
+                                       str = parse(str, indent+1);
+                                       break;
+                               case '=':
+                                       if(had_equals) return NULL;
+                                       had_equals = 1;
+                                       break;
+                               case '\'':
+                               case '"':
+                                       char end_char = *str;
+                                       if(had_equals) second = str;
+                                       else first = str;
+                                       for(str++; *str != end_char; str++) {
+                                               if(*str == '\0') return NULL;
+                                               if(*str == '\\') {
+                                                       if(*(str+1) == '\0') 
return NULL;
+                                                       else str++;
+                                               }
+                                       }
+                                       break;
+                               case ',':
+                                       if(is_struct && !had_equals) return 
NULL;
+                                       break;
+                               case '}':
+                                       break;
+                       }
+                       str++;
+               }
+               for(char* ws = str-1; *ws == ' '; ws--)
+                       *ws = '\0';
+
+
+               if(*str == '\0') return NULL;
+       }
+       return str;
+}
+
+int parse_struct(char* str, int indent) {
        char* token = str;
        char* name;
        char* value;
@@ -91,7 +145,7 @@
        char* line;
        while(line = getl()) {
                if(*line == '\0') break;
-               printf("\nreturned: %d\n", parse(line, 1));
+               printf("\nreturned: %d\n", parse_struct(line, 1));
                free(line);
        }
        if(line) free(line);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]