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 
00036 #ifndef OW_TMPFILE_HPP_INCLUDE_GUARD_
00037 #define OW_TMPFILE_HPP_INCLUDE_GUARD_
00038 #include "OW_config.h"
00039 #include "OW_IntrusiveReference.hpp"
00040 #include "OW_IntrusiveCountableBase.hpp"
00041 #include "OW_String.hpp"
00042 #ifdef OW_HAVE_UNISTD_H
00043 #include <unistd.h>
00044 #endif
00045 #ifdef OW_WIN32
00046 #include <io.h>
00047 #endif
00048 
00049 namespace OW_NAMESPACE
00050 {
00051 
00052 class OW_COMMON_API TmpFileImpl : public IntrusiveCountableBase
00053 {
00054 public:
00055    TmpFileImpl();
00056    TmpFileImpl(String const& filename);
00057    ~TmpFileImpl();
00058    size_t read(void* bfr, size_t numberOfBytes, long offset=-1L);
00059    size_t write(const void* bfr, size_t numberOfBytes, long offset=-1L);
00060 
00061 #ifdef OW_WIN32
00062    int seek(long offset, int whence=SEEK_SET);
00063    long tell();
00064    void rewind();
00065    int flush();
00066 #else
00067    int seek(long offset, int whence=SEEK_SET)
00068       { return ::lseek(m_hdl, offset, whence); }
00069    long tell() { return ::lseek(m_hdl, 0, SEEK_CUR); }
00070    void rewind() { ::lseek(m_hdl, 0, SEEK_SET); }
00071    int flush() { return 0; }
00072 #endif
00073 
00074    void newFile() { open(); }
00075    long getSize();
00076    String releaseFile();
00077 private:
00078    void open();
00079    int close();
00080    TmpFileImpl(const TmpFileImpl& arg);   
00081    TmpFileImpl& operator= (const TmpFileImpl& arg);   
00082    char* m_filename;
00083 #ifndef OW_WIN32
00084    int m_hdl;
00085 #else
00086    HANDLE m_hdl;
00087 #endif
00088 };
00089 
00090 class OW_COMMON_API TmpFile
00091 {
00092 public:
00093    TmpFile() :
00094       m_impl(new TmpFileImpl) {  }
00095    TmpFile(String const& filename)
00096       : m_impl(new TmpFileImpl(filename))
00097    {}
00098    TmpFile(const TmpFile& arg) :
00099       m_impl(arg.m_impl) {}
00100    TmpFile& operator= (const TmpFile& arg)
00101    {
00102       m_impl = arg.m_impl;
00103       return *this;
00104    }
00105    ~TmpFile()  {  }
00106    size_t read(void* bfr, size_t numberOfBytes, long offset=-1L)
00107    {
00108       return m_impl->read(bfr, numberOfBytes, offset);
00109    }
00110    size_t write(const void* bfr, size_t numberOfBytes, long offset=-1L)
00111    {
00112       return m_impl->write(bfr, numberOfBytes, offset);
00113    }
00114    int seek(long offset, int whence=SEEK_SET)
00115       { return m_impl->seek(offset, whence); }
00116    long tell() { return m_impl->tell(); }
00117    void rewind() { m_impl->rewind(); }
00118    int flush() { return m_impl->flush(); }
00119    void newFile() { m_impl->newFile(); }
00120    long getSize() { return m_impl->getSize(); }
00121    String releaseFile() { return m_impl->releaseFile(); }
00122 private:
00123 
00124 #ifdef OW_WIN32
00125 #pragma warning (push)
00126 #pragma warning (disable: 4251)
00127 #endif
00128 
00129    IntrusiveReference<TmpFileImpl> m_impl;
00130 
00131 #ifdef OW_WIN32
00132 #pragma warning (pop)
00133 #endif
00134 
00135 };
00136 
00137 } 
00138 
00139 #endif