1 //=============================================================================
2 //
3 // Adventure Game Studio (AGS)
4 //
5 // Copyright (C) 1999-2011 Chris Jones and 2011-20xx others
6 // The full list of copyright holders can be found in the Copyright.txt
7 // file, which is part of this source code distribution.
8 //
9 // The AGS source code is provided under the Artistic License 2.0.
10 // A copy of this license can be found in the file License.txt and at
11 // http://www.opensource.org/licenses/artistic-license-2.0.php
12 //
13 //=============================================================================
14 //
15 // Platform-independent File functions
16 //
17 //=============================================================================
18 #ifndef __AGS_CN_UTIL__FILE_H
19 #define __AGS_CN_UTIL__FILE_H
20 
21 #include "util/string.h"
22 
23 namespace AGS
24 {
25 namespace Common
26 {
27 
28 // Forward declarations
29 class Stream;
30 
31 enum FileOpenMode
32 {
33     kFile_Open,         // Open existing file
34     kFile_Create,       // Create new file, or open existing one
35     kFile_CreateAlways  // Always create a new file, replacing any existing one
36 };
37 
38 enum FileWorkMode
39 {
40     kFile_Read,
41     kFile_Write,
42     kFile_ReadWrite
43 };
44 
45 namespace File
46 {
47     // Returns size of a file, or -1 if no such file found
48     int         GetFileSize(const String &filename);
49     // Tests if file could be opened for reading
50     bool        TestReadFile(const String &filename);
51     // Opens a file for writing or creates new one if it does not exist; deletes file if it was created during test
52     bool        TestWriteFile(const String &filename);
53     // Create new empty file and deletes it; returns TRUE if was able to create file
54     bool        TestCreateFile(const String &filename);
55     // Deletes existing file; returns TRUE if was able to delete one
56     bool        DeleteFile(const String &filename);
57 
58     // Sets FileOpenMode and FileWorkMode values corresponding to C-style file open mode string
59     bool        GetFileModesFromCMode(const String &cmode, FileOpenMode &open_mode, FileWorkMode &work_mode);
60     // Gets C-style file mode from FileOpenMode and FileWorkMode
61     String      GetCMode(FileOpenMode open_mode, FileWorkMode work_mode);
62 
63     Stream      *OpenFile(const String &filename, FileOpenMode open_mode, FileWorkMode work_mode);
64     // Convenience helpers
65     // Create a totally new file, overwrite existing one
CreateFile(const String & filename)66     inline Stream *CreateFile(const String &filename)
67     {
68         return OpenFile(filename, kFile_CreateAlways, kFile_Write);
69     }
70     // Open existing file for reading
OpenFileRead(const String & filename)71     inline Stream *OpenFileRead(const String &filename)
72     {
73         return OpenFile(filename, kFile_Open, kFile_Read);
74     }
75     // Open existing file for writing (append) or create if it does not exist
OpenFileWrite(const String & filename)76     inline Stream *OpenFileWrite(const String &filename)
77     {
78         return OpenFile(filename, kFile_Create, kFile_Write);
79     }
80 } // namespace File
81 
82 } // namespace Common
83 } // namespace AGS
84 
85 #endif // __AGS_CN_UTIL__FILE_H
86