home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / h / vd2 / system / file.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  9.4 KB  |  324 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #ifndef f_VD2_SYSTEM_FILE_H
  27. #define f_VD2_SYSTEM_FILE_H
  28.  
  29. #ifdef _MSC_VER
  30.     #pragma once
  31. #endif
  32.  
  33. #include <limits.h>
  34. #include <stdarg.h>
  35. #include <vd2/system/vdtypes.h>
  36. #include <vd2/system/vdalloc.h>
  37. #include <vd2/system/vdstl.h>
  38. #include <vector>
  39.  
  40. #ifdef WIN32
  41.     typedef void *VDFileHandle;                // this needs to match wtypes.h definition for HANDLE
  42. #else
  43.     #error No operating system target declared??
  44. #endif
  45.  
  46. namespace nsVDFile {
  47.     enum eSeekMode {
  48.         kSeekStart=0, kSeekCur, kSeekEnd
  49.     };
  50.  
  51.     enum eFlags {
  52.         kRead            = 0x00000001,
  53.         kWrite            = 0x00000002,
  54.         kReadWrite        = kRead | kWrite,
  55.  
  56.         kDenyNone        = 0x00000000,
  57.         kDenyRead        = 0x00000010,
  58.         kDenyWrite        = 0x00000020,
  59.         kDenyAll        = kDenyRead | kDenyWrite,
  60.  
  61.         kOpenExisting        = 0x00000100,
  62.         kOpenAlways            = 0x00000200,
  63.         kCreateAlways        = 0x00000300,
  64.         kCreateNew            = 0x00000400,
  65.         kTruncateExisting    = 0x00000500,        // not particularly useful, really
  66.         kCreationMask        = 0x0000FF00,
  67.  
  68.         kSequential            = 0x00010000,
  69.         kRandomAccess        = 0x00020000,
  70.         kUnbuffered            = 0x00040000,        // much faster on Win32 thanks to the crappy cache, but possibly bad in Unix?
  71.         kWriteThrough        = 0x00080000,
  72.  
  73.         kAllFileFlags        = 0xFFFFFFFF
  74.     };
  75. };
  76.  
  77. class VDFile {
  78. protected:
  79.     VDFileHandle    mhFile;
  80.     vdautoptr2<wchar_t>    mpFilename;
  81.     sint64            mFilePosition;
  82.  
  83. private:
  84.     VDFile(const VDFile&);
  85.     const VDFile& operator=(const VDFile& f);
  86.  
  87. public:
  88.     VDFile() : mhFile(NULL) {}
  89.     VDFile(const char *pszFileName, uint32 flags = nsVDFile::kRead | nsVDFile::kDenyWrite | nsVDFile::kOpenExisting);
  90.     VDFile(const wchar_t *pwszFileName, uint32 flags = nsVDFile::kRead | nsVDFile::kDenyWrite | nsVDFile::kOpenExisting);
  91.     VDFile(VDFileHandle h);
  92.     ~VDFile();
  93.  
  94.     // The "NT" functions are non-throwing and return success/failure; the regular functions throw exceptions
  95.     // when something bad happens.
  96.  
  97.     void    open(const char *pszFileName, uint32 flags = nsVDFile::kRead | nsVDFile::kDenyWrite | nsVDFile::kOpenExisting);
  98.     void    open(const wchar_t *pwszFileName, uint32 flags = nsVDFile::kRead | nsVDFile::kDenyWrite | nsVDFile::kOpenExisting);
  99.  
  100.     bool    openNT(const wchar_t *pwszFileName, uint32 flags = nsVDFile::kRead | nsVDFile::kDenyWrite | nsVDFile::kOpenExisting);
  101.  
  102.     bool    closeNT();
  103.     void    close();
  104.     bool    truncateNT();
  105.     void    truncate();
  106.  
  107.     // extendValid() pushes the valid threshold of a file out, so that the system allocates
  108.     // space for a file without ensuring that it is cleared.  It is mainly useful for
  109.     // preallocating a file without waiting for the system to clear all of it.  The caveats:
  110.     //
  111.     // - only required on NTFS
  112.     // - requires Windows XP or Windows Server 2003
  113.     // - does not work on compressed or sparse files
  114.     //
  115.     // As such, it shouldn't normally be relied upon, and extendValidNT() should be the call
  116.     // of choice.
  117.     //
  118.     // enableExtendValid() must be called beforehand, as SeVolumeNamePrivilege must be
  119.     // enabled on the process before the file is opened!
  120.  
  121.     bool    extendValidNT(sint64 pos);
  122.     void    extendValid(sint64 pos);
  123.     static bool enableExtendValid();
  124.  
  125.     sint64    size();
  126.     void    read(void *buffer, long length);
  127.     long    readData(void *buffer, long length);
  128.     void    write(const void *buffer, long length);
  129.     long    writeData(const void *buffer, long length);
  130.     bool    seekNT(sint64 newPos, nsVDFile::eSeekMode mode = nsVDFile::kSeekStart);
  131.     void    seek(sint64 newPos, nsVDFile::eSeekMode mode = nsVDFile::kSeekStart);
  132.     bool    skipNT(sint64 delta);
  133.     void    skip(sint64 delta);
  134.     sint64    tell();
  135.  
  136.     bool    isOpen();
  137.     VDFileHandle    getRawHandle();
  138.  
  139.     const wchar_t *getFilenameForError() const { return mpFilename; }
  140.  
  141.     // unbuffered I/O requires aligned buffers ("unbuffers")
  142.     static void *AllocUnbuffer(size_t nBytes);
  143.     static void FreeUnbuffer(void *p);
  144.  
  145. protected:
  146.     bool    open_internal(const char *pszFilename, const wchar_t *pwszFilename, uint32 flags, bool throwOnError);
  147. };
  148.  
  149. ///////////////////////////////////////////////////////////////////////////
  150.  
  151. template<class T>
  152. class VDFileUnbufferAllocator {
  153. public:
  154.     typedef    size_t        size_type;
  155.     typedef    ptrdiff_t    difference_type;
  156.     typedef    T*            pointer;
  157.     typedef    const T*    const_pointer;
  158.     typedef    T&            reference;
  159.     typedef    const T&    const_reference;
  160.     typedef    T            value_type;
  161.  
  162.     template<class U> struct rebind { typedef VDFileUnbufferAllocator<U> other; };
  163.  
  164.     pointer            address(reference x) const            { return &x; }
  165.     const_pointer    address(const_reference x) const    { return &x; }
  166.  
  167.     pointer            allocate(size_type n, void *p = 0)    { return (pointer)VDFile::AllocUnbuffer(n * sizeof(T)); }
  168.     void            deallocate(pointer p, size_type n)    { VDFile::FreeUnbuffer(p); }
  169.     size_type        max_size() const throw()            { return MAX_INT; }
  170.  
  171.     void            construct(pointer p, const T& val)    { new((void *)p) T(val); }
  172.     void            destroy(pointer p)                    { ((T*)p)->~T(); }
  173.  
  174. #if defined(_MSC_VER) && _MSC_VER < 1300
  175.     char *            _Charalloc(size_type n)                { return (char *)allocate((n + sizeof(T) - 1) / sizeof(T)); }
  176. #endif
  177. };
  178.  
  179. ///////////////////////////////////////////////////////////////////////////
  180.  
  181. class IVDStream {
  182. public:
  183.     virtual const wchar_t *GetNameForError() = 0;
  184.     virtual sint64    Pos() = 0;
  185.     virtual void    Read(void *buffer, sint32 bytes) = 0;
  186.     virtual sint32    ReadData(void *buffer, sint32 bytes) = 0;
  187.     virtual void    Write(const void *buffer, sint32 bytes) = 0;
  188. };
  189.  
  190. class IVDRandomAccessStream : public IVDStream {
  191. public:
  192.     virtual sint64    Length() = 0;
  193.     virtual void    Seek(sint64 offset) = 0;
  194. };
  195.  
  196. class VDFileStream : public VDFile, public IVDRandomAccessStream {
  197. private:
  198.     VDFileStream(const VDFile&);
  199.     const VDFileStream& operator=(const VDFileStream& f);
  200.  
  201. public:
  202.     VDFileStream() {}
  203.     VDFileStream(const char *pszFileName, uint32 flags = nsVDFile::kRead | nsVDFile::kDenyWrite | nsVDFile::kOpenExisting)
  204.         : VDFile(pszFileName, flags) {}
  205.     VDFileStream(const wchar_t *pwszFileName, uint32 flags = nsVDFile::kRead | nsVDFile::kDenyWrite | nsVDFile::kOpenExisting)
  206.         : VDFile(pwszFileName, flags) {}
  207.     VDFileStream(VDFileHandle h) : VDFile(h) {}
  208.     ~VDFileStream();
  209.  
  210.     const wchar_t *GetNameForError();
  211.     sint64    Pos();
  212.     void    Read(void *buffer, sint32 bytes);
  213.     sint32    ReadData(void *buffer, sint32 bytes);
  214.     void    Write(const void *buffer, sint32 bytes);
  215.     sint64    Length();
  216.     void    Seek(sint64 offset);
  217. };
  218.  
  219. class VDMemoryStream : public IVDRandomAccessStream {
  220. public:
  221.     VDMemoryStream(const void *pSrc, uint32 len);
  222.  
  223.     const wchar_t *GetNameForError();
  224.     sint64    Pos();
  225.     void    Read(void *buffer, sint32 bytes);
  226.     sint32    ReadData(void *buffer, sint32 bytes);
  227.     void    Write(const void *buffer, sint32 bytes);
  228.     sint64    Length();
  229.     void    Seek(sint64 offset);
  230.  
  231. protected:
  232.     const char        *mpSrc;
  233.     const uint32    mLength;
  234.     uint32            mPos;
  235. };
  236.  
  237. class VDBufferedStream : public IVDRandomAccessStream {
  238. public:
  239.     VDBufferedStream(IVDRandomAccessStream *pSrc, uint32 bufferSize);
  240.     ~VDBufferedStream();
  241.  
  242.     const wchar_t *GetNameForError();
  243.     sint64    Pos();
  244.     void    Read(void *buffer, sint32 bytes);
  245.     sint32    ReadData(void *buffer, sint32 bytes);
  246.     void    Write(const void *buffer, sint32 bytes);
  247.  
  248.     sint64    Length();
  249.     void    Seek(sint64 offset);
  250.  
  251.     void    Skip(sint64 size);
  252.  
  253. protected:
  254.     IVDRandomAccessStream *mpSrc;
  255.     vdblock<char>    mBuffer;
  256.     sint64        mBasePosition;
  257.     uint32        mBufferOffset;
  258.     uint32        mBufferValidSize;
  259. };
  260.  
  261. class VDTextStream {
  262. public:
  263.     VDTextStream(IVDStream *pSrc);
  264.     ~VDTextStream();
  265.  
  266.     const char *GetNextLine();
  267.  
  268. protected:
  269.     IVDStream    *mpSrc;
  270.     uint32        mBufferPos;
  271.     uint32        mBufferLimit;
  272.     enum {
  273.         kFetchLine,
  274.         kEatNextIfCR,
  275.         kEatNextIfLF
  276.     } mState;
  277.  
  278.     enum {
  279.         kFileBufferSize = 4096
  280.     };
  281.  
  282.     vdfastvector<char>    mLineBuffer;
  283.     vdblock<char>        mFileBuffer;
  284. };
  285.  
  286. class VDTextInputFile {
  287. public:
  288.     VDTextInputFile(const wchar_t *filename, uint32 flags = nsVDFile::kOpenExisting);
  289.     ~VDTextInputFile();
  290.  
  291.     inline const char *GetNextLine() {
  292.         return mTextStream.GetNextLine();
  293.     }
  294.  
  295. protected:
  296.     VDFileStream    mFileStream;
  297.     VDTextStream    mTextStream;
  298. };
  299.  
  300. class VDTextOutputStream {
  301. public:
  302.     VDTextOutputStream(IVDStream *stream);
  303.     ~VDTextOutputStream();
  304.  
  305.     void Flush();
  306.  
  307.     void Write(const char *s, int len);
  308.     void PutLine();
  309.     void PutLine(const char *s);
  310.     void FormatLine(const char *format, ...);
  311.  
  312. protected:
  313.     void FormatLine2(const char *format, va_list val);
  314.     void PutData(const char *s, int len);
  315.  
  316.     enum { kBufSize = 4096 };
  317.  
  318.     int            mLevel;
  319.     IVDStream    *mpDst;
  320.     char        mBuf[kBufSize];
  321. };
  322.  
  323. #endif
  324.