ucx/map.c

changeset 43
02f38adea013
parent 42
ff3dd1ee7dee
child 44
46356d74e873
     1.1 --- a/ucx/map.c	Thu Oct 04 16:03:18 2012 +0200
     1.2 +++ b/ucx/map.c	Thu Oct 04 18:23:32 2012 +0200
     1.3 @@ -4,6 +4,9 @@
     1.4  
     1.5  #include <stdlib.h>
     1.6  #include <string.h>
     1.7 +#ifndef _WIN32
     1.8 +#include <unistd.h>
     1.9 +#endif /* not _WIN32 */
    1.10  
    1.11  #include "map.h"
    1.12  
    1.13 @@ -192,13 +195,13 @@
    1.14  
    1.15  int ucx_map_load(UcxMap *map, FILE *f) {
    1.16  
    1.17 -    char c; int r, n;
    1.18 +    int c; int r, n;
    1.19  
    1.20      char *key, *value;
    1.21  
    1.22 -    while ((c = (char) fgetc(f)) > 0) {
    1.23 +    while ((c = fgetc(f)) > 0) {
    1.24          /* Discard leading spaces and comments */
    1.25 -        if (c == ' ') continue;
    1.26 +        if (c < 33) continue;
    1.27          if (c == '#' || c == '!') {
    1.28              while ((c = (char) fgetc(f)) > 0) {
    1.29                  if (c == '\n') break;
    1.30 @@ -218,28 +221,39 @@
    1.31              }
    1.32              key[r] = c;
    1.33              r++;
    1.34 -        } while ((c = (char) fgetc(f)) > 0);
    1.35 -        if (c == 0) {
    1.36 +        } while ((c = fgetc(f)) > 0);
    1.37 +        if (c <= 0) {
    1.38              free(key);
    1.39              return 1;
    1.40          }
    1.41          key[r] = 0;
    1.42 +        while (key[--r] == ' ') key[r] = 0;
    1.43 +
    1.44 +        /* skip whitespaces */
    1.45 +        while ((c = fgetc(f)) > 0) {
    1.46 +            if (c > 32) break;
    1.47 +        }
    1.48 +        if (c <= 0) {
    1.49 +            free(key);
    1.50 +            return 1;
    1.51 +        }
    1.52  
    1.53          /* read into value buffer */
    1.54          n = 64;
    1.55          value = malloc(n);
    1.56          r = 0;
    1.57 -        while ((c = (char) fgetc(f)) > 0) {
    1.58 +        do {
    1.59              if (c == '\n') break;
    1.60 -            if (r > n - 1) {
    1.61 +            if (r > n - 2) {
    1.62                  n *= 2;
    1.63                  value = realloc(value, n);
    1.64              }
    1.65              value[r] = c;
    1.66              r++;
    1.67 -        }
    1.68 -        value = realloc(value, r+1);
    1.69 +        } while ((c = fgetc(f)) > 0);
    1.70          value[r] = 0;
    1.71 +        while (value[--r] < 33) value[r] = 0;
    1.72 +        value = realloc(value, r+2);
    1.73  
    1.74          ucx_map_cstr_put(map, key, value);
    1.75          free(key);

mercurial