Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
TextStream.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef TextStream_INCLUDE_ONCE
33 #define TextStream_INCLUDE_ONCE
34 
36 
37 namespace vl
38 {
39 
40 //-----------------------------------------------------------------------------
41 // TextStream
42 //-----------------------------------------------------------------------------
46  class VLCORE_EXPORT TextStream: public BufferedStream<unsigned char, 128*1024>
47  {
49 
50  public:
52  {
53  setInputFile(file);
54  }
56  {
57  if(inputFile())
58  inputFile()->close();
59  }
60 
61  void ungetLine(const std::string& utf8)
62  {
63  ungetToken('\n');
64  for(size_t i=utf8.size(); i--;)
65  ungetToken(utf8[i]);
66  }
67  bool readLine(std::string& utf8)
68  {
69  utf8.clear();
70  if ( !inputFile()->isOpen() )
71  if(!inputFile()->open(OM_ReadOnly))
72  return false;
73 
74  unsigned char ch = 0;
75  bool ok = false;
76  while( readToken(&ch) )
77  {
78  ok = true;
79  if ( ch == '\r' || ch == '\n' )
80  {
81  readToken(&ch);
82  if ( ch != '\r' && ch != '\n' )
83  ungetToken(ch);
84  break;
85  }
86  else
87  utf8 += ch;
88  }
89  return ok;
90  }
91 
93  bool readLine(String& line)
94  {
95  line.clear();
96  std::vector<unsigned char> utf8;
97 
98  if ( !inputFile()->isOpen() )
99  if(!inputFile()->open(OM_ReadOnly))
100  return false;
101 
102  unsigned char ch = 0;
103  bool ok = false;
104  while( readToken(&ch) )
105  {
106  ok = true;
107  if ( ch == '\r' || ch == '\n' )
108  {
109  readToken(&ch);
110  if ( ch != '\r' && ch != '\n' )
111  ungetToken(ch);
112  break;
113  }
114  else
115  utf8.push_back(ch);
116  }
117  if(!utf8.empty())
118  line = String::fromUTF8((char*)&utf8[0], (int)utf8.size());
119  return ok;
120  }
121 
123  bool readLineCR(String& line)
124  {
125  line.clear();
126  std::vector<unsigned char> utf8;
127 
128  if ( !inputFile()->isOpen() )
129  if(!inputFile()->open(OM_ReadOnly))
130  return false;
131 
132  unsigned char ch = 0;
133  while( readToken(&ch) )
134  {
135  if ( ch == '\r' )
136  break;
137  else
138  utf8.push_back(ch);
139  }
140  if(!utf8.empty())
141  line = String::fromUTF8((char*)&utf8[0], (int)utf8.size());
142  return !line.empty();
143  }
144 
146  bool readLineLF(String& line)
147  {
148  line.clear();
149  std::vector<unsigned char> utf8;
150 
151  if ( !inputFile()->isOpen() )
152  if(!inputFile()->open(OM_ReadOnly))
153  return false;
154 
155  unsigned char ch = 0;
156  while( readToken(&ch) )
157  {
158  if ( ch == '\n' )
159  break;
160  else
161  utf8.push_back(ch);
162  }
163  if(!utf8.empty())
164  line = String::fromUTF8((char*)&utf8[0], (int)utf8.size());
165  return !line.empty();
166  }
167 
168  bool readInt(int& i, bool hex=false);
169 
170  bool readDouble(double& d);
171 
172  bool readString(String& token)
173  {
174  token.clear();
175  unsigned char ch = 0;
176  while ( readToken(&ch) )
177  {
178  if ( ch == '\r' || ch == '\n' || ch == '\t' || ch == ' ' )
179  {
180  if ( token.empty() )
181  continue;
182  else
183  return true;
184  }
185  else
186  token += ch;
187  }
188  return !token.empty();
189  }
190 
191  bool readStdString(std::string& token)
192  {
193  token.clear();
194  unsigned char ch = 0;
195  while ( readToken(&ch) )
196  {
197  if ( ch == '\r' || ch == '\n' || ch == '\t' || ch == ' ' )
198  {
199  if ( token.empty() )
200  continue;
201  else
202  return true;
203  }
204  else
205  token += ch;
206  }
207  return !token.empty();
208  }
209 
211  {
212  bool open = false;
213  token.clear();
214  unsigned char ch = 0;
215  while ( readToken(&ch) )
216  {
217  // cannot be a multiline quote
218  if ( ch == '\r' || ch == '\n' || (!open && ( ch == '\t' || ch == ' ')) )
219  {
220  if ( token.empty() )
221  continue;
222  else
223  return true;
224  }
225  else
226  token += ch;
227 
228  if (ch == '\"' || ch == '\'')
229  open = !open;
230  }
231  return !token.empty();
232  }
233 
234  protected:
236  std::string mTmpStdStr;
237  };
238 //-----------------------------------------------------------------------------
239 }
240 
241 #endif
An abstract class representing a file.
Definition: VirtualFile.hpp:60
bool readLine(std::string &utf8)
Definition: TextStream.hpp:67
#define VL_GROUP(...)
Definition: TypeInfo.hpp:77
static String fromUTF8(const char *str, int byte_count=-1)
Accepts strings with and without UTF8 signature.
Definition: String.cpp:1000
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
The BufferedStream class is a template class that that performs a buffered read of Element_Type data ...
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Visualization Library main namespace.
The TextStream class can be used to conveniently read or parse utf8-encoded text files.
Definition: TextStream.hpp:46
bool readLineCR(String &line)
Reads a CR terminated line.
Definition: TextStream.hpp:123
bool readLine(String &line)
Reads a CR or LF or CR/LF or LF/CR terminated line.
Definition: TextStream.hpp:93
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
#define NULL
Definition: OpenGLDefs.hpp:81
TextStream(VirtualFile *file=NULL)
Definition: TextStream.hpp:51
std::string mTmpStdStr
Definition: TextStream.hpp:236
bool readStdString(std::string &token)
Definition: TextStream.hpp:191
void ungetLine(const std::string &utf8)
Definition: TextStream.hpp:61
bool readString(String &token)
Definition: TextStream.hpp:172
String & clear()
Clears the string.
Definition: String.hpp:142
bool readLineLF(String &line)
Reads a LF terminated line.
Definition: TextStream.hpp:146
bool readQuotedString(String &token)
Definition: TextStream.hpp:210