source: rtems-tools/tester/covoar/ConfigFile.cc @ 0c4884a

Last change on this file since 0c4884a was fcef37b, checked in by Joel Sherrill <joel@…>, on 04/06/21 at 20:24:08

covoar: Remove training white spaces

  • Property mode set to 100644
File size: 3.6 KB
Line 
1
2/*! @file ConfigFile.cc
3 *  @brief ConfigFile Implementation
4 *
5 *  This file contains the implementation of the FileReader class.
6 */
7
8#include "ConfigFile.h"
9#include <string.h>
10#include <stdio.h>
11#include <ctype.h>
12
13namespace Configuration {
14
15  FileReader::FileReader(
16    Options_t *options
17  )
18  {
19    options_m = options;
20  }
21
22  FileReader::~FileReader()
23  {
24  }
25
26  bool FileReader::processFile(
27    const char* const     file
28  )
29  {
30    #define METHOD "FileReader::processFile - "
31    FILE *  in;
32    char    line[256];
33    char    option[256];
34    char    value[256];
35    int     line_no;
36    int     i;
37    int     j;
38
39    if ( file == NULL ) {
40      fprintf( stderr, METHOD "NULL filename\n" );
41      return false;
42    }
43
44    in = fopen( file, "r" );
45    if ( !in ) {
46      fprintf( stderr, METHOD "unable to open %s\n", file );
47      return false;
48    }
49
50    line_no = 0;
51    while (fgets(line, sizeof(line), in) != NULL) {
52      int length;
53
54      line_no++;
55
56      length = (int) strlen( line );
57      if ( line[length - 1] != '\n' ) {
58        fprintf(
59          stderr,
60          "%s: line %d is too long",
61          file,
62          line_no
63        );
64        continue;
65      }
66
67      line[length - 1] = '\0';
68      length--;
69
70      /*
71       *  Strip off comments at end of line
72       *
73       *      LHS = RHS   # comment
74       */
75      for (i=0 ; i<length ; i++ ) {
76        if ( line[i] == '#' ) {
77          line[i] = '\0';
78          length = i;
79          break;
80        }
81      }
82
83      /*
84       *  Strip off trailing white space
85       */
86      for (i=length-1 ; i>=0 && isspace(line[i]) ; i-- )
87        ;
88
89      line[i+1] = '\0';
90      length = i+1;
91
92      /* Ignore empty lines.  We have stripped
93       * all comments and blanks therefore, only
94       * an empty string needs to be checked.
95       */
96      if (line[0] == '\0')
97        continue;
98
99      if (sscanf(line, "%s", option) != 1) {
100        fprintf(
101          stderr,
102          "%s: line %d is invalid: %s\n",
103          file,
104          line_no,
105          line
106        );
107        continue;
108      }
109
110      for (i=0; ((line[i] != '=') && (i<length)); i++)
111        ;
112
113      if (i == length) {
114        fprintf(
115          stderr,
116          "%s: line %d is invalid: %s\n",
117          file,
118          line_no,
119          line
120        );
121        continue;
122      }
123
124      i++;
125      value[0] = '\0';
126      while ( isspace(line[i]) )
127        i++;
128      for (j=0; line[i] != '\0'; i++, j++ )
129        value[j] = line[i];
130      value[j] = '\0';
131      if (value[0] == '\0') {
132        fprintf(
133          stderr,
134          "%s: line %d is invalid: %s\n",
135          file,
136          line_no,
137          line
138        );
139        continue;
140      }
141
142      if ( !setOption(option, value) ) {
143        fprintf(
144          stderr,
145          "%s: line %d: option %s is unknown\n",
146          file,
147          line_no,
148          option
149        );
150        continue;
151      }
152
153    }
154
155    return false;
156  }
157
158  bool FileReader::setOption(
159    const char* const option,
160    const char* const value
161  )
162  {
163    Options_t *o;
164
165    for ( o=options_m ; o->option ; o++ ) {
166      if ( !strcmp( o->option, option ) ) {
167        o->value = strdup( value );
168        return true;
169      }
170    }
171    return false;
172  }
173
174  const char *FileReader::getOption(
175    const char* const option
176  )
177  {
178    Options_t *o;
179
180    for ( o=options_m ; o->option ; o++ ) {
181      if ( !strcmp( o->option, option ) ) {
182        return o->value;
183      }
184    }
185    return NULL;
186  }
187
188  void FileReader::printOptions(void)
189  {
190    Options_t *o;
191
192    for ( o=options_m ; o->option ; o++ ) {
193      fprintf( stderr, "(%s)=(%s)\n", o->option, o->value );
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.