ginormo merge-back with Kumu, SMPTE MIC key and MPEG parser fix
[asdcplib.git] / src / KM_fileio.h
1 /*
2 Copyright (c) 2004-2006, 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    KM_fileio.h
28     \version $Id$
29     \brief   portable file i/o
30   */
31
32 #ifndef _KM_FILEIO_H_
33 #define _KM_FILEIO_H_
34
35 #include <KM_util.h>
36 #include <string>
37
38 #ifdef KM_WIN32
39 # include <io.h>
40 #else
41 # include <dirent.h>
42 # include <unistd.h>
43 # include <time.h>
44 # include <sys/types.h>
45 #endif
46
47 #include <sys/stat.h>
48
49
50
51 namespace Kumu
52 {
53 #ifdef KM_WIN32
54   //
55   class DirScanner
56     {
57     public:
58       __int64               m_Handle;
59       struct _finddatai64_t m_FileInfo;
60
61       DirScanner()  {};
62       ~DirScanner() { Close(); }
63       Result_t Open(const char*);
64       Result_t Close();
65       Result_t GetNext(char*);
66     };
67 #else // KM_WIN32
68   // POSIX directory scanner
69   //
70   class DirScanner
71     {
72     public:
73       DIR*       m_Handle;
74
75       DirScanner() : m_Handle(NULL) {}
76       ~DirScanner() { Close(); }
77       
78       Result_t  Open(const char*);
79       Result_t  Close();
80       Result_t  GetNext(char*);
81     };
82 #endif // KM_WIN32
83
84 #ifdef KM_WIN32
85   typedef __int64  fsize_t;
86   typedef __int64  fpos_t;
87
88   enum SeekPos_t {
89     SP_BEGIN = FILE_BEGIN,
90     SP_POS   = FILE_CURRENT,
91     SP_END   = FILE_END
92   };
93 #else
94   typedef off_t    fsize_t;
95   typedef off_t    fpos_t;
96   typedef int      HANDLE;
97   const HANDLE INVALID_HANDLE_VALUE = -1L;
98
99   enum SeekPos_t {
100     SP_BEGIN = SEEK_SET,
101     SP_POS   = SEEK_CUR,
102     SP_END   = SEEK_END
103   };
104 #endif
105
106   const ui32_t Kilobyte = 1024;
107   const ui32_t Megabyte = Kilobyte * Kilobyte;
108   const ui32_t Gigabyte = Megabyte * Kilobyte;
109
110   const ui32_t MaxFilePath = Kilobyte;
111
112   bool     PathIsFile(const char* pathname);
113   bool     PathIsDirectory(const char* pathname);
114   fsize_t  FileSize(const char* pathname);
115
116   // reads an entire file into a string
117   Result_t ReadFileIntoString(const char* filename, std::string& outString, ui32_t max_size = 256 * Kilobyte);
118
119   //
120   class FileReader
121     {
122       KM_NO_COPY_CONSTRUCT(FileReader);
123
124     protected:
125       std::string m_Filename;
126       HANDLE      m_Handle;
127
128     public:
129       FileReader() : m_Handle(INVALID_HANDLE_VALUE) {}
130       virtual ~FileReader() { Close(); }
131
132       Result_t OpenRead(const char*) const;                          // open the file for reading
133       Result_t Close() const;                                        // close the file
134       fsize_t  Size() const;                                         // returns the file's current size
135       Result_t Seek(Kumu::fpos_t = 0, SeekPos_t = SP_BEGIN) const;   // move the file pointer
136       Result_t Tell(Kumu::fpos_t* pos) const;                        // report the file pointer's location
137       Result_t Read(byte_t*, ui32_t, ui32_t* = 0) const;             // read a buffer of data
138
139       inline Kumu::fpos_t Tell() const                               // report the file pointer's location
140         {
141           Kumu::fpos_t tmp_pos;
142           Tell(&tmp_pos);
143           return tmp_pos;
144         }
145
146       inline bool IsOpen() {                                         // returns true if the file is open
147         return (m_Handle != INVALID_HANDLE_VALUE);
148       }
149     };
150
151   //
152   class FileWriter : public FileReader
153     {
154       class h__iovec;
155       mem_ptr<h__iovec>  m_IOVec;
156       KM_NO_COPY_CONSTRUCT(FileWriter);
157
158     public:
159       FileWriter();
160       virtual ~FileWriter();
161
162       Result_t OpenWrite(const char*);                               // open a new file, overwrites existing
163       Result_t OpenModify(const char*);                              // open a file for read/write
164
165       // this part of the interface takes advantage of the iovec structure on
166       // platforms that support it. For each call to Writev(const byte_t*, ui32_t, ui32_t*),
167       // the given buffer is added to an internal iovec struct. All items on the list
168       // are written to disk by a call to Writev();
169       Result_t Writev(const byte_t*, ui32_t);                       // queue buffer for "gather" write
170       Result_t Writev(ui32_t* = 0);                                 // write all queued buffers
171
172       // if you call this while there are unwritten items on the iovec list,
173       // the iovec list will be written to disk before the given buffer,as though
174       // you had called Writev() first.
175       Result_t Write(const byte_t*, ui32_t, ui32_t* = 0);            // write buffer to disk
176    };
177
178 } // namespace Kumu
179
180
181 #endif // _KM_FILEIO_H_
182
183
184 //
185 // end KM_fileio.h
186 //