TmpFile.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 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 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #ifndef BLOCXX_TMPFILE_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_TMPFILE_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/IntrusiveReference.hpp"
00042 #include "blocxx/IntrusiveCountableBase.hpp"
00043 #include "blocxx/String.hpp"
00044 #ifdef BLOCXX_HAVE_UNISTD_H
00045 #include <unistd.h>
00046 #endif
00047 #ifdef BLOCXX_WIN32
00048 #include <io.h>
00049 #endif
00050 
00051 namespace BLOCXX_NAMESPACE
00052 {
00053 
00054 class BLOCXX_COMMON_API TmpFileImpl : public IntrusiveCountableBase
00055 {
00056 public:
00057    TmpFileImpl();
00058    TmpFileImpl(String const& filename);
00059    ~TmpFileImpl();
00060    size_t read(void* bfr, size_t numberOfBytes, long offset=-1L);
00061    size_t write(const void* bfr, size_t numberOfBytes, long offset=-1L);
00062 
00063 #ifdef BLOCXX_WIN32
00064    int seek(long offset, int whence=SEEK_SET);
00065    long tell();
00066    void rewind();
00067    int flush();
00068 #else
00069    int seek(long offset, int whence=SEEK_SET)
00070       { return ::lseek(m_hdl, offset, whence); }
00071    long tell() { return ::lseek(m_hdl, 0, SEEK_CUR); }
00072    void rewind() { ::lseek(m_hdl, 0, SEEK_SET); }
00073    int flush() { return 0; }
00074 #endif
00075 
00076    void newFile() { open(); }
00077    long getSize();
00078    String releaseFile();
00079 private:
00080    void open();
00081    int close();
00082    TmpFileImpl(const TmpFileImpl& arg);   // Not implemented
00083    TmpFileImpl& operator= (const TmpFileImpl& arg);   // Not implemented
00084    char* m_filename;
00085 #ifndef BLOCXX_WIN32
00086    int m_hdl;
00087 #else
00088    HANDLE m_hdl;
00089 #endif
00090 };
00091 
00092 class BLOCXX_COMMON_API TmpFile
00093 {
00094 public:
00095    TmpFile() :
00096       m_impl(new TmpFileImpl) {  }
00097    TmpFile(String const& filename)
00098       : m_impl(new TmpFileImpl(filename))
00099    {}
00100    TmpFile(const TmpFile& arg) :
00101       m_impl(arg.m_impl) {}
00102    TmpFile& operator= (const TmpFile& arg)
00103    {
00104       m_impl = arg.m_impl;
00105       return *this;
00106    }
00107    ~TmpFile()  {  }
00108    size_t read(void* bfr, size_t numberOfBytes, long offset=-1L)
00109    {
00110       return m_impl->read(bfr, numberOfBytes, offset);
00111    }
00112    size_t write(const void* bfr, size_t numberOfBytes, long offset=-1L)
00113    {
00114       return m_impl->write(bfr, numberOfBytes, offset);
00115    }
00116    int seek(long offset, int whence=SEEK_SET)
00117       { return m_impl->seek(offset, whence); }
00118    long tell() { return m_impl->tell(); }
00119    void rewind() { m_impl->rewind(); }
00120    int flush() { return m_impl->flush(); }
00121    void newFile() { m_impl->newFile(); }
00122    long getSize() { return m_impl->getSize(); }
00123    String releaseFile() { return m_impl->releaseFile(); }
00124 private:
00125 
00126 #ifdef BLOCXX_WIN32
00127 #pragma warning (push)
00128 #pragma warning (disable: 4251)
00129 #endif
00130 
00131    IntrusiveReference<TmpFileImpl> m_impl;
00132 
00133 #ifdef BLOCXX_WIN32
00134 #pragma warning (pop)
00135 #endif
00136 
00137 };
00138 
00139 } // end namespace BLOCXX_NAMESPACE
00140 
00141 #endif

Generated on Fri Jun 16 15:39:09 2006 for blocxx by  doxygen 1.4.6