openModeller  Version 1.5.0
my_file_parser.cpp
Go to the documentation of this file.
1 
27 #include <file_parser.hh>
28 #include <string.h>
29 #include <stdio.h>
30 
31 #if 0
32 // Need an ostream inserter for the special icstring type.
33 inline std::ostream& operator<<( std::ostream& strm, const FileParser::icstring& s )
34 {
35  return strm << std::string(s.data(),s.length() );
36 }
37 #endif
38 
39 /****************************************************************/
40 /**************************** File Parser ***********************/
41 
42 static char error[256];
43 
44 /******************/
45 /*** constructor ***/
46 
47 FileParser::FileParser( std::string const file )
48 {
49  if ( ! load( file ) )
50  {
51  sprintf( error, "File '%s' was not found.\n", file.c_str() );
52  fprintf( stderr, "%s", error );
53  throw error;
54  }
55 }
56 
57 
58 /******************/
59 /*** destructor ***/
60 
62 {
63 }
64 
65 /************/
66 /*** load ***/
67 int
68 FileParser::load( std::string const file )
69 {
70  FILE *fd = fopen( file.c_str(), "r" );
71  if ( ! fd )
72  return 0;
73 
74  f_lst.clear();
75  f_seclst.clear();
76 
77  char *sec = NULL;
78  SectionList::iterator secit;
79 
80  const int size = 1024;
81  char line[size];
82 
83  while ( fgets( line, size, fd ) ) {
84  // Find the first # which indicates the start of comment.
85  char *sep = strchr( line,'#' );
86  // If it's at the beginning of the line
87  // loop.
88  if ( sep == line )
89  continue;
90 
91  // If it's not at the beginning of the line,
92  // assign it to "null", terminating the string,
93  // and effectively commenting to end of line.
94  if ( sep )
95  *sep = '\0';
96 
97  // Find the start of the key.
98  // Trim the whitespace from the front of the line.
99  char *start_key = line;
100  while ( isspace( *start_key ) && (*start_key != '\0') ) {
101  start_key++;
102  }
103 
104  // Nothing but whitespace. Loop.
105  if ( *start_key == '\0' )
106  continue;
107 
108  // Are we inside a section [section_name]?
109  char *sec_ini = strchr( line, '[' );
110  char *sec_end = strchr( line, ']' );
111  if ( sec_ini && sec_end ) {
112 
113  sec = sec_ini+1;
114  // Left trim the whitespace.
115  while ( isspace( *sec ) && ( *sec != '\0' ) ) {
116  sec++;
117  }
118  // Right trim the value.
119  char *tmp = sec + strlen( sec ) - 1;
120  while ( isspace( *tmp ) || ( *tmp != ']' ) )
121  *tmp-- = '\0';
122  ItemList lst;
123  f_seclst.push_back( std::make_pair( sec, lst ) );
124  secit = f_seclst.end() -1;
125  continue;
126  }
127 
128  // Separate key and value
129  sep = strchr( line, '=' );
130  if ( sep ) {
131  // Find the start of the value.
132  char *start_val = sep+1;
133  // Left trim the whitespace.
134  while ( isspace( *start_val ) && ( *start_val != '\0' ) ) {
135  start_val++;
136  }
137  // No value? loop.
138  if ( *start_val == '\0' )
139  continue;
140 
141  // Null terminate the key.
142  *sep-- = '\0';
143  // and right trim it.
144  while( isspace( *sep ) )
145  *sep-- = '\0';
146 
147  // Right trim the value.
148  sep = start_val + strlen( start_val ) - 1;
149  // Remember the \0 we substitued for the '='?
150  // use that to terminate the loop.
151  while ( isspace( *sep ) && ( *sep != '\0' ) )
152  *sep-- = '\0';
153 
154  if ( *sep == '\0' )
155  continue;
156 
157  if ( sec != NULL ) {
158 
159  (*secit).second.push_back( std::make_pair( start_key, start_val ) );
160  }
161  else {
162 
163  f_lst.push_back( std::make_pair( start_key, start_val ) );
164  }
165  }
166  }
167 
168  fclose( fd );
169  return 1;
170 }
171 
172 
173 /************/
174 /*** _get ***/
175 std::string
176 FileParser::_get( std::string const key, ItemList lst ) const
177 {
178  ItemList::const_iterator it = lst.begin();
179 
180  while ( it != lst.end() ) {
181 
182  if ( it->first == key.c_str() ) {
183 
184  return it->second;
185  }
186 
187  ++it;
188  }
189 
190  return "";
191 }
192 
193 
194 /***********/
195 /*** get ***/
196 std::string
197 FileParser::get( std::string const key ) const
198 {
199  return _get( key, f_lst );
200 }
201 
202 
203 /***********/
204 /*** get ***/
205 std::string
206 FileParser::get( std::string const section, std::string const key ) const
207 {
208  SectionList::const_iterator it = f_seclst.begin();
209 
210  while ( it != f_seclst.end() ) {
211 
212  if ( it->first == section.c_str() ) {
213 
214  return _get( key, it->second );
215  }
216 
217  ++it;
218  }
219 
220  return "";
221 }
222 
223 
224 /**************/
225 /*** _count ***/
226 int
227 FileParser::_count( std::string const key, ItemList lst ) const
228 {
229  int n = 0;
230 
231  ItemList::const_iterator it = lst.begin();
232 
233  while ( it != lst.end() ) {
234 
235  if ( it->first == key.c_str() ) {
236 
237  ++n;
238  }
239 
240  ++it;
241  }
242 
243  return n;
244 }
245 
246 
247 /*************/
248 /*** count ***/
249 int
250 FileParser::count( std::string const key ) const
251 {
252  return _count( key, f_lst );
253 }
254 
255 
256 /*************/
257 /*** count ***/
258 int
259 FileParser::count( std::string const section, std::string const key ) const
260 {
261  SectionList::const_iterator it = f_seclst.begin();
262 
263  while ( it != f_seclst.end() ) {
264 
265  if ( it->first == section.c_str() ) {
266 
267  return _count( key, it->second );
268  }
269 
270  ++it;
271  }
272 
273  return 0;
274 }
275 
276 
277 /****************/
278 /*** _get All ***/
279 std::vector<std::string>
280 FileParser::_getAll( std::string const key, ItemList lst ) const
281 {
282  std::vector<std::string> values;
283  ItemList::const_iterator it = lst.begin();
284 
285  while ( it != lst.end() ) {
286 
287  if ( it->first == key.c_str() ) {
288 
289  values.push_back( it->second );
290  }
291 
292  ++it;
293  }
294  return values;
295 }
296 
297 
298 /***************/
299 /*** get All ***/
300 std::vector<std::string>
301 FileParser::getAll( std::string const key ) const
302 {
303  return _getAll( key, f_lst );
304 }
305 
306 
307 /***************/
308 /*** get All ***/
309 std::vector<std::string>
310 FileParser::getAll( std::string const section, std::string const key ) const
311 {
312  SectionList::const_iterator it = f_seclst.begin();
313 
314  while ( it != f_seclst.end() ) {
315 
316  if ( it->first == section.c_str() ) {
317 
318  return _getAll( key, it->second );
319  }
320 
321  ++it;
322  }
323 
324  std::vector<std::string> values;
325  return values;
326 }
int count(const std::string &key) const
Definition: FileParser.cpp:165
FileParser(const std::string &file)
Definition: FileParser.cpp:47
int load(const std::string &file)
Definition: FileParser.cpp:68
ItemList f_lst
Definition: FileParser.hh:80
ostream & operator<<(ostream &os, const Sample &value)
Definition: Sample.cpp:484
SectionList f_seclst
static char error[256]
std::vector< std::string > getAll(const std::string &key) const
Definition: FileParser.cpp:185
int _count(std::string const key, ItemList lst) const
std::string _get(std::string const key, ItemList lst) const
std::vector< std::string > _getAll(std::string const key, ItemList lst) const
std::string get(const std::string &key) const
Definition: FileParser.cpp:145
std::vector< Item > ItemList
Definition: FileParser.hh:78