blocxx

TempFileStream.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
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    // for input
00144    int underflow();
00145    // for output
00146    std::streamsize xsputn(const char* s, std::streamsize n);
00147    virtual int overflow(int c);
00148    //virtual int sync();
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    // prohibit copying and assigning
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    // disallow copying and assigning
00256    TempFileStream(const TempFileStream&);
00257    TempFileStream& operator=(const TempFileStream&);
00258 };
00259 
00260 } // end namespace BLOCXX_NAMESPACE
00261 
00262 #endif