-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.c
206 lines (183 loc) · 5.32 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "parser.h"
#include "menu.h"
/**
* Taken from http://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way
* @param str
* @return
*/
static char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while (isspace(*str)) str++;
if (*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) end--;
// Write new null terminator
*(end + 1) = 0;
return str;
}
TypGraphe* parse(FILE* f)
{
char buf[PARSER_LINE_BUFFER_SIZE];
char* read;
size_t line = 0;
long int sommet, sommetParent, poids;
bool orienter;
TypGraphe* res = NULL;
errorcode err;
if (f)
{
//read #
line += 1;
read = fgets(buf, sizeof (buf), f);
if (read)
{
read = trimwhitespace(buf);
if (!read || *read != '#')
{
printf("erreur parsage line %u; '%s' attendu, on a : '%s'", line,
"#", read ? read : "EOF");
return NULL;
}
}
else
{
printf("erreur parsage line %u; EOF", line);
return NULL;
}
//read nombre maximum de sommets
line += 1;
read = fgets(buf, sizeof (buf), f);
if (read)
{
read = trimwhitespace(buf);
sommet = strtol(read, NULL, 0);
if (!sommet)
{
printf("erreur parsage line %u; '%s' attendu, on a : '%s'", line,
"CHIFFRE", read ? read : "EOF");
return NULL;
}
}
else
{
printf("erreur parsage line %u; EOF", line);
return NULL;
}
//read #
line += 1;
read = fgets(buf, sizeof (buf), f);
if (read)
{
read = trimwhitespace(buf);
if (!read || *read != '#')
{
printf("erreur parsage line %u; '%s' attendu, on a : '%s'", line,
"#", read ? read : "EOF");
return NULL;
}
}
else
{
printf("erreur parsage line %u; EOF", line);
return NULL;
}
//read 'o' ou 'n'
line += 1;
read = fgets(buf, sizeof (buf), f);
if (read)
{
read = trimwhitespace(buf);
if (!read || !(*read == 'o' || *read == 'n'))
{
printf("erreur parsage line %u; '%s' attendu, on a : '%s'", line,
"o|n", read ? read : "EOF");
return NULL;
}
orienter = *read == 'o';
}
else
{
printf("erreur parsage line %u; EOF", line);
return NULL;
}
res = newTypGraphe(sommet, orienter);
if (!res)
{
return NULL;
}
//read #
line += 1;
read = fgets(buf, sizeof (buf), f);
if (read)
{
read = trimwhitespace(buf);
if (!read || *read != '#')
{
printf("erreur parsage line %u; '%s' attendu, on a : '%s'", line,
"#", read ? read : "EOF");
return NULL;
}
}
else
{
printf("erreur parsage line %u; EOF", line);
return NULL;
}
//maintenant on parse les sommets et les arretes
do
{
line += 1;
read = fgets(buf, sizeof (buf), f);
if (read)
{
read = trimwhitespace(buf);
char* token = strtok(read, ":");
read = trimwhitespace(token);
char *endptr;
sommetParent = strtol(read, &endptr, 0);
if (read == endptr)
{
printf("erreur parsage line %u; '%s' attendu, on a : '%s'", line,
"CHIFFRE", read ? read : "EOF");
return NULL;
}
err = insertionSommetTypGraphe(res, sommetParent);
if (err < 0 && err != -2)
{
handle_error_code(err);
return NULL;
}
token = strtok(NULL, ","); //les arretes
while (token)
{
int scanfres = sscanf(token, " ( %u / %u ) ", &sommet, &poids);
if (scanfres != 2)
{
printf("erreur parsage line %u; une definition d'arrete est attendue", line);
}
if (!checkSommetExist(res, sommet))
{
//crée automatiquement le sommet
insertionSommetTypGraphe(res, sommet);
}
err = insertionAreteTypGraphe(res, sommetParent, sommet, poids);
if (err < 0)
{
handle_error_code(err);
return NULL;
}
token = strtok(NULL, ",");
}
}
}
while (read);
}
return res;
}