Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/TextStream.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
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 // TextStream
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         // cannot be a multiline quote
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

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:31.
Permission is granted to use this page to write and publish articles regarding Visualization Library.