FileParser.h

Go to the documentation of this file.
00001 /*
00002  *  BEEN: Benchmarking Environment
00003  *  ==============================
00004  *
00005  *  File author: Branislav Repcek
00006  *
00007  *  GNU Lesser General Public License Version 2.1
00008  *  ---------------------------------------------
00009  *  Copyright (C) 2004-2006 Distributed Systems Research Group,
00010  *  Faculty of Mathematics and Physics, Charles University in Prague
00011  *
00012  *  This library is free software; you can redistribute it and/or
00013  *  modify it under the terms of the GNU Lesser General Public
00014  *  License version 2.1, as published by the Free Software Foundation.
00015  *
00016  *  This library is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *  Lesser General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU Lesser General Public
00022  *  License along with this library; if not, write to the Free Software
00023  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00024  *  MA  02111-1307  USA
00025  */
00026 
00034 #ifndef FILE_PARSER_INCLUDED
00035 #define FILE_PARSER_INCLUDED
00036 
00037 #include <vector>
00038 #include <utility>
00039 #include <sstream>
00040 #include <string>
00041 #include <ctype.h>
00042 #include "../common/UnicodeString.h"
00043 
00044 namespace hwdet
00045 {
00046     
00048     bool ReadLines(const char *file_name, std::vector< String > &lines);
00049     
00051     String GetFieldName(const String &line, size_t *ends_at = NULL,
00052                         const String &end = TXT(":"), const size_t start_at = 0);
00053     
00055     bool BeginsWith(const String &str, const String &what);
00056     
00075     template< typename T >
00076     bool GetFieldValue(const std::string &line, const size_t index, T &result, 
00077                        const size_t start_at = 0, const char *delims = "\t ")
00078     {
00079         size_t field_index = 0;
00080         bool in_field = false;
00081         size_t pos = start_at;
00082 
00083         for ( ; pos < line.size(); ++pos)
00084         {
00085             if (strchr(delims, line[pos]) == NULL)
00086             {
00087                 if (!in_field)
00088                 {
00089                     in_field = true;
00090                     ++field_index;
00091                     if (field_index == index)
00092                     {
00093                         break;
00094                     }
00095                     if (field_index > index)
00096                     {
00097                         return false;
00098                     }
00099                 }
00100             }
00101             else
00102             {
00103                 in_field = false;
00104             }
00105         }
00106 
00107         if (field_index == index)
00108         {
00109             std::stringstream stream;
00110 
00111             for ( ; (pos < line.size()) && (!isspace(line[pos])); ++pos)
00112             {
00113                 stream << line[pos];
00114             }
00115 
00116             stream >> result;
00117             return !stream.fail();
00118         }
00119 
00120         return false;
00121     }
00122 }
00123 
00124 #endif

Generated on Tue Dec 19 17:43:51 2006 for Detector for Linux by  doxygen 1.4.7