27 #include <file_parser.hh>
33 inline std::ostream&
operator<<( std::ostream& strm,
const FileParser::icstring& s )
35 return strm << std::string(s.data(),s.length() );
51 sprintf( error,
"File '%s' was not found.\n", file.c_str() );
52 fprintf( stderr,
"%s", error );
70 FILE *fd = fopen( file.c_str(),
"r" );
78 SectionList::iterator secit;
80 const int size = 1024;
83 while ( fgets( line, size, fd ) ) {
85 char *sep = strchr( line,
'#' );
99 char *start_key = line;
100 while ( isspace( *start_key ) && (*start_key !=
'\0') ) {
105 if ( *start_key ==
'\0' )
109 char *sec_ini = strchr( line,
'[' );
110 char *sec_end = strchr( line,
']' );
111 if ( sec_ini && sec_end ) {
115 while ( isspace( *sec ) && ( *sec !=
'\0' ) ) {
119 char *tmp = sec + strlen( sec ) - 1;
120 while ( isspace( *tmp ) || ( *tmp !=
']' ) )
123 f_seclst.push_back( std::make_pair( sec, lst ) );
129 sep = strchr( line,
'=' );
132 char *start_val = sep+1;
134 while ( isspace( *start_val ) && ( *start_val !=
'\0' ) ) {
138 if ( *start_val ==
'\0' )
144 while( isspace( *sep ) )
148 sep = start_val + strlen( start_val ) - 1;
151 while ( isspace( *sep ) && ( *sep !=
'\0' ) )
159 (*secit).second.push_back( std::make_pair( start_key, start_val ) );
163 f_lst.push_back( std::make_pair( start_key, start_val ) );
178 ItemList::const_iterator it = lst.begin();
180 while ( it != lst.end() ) {
182 if ( it->first == key.c_str() ) {
208 SectionList::const_iterator it =
f_seclst.begin();
212 if ( it->first == section.c_str() ) {
214 return _get( key, it->second );
231 ItemList::const_iterator it = lst.begin();
233 while ( it != lst.end() ) {
235 if ( it->first == key.c_str() ) {
261 SectionList::const_iterator it =
f_seclst.begin();
265 if ( it->first == section.c_str() ) {
267 return _count( key, it->second );
279 std::vector<std::string>
282 std::vector<std::string> values;
283 ItemList::const_iterator it = lst.begin();
285 while ( it != lst.end() ) {
287 if ( it->first == key.c_str() ) {
289 values.push_back( it->second );
300 std::vector<std::string>
309 std::vector<std::string>
312 SectionList::const_iterator it =
f_seclst.begin();
316 if ( it->first == section.c_str() ) {
318 return _getAll( key, it->second );
324 std::vector<std::string> values;
int count(const std::string &key) const
FileParser(const std::string &file)
int load(const std::string &file)
ostream & operator<<(ostream &os, const Sample &value)
std::vector< std::string > getAll(const std::string &key) const
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
std::vector< Item > ItemList