190 return 1; |
193 return 1; |
191 } |
194 } |
192 |
195 |
193 int ucx_map_load(UcxMap *map, FILE *f) { |
196 int ucx_map_load(UcxMap *map, FILE *f) { |
194 |
197 |
195 char c; int r, n; |
198 int c; int r, n; |
196 |
199 |
197 char *key, *value; |
200 char *key, *value; |
198 |
201 |
199 while ((c = (char) fgetc(f)) > 0) { |
202 while ((c = fgetc(f)) > 0) { |
200 /* Discard leading spaces and comments */ |
203 /* Discard leading spaces and comments */ |
201 if (c == ' ') continue; |
204 if (c < 33) continue; |
202 if (c == '#' || c == '!') { |
205 if (c == '#' || c == '!') { |
203 while ((c = (char) fgetc(f)) > 0) { |
206 while ((c = (char) fgetc(f)) > 0) { |
204 if (c == '\n') break; |
207 if (c == '\n') break; |
205 } |
208 } |
206 continue; |
209 continue; |
216 n *= 2; |
219 n *= 2; |
217 key = realloc(key, n); |
220 key = realloc(key, n); |
218 } |
221 } |
219 key[r] = c; |
222 key[r] = c; |
220 r++; |
223 r++; |
221 } while ((c = (char) fgetc(f)) > 0); |
224 } while ((c = fgetc(f)) > 0); |
222 if (c == 0) { |
225 if (c <= 0) { |
223 free(key); |
226 free(key); |
224 return 1; |
227 return 1; |
225 } |
228 } |
226 key[r] = 0; |
229 key[r] = 0; |
|
230 while (key[--r] == ' ') key[r] = 0; |
|
231 |
|
232 /* skip whitespaces */ |
|
233 while ((c = fgetc(f)) > 0) { |
|
234 if (c > 32) break; |
|
235 } |
|
236 if (c <= 0) { |
|
237 free(key); |
|
238 return 1; |
|
239 } |
227 |
240 |
228 /* read into value buffer */ |
241 /* read into value buffer */ |
229 n = 64; |
242 n = 64; |
230 value = malloc(n); |
243 value = malloc(n); |
231 r = 0; |
244 r = 0; |
232 while ((c = (char) fgetc(f)) > 0) { |
245 do { |
233 if (c == '\n') break; |
246 if (c == '\n') break; |
234 if (r > n - 1) { |
247 if (r > n - 2) { |
235 n *= 2; |
248 n *= 2; |
236 value = realloc(value, n); |
249 value = realloc(value, n); |
237 } |
250 } |
238 value[r] = c; |
251 value[r] = c; |
239 r++; |
252 r++; |
240 } |
253 } while ((c = fgetc(f)) > 0); |
241 value = realloc(value, r+1); |
|
242 value[r] = 0; |
254 value[r] = 0; |
|
255 while (value[--r] < 33) value[r] = 0; |
|
256 value = realloc(value, r+2); |
243 |
257 |
244 ucx_map_cstr_put(map, key, value); |
258 ucx_map_cstr_put(map, key, value); |
245 free(key); |
259 free(key); |
246 } |
260 } |
247 |
261 |