Go to the documentation of this file.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
00027
00028
00029
00030
00031
00032 #ifndef TextStream_INCLUDE_ONCE
00033 #define TextStream_INCLUDE_ONCE
00034
00035 #include <vlCore/BufferedStream.hpp>
00036
00037 namespace vl
00038 {
00039
00040
00041
00042
00046 class VLCORE_EXPORT TextStream: public BufferedStream<unsigned char, 128*1024>
00047 {
00048 VL_INSTRUMENT_CLASS(vl::TextStream, VL_GROUP(BufferedStream<unsigned char, 128*1024>))
00049
00050 public:
00051 TextStream(VirtualFile* file=NULL)
00052 {
00053 setInputFile(file);
00054 }
00055 ~TextStream()
00056 {
00057 if(inputFile())
00058 inputFile()->close();
00059 }
00060
00061 void ungetLine(const std::string& utf8)
00062 {
00063 ungetToken('\n');
00064 for(size_t i=utf8.size(); i--;)
00065 ungetToken(utf8[i]);
00066 }
00067 bool readLine(std::string& utf8)
00068 {
00069 utf8.clear();
00070 if ( !inputFile()->isOpen() )
00071 if(!inputFile()->open(OM_ReadOnly))
00072 return false;
00073
00074 unsigned char ch = 0;
00075 bool ok = false;
00076 while( readToken(&ch) )
00077 {
00078 ok = true;
00079 if ( ch == '\r' || ch == '\n' )
00080 {
00081 readToken(&ch);
00082 if ( ch != '\r' && ch != '\n' )
00083 ungetToken(ch);
00084 break;
00085 }
00086 else
00087 utf8 += ch;
00088 }
00089 return ok;
00090 }
00091
00093 bool readLine(String& line)
00094 {
00095 line.clear();
00096 std::vector<unsigned char> utf8;
00097
00098 if ( !inputFile()->isOpen() )
00099 if(!inputFile()->open(OM_ReadOnly))
00100 return false;
00101
00102 unsigned char ch = 0;
00103 bool ok = false;
00104 while( readToken(&ch) )
00105 {
00106 ok = true;
00107 if ( ch == '\r' || ch == '\n' )
00108 {
00109 readToken(&ch);
00110 if ( ch != '\r' && ch != '\n' )
00111 ungetToken(ch);
00112 break;
00113 }
00114 else
00115 utf8.push_back(ch);
00116 }
00117 if(!utf8.empty())
00118 line = String::fromUTF8((char*)&utf8[0], (int)utf8.size());
00119 return ok;
00120 }
00121
00123 bool readLineCR(String& line)
00124 {
00125 line.clear();
00126 std::vector<unsigned char> utf8;
00127
00128 if ( !inputFile()->isOpen() )
00129 if(!inputFile()->open(OM_ReadOnly))
00130 return false;
00131
00132 unsigned char ch = 0;
00133 while( readToken(&ch) )
00134 {
00135 if ( ch == '\r' )
00136 break;
00137 else
00138 utf8.push_back(ch);
00139 }
00140 if(!utf8.empty())
00141 line = String::fromUTF8((char*)&utf8[0], (int)utf8.size());
00142 return !line.empty();
00143 }
00144
00146 bool readLineLF(String& line)
00147 {
00148 line.clear();
00149 std::vector<unsigned char> utf8;
00150
00151 if ( !inputFile()->isOpen() )
00152 if(!inputFile()->open(OM_ReadOnly))
00153 return false;
00154
00155 unsigned char ch = 0;
00156 while( readToken(&ch) )
00157 {
00158 if ( ch == '\n' )
00159 break;
00160 else
00161 utf8.push_back(ch);
00162 }
00163 if(!utf8.empty())
00164 line = String::fromUTF8((char*)&utf8[0], (int)utf8.size());
00165 return !line.empty();
00166 }
00167
00168 bool readInt(int& i, bool hex=false);
00169
00170 bool readDouble(double& d);
00171
00172 bool readString(String& token)
00173 {
00174 token.clear();
00175 unsigned char ch = 0;
00176 while ( readToken(&ch) )
00177 {
00178 if ( ch == '\r' || ch == '\n' || ch == '\t' || ch == ' ' )
00179 {
00180 if ( token.empty() )
00181 continue;
00182 else
00183 return true;
00184 }
00185 else
00186 token += ch;
00187 }
00188 return !token.empty();
00189 }
00190
00191 bool readStdString(std::string& token)
00192 {
00193 token.clear();
00194 unsigned char ch = 0;
00195 while ( readToken(&ch) )
00196 {
00197 if ( ch == '\r' || ch == '\n' || ch == '\t' || ch == ' ' )
00198 {
00199 if ( token.empty() )
00200 continue;
00201 else
00202 return true;
00203 }
00204 else
00205 token += ch;
00206 }
00207 return !token.empty();
00208 }
00209
00210 bool readQuotedString(String& token)
00211 {
00212 bool open = false;
00213 token.clear();
00214 unsigned char ch = 0;
00215 while ( readToken(&ch) )
00216 {
00217
00218 if ( ch == '\r' || ch == '\n' || (!open && ( ch == '\t' || ch == ' ')) )
00219 {
00220 if ( token.empty() )
00221 continue;
00222 else
00223 return true;
00224 }
00225 else
00226 token += ch;
00227
00228 if (ch == '\"' || ch == '\'')
00229 open = !open;
00230 }
00231 return !token.empty();
00232 }
00233
00234 protected:
00235 String mTmpStr;
00236 std::string mTmpStdStr;
00237 };
00238
00239 }
00240
00241 #endif