kumu merge
[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   // Writes a string to a file, overwrites the existing file if present.
120   Result_t WriteStringIntoFile(const char* filename, const std::string& inString);
121
122   //
123   class FileReader
124     {
125       KM_NO_COPY_CONSTRUCT(FileReader);
126
127     protected:
128       std::string m_Filename;
129       HANDLE      m_Handle;
130
131     public:
132       FileReader() : m_Handle(INVALID_HANDLE_VALUE) {}
133       virtual ~FileReader() { Close(); }
134
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
141
142       inline Kumu::fpos_t Tell() const                               // report the file pointer's location
143         {
144           Kumu::fpos_t tmp_pos;
145           Tell(&tmp_pos);
146           return tmp_pos;
147         }
148
149       inline bool IsOpen() {                                         // returns true if the file is open
150         return (m_Handle != INVALID_HANDLE_VALUE);
151       }
152     };
153
154   //
155   class FileWriter : public FileReader
156     {
157       class h__iovec;
158       mem_ptr<h__iovec>  m_IOVec;
159       KM_NO_COPY_CONSTRUCT(FileWriter);
160
161     public:
162       FileWriter();
163       virtual ~FileWriter();
164
165       Result_t OpenWrite(const char*);                               // open a new file, overwrites existing
166       Result_t OpenModify(const char*);                              // open a file for read/write
167
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
174
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
179    };
180
181 } // namespace Kumu
182
183
184 #endif // _KM_FILEIO_H_
185
186
187 //
188 // end KM_fileio.h
189 //