summaryrefslogtreecommitdiffstats
path: root/tester/covoar/ConfigFile.cc
blob: c16b64a54b06075eec1ec8785d873779b9e769d8 (plain) (blame)
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

/*! @file ConfigFile.cc
 *  @brief ConfigFile Implementation
 *
 *  This file contains the implementation of the FileReader class.
 */

#include "ConfigFile.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>

namespace Configuration {

  FileReader::FileReader(
    Options_t *options
  )
  {
    options_m = options;
  }

  FileReader::~FileReader()
  {
  }

  bool FileReader::processFile(
    const char* const     file
  )
  {
    #define METHOD "FileReader::processFile - "
    FILE *  in;
    char    line[256];
    char    option[256];
    char    value[256];
    int     line_no;
    int     i;
    int     j;

    if ( file == NULL ) {
      fprintf( stderr, METHOD "NULL filename\n" );
      return false;
    }

    in = fopen( file, "r" );
    if ( !in ) {
      fprintf( stderr, METHOD "unable to open %s\n", file );
      return false;
    }

    line_no = 0;
    while (fgets(line, sizeof(line), in) != NULL) {
      int length;

      line_no++;

      length = (int) strlen( line );
      if ( line[length - 1] != '\n' ) {
        fprintf(
          stderr,
          "%s: line %d is too long",
          file,
          line_no
        );
        continue;
      }

      line[length - 1] = '\0';
      length--;

      /*
       *  Strip off comments at end of line
       *
       *      LHS = RHS   # comment
       */
      for (i=0 ; i<length ; i++ ) {
        if ( line[i] == '#' ) {
          line[i] = '\0';
          length = i;
          break;
        }
      }

      /*
       *  Strip off trailing white space
       */
      for (i=length-1 ; i>=0 && isspace(line[i]) ; i-- )
        ;

      line[i+1] = '\0';
      length = i+1;

      /* Ignore empty lines.  We have stripped
       * all comments and blanks therefore, only
       * an empty string needs to be checked.
       */
      if (line[0] == '\0')
        continue;

      if (sscanf(line, "%s", option) != 1) {
        fprintf(
          stderr,
          "%s: line %d is invalid: %s\n",
          file,
          line_no,
          line
        );
        continue;
      }

      for (i=0; ((line[i] != '=') && (i<length)); i++)
        ;

      if (i == length) {
        fprintf(
          stderr,
          "%s: line %d is invalid: %s\n",
          file,
          line_no,
          line
        );
        continue;
      }

      i++;
      value[0] = '\0';
      while ( isspace(line[i]) )
        i++;
      for (j=0; line[i] != '\0'; i++, j++ )
        value[j] = line[i];
      value[j] = '\0';
      if (value[0] == '\0') {
        fprintf(
          stderr,
          "%s: line %d is invalid: %s\n",
          file,
          line_no,
          line
        );
        continue;
      }

      if ( !setOption(option, value) ) {
        fprintf(
          stderr,
          "%s: line %d: option %s is unknown\n",
          file,
          line_no,
          option
        );
        continue;
      }

    }

    return false;
  }

  bool FileReader::setOption(
    const char* const option,
    const char* const value
  )
  {
    Options_t *o;

    for ( o=options_m ; o->option ; o++ ) {
      if ( !strcmp( o->option, option ) ) {
        o->value = strdup( value );
        return true;
      }
    }
    return false;
  }

  const char *FileReader::getOption(
    const char* const option
  )
  {
    Options_t *o;

    for ( o=options_m ; o->option ; o++ ) {
      if ( !strcmp( o->option, option ) ) {
        return o->value;
      }
    }
    return NULL;
  }

  void FileReader::printOptions(void)
  {
    Options_t *o;

    for ( o=options_m ; o->option ; o++ ) {
      fprintf( stderr, "(%s)=(%s)\n", o->option, o->value );
    }
  }
}