00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00039 #ifndef BLOCXX_TEMPFILESTREAM_HPP_INCLUDE_GUARD_
00040 #define BLOCXX_TEMPFILESTREAM_HPP_INCLUDE_GUARD_
00041 #include "blocxx/BLOCXX_config.h"
00042 #include "blocxx/Types.hpp"
00043 #include "blocxx/String.hpp"
00044 #include "blocxx/AutoPtr.hpp"
00045 #include "blocxx/CommonFwd.hpp"
00046 #include "blocxx/File.hpp"
00047
00048 #if defined(BLOCXX_HAVE_STREAMBUF)
00049 #include <streambuf>
00050 #elif defined(BLOCXX_HAVE_STREAMBUF_H)
00051 #include <streambuf.h>
00052 #endif
00053
00054 #if defined(BLOCXX_HAVE_ISTREAM) && defined(BLOCXX_HAVE_OSTREAM)
00055 #include <istream>
00056 #include <ostream>
00057 #else
00058 #include <iostream>
00059 #endif
00060
00070 namespace BLOCXX_NAMESPACE
00071 {
00072
00073 class BLOCXX_COMMON_API TempFileBuffer : public std::streambuf
00074 {
00075 public:
00076 enum EKeepFileFlag
00077 {
00078 E_DONT_KEEP_FILE,
00079 E_KEEP_FILE
00080 };
00081
00091 TempFileBuffer(size_t bufSize, EKeepFileFlag keepflg=E_DONT_KEEP_FILE);
00104 TempFileBuffer(const String& dir, size_t bufSize, EKeepFileFlag keepflg=E_DONT_KEEP_FILE);
00108 ~TempFileBuffer();
00112 std::streamsize getSize();
00116 void rewind();
00125 void reset();
00134 String releaseFileAndReset();
00141 bool usingTempFile() const;
00142 protected:
00143
00144 int underflow();
00145
00146 std::streamsize xsputn(const char* s, std::streamsize n);
00147 virtual int overflow(int c);
00148
00149 void initBuffers();
00150 void initGetBuffer();
00151 void initPutBuffer();
00152 int buffer_to_device(const char* c, int n);
00153 int buffer_from_device(char* c, int n);
00154 private:
00155 size_t m_bufSize;
00156 char* m_buffer;
00157 File m_tempFile;
00158 std::streamsize m_readPos;
00159 std::streamsize m_writePos;
00160 bool m_isEOF;
00161 String m_dir;
00162 EKeepFileFlag m_keepFlag;
00163 String m_filePath;
00164
00165 int buffer_in();
00166 int buffer_out();
00167
00168 TempFileBuffer(const TempFileBuffer& arg);
00169 TempFileBuffer& operator=(const TempFileBuffer& arg);
00170 };
00171
00172
00179 class BLOCXX_COMMON_API TempFileStream : public std::iostream
00180 {
00181 public:
00195 TempFileStream(size_t bufSize = 4096, TempFileBuffer::EKeepFileFlag keepflg=TempFileBuffer::E_DONT_KEEP_FILE);
00210 TempFileStream(const String& dir, size_t bufSize = 4096,
00211 TempFileBuffer::EKeepFileFlag keepflg=TempFileBuffer::E_DONT_KEEP_FILE);
00216 std::streamsize getSize() { return m_buffer->getSize(); }
00220 void rewind();
00225 void reset();
00234 String releaseFileAndReset();
00241 bool usingTempFile() const;
00242 private:
00243
00244 #ifdef BLOCXX_WIN32
00245 #pragma warning (push)
00246 #pragma warning (disable: 4251)
00247 #endif
00248
00249 AutoPtr<TempFileBuffer> m_buffer;
00250
00251 #ifdef BLOCXX_WIN32
00252 #pragma warning (pop)
00253 #endif
00254
00255
00256 TempFileStream(const TempFileStream&);
00257 TempFileStream& operator=(const TempFileStream&);
00258 };
00259
00260 }
00261
00262 #endif