# Visualization Library

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

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/BufferedStream.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 BufferedStream_INCLUDE_ONCE
    00033 #define BufferedStream_INCLUDE_ONCE
    00034
    00035 #include <vlCore/String.hpp>
    00036 #include <vlCore/VirtualFile.hpp>
    00037 #include <string>
    00038 #include <vector>
    00039
    00040 namespace vl
    00041 {
    00042 //-----------------------------------------------------------------------------
    00043 // BufferedStream
    00044 //-----------------------------------------------------------------------------
    00048   template<class Element_Type, int Chunk_Size>
    00049   class BufferedStream: public Object
    00050   {
    00051     VL_INSTRUMENT_CLASS(VL_GROUP(vl::BufferedStream<Element_Type, Chunk_Size>), Object)
    00052
    00053   public:
    00054     BufferedStream()
    00055     {
    00056       mSize = 0;
    00057       mPtr  = 0;
    00058       mBuffer.resize(Chunk_Size);
    00059       mIsEndOfFile = true;
    00060     }
    00061
    00062     void seek(long long pos)
    00063     {
    00064       mSize = 0;
    00065       mPtr  = 0;
    00066       if ( inputFile() )
    00067         inputFile()->seekSet(pos);
    00068     }
    00069
    00070     bool readToken(Element_Type* token)
    00071     {
    00072       if ( mUngetBuffer.size() )
    00073       {
    00074         *token = mUngetBuffer.back();
    00075         mUngetBuffer.pop_back();
    00076         return true;
    00077       }
    00078
    00079       if (bufferEmpty())
    00080       {
    00081         fillBuffer();
    00082       }
    00083
    00084       if (bufferEmpty())
    00085       {
    00086         mIsEndOfFile = true;
    00087         return false;
    00088       }
    00089       else
    00090       {
    00091         *token = mBuffer[mPtr];
    00092         mPtr++;
    00093         return true;
    00094       }
    00095     }
    00096
    00097     bool readTextChar(Element_Type& ch)
    00098     {
    00099       if (!readToken(&ch))
    00100         return false;
    00101
    00102       Element_Type ch2 = 0;
    00103       switch(ch)
    00104       {
    00105       case 10:
    00106         ch = '\n';
    00107         if (readToken(&ch2) && ch2 != 13)
    00108           ungetToken(ch2);
    00109         break;
    00110
    00111       case 13:
    00112         ch = '\n';
    00113         if (readToken(&ch2) && ch2 != 10)
    00114           ungetToken(ch2);
    00115         break;
    00116       }
    00117
    00118       return true;
    00119     }
    00120
    00121     void ungetToken(const Element_Type& token)
    00122     {
    00123       mUngetBuffer.push_back(token);
    00124     }
    00125
    00126     bool bufferEmpty()
    00127     {
    00128       return mPtr == mSize;
    00129     }
    00130
    00131     int fillBuffer()
    00132     {
    00133       if ( inputFile() )
    00134       {
    00135         if ( !inputFile()->isOpen() )
    00136           if(!inputFile()->open(OM_ReadOnly))
    00137             return 0;
    00138
    00139         mPtr = 0;
    00140         mSize = (int)inputFile()->read(&mBuffer[0], Chunk_Size);
    00141         return mSize;
    00142       }
    00143       else
    00144       {
    00145         return 0;
    00146       }
    00147     }
    00148
    00149     bool isEndOfFile() const { return mIsEndOfFile; }
    00150
    00151     void setInputFile(VirtualFile* file)
    00152     {
    00153       mInputFile = file;
    00154       mIsEndOfFile = false;
    00155     }
    00156
    00157     VirtualFile* inputFile() { return mInputFile.get(); }
    00158
    00159     const VirtualFile* inputFile() const { return mInputFile.get(); }
    00160
    00161   protected:
    00162     ref<VirtualFile> mInputFile;
    00163     std::vector<Element_Type> mUngetBuffer;
    00164     std::vector<Element_Type> mBuffer;
    00165     int mPtr;
    00166     int mSize;
    00167     bool mIsEndOfFile;
    00168   };
    00169 }
    00170
    00171 #endif

Visualization Library v2011.09.1162 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu Jan 26 2012 00:43:26.
Permission is granted to use this page to write and publish articles regarding Visualization Library.