2 Copyright (c) 2004-2006, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
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.
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.
29 \brief portable file i/o
44 # include <sys/types.h>
59 struct _finddatai64_t m_FileInfo;
62 ~DirScanner() { Close(); }
63 Result_t Open(const char*);
65 Result_t GetNext(char*);
68 // POSIX directory scanner
75 DirScanner() : m_Handle(NULL) {}
76 ~DirScanner() { Close(); }
78 Result_t Open(const char*);
80 Result_t GetNext(char*);
85 typedef __int64 fsize_t;
86 typedef __int64 fpos_t;
89 SP_BEGIN = FILE_BEGIN,
90 SP_POS = FILE_CURRENT,
94 typedef off_t fsize_t;
97 const HANDLE INVALID_HANDLE_VALUE = -1L;
106 const ui32_t Kilobyte = 1024;
107 const ui32_t Megabyte = Kilobyte * Kilobyte;
108 const ui32_t Gigabyte = Megabyte * Kilobyte;
110 const ui32_t MaxFilePath = Kilobyte;
112 bool PathIsFile(const char* pathname);
113 bool PathIsDirectory(const char* pathname);
114 fsize_t FileSize(const char* pathname);
116 // reads an entire file into a string
117 Result_t ReadFileIntoString(const char* filename, std::string& outString, ui32_t max_size = 256 * Kilobyte);
122 KM_NO_COPY_CONSTRUCT(FileReader);
125 std::string m_Filename;
129 FileReader() : m_Handle(INVALID_HANDLE_VALUE) {}
130 virtual ~FileReader() { Close(); }
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
139 inline Kumu::fpos_t Tell() const // report the file pointer's location
141 Kumu::fpos_t tmp_pos;
146 inline bool IsOpen() { // returns true if the file is open
147 return (m_Handle != INVALID_HANDLE_VALUE);
152 class FileWriter : public FileReader
155 mem_ptr<h__iovec> m_IOVec;
156 KM_NO_COPY_CONSTRUCT(FileWriter);
160 virtual ~FileWriter();
162 Result_t OpenWrite(const char*); // open a new file, overwrites existing
163 Result_t OpenModify(const char*); // open a file for read/write
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
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
181 #endif // _KM_FILEIO_H_