diff options
Diffstat (limited to 'pathologist/src/struct_parse')
-rw-r--r-- | pathologist/src/struct_parse/exmpl.c | 31 | ||||
-rw-r--r-- | pathologist/src/struct_parse/p.c | 147 | ||||
-rw-r--r-- | pathologist/src/struct_parse/test.sh | 2 | ||||
-rw-r--r-- | pathologist/src/struct_parse/test.txt | 1 | ||||
-rw-r--r-- | pathologist/src/struct_parse/test1.txt | 1 | ||||
-rw-r--r-- | pathologist/src/struct_parse/test2.txt | 1 | ||||
-rw-r--r-- | pathologist/src/struct_parse/test3.txt | 1 | ||||
-rw-r--r-- | pathologist/src/struct_parse/test4.txt | 1 |
8 files changed, 185 insertions, 0 deletions
diff --git a/pathologist/src/struct_parse/exmpl.c b/pathologist/src/struct_parse/exmpl.c new file mode 100644 index 0000000..00d3a6f --- /dev/null +++ b/pathologist/src/struct_parse/exmpl.c | |||
@@ -0,0 +1,31 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | |||
4 | struct a { | ||
5 | char c; | ||
6 | char c2; | ||
7 | char* s[2]; | ||
8 | struct a* a; | ||
9 | char r[4]; | ||
10 | int i[2]; | ||
11 | float f; | ||
12 | }; | ||
13 | |||
14 | int main(int argc, char* argv[]) { | ||
15 | struct a a; | ||
16 | struct a b; | ||
17 | a.c = '\''; | ||
18 | a.c2 = '"'; | ||
19 | a.s[0] = "bliblablu\""; | ||
20 | a.s[1] = "trololo"; | ||
21 | a.a = &b; | ||
22 | a.r[0] = '\n'; | ||
23 | a.r[1] = 'a'; | ||
24 | a.r[2] = ''; | ||
25 | a.r[3] = '"'; | ||
26 | a.i[0] = -5; | ||
27 | a.i[1] = 42; | ||
28 | a.f = 3.141f; | ||
29 | |||
30 | return 0; | ||
31 | } | ||
diff --git a/pathologist/src/struct_parse/p.c b/pathologist/src/struct_parse/p.c new file mode 100644 index 0000000..c99a654 --- /dev/null +++ b/pathologist/src/struct_parse/p.c | |||
@@ -0,0 +1,147 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | |||
5 | char* parse(char* str, int indent); | ||
6 | |||
7 | // parses one pair of curly braces recursively. | ||
8 | // expects first char to be the opening '{' | ||
9 | // returns pointer to the next char after the closing '}' or NULL on error | ||
10 | char* parse(char* str, int indent) { | ||
11 | int is_struct = 0, is_array = 0, had_equals = 0, had_children = 0, had_end = 0; | ||
12 | char* first = NULL; | ||
13 | char* second = NULL; | ||
14 | char* ws = NULL; | ||
15 | char* count = NULL; | ||
16 | |||
17 | if(*str++ != '{') return NULL; | ||
18 | while(*str == ' ') str++; | ||
19 | if(!*str) return NULL; | ||
20 | while(*str) { | ||
21 | first = str; | ||
22 | second = NULL; | ||
23 | had_equals = 0; | ||
24 | had_children = 0; | ||
25 | had_end = 0; | ||
26 | count = NULL; | ||
27 | while(*str) { | ||
28 | if(*str == '{') { | ||
29 | if( (is_struct && !had_equals) || | ||
30 | (str != first && str != second) ) return NULL; | ||
31 | if(str == second) { | ||
32 | printf("%*s<expression name=\"%s\">\n", 2*indent, "", first); | ||
33 | had_children = 1; | ||
34 | } | ||
35 | str = parse(str, indent+1); | ||
36 | //continue; // have to check this char => no str++ at end of loop. | ||
37 | } else if(*str == '=') { | ||
38 | if(had_equals || is_array) return NULL; | ||
39 | had_equals = 1; | ||
40 | if(!is_struct && !is_array) | ||
41 | is_struct = 1; | ||
42 | for(ws = str-1; *ws == ' '; ws--) | ||
43 | *ws = '\0'; | ||
44 | *str = '\0'; | ||
45 | while(*++str == ' '); | ||
46 | second = str; | ||
47 | continue; // have to check this char => no str++ at end of loop. | ||
48 | } else if(*str == '\'' || *str == '"') { | ||
49 | char end_char = *str; | ||
50 | if(had_equals) second = str; | ||
51 | else first = str; | ||
52 | for(str++; *str != end_char; str++) { | ||
53 | if(!*str) return NULL; | ||
54 | if(*str == '\\') { | ||
55 | if(!*(str+1)) return NULL; | ||
56 | else str++; | ||
57 | } | ||
58 | } | ||
59 | } else if(*str == '<') { | ||
60 | for(ws = str-1; *ws == ' '; ws--) | ||
61 | *ws = '\0'; | ||
62 | if(!(str = strpbrk(str+1, "0123456789>"))) return NULL; | ||
63 | if(*str == '>') continue; | ||
64 | count = str; | ||
65 | if(!(str = strpbrk(str+1, " >"))) return NULL; | ||
66 | if(*str == ' ') | ||
67 | *str = '\0'; | ||
68 | else { | ||
69 | *str = '\0'; | ||
70 | if(!(str = strchr(str+1, '>'))) return NULL; | ||
71 | } | ||
72 | } else if(*str == ',') { | ||
73 | if(is_struct && !had_equals) return NULL; | ||
74 | is_array = !is_struct; | ||
75 | is_struct = !is_array; | ||
76 | break; | ||
77 | } else if(*str == '}') { | ||
78 | had_end = 1; | ||
79 | break; | ||
80 | } | ||
81 | str++; | ||
82 | } | ||
83 | for(ws = str-1; *ws == ' '; ws--) | ||
84 | *ws = '\0'; | ||
85 | *str = '\0'; | ||
86 | if(had_children) | ||
87 | printf("%*s</expression>\n", 2*indent, ""); | ||
88 | else if(is_array) | ||
89 | if(count) | ||
90 | printf("%*s<element count=\"%s\">%s</element>\n", 2*indent, "", count, first); | ||
91 | else | ||
92 | printf("%*s<element>%s</element>\n", 2*indent, "", first); | ||
93 | else | ||
94 | printf("%*s<expression name=\"%s\">%s</expression>\n", 2*indent, "", first, second); | ||
95 | if(had_end) return str; | ||
96 | str++; | ||
97 | while(*str == ' ') str++; | ||
98 | } | ||
99 | return NULL; | ||
100 | } | ||
101 | |||
102 | |||
103 | char * getl(void) { | ||
104 | char * line = malloc(128), * linep = line; | ||
105 | size_t lenmax = 128, len = lenmax; | ||
106 | int c; | ||
107 | |||
108 | if(line == NULL) | ||
109 | return NULL; | ||
110 | |||
111 | for(;;) { | ||
112 | c = fgetc(stdin); | ||
113 | if(c == EOF) | ||
114 | break; | ||
115 | |||
116 | if(--len == 0) { | ||
117 | len = lenmax; | ||
118 | char * linen = realloc(linep, lenmax *= 2); | ||
119 | |||
120 | if(linen == NULL) { | ||
121 | free(linep); | ||
122 | return NULL; | ||
123 | } | ||
124 | line = linen + (line - linep); | ||
125 | linep = linen; | ||
126 | } | ||
127 | |||
128 | if((*line = c) == '\n') | ||
129 | break; | ||
130 | line++; | ||
131 | } | ||
132 | *line = '\0'; | ||
133 | return linep; | ||
134 | } | ||
135 | |||
136 | |||
137 | int main(int argc, char* argv[]) { | ||
138 | char* line; | ||
139 | while(line = getl()) { | ||
140 | if(*line == '\0') break; | ||
141 | if(!parse(line, 1)) | ||
142 | printf("=====FAILED!=====\n\n"); | ||
143 | free(line); | ||
144 | } | ||
145 | if(line) free(line); | ||
146 | return 0; | ||
147 | } | ||
diff --git a/pathologist/src/struct_parse/test.sh b/pathologist/src/struct_parse/test.sh new file mode 100644 index 0000000..2ab8cec --- /dev/null +++ b/pathologist/src/struct_parse/test.sh | |||
@@ -0,0 +1,2 @@ | |||
1 | #!/bin/sh | ||
2 | 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 | ||
diff --git a/pathologist/src/struct_parse/test.txt b/pathologist/src/struct_parse/test.txt new file mode 100644 index 0000000..030fe36 --- /dev/null +++ b/pathologist/src/struct_parse/test.txt | |||
@@ -0,0 +1 @@ | |||
{value = {publish = {pc = 0x607a20, fi = 0x60b580, cctx = 0x0, pctx = 0x0, filename = 0x60b9e0 "/home/grothoff/svn/gnunet/src/core/../../COPYING", size = 35147, eta = { rel_value = 18446744073709551615}, duration = {rel_value = 1360752708604}, completed = 0, anonymity = 1, specifics = {progress = {data = 0x0, offset = 0, data_len = 140737349529600, depth = 4158564850}, resume = {message = 0x0, chk_uri = 0x0}, completed = {chk_uri = 0x0}, error = {message = 0x0}}}, download = {dc = 0x607a20, cctx = 0x60b580, pctx = 0x0, sctx = 0x0, uri = 0x60b9e0, filename = 0x894b <Address 0x894b out of bounds>, size = 18446744073709551615, eta = {rel_value = 1360752708604}, duration = { rel_value = 0}, completed = 1, anonymity = 0, is_active = 0, specifics = {progress = {data = 0x0, offset = 140737349529600, data_len = 140737351952882, block_download_duration = { rel_value = 140733193388033}, depth = 0, respect_offered = 0, num_transmissions = 6336704}, start = {meta = 0x0}, resume = {meta = 0x0, message = 0x7ffff7b9b000 "\177ELF\002\001\001"}, error = {message = 0x0}}}, search = {sc = 0x607a20, cctx = 0x60b580, pctx = 0x0, query = 0x0, duration = {rel_value = 6339040}, anonymity = 35147, specifics = {result = {meta = 0xffffffffffffffff, uri = 0x13cd32e8bfc, result = 0x0, applicability_rank = 1}, resume_result = {meta = 0xffffffffffffffff, uri = 0x13cd32e8bfc, result = 0x0, availability_rank = 1, availability_certainty = 0, applicability_rank = 0}, update = {cctx = 0xffffffffffffffff, meta = 0x13cd32e8bfc, uri = 0x0, availability_rank = 1, availability_certainty = 0, applicability_rank = 0, current_probe_time = {rel_value = 0}}, result_suspend = {cctx = 0xffffffffffffffff, meta = 0x13cd32e8bfc, uri = 0x0}, result_stopped = {cctx = 0xffffffffffffffff, meta = 0x13cd32e8bfc, uri = 0x0}, resume = { message = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, is_paused = -751924228}, error = { message = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>}, ns = {ns = 0xffffffffffffffff, name = 0x13cd32e8bfc <Address 0x13cd32e8bfc out of bounds>, root = 0x0, meta = 0x1, id = {bits = {0, 0, 0, 0, 4156141568, 32767, 4158564850, 32767, 1, 32767, 0, 0, 6336704, 0, 4156149880, 32767}}}}}, unindex = {uc = 0x607a20, cctx = 0x60b580, filename = 0x0, size = 0, eta = {rel_value = 6339040}, duration = {rel_value = 35147}, completed = 18446744073709551615, specifics = {progress = {data = 0x13cd32e8bfc, offset = 0, data_len = 1, depth = 0}, resume = {message = 0x13cd32e8bfc <Address 0x13cd32e8bfc out of bounds>}, error = {message = 0x13cd32e8bfc <Address 0x13cd32e8bfc out of bounds>}}}}, status = GNUNET_FS_STATUS_PUBLISH_START} | |||
diff --git a/pathologist/src/struct_parse/test1.txt b/pathologist/src/struct_parse/test1.txt new file mode 100644 index 0000000..9ddec90 --- /dev/null +++ b/pathologist/src/struct_parse/test1.txt | |||
@@ -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...} | |||
diff --git a/pathologist/src/struct_parse/test2.txt b/pathologist/src/struct_parse/test2.txt new file mode 100644 index 0000000..e89ac3a --- /dev/null +++ b/pathologist/src/struct_parse/test2.txt | |||
@@ -0,0 +1 @@ | |||
{"buil\"e" , '" ueuil \"\'', "\\\\", 0 ,1560576,0, 1560100,0 , 1560100} | |||
diff --git a/pathologist/src/struct_parse/test3.txt b/pathologist/src/struct_parse/test3.txt new file mode 100644 index 0000000..39bbf8c --- /dev/null +++ b/pathologist/src/struct_parse/test3.txt | |||
@@ -0,0 +1 @@ | |||
{data = 0x0,offset=0, data_len= 140737349529600 ,depth =4158564850 } | |||
diff --git a/pathologist/src/struct_parse/test4.txt b/pathologist/src/struct_parse/test4.txt new file mode 100644 index 0000000..3735a52 --- /dev/null +++ b/pathologist/src/struct_parse/test4.txt | |||
@@ -0,0 +1 @@ | |||
{0 <repeats 9274 times>, -136430974, 32767, 0 <repeats 18 times>, 1560576, 0,1560100, 0, 1560100, 0, 0} | |||