openModeller  Version 1.5.0
FileParser.cpp
Go to the documentation of this file.
1 
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( const std::string & 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( const std::string & file )
69 {
70  FILE *fd = fopen( file.c_str(), "r" );
71  if ( ! fd )
72  return 0;
73 
74  f_lst.clear();
75 
76  const int size = 1024;
77  char line[size];
78 
79  while ( fgets( line, size, fd ) ) {
80  // Find the first # which indicates the start of comment.
81  char *sep = strchr( line,'#' );
82  // If it's at the beginning of the line
83  // loop.
84  if ( sep == line )
85  continue;
86 
87  // If it's not at the beginning of the line,
88  // assign it to "null", terminating the string,
89  // and effectively commenting to end of line.
90  if ( sep )
91  *sep = '\0';
92 
93  // Find the start of the key.
94  // Trim the whitespace from the front of the line.
95  char *start_key = line;
96  while ( isspace( *start_key ) && (*start_key != '\0') ) {
97  start_key++;
98  }
99 
100  // Nothing but whitespace. Loop.
101  if ( *start_key == '\0' )
102  continue;
103 
104  // Separate key and value
105  sep = strchr( line, '=' );
106  if ( sep ) {
107  // Find the start of the value.
108  char *start_val = sep+1;
109  // Left trim the whitespace.
110  while ( isspace( *start_val ) && ( *start_val != '\0' ) ) {
111  start_val++;
112  }
113  // No value? loop.
114  if ( *start_val == '\0' )
115  continue;
116 
117  // Null terminate the key.
118  *sep-- = '\0';
119  // and right trim it.
120  while( isspace( *sep ) )
121  *sep-- = '\0';
122 
123  // Right trim the value.
124  sep = start_val + strlen( start_val ) - 1;
125  // Remember the \0 we substitued for the '='?
126  // use that to terminate the loop.
127  while ( isspace( *sep ) && ( *sep != '\0' ) )
128  *sep-- = '\0';
129 
130  if ( *sep == '\0' )
131  continue;
132 
133  f_lst.push_back( std::make_pair( start_key, start_val ) );
134  }
135  }
136 
137  fclose( fd );
138  return 1;
139 }
140 
141 
142 /***********/
143 /*** get ***/
144 std::string
145 FileParser::get( const std::string & key ) const
146 {
147  ItemList::const_iterator it = f_lst.begin();
148 
149  while ( it != f_lst.end() ) {
150 
151  if ( (*it).first == key.c_str() ) {
152 
153  return it->second;
154  }
155 
156  ++it;
157  }
158  return "";
159 }
160 
161 
162 /*************/
163 /*** count ***/
164 int
165 FileParser::count( const std::string & key ) const
166 {
167  int n = 0;
168 
169  ItemList::const_iterator it = f_lst.begin();
170 
171  while ( it != f_lst.end() ) {
172 
173  if ( it->first == key.c_str() ) {
174 
175  ++n;
176  }
177  ++it;
178  }
179  return n;
180 }
181 
182 /***************/
183 /*** get All ***/
184 std::vector<std::string>
185 FileParser::getAll( const std::string & key ) const
186 {
187  std::vector<std::string> values;
188  ItemList::const_iterator it = f_lst.begin();
189 
190  while ( it != f_lst.end() ) {
191 
192  if ( it->first == key.c_str() ) {
193 
194  values.push_back( it->second );
195  }
196 
197  ++it;
198  }
199  return values;
200 }
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
std::vector< std::string > getAll(const std::string &key) const
Definition: FileParser.cpp:185
std::string get(const std::string &key) const
Definition: FileParser.cpp:145
static char error[256]
Definition: FileParser.cpp:42