1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 //=============================================================================
24 //
25 // Platform-independent File functions
26 //
27 //=============================================================================
28
29 #ifndef AGS_SHARED_UTIL_FILE_H
30 #define AGS_SHARED_UTIL_FILE_H
31
32 #include "ags/shared/core/platform.h"
33 #include "ags/shared/util/string.h"
34
35 namespace AGS3 {
36 namespace AGS {
37 namespace Shared {
38
39 // Forward declarations
40 class Stream;
41
42 enum FileOpenMode {
43 kFile_Open, // Open existing file
44 kFile_Create, // Create new file, or open existing one
45 kFile_CreateAlways // Always create a new file, replacing any existing one
46 };
47
48 enum FileWorkMode {
49 kFile_Read,
50 kFile_Write,
51 kFile_ReadWrite
52 };
53
54 namespace File {
55 // Returns size of a file, or -1 if no such file found
56 soff_t GetFileSize(const String &filename);
57 // Tests if file could be opened for reading
58 bool TestReadFile(const String &filename);
59 // Opens a file for writing or creates new one if it does not exist; deletes file if it was created during test
60 bool TestWriteFile(const String &filename);
61 // Create new empty file and deletes it; returns TRUE if was able to create file
62 bool TestCreateFile(const String &filename);
63 // Deletes existing file; returns TRUE if was able to delete one
64 bool DeleteFile(const String &filename);
65
66 // Sets FileOpenMode and FileWorkMode values corresponding to C-style file open mode string
67 bool GetFileModesFromCMode(const String &cmode, FileOpenMode &open_mode, FileWorkMode &work_mode);
68 // Gets C-style file mode from FileOpenMode and FileWorkMode
69 String GetCMode(FileOpenMode open_mode, FileWorkMode work_mode);
70
71 // Opens file in the given mode
72 Stream *OpenFile(const String &filename, FileOpenMode open_mode, FileWorkMode work_mode);
73 // Opens file for reading restricted to the arbitrary offset range
74 Stream *OpenFile(const String &filename, soff_t start_off, soff_t end_off);
75 // Convenience helpers
76 // Create a totally new file, overwrite existing one
CreateFile(const String & filename)77 inline Stream *CreateFile(const String &filename) {
78 return OpenFile(filename, kFile_CreateAlways, kFile_Write);
79 }
80 // Open existing file for reading
OpenFileRead(const String & filename)81 inline Stream *OpenFileRead(const String &filename) {
82 return OpenFile(filename, kFile_Open, kFile_Read);
83 }
84 // Open existing file for writing (append) or create if it does not exist
OpenFileWrite(const String & filename)85 inline Stream *OpenFileWrite(const String &filename) {
86 return OpenFile(filename, kFile_Create, kFile_Write);
87 }
88
89 // Opens stdin stream for reading
90 Stream *OpenStdin();
91 // Opens stdout stream for writing
92 Stream *OpenStdout();
93 // Opens stderr stream for writing
94 Stream *OpenStderr();
95
96 // Case insensitive find file
97 String FindFileCI(const String &dir_name, const String &file_name);
98 // Case insensitive file open: looks up for the file using FindFileCI
99 Stream *OpenFileCI(const String &file_name,
100 FileOpenMode open_mode = kFile_Open,
101 FileWorkMode work_mode = kFile_Read);
102
103 } // namespace File
104
105 } // namespace Shared
106 } // namespace AGS
107 } // namespace AGS3
108
109 #endif
110