MemFile.h

00001 // $Id: MemFile.h 21 2010-09-05 04:18:17Z cschwarz1 $
00002 
00003 #ifndef BASE_MEMFILE_H
00004 #define BASE_MEMFILE_H
00005 
00006 #include "File.h"
00007 
00008 namespace base {
00010 
00014     class PPBASE_EXPORT MemFile {
00015     public:
00017 
00020         MemFile(size_t limit = 1048576);
00021 
00023         ~MemFile();
00024 
00032         MemFile *clone() const;
00033 
00035 
00038         size_t getPos() const;
00039 
00041 
00045         const char *getSourceBuffer(size_t *rsize = NULL) const;
00046 
00048 
00051         File *getSourceFile() const;
00052 
00054 
00057         String *getSourceString() const;
00058 
00060 
00063         bool isInMemory() const;
00064 
00066 
00070         const char *loadIntoMemory(size_t *rsize = NULL);
00071 
00073 
00079         size_t read(void *buf, size_t count, bool exact = false);
00080 
00082 
00088         size_t read(String &buf, size_t count, bool exact = false);
00089 
00091 
00096         bool readln(String &rbuf, bool strip = false);
00097 
00099 
00102         File *saveToFile();
00103 
00105 
00108         void seek(size_t pos);
00109 
00111         void seekBOF();
00112 
00114         void seekEOF();
00115 
00117 
00120         void setLimit(size_t limit);
00121 
00123 
00126         void setSource(const MemFile *source);
00127 
00129 
00134         void setSourceBuffer(const char *buf, size_t bufsize, bool own = false);
00135 
00137 
00141         void setSourceFile(File *file, bool own = false);
00142 
00144 
00148         void setSourceFile(const String &name, int flags = File::ofReadOnly);
00149 
00151 
00155         void setSourceString(String *str, bool own = false);
00156 
00158 
00161         size_t size() const;
00162 
00164 
00167         void truncate(size_t len);
00168 
00170 
00176         size_t write(const void *buf, size_t count, bool exact = true);
00177 
00179 
00184         size_t write(const String &buf, bool exact = true);
00185 
00186     private:
00187         const char  *_buf;        
00188         bool         _buf_own;    
00189         File        *_file;       
00190         bool         _file_own;   
00191         size_t       _limit;      
00192         size_t       _pos;        
00193         size_t       _size;       
00194         String      *_str;        
00195         bool         _str_own;    
00196 
00197         DISALLOW_COPY_CONSTRUCTOR_AND_ASSIGNMENT(MemFile);
00198     };
00199 }
00200 
00201 #endif