ac45cf08a1d2beb2bc50ff399fa5deed0fd8fc21
[libdcp.git] / asdcplib / src / KM_fileio.h
1 /*
2 Copyright (c) 2004-2009, 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: KM_fileio.h,v 1.17 2009/06/22 05:49:02 jhurst Exp $
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 #include <regex.h>
46 #endif
47
48 #include <sys/stat.h>
49
50
51
52 namespace Kumu
53 {
54   //
55   class DirScanner
56     {
57     public:
58 #ifdef KM_WIN32
59       __int64               m_Handle;
60       struct _finddatai64_t m_FileInfo;
61 #else
62       DIR*       m_Handle;
63 #endif
64
65       DirScanner(void);
66       ~DirScanner() { Close(); }
67
68       Result_t Open(const char*);
69       Result_t Close();
70       Result_t GetNext(char*);
71     };
72
73 #ifdef KM_WIN32
74   typedef __int64  fsize_t;
75   typedef __int64  fpos_t;
76   typedef HANDLE FileHandle;
77
78   enum SeekPos_t {
79     SP_BEGIN = FILE_BEGIN,
80     SP_POS   = FILE_CURRENT,
81     SP_END   = FILE_END
82   };
83 #else
84   typedef off_t    fsize_t;
85   typedef off_t    fpos_t;
86   typedef int      FileHandle;
87   const FileHandle INVALID_HANDLE_VALUE = -1L;
88
89   enum SeekPos_t {
90     SP_BEGIN = SEEK_SET,
91     SP_POS   = SEEK_CUR,
92     SP_END   = SEEK_END
93   };
94 #endif
95
96   //
97 #ifndef KM_SMALL_FILES_OK
98   template <bool sizecheck>    void compile_time_size_checker();
99   template <> inline void compile_time_size_checker<false>() {}
100   //
101   // READ THIS if your compiler is complaining about a previously declared implementation of
102   // compile_time_size_checker(). For example, GCC 4.0.1 looks like this:
103   //
104   // error: 'void Kumu::compile_time_size_checker() [with bool sizecheck = false]' previously declared here
105   //
106   // This is happening because the equality being tested below is false. The reason for this 
107   // will depend on your OS, but on Linux it is probably because you have not used -D_FILE_OFFSET_BITS=64
108   // Adding this magic macro to your CFLAGS will get you going again. If you are on a system that
109   // does not support 64-bit files, you can disable this check by using -DKM_SMALL_FILES_OK. You
110   // will then of course be limited to file sizes < 4GB.
111   //
112   template <> inline void compile_time_size_checker<sizeof(Kumu::fsize_t)==sizeof(ui64_t)>() {}
113 #endif
114   //
115
116   const ui32_t Kilobyte = 1024;
117   const ui32_t Megabyte = Kilobyte * Kilobyte;
118   const ui32_t Gigabyte = Megabyte * Kilobyte;
119
120   const ui32_t MaxFilePath = Kilobyte;
121
122
123   //------------------------------------------------------------------------------------------
124   // Path Manglers
125   //------------------------------------------------------------------------------------------
126
127   // types
128   typedef std::list<std::string> PathCompList_t; // a list of path components
129   typedef std::list<std::string> PathList_t; // a list of paths
130
131   // tests
132   bool        PathExists(const std::string& Path); // true if the path exists in the filesystem
133   bool        PathIsFile(const std::string& Path); // true if the path exists in the filesystem and is a file
134   bool        PathIsDirectory(const std::string& Path); // true if the path exists in the filesystem and is a directory
135   fsize_t     FileSize(const std::string& Path); // returns the size of a regular file, 0 for a directory or device
136   bool        PathsAreEquivalent(const std::string& lhs, const std::string& rhs); // true if paths point to the same filesystem entry
137
138   // Returns free space and total space available for the given path
139   Result_t    FreeSpaceForPath(const std::string& path, Kumu::fsize_t& free_space, Kumu::fsize_t& total_space);
140
141   // split and reassemble paths as lists of path components
142   PathCompList_t& PathToComponents(const std::string& Path, PathCompList_t& CList, char separator = '/'); // removes '//'
143   std::string ComponentsToPath(const PathCompList_t& CList, char separator = '/');
144   std::string ComponentsToAbsolutePath(const PathCompList_t& CList, char separator = '/'); // add separator to the front
145   bool        PathHasComponents(const std::string& Path, char separator = '/'); // true if paths starts with separator
146
147   bool        PathIsAbsolute(const std::string& Path, char separator = '/'); // true if path begins with separator
148   std::string PathMakeAbsolute(const std::string& Path, char separator = '/'); // compute position of relative path using getcwd()
149   std::string PathMakeLocal(const std::string& Path, const std::string& Parent); // remove Parent from front of Path, if it exists
150   std::string PathMakeCanonical(const std::string& Path, char separator = '/'); // remove '.' and '..'
151
152   // common operations
153   std::string PathBasename(const std::string& Path, char separator = '/'); // returns right-most path element (list back())
154   std::string PathDirname(const std::string& Path, char separator = '/'); // returns everything but the right-most element
155   std::string PathGetExtension(const std::string& Path); // returns everything in the right-most element following the right-most '.'
156   std::string PathSetExtension(const std::string& Path, const std::string& Extension); // empty extension removes '.' as well
157
158   std::string PathJoin(const std::string& Path1, const std::string& Path2, char separator = '/');
159   std::string PathJoin(const std::string& Path1, const std::string& Path2, const std::string& Path3, char separator = '/');
160   std::string PathJoin(const std::string& Path1, const std::string& Path2,
161                        const std::string& Path3, const std::string& Path4, char separator = '/');
162
163
164   //------------------------------------------------------------------------------------------
165   // Path Search
166   //------------------------------------------------------------------------------------------
167
168   // An interface for a path matching function, used by FindInPath() and FindInPaths() below
169   //
170   class IPathMatch
171   {
172   public:
173     virtual ~IPathMatch() {}
174     virtual bool Match(const std::string& s) const = 0;
175   };
176
177   // matches any pathname
178  class PathMatchAny : public IPathMatch
179   {
180   public:
181     virtual ~PathMatchAny() {}
182     inline bool Match(const std::string& s) const { return true; }
183   };
184
185 #ifndef KM_WIN32
186   // matches pathnames using a regular expression
187  class PathMatchRegex : public IPathMatch
188   {
189     regex_t m_regex;
190     PathMatchRegex();
191     const PathMatchRegex& operator=(const PathMatchRegex&);
192
193   public:
194     PathMatchRegex(const std::string& Pattern);
195     PathMatchRegex(const PathMatchRegex&);
196     virtual ~PathMatchRegex();
197     bool Match(const std::string& s) const;
198   };
199
200   // matches pathnames using a Bourne shell glob expression
201  class PathMatchGlob : public IPathMatch
202   {
203     regex_t m_regex;
204     PathMatchGlob();
205     const PathMatchGlob& operator=(const PathMatchGlob&);
206
207   public:
208     PathMatchGlob(const std::string& Pattern);
209     PathMatchGlob(const PathMatchGlob&);
210     virtual ~PathMatchGlob();
211     bool Match(const std::string& s) const;
212   };
213 #endif /* !KM_WIN32 */
214
215   // Search all paths in SearchPaths for filenames matching Pattern (no directories are returned).
216   // Put results in FoundPaths. Returns after first find if one_shot is true.
217   PathList_t& FindInPath(const IPathMatch& Pattern, const std::string& SearchDir,
218                          PathList_t& FoundPaths, bool one_shot = false, char separator = '/');
219
220   PathList_t& FindInPaths(const IPathMatch& Pattern, const PathList_t& SearchPaths,
221                           PathList_t& FoundPaths, bool one_shot = false, char separator = '/');
222
223   //------------------------------------------------------------------------------------------
224   // Directory Manipulation
225   //------------------------------------------------------------------------------------------
226
227   // Create a directory, creates intermediate directories as necessary
228   Result_t CreateDirectoriesInPath(const std::string& Path);
229
230   // Delete a file (fails if the path points to a directory)
231   Result_t DeleteFile(const std::string& filename);
232
233   // Recursively remove a file or directory
234   Result_t DeletePath(const std::string& pathname);
235
236   //------------------------------------------------------------------------------------------
237   // File I/O Wrappers
238   //------------------------------------------------------------------------------------------
239
240   // Instant IO for strings
241   //
242   // Reads an entire file into a string.
243   Result_t ReadFileIntoString(const char* filename, std::string& outString, ui32_t max_size = 8 * Megabyte);
244
245   // Writes a string to a file, overwrites the existing file if present.
246   Result_t WriteStringIntoFile(const char* filename, const std::string& inString);
247
248   // Instant IO for archivable objects
249   //
250   // Unarchives a file into an object
251   Result_t ReadFileIntoObject(const std::string& Filename, IArchive& Object, ui32_t max_size = 8 * Kumu::Megabyte);
252
253   // Archives an object into a file
254   Result_t WriteObjectIntoFile(const IArchive& Object, const std::string& Filename);
255
256   // Instant IO for memory buffers
257   //
258   // Unarchives a file into a buffer
259   Result_t ReadFileIntoBuffer(const std::string& Filename, Kumu::ByteString& Buffer,
260                               ui32_t max_size = 8 * Kumu::Megabyte);
261
262   // Archives a buffer into a file
263   Result_t WriteBufferIntoFile(const Kumu::ByteString& Buffer, const std::string& Filename);
264
265
266   //------------------------------------------------------------------------------------------
267   // File I/O
268   //------------------------------------------------------------------------------------------
269
270   //
271   class FileReader
272     {
273       KM_NO_COPY_CONSTRUCT(FileReader);
274
275     protected:
276       std::string m_Filename;
277       FileHandle  m_Handle;
278
279     public:
280       FileReader() : m_Handle(INVALID_HANDLE_VALUE) {}
281       virtual ~FileReader() { Close(); }
282
283       Result_t OpenRead(const char*) const;                          // open the file for reading
284       Result_t Close() const;                                        // close the file
285       fsize_t  Size() const;                                         // returns the file's current size
286       Result_t Seek(Kumu::fpos_t = 0, SeekPos_t = SP_BEGIN) const;   // move the file pointer
287       Result_t Tell(Kumu::fpos_t* pos) const;                        // report the file pointer's location
288       Result_t Read(byte_t*, ui32_t, ui32_t* = 0) const;             // read a buffer of data
289
290       inline Kumu::fpos_t Tell() const                               // report the file pointer's location
291         {
292           Kumu::fpos_t tmp_pos;
293           Tell(&tmp_pos);
294           return tmp_pos;
295         }
296
297       inline bool IsOpen() {                                         // returns true if the file is open
298         return (m_Handle != INVALID_HANDLE_VALUE);
299       }
300     };
301
302   //
303   class FileWriter : public FileReader
304     {
305       class h__iovec;
306       mem_ptr<h__iovec>  m_IOVec;
307       KM_NO_COPY_CONSTRUCT(FileWriter);
308
309     public:
310       FileWriter();
311       virtual ~FileWriter();
312
313       Result_t OpenWrite(const char*);                               // open a new file, overwrites existing
314       Result_t OpenModify(const char*);                              // open a file for read/write
315
316       // this part of the interface takes advantage of the iovec structure on
317       // platforms that support it. For each call to Writev(const byte_t*, ui32_t, ui32_t*),
318       // the given buffer is added to an internal iovec struct. All items on the list
319       // are written to disk by a call to Writev();
320       Result_t Writev(const byte_t*, ui32_t);                       // queue buffer for "gather" write
321       Result_t Writev(ui32_t* = 0);                                 // write all queued buffers
322
323       // if you call this while there are unwritten items on the iovec list,
324       // the iovec list will be written to disk before the given buffer,as though
325       // you had called Writev() first.
326       Result_t Write(const byte_t*, ui32_t, ui32_t* = 0);            // write buffer to disk
327    };
328
329   Result_t CreateDirectoriesInPath(const std::string& Path);
330   Result_t FreeSpaceForPath(const std::string& path, Kumu::fsize_t& free_space, Kumu::fsize_t& total_space);
331   Result_t DeleteFile(const std::string& filename);
332   Result_t DeletePath(const std::string& pathname);
333
334 } // namespace Kumu
335
336
337 #endif // _KM_FILEIO_H_
338
339
340 //
341 // end KM_fileio.h
342 //