pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_TextFile.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_TEXTFILE_H
00040 #define PLONK_TEXTFILE_H
00041 
00042 #include "../core/plonk_CoreForwardDeclarations.h"
00043 #include "plonk_FilesForwardDeclarations.h"
00044 #include "../core/plonk_SmartPointer.h"
00045 #include "../core/plonk_WeakPointer.h"
00046 #include "../core/plonk_SmartPointerContainer.h"
00047 #include "../containers/plonk_Text.h"
00048 
00049 
00050 class TextFileInternal : public SmartPointer
00051 {
00052 public:
00053     TextFileInternal() throw();
00054     TextFileInternal (Text const& path, 
00055                       const bool writable = false, 
00056                       const bool clearContents = false) throw();
00057     TextFileInternal (FilePathArray const& fileArray, const int multiMode) throw();
00058     TextFileInternal (TextFileQueue const& fileQueue) throw();
00059 
00060     ~TextFileInternal();
00061     
00062     LongLong getPosition() const throw();
00063     void setPosition (const LongLong position) throw();
00064     
00065     void setEof() throw();
00066     bool isEof() const throw();
00067     bool canRead() const throw();
00068     bool canWrite() const throw();
00069 
00070     Text readLine (const int maximumLength = 1024) throw();
00071     Text readAll() throw();
00072     Text read (const int numBytes) throw();
00073     
00074         void writeValue (const char value) throw();
00075         void writeValue (const short value) throw();
00076     void writeValue (const int value) throw();
00077         void writeValue (const long value) throw();
00078     void writeValue (const LongLong value) throw();
00079     void writeValue (const unsigned char value) throw();
00080     void writeValue (const unsigned short value) throw();
00081     void writeValue (const unsigned int value) throw();
00082         void writeValue (const unsigned long value) throw();
00083     void writeValue (const UnsignedLongLong value) throw();
00084         void writeValue (const float value) throw();
00085         void writeValue (const double value) throw();
00086         void write (const char* text) throw();
00087         void writeLine (const char* text) throw();
00088     
00089     void disownPeer (PlankFileRef otherFile) throw();
00090 
00091 private:
00092     inline PlankFileRef getPeerRef() { return static_cast<PlankFileRef> (&peer); }
00093     inline const PlankFileRef getPeerRef() const { return const_cast<const PlankFileRef> (&peer); }
00094     
00095     PlankFile peer;
00096 };
00097 
00101 class TextFile : public SmartPointerContainer<TextFileInternal>
00102 {
00103 public:
00104     typedef TextFileInternal                        Internal;
00105     typedef SmartPointerContainer<Internal>         Base;
00106     typedef WeakPointerContainer<TextFile>          Weak;
00107     
00108     enum MultiFileTypes
00109     {
00110         MultiFileUnknown = PLANKMULITFILE_MODE_UNKNOWN,
00111         MultiFileArraySequenceOnce = PLANKMULITFILE_MODE_ARRAYSEQUENCEONCE,
00112         MultiFileArraySequenceLoop = PLANKMULITFILE_MODE_ARRAYSEQUENCELOOP,
00113         MultiFileArrayRandom = PLANKMULITFILE_MODE_ARRAYRANDOM,
00114         MultiFileArrayRandomNoRepeat = PLANKMULITFILE_MODE_ARRAYRANDOMNOREPEAT,
00115         MultiFileArrayIndexRef = PLANKMULITFILE_MODE_ARRAYINDEXREF,
00116         MultiFileQueue = PLANKMULITFILE_MODE_QUEUE,
00117         MultiFileCustom = PLANKMULITFILE_MODE_CUSTOM
00118     };
00119     
00122     TextFile() throw()
00123     :   Base (new Internal())
00124     {
00125     }
00126     
00129         TextFile (Text const& path) throw()
00130         :       Base (new Internal (path))
00131         {
00132         }
00133     
00142     TextFile (Text const& path, const bool writable, const bool clearContents = false) throw()
00143         :       Base (new Internal (path, writable, clearContents))
00144         {
00145         }    
00146     
00148     TextFile (const char* path) throw()
00149         :       Base (new Internal (path))
00150         {
00151         }
00152     
00161     TextFile (const char* path, const bool writable, const bool clearContents = false) throw()
00162         :       Base (new Internal (path, writable, clearContents))
00163         {
00164         }
00165     
00168     TextFile (FilePathArray const& fileArray, const int multiMode) throw()
00169     :   Base (new Internal (fileArray, multiMode))
00170     {
00171     }
00172     
00175     TextFile (TextFileQueue const& fileQueue) throw()
00176     :   Base (new Internal (fileQueue))
00177     {
00178     }
00179         
00181     explicit TextFile (Internal* internalToUse) throw() 
00182         :       Base (internalToUse)
00183         {
00184         }    
00185     
00188     TextFile (TextFile const& copy) throw()
00189     :   Base (static_cast<Base const&> (copy))
00190     {
00191     }
00192     
00194     TextFile& operator= (TextFile const& other) throw()
00195         {
00196                 if (this != &other)
00197             this->setInternal (other.getInternal());//this->setInternal (other.containerCopy().getInternal());
00198         
00199         return *this;
00200         }
00201         
00205     static TextFile fromWeak (Weak const& weak) throw()
00206     {
00207         return weak.fromWeak();
00208     }    
00209     
00212         Text readLine (const int maximumLength = 1024) throw()
00213         {
00214         return getInternal()->readLine (maximumLength);
00215         }
00216         
00217         Text readAll() throw()
00218         {
00219                 return getInternal()->readAll();
00220         }
00221     
00222     Text read (const int numBytes) throw()
00223         {
00224                 return getInternal()->read (numBytes);
00225         }
00226         
00228     LongLong getPosition() const throw()
00229     {
00230         return getInternal()->getPosition();
00231     }
00232     
00235     void setPosition (const LongLong position) throw()
00236     {
00237         getInternal()->setPosition (position);
00238     }
00239 
00241     void setEof() throw()
00242     {
00243         getInternal()->setEof();
00244     }    
00245     
00247         bool isEof() const throw()
00248         {
00249                 return getInternal()->isEof();
00250         }
00251     
00255     template<class ValueType>
00256     void writeValue (const ValueType value) throw()
00257     {
00258         getInternal()->writeValue (value);
00259     }
00260         
00262         void write (const char* text) throw()
00263     {
00264         getInternal()->write (text);
00265     }
00266     
00268     void write (Text const& text) throw()
00269     {
00270         getInternal()->write (text.getArray());
00271     }    
00272     
00274         void writeLine (const char* text) throw()
00275     {
00276         getInternal()->writeLine (text);
00277     }
00278     
00280     void writeLine (Text const& text) throw()
00281     {
00282         getInternal()->writeLine (text.getArray());
00283     }    
00284     
00285     void disownPeer (PlankFileRef otherFile) throw()
00286     {
00287         getInternal()->disownPeer (otherFile);
00288     }
00289     
00290     PLONK_OBJECTARROWOPERATOR(TextFile);
00291 
00292 };
00293 
00294 
00295 
00296 #endif // PLONK_TEXTFILE_H
 All Classes Functions Typedefs Enumerations Enumerator Properties