00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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