now reads/writes 3-partition files
[asdcplib.git] / FileIO.h
1 /*
2 Copyright (c) 2003-2005, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    FileIO.h
28     \version $Id$
29     \brief   Cross-platform, simple file accessors
30 */
31
32
33 #ifndef _FILEIO_H_
34 #define _FILEIO_H_
35
36 #include <AS_DCP_system.h>
37 #include <sys/stat.h>
38
39 #ifdef WIN32
40 #include <io.h>
41 #else
42 #include <unistd.h>
43 #include <sys/types.h>
44 #endif
45
46 namespace ASDCP {
47 #ifdef WIN32
48   typedef __int64  fsize_t;
49   typedef __int64  fpos_t;
50
51   enum SeekPos_t {
52     SP_BEGIN = FILE_BEGIN,
53     SP_POS   = FILE_CURRENT,
54     SP_END   = FILE_END
55   };
56 #else
57   typedef off_t    fsize_t;
58   typedef off_t    fpos_t;
59   typedef int      HANDLE;
60   const HANDLE INVALID_HANDLE_VALUE = -1L;
61
62   enum SeekPos_t {
63     SP_BEGIN = SEEK_SET,
64     SP_POS   = SEEK_CUR,
65     SP_END   = SEEK_END
66   };
67 #endif
68
69   bool    PathIsFile(const char* pathname);
70   bool    PathIsDirectory(const char* pathname);
71   fsize_t FileSize(const char* pathname);
72
73   //
74   class FileReader
75     {
76       ASDCP_NO_COPY_CONSTRUCT(FileReader);
77
78     protected:
79       std::string m_Filename;
80       HANDLE      m_Handle;
81
82     public:
83       FileReader() : m_Handle(INVALID_HANDLE_VALUE) {}
84       virtual ~FileReader() { Close(); }
85
86       Result_t OpenRead(const char*) const;                          // open the file for reading
87       Result_t Close() const;                                        // close the file
88       fsize_t  Size() const;                                         // returns the file's current size
89       Result_t Seek(ASDCP::fpos_t = 0, SeekPos_t = SP_BEGIN) const;  // move the file pointer
90       Result_t Tell(ASDCP::fpos_t* pos) const;                       // report the file pointer's location
91       Result_t Read(byte_t*, ui32_t, ui32_t* = 0) const;             // read a buffer of data
92
93       inline ASDCP::fpos_t Tell() const                              // report the file pointer's location
94         {
95           ASDCP::fpos_t tmp_pos;
96           Tell(&tmp_pos);
97           return tmp_pos;
98         }
99
100       inline bool IsOpen() {                                         // returns true if the file is open
101         return (m_Handle != INVALID_HANDLE_VALUE);
102       }
103     };
104
105
106   //
107   class FileWriter : public FileReader
108     {
109       class h__iovec;
110       mem_ptr<h__iovec>  m_IOVec;
111       ASDCP_NO_COPY_CONSTRUCT(FileWriter);
112
113     public:
114       FileWriter();
115       virtual ~FileWriter();
116
117       Result_t OpenWrite(const char*);                               // open a new file, overwrites existing
118       Result_t OpenModify(const char*);                              // open a file for read/write
119
120       // this part of the interface takes advantage of the iovec structure on
121       // platforms that support it. For each call to Writev(const byte_t*, ui32_t, ui32_t*),
122       // the given buffer is added to an internal iovec struct. All items on the list
123       // are written to disk by a call to Writev();
124       Result_t Writev(const byte_t*, ui32_t);                       // queue buffer for write
125       Result_t Writev(ui32_t* = 0);                                 // write all queued buffers
126
127       // if you call this while there are unwritten items on the iovec list,
128       // the iovec list will be written to disk before the givn buffer,as though
129       // you had called Writev() first.
130       Result_t Write(const byte_t*, ui32_t, ui32_t* = 0);            // write buffer to disk
131
132
133    };
134
135 } // namespace ASDCP
136
137
138 #endif // _FILEREADER_H_
139
140
141 //
142 // end FileReader.h
143 //