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