[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r26359 - monkey/trunk/pathologist/src/struct_parse
From: |
gnunet |
Subject: |
[GNUnet-SVN] r26359 - monkey/trunk/pathologist/src/struct_parse |
Date: |
Fri, 8 Mar 2013 15:16:33 +0100 |
Author: teichm
Date: 2013-03-08 15:16:33 +0100 (Fri, 08 Mar 2013)
New Revision: 26359
Added:
monkey/trunk/pathologist/src/struct_parse/test1.txt
monkey/trunk/pathologist/src/struct_parse/test2.txt
monkey/trunk/pathologist/src/struct_parse/test3.txt
monkey/trunk/pathologist/src/struct_parse/test4.txt
Removed:
monkey/trunk/pathologist/src/struct_parse/test_array.txt
Modified:
monkey/trunk/pathologist/src/struct_parse/p.c
monkey/trunk/pathologist/src/struct_parse/test.sh
Log:
parses structs AND arrays now. add some more tests
Modified: monkey/trunk/pathologist/src/struct_parse/p.c
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/p.c 2013-03-08 13:27:39 UTC
(rev 26358)
+++ monkey/trunk/pathologist/src/struct_parse/p.c 2013-03-08 14:16:33 UTC
(rev 26359)
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
char* parse(char* str, int indent);
@@ -7,106 +8,98 @@
// 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;
+ int is_struct = 0, is_array = 0, had_equals = 0, had_children = 0,
had_end = 0;
+ char* first = NULL;
+ char* second = NULL;
+ char* ws = NULL;
+ char* count = NULL;
if(*str++ != '{') return NULL;
while(*str == ' ') str++;
- if(*str == '\0') return NULL;
- while(BLABLABLA) {
+ if(!*str) return NULL;
+ while(*str) {
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++;
- }
+ second = NULL;
+ had_equals = 0;
+ had_children = 0;
+ had_end = 0;
+ count = NULL;
+ while(*str) {
+ if(*str == '{') {
+ if( (is_struct && !had_equals) ||
+ (str != first && str != second) )
return NULL;
+ if(str == second) {
+ printf("%*s<expression name=\"%s\">\n",
2*indent, "", first);
+ had_children = 1;
+ }
+ str = parse(str, indent+1);
+ //continue; // have to check this char => no
str++ at end of loop.
+ } else if(*str == '=') {
+ if(had_equals || is_array) return NULL;
+ had_equals = 1;
+ if(!is_struct && !is_array)
+ is_struct = 1;
+ for(ws = str-1; *ws == ' '; ws--)
+ *ws = '\0';
+ *str = '\0';
+ while(*++str == ' ');
+ second = str;
+ continue; // have to check this char => no
str++ at end of loop.
+ } else if(*str == '\'' || *str == '"') {
+ char end_char = *str;
+ if(had_equals) second = str;
+ else first = str;
+ for(str++; *str != end_char; str++) {
+ if(!*str) return NULL;
+ if(*str == '\\') {
+ if(!*(str+1)) return NULL;
+ else str++;
}
- break;
- case ',':
- if(is_struct && !had_equals) return
NULL;
- break;
- case '}':
- break;
+ }
+ } else if(*str == '<') {
+ for(ws = str-1; *ws == ' '; ws--)
+ *ws = '\0';
+ if(!(str = strpbrk(str+1, "0123456789>")))
return NULL;
+ if(*str == '>') continue;
+ count = str;
+ if(!(str = strpbrk(str+1, " >"))) return NULL;
+ if(*str == ' ')
+ *str = '\0';
+ else {
+ *str = '\0';
+ if(!(str = strchr(str+1, '>'))) return
NULL;
+ }
+ } else if(*str == ',') {
+ if(is_struct && !had_equals) return NULL;
+ is_array = !is_struct;
+ is_struct = !is_array;
+ break;
+ } else if(*str == '}') {
+ had_end = 1;
+ break;
}
str++;
}
- for(char* ws = str-1; *ws == ' '; ws--)
+ for(ws = str-1; *ws == ' '; ws--)
*ws = '\0';
-
-
- if(*str == '\0') return NULL;
+ *str = '\0';
+ if(had_children)
+ printf("%*s</expression>\n", 2*indent, "");
+ else if(is_array)
+ if(count)
+ printf("%*s<element
count=\"%s\">%s</element>\n", 2*indent, "", count, first);
+ else
+ printf("%*s<element>%s</element>\n", 2*indent,
"", first);
+ else
+ printf("%*s<expression name=\"%s\">%s</expression>\n",
2*indent, "", first, second);
+ if(had_end) return str;
+ str++;
+ while(*str == ' ') str++;
}
- return str;
+ return NULL;
}
-int parse_struct(char* str, int indent) {
- char* token = str;
- char* name;
- char* value;
- int i, ret = 1;
- if(str[0] != '{') return 0;
- while(*token != '\0' && (*token == '{' || *token == ' ')) token++;
- if(*token == '\0') return 0;
- while(*token != '\0') {
- i=1;
- name = token;
- while(*token != '\0' && *token != ' ' && *token != '=')
- if(*token == '{' || *token == '}')
- return 0;
- else
- token++;
- if(*token == '\0') return 0;
- *(token++) = '\0';
- while(*token != '\0' && (*token == ' ' || *token == '='))
token++;
- if(*token == '\0') return 0;
- value = token;
- if(*token == '{') {
- while(i>0) {
- token++;
- if(*token == '\0') return 0;
- if(*token == '{') i++;
- if(*token == '}') i--;
- }
- token++;
- } else {
- while(*token != '\0' && *token != ',' && *token != '}')
token++;
- if(*token == '\0') return 0;
- }
- if(*token == '}')
- if(*(token+1) != '\0') return 0;
- else {
- *(token++) = '\0';
- printf("%*s<expression
name=\"%s\">%s</expression>\n", 2*indent, "", name, value);
- break;
- }
- *(token++) = '\0';
- if(!i && printf("%*s<expression name=\"%s\">\n", 2*indent, "",
name)) {
- if(!parse(value, indent+1)) ret = 0;
- printf("%*s</expression>\n", 2*indent, "");
- } else printf("%*s<expression name=\"%s\">%s</expression>\n",
2*indent, "", name, value);
- while(*token == ',' || *token == ' ') token++;
- if(*token == '}' && *(token+1) == '\0') break;
- }
- return ret;
-}
-
char * getl(void) {
char * line = malloc(128), * linep = line;
size_t lenmax = 128, len = lenmax;
@@ -145,7 +138,8 @@
char* line;
while(line = getl()) {
if(*line == '\0') break;
- printf("\nreturned: %d\n", parse_struct(line, 1));
+ if(!parse(line, 1))
+ printf("=====FAILED!=====\n\n");
free(line);
}
if(line) free(line);
Modified: monkey/trunk/pathologist/src/struct_parse/test.sh
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/test.sh 2013-03-08 13:27:39 UTC
(rev 26358)
+++ monkey/trunk/pathologist/src/struct_parse/test.sh 2013-03-08 14:16:33 UTC
(rev 26359)
@@ -1,2 +1,2 @@
#!/bin/sh
-for n in `seq 1 1000`; do zzuf -r 0.00$n -s $n -i valgrind -q --leak-check=yes
./a.out < test.txt ; done
+for n in `seq 1 1000`; do zzuf -r 0.00$n -s $n -i valgrind -q
--leak-check=summary --track-origins=yes ./a.out < test.txt; done
Added: monkey/trunk/pathologist/src/struct_parse/test1.txt
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/test1.txt
(rev 0)
+++ monkey/trunk/pathologist/src/struct_parse/test1.txt 2013-03-08 14:16:33 UTC
(rev 26359)
@@ -0,0 +1 @@
+{0 <repeats 9274 times>, -136430974, 32767, 0 <repeats 18 times>, 1560576, 0,
1560100, 0, 1560100, 0, 0, 0, 5, 0, 3657728, 0, 3678208, 0, 3677624, 0,
3696728, 0, 1560576, 0, 3, 0 <repeats 47 times>, -136436024, 32767, 0, 0, 0, 0,
0, 0, -134241024, 32767, 47, 0, -136413867, 32767, 0, 0, -134239856, 32767, 32,
0, 0, 1, -134241104, 32767, 0, 0, -8304, 32767, -136414759, 32767, -134225464,
32767, -134240992, 32767, -8304, 32767, -136431348, 32767, 1811964521,
778265193, 909012851, 1932419840, 3550831, 3550831, -136398771, 32767, 0, 0,
-140067357, 32767, 0, 0, -136427454, 32767, -136478720, 32767, -136460200,
32767, -136479304, 32767, 0, 0, -8656, 32767, -134241024, 32767, 4195056, 0,
-134225504, 32767, 3, 1, -8136, 0, 6, 0, 3696728, 0, 2053, 0, 1324524, 0, 1, 0,
33261, 0, 0, 0, 0, 0, 1583120, 0, 4096, 0, 3104, 0, 1349347463, 0, 0, 0,
1342975530, 0, 0, 0, 1349347464, 0 <repeats 11 times>, 4195056, 0, -134225464,
32767, -134229952, 32767, 0, 0, -134224528, 32767, 0, 0, -136427174
, 32767,
0, 32767, -7304, 32767, 0, 0, 0, 0, 0, 0, -134261768, 32767, 0, 0, 7, 0, 0,
1...}
Added: monkey/trunk/pathologist/src/struct_parse/test2.txt
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/test2.txt
(rev 0)
+++ monkey/trunk/pathologist/src/struct_parse/test2.txt 2013-03-08 14:16:33 UTC
(rev 26359)
@@ -0,0 +1 @@
+{"buil\"e" , '" ueuil \"\'', "\\\\", 0 ,1560576,0, 1560100,0 , 1560100}
Added: monkey/trunk/pathologist/src/struct_parse/test3.txt
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/test3.txt
(rev 0)
+++ monkey/trunk/pathologist/src/struct_parse/test3.txt 2013-03-08 14:16:33 UTC
(rev 26359)
@@ -0,0 +1 @@
+{data = 0x0,offset=0, data_len= 140737349529600 ,depth =4158564850 }
Added: monkey/trunk/pathologist/src/struct_parse/test4.txt
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/test4.txt
(rev 0)
+++ monkey/trunk/pathologist/src/struct_parse/test4.txt 2013-03-08 14:16:33 UTC
(rev 26359)
@@ -0,0 +1 @@
+{0 <repeats 9274 times>, -136430974, 32767, 0 <repeats 18 times>, 1560576,
0,1560100, 0, 1560100, 0, 0}
Deleted: monkey/trunk/pathologist/src/struct_parse/test_array.txt
===================================================================
--- monkey/trunk/pathologist/src/struct_parse/test_array.txt 2013-03-08
13:27:39 UTC (rev 26358)
+++ monkey/trunk/pathologist/src/struct_parse/test_array.txt 2013-03-08
14:16:33 UTC (rev 26359)
@@ -1 +0,0 @@
-{0 <repeats 9274 times>, -136430974, 32767, 0 <repeats 18 times>, 1560576, 0,
1560100, 0, 1560100, 0, 0, 0, 5, 0, 3657728, 0, 3678208, 0, 3677624, 0,
3696728, 0, 1560576, 0, 3, 0 <repeats 47 times>, -136436024, 32767, 0, 0, 0, 0,
0, 0, -134241024, 32767, 47, 0, -136413867, 32767, 0, 0, -134239856, 32767, 32,
0, 0, 1, -134241104, 32767, 0, 0, -8304, 32767, -136414759, 32767, -134225464,
32767, -134240992, 32767, -8304, 32767, -136431348, 32767, 1811964521,
778265193, 909012851, 1932419840, 3550831, 3550831, -136398771, 32767, 0, 0,
-140067357, 32767, 0, 0, -136427454, 32767, -136478720, 32767, -136460200,
32767, -136479304, 32767, 0, 0, -8656, 32767, -134241024, 32767, 4195056, 0,
-134225504, 32767, 3, 1, -8136, 0, 6, 0, 3696728, 0, 2053, 0, 1324524, 0, 1, 0,
33261, 0, 0, 0, 0, 0, 1583120, 0, 4096, 0, 3104, 0, 1349347463, 0, 0, 0,
1342975530, 0, 0, 0, 1349347464, 0 <repeats 11 times>, 4195056, 0, -134225464,
32767, -134229952, 32767, 0, 0, -134224528, 32767, 0, 0, -136427174
, 32767,
0, 32767, -7304, 32767, 0, 0, 0, 0, 0, 0, -134261768, 32767, 0, 0, 7, 0, 0,
1...}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r26359 - monkey/trunk/pathologist/src/struct_parse,
gnunet <=