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 = 8 * Megabyte);
119 // Writes a string to a file, overwrites the existing file if present.
120 Result_t WriteStringIntoFile(const char* filename, const std::string& inString);
125 KM_NO_COPY_CONSTRUCT(FileReader);
128 std::string m_Filename;
132 FileReader() : m_Handle(INVALID_HANDLE_VALUE) {}
133 virtual ~FileReader() { Close(); }
135 Result_t OpenRead(const char*) const; // open the file for reading
136 Result_t Close() const; // close the file
137 fsize_t Size() const; // returns the file's current size
138 Result_t Seek(Kumu::fpos_t = 0, SeekPos_t = SP_BEGIN) const; // move the file pointer
139 Result_t Tell(Kumu::fpos_t* pos) const; // report the file pointer's location
140 Result_t Read(byte_t*, ui32_t, ui32_t* = 0) const; // read a buffer of data
142 inline Kumu::fpos_t Tell() const // report the file pointer's location
144 Kumu::fpos_t tmp_pos;
149 inline bool IsOpen() { // returns true if the file is open
150 return (m_Handle != INVALID_HANDLE_VALUE);
155 class FileWriter : public FileReader
158 mem_ptr<h__iovec> m_IOVec;
159 KM_NO_COPY_CONSTRUCT(FileWriter);
163 virtual ~FileWriter();
165 Result_t OpenWrite(const char*); // open a new file, overwrites existing
166 Result_t OpenModify(const char*); // open a file for read/write
168 // this part of the interface takes advantage of the iovec structure on
169 // platforms that support it. For each call to Writev(const byte_t*, ui32_t, ui32_t*),
170 // the given buffer is added to an internal iovec struct. All items on the list
171 // are written to disk by a call to Writev();
172 Result_t Writev(const byte_t*, ui32_t); // queue buffer for "gather" write
173 Result_t Writev(ui32_t* = 0); // write all queued buffers
175 // if you call this while there are unwritten items on the iovec list,
176 // the iovec list will be written to disk before the given buffer,as though
177 // you had called Writev() first.
178 Result_t Write(const byte_t*, ui32_t, ui32_t* = 0); // write buffer to disk
184 #endif // _KM_FILEIO_H_