-
Notifications
You must be signed in to change notification settings - Fork 519
/
Copy pathIFileSystem.h
549 lines (419 loc) · 24.8 KB
/
IFileSystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* Copyright (c) 2018-2019 Confetti Interactive Inc.
*
* This file is part of The-Forge
* (see https://github.com/ConfettiFX/The-Forge).
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef IFileSystem_h
#define IFileSystem_h
#include "../Interfaces/IOperatingSystem.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct FileSystem FileSystem;
typedef struct Path Path;
typedef struct FileStream FileStream;
typedef void (*FileDialogCallbackFn)(const Path* path, void* userData);
// A note on memory management for functions defined in this file:
//
// Any function that returns a pointer that does not have 'Get' in its
// name must have its result freed using the appropriate function
// (e.g. fsFreePath, fsFreeFileWatcher, fsFreeFileSystem, fsCloseStream).
//
// For functions whose names contain 'Get', the return value is guaranteed
// to be valid only while its parent object (usually the first argument to the
// function) lives.
// MARK: - Initialization
/// Initializes the FileSystem API
bool fsInitAPI(void);
/// Frees resources associated with the FileSystem API
void fsDeinitAPI(void);
// MARK: - Path Operations
/// Creates a new Path from `absolutePathString`, where `absolutePathString` is a valid path from the root of
/// `fileSystem`.
///
/// - Returns: The newly created path, or NULL if absolutePathString is not a valid path.
Path* fsCreatePath(const FileSystem* fileSystem, const char* absolutePathString);
/// Returns a copy of `path` for which the caller has ownership.
Path* fsCopyPath(const Path* path);
/// Frees `path`'s memory, invalidating it for any future calls.
void fsFreePath(Path* path);
/// Returns a reference to the FileSystem that `path` references.
FileSystem* fsGetPathFileSystem(const Path* path);
/// Returns the number of UTF-8 codepoints comprising `path`.
size_t fsGetPathLength(const Path* path);
/// Appends `pathComponent` to `basePath`, returning a new Path for which the caller has ownership.
/// `basePath` is assumed to be a directory.
Path* fsAppendPathComponent(const Path* basePath, const char* pathComponent);
/// Appends `newExtension` to `basePath`, returning a new Path for which the caller has ownership.
/// If `basePath` already has an extension, `newExtension` will be appended to the end.
Path* fsAppendPathExtension(const Path* basePath, const char* newExtension);
/// Appends `newExtension` to `basePath`, returning a new Path for which the caller has ownership.
/// If `basePath` already has an extension, its previous extension will be replaced by `newExtension`.
Path* fsReplacePathExtension(const Path* path, const char* newExtension);
typedef struct PathComponent {
const char* buffer;
size_t length;
} PathComponent;
/// Splits `path`` path into its components and returns those components by reference.
/// The `buffer` referenced in the out values is guaranteed to live for as long as the Path object lives.
/// Parameters:
/// - directoryName The PathComponent to fill with `path`'s parent directory name, if it exists. May be NULL.
/// - fileName The PathComponent to fill with `path`'s file name. May be NULL.
/// - extension The PathComponent to fill with `path`'s path extension, if it exists. May be NULL. The PathComponent's buffer is guaranteed to either be NULL or a NULL-terminated string.
void fsGetPathComponents(const Path* path, PathComponent* directoryName, PathComponent* fileName, PathComponent* extension);
/// Copies `path`'s parent path, returning a new Path for which the caller has ownership. May return NULL if `path` has no parent.
Path* fsCopyParentPath(const Path* path);
/// Returns `path`'s directory name as a PathComponent. The return value is guaranteed to live for as long as `path` lives.
PathComponent fsGetPathDirectoryName(const Path* path);
/// Returns `path`'s file name as a PathComponent. The return value is guaranteed to live for as long as `path` lives.
PathComponent fsGetPathFileName(const Path* path);
/// Returns `path`'s extension, excluding the '.'. The return value is guaranteed to live for as long as `path` lives.
/// The returned PathComponent's buffer is guaranteed to either be NULL or a NULL-terminated string.
PathComponent fsGetPathExtension(const Path* path);
/// Copies `path`'s lowercased path extension to buffer, writing at most maxLength UTF-8 codepoints.
/// Returns the length of the full extension; if this is larger than maxLength only part of the extension was written.
/// buffer will be null-terminated if there is sufficient space.
size_t fsGetLowercasedPathExtension(const Path* path, char* buffer, size_t maxLength);
/// Returns the native path string representing this path.
/// The path string is guaranteed to live for as long as the Path object lives.
const char* fsGetPathAsNativeString(const Path* path);
/// Returns true if `pathA` and `pathB` point to the same file within the same FileSystem.
bool fsPathsEqual(const Path* pathA, const Path* pathB);
// MARK: - FileSystem-Independent Queries
/// Copies the current working directory path, returning a new Path for which the caller has ownership.
Path* fsCopyWorkingDirectoryPath(void);
/// Copies the path for the directory containing either the executable (on most platforms) or the app bundle (on macOS),
/// returning a new Path for which the caller has ownership.
Path* fsCopyProgramDirectoryPath(void);
/// Copies the path to the currently running executable, returning a new Path for which the caller has ownership.
Path* fsCopyExecutablePath(void);
/// Copies the executable name (excluding its extension) to `buffer`, writing at most `maxLength` UTF-8 codepoints.
/// Returns the length of the full executable name; if this is larger than `maxLength` only part of the executable name was written.
/// buffer will be null-terminated if there is sufficient space.
size_t fsGetExecutableName(char* buffer, size_t maxLength);
/// Copies the path to the preferences directory for `organisation` and `application`, returning a new Path for which the caller has ownership.
/// The path may not exist in its underlying file system; call `fsCreateDirectory` to create it.
Path* fsCopyPreferencesDirectoryPath(const char* organisation, const char* application);
/// Copies the path to the user's documents directory, returning a new Path for which the caller has ownership.
Path* fsCopyUserDocumentsDirectoryPath(void);
/// Copies the preferred path for output log files, returning a new Path for which the caller has ownership.
Path* fsCopyLogFileDirectoryPath(void);
/// Displays an open file dialog for files in `directory`, calling `callback` for the selected file. The selection is filtered to only include files with extensions `fileExtensions` if `fileExtensionCount` is non-zero.
void fsShowOpenFileDialog(const char* title, const Path* directory, FileDialogCallbackFn callback, void* userData, const char* fileDesc, const char** fileExtensions, size_t fileExtensionCount);
/// Displays a save file dialog for files in `directory`, calling `callback` for the selected file. The selection is filtered to only include files with extensions `fileExtensions` if `fileExtensionCount` is non-zero.
void fsShowSaveFileDialog(const char* title, const Path* directory, FileDialogCallbackFn callback, void* userData, const char* fileDesc, const char** fileExtensions, size_t fileExtensionCount);
// MARK: - File and Directory Queries
/// Gets the creation time of the file at `filePath`. Undefined if no file exists at `filePath`.
time_t fsGetCreationTime(const Path* filePath);
/// Gets the time of last access for the file at `filePath`. Undefined if no file exists at `filePath`.
time_t fsGetLastAccessedTime(const Path* filePath);
/// Gets the time of last modification for the file at `filePath`. Undefined if no file exists at `filePath`.
time_t fsGetLastModifiedTime(const Path* filePath);
/// Copies the file at `sourcePath` to `destinationPath`. If a file already exists at `destinationPath`, the file is only copied if
/// `overwritesIfExists` is true. Returns true if the file was successfully copied and false otherwise.
bool fsCopyFile(const Path* sourcePath, const Path* destinationPath, bool overwriteIfExists);
/// Creates a directory at `directoryPath`, recursively creating parent directories if necessary.
/// Returns true if the directory was able to be created.
bool fsCreateDirectory(const Path* directoryPath);
/// Deletes the file at `path`. Returns true if the file was successfully deleted.
bool fsDeleteFile(const Path* path);
/// Returns true if a file (including directories) exists at `path`.
bool fsFileExists(const Path* path);
/// Returns true if a file exists at `path` and that file is a directory.
bool fsDirectoryExists(const Path* path);
/// Enumerates all files with the extension `extension` in `directory`, calling `processFile` for each match.
/// `processFile` should return true if enumerating should continue or false to stop early.
void fsEnumerateFilesWithExtension(const Path* directory, const char* extension, bool (*processFile)(const Path*, void* userData), void* userData);
/// Enumerates all subdirectories of `directory`, calling `processDirectory` for each match.
/// `processDirectory` should return true if enumerating should continue or false to stop early.
void fsEnumerateSubDirectories(const Path* directory, bool (*processDirectory)(const Path*, void* userData), void* userData);
// MARK: - Resource Directories
typedef enum ResourceDirectory
{
/// The main application's shader binaries folder
RD_SHADER_BINARIES = 0,
/// The main application's shader source directory
RD_SHADER_SOURCES,
/// The main application's texture source directory (TODO processed texture folder)
RD_TEXTURES,
RD_MESHES,
RD_BUILTIN_FONTS,
RD_GPU_CONFIG,
RD_ANIMATIONS,
RD_AUDIO,
RD_OTHER_FILES,
// Libraries can have their own directories.
// Up to 100 libraries are supported.
____rd_lib_counter_begin = RD_OTHER_FILES,
// Add libraries here
RD_MIDDLEWARE_0,
RD_MIDDLEWARE_1,
RD_MIDDLEWARE_2,
____rd_lib_counter_end = ____rd_lib_counter_begin + 99 * 3,
RD_ROOT,
RD_COUNT
} ResourceDirectory;
/// Gets the default relative path for `resourceDir``. For example, given `RD_SHADER_BINARIES`, the return value
/// might be `Shaders/D3D12/Binary` for a DirectX 12 application.
const char* fsGetDefaultRelativePathForResourceDirectory(ResourceDirectory resourceDir);
/// Returns true if resources are bundled together with the application on the target platform.
bool fsPlatformUsesBundledResources(void);
/// Copies the path to the root ResourceDirectory for `fileSystem`, returning a new Path for which the caller has ownership.
Path* fsCopyResourceDirectoryRootPath(void);
/// Copies the path to `resourceDir` within `fileSystem`, returning a new Path for which the caller has ownership.
Path* fsCopyPathForResourceDirectory(ResourceDirectory resourceDir);
/// Forms a path by appending `relativePath` to the path for `resourceDir` in `fileSystem`, returning a new Path for which the caller has ownership.
Path* fsCopyPathInResourceDirectory(ResourceDirectory resourceDir, const char* relativePath);
/// Returns true if a file exists at `relativePath` within `resourceDir` on `fileSystem`.
bool fsFileExistsInResourceDirectory(ResourceDirectory resourceDir, const char* relativePath);
/// Sets the root resource directory path (which all other resource directory paths are by default relative to) to `path`.
/// Equivalent to calling fsSetPathForResourceDirectory with RD_ROOT.
///
/// NOTE: This call is not thread-safe. It is the application's responsibility to ensure that
/// no modifications to the file system are occurring at the time of this call.
void fsSetResourceDirectoryRootPath(const Path* path);
/// Sets the absolute path for `resourceDir` to `path`. Future modifications through `fsSetResourceDirectoryRootPath`
/// do not affect `resourceDir` after this call.
///
/// NOTE: This call is not thread-safe. It is the application's responsibility to ensure that
/// no modifications to the file system are occurring at the time of this call.
void fsSetPathForResourceDirectory(ResourceDirectory resourceDir, const Path* path);
/// Sets the relative path for `resourceDir` on `fileSystem` to `relativePath`, where the base path is the root resource directory path.
/// If `fsSetResourceDirectoryRootPath` is called after this function, this function must be called again to ensure `resourceDir`'s path
/// is relative to the new root path.
///
/// NOTE: This call is not thread-safe. It is the application's responsibility to ensure that
/// no modifications to the file system are occurring at the time of this call.
void fsSetRelativePathForResourceDirectory(ResourceDirectory resourceDir, const char* relativePath);
/// Resets all resource directories (including the root) on `fileSystem` to their default values.
///
/// NOTE: This call is not thread-safe. It is the application's responsibility to ensure that
/// no modifications to the file system are occurring at the time of this call.
void fsResetResourceDirectories(void);
// MARK: - FileStream
typedef enum SeekBaseOffset
{
SBO_START_OF_FILE = 0,
SBO_CURRENT_POSITION,
SBO_END_OF_FILE,
} SeekBaseOffset;
typedef enum FileMode
{
FM_READ = 1 << 0,
FM_WRITE = 1 << 1,
FM_APPEND = 1 << 2,
FM_BINARY = 1 << 3,
FM_READ_WRITE = FM_READ | FM_WRITE,
FM_READ_APPEND = FM_READ | FM_APPEND,
FM_WRITE_BINARY = FM_WRITE | FM_BINARY,
FM_READ_BINARY = FM_READ | FM_BINARY,
FM_APPEND_BINARY = FM_APPEND | FM_BINARY,
FM_READ_WRITE_BINARY = FM_READ | FM_WRITE | FM_BINARY,
FM_READ_APPEND_BINARY = FM_READ | FM_APPEND | FM_BINARY,
} FileMode;
/// Converts `modeStr` to a `FileMode` mask, where `modeStr` follows the C standard library conventions
/// for `fopen` parameter strings.
FileMode fsFileModeFromString(const char* modeStr);
/// Converts `mode` to a string which is compatible with the C standard library conventions for `fopen`
/// parameter strings.
const char* fsFileModeToString(FileMode mode);
/// Opens the file at `filePath` using the mode `mode`, returning a new FileStream that can be used
/// to read from or modify the file. May return NULL if the file could not be opened.
FileStream* fsOpenFile(const Path* filePath, FileMode mode);
/// Opens the file at `relativePath` within `resourceDir` using the mode `mode`, returning
/// a new FileStream that can be used to read from or modify the file. May return NULL if the file could not be opened.
FileStream* fsOpenFileInResourceDirectory(ResourceDirectory resourceDir, const char *relativePath, FileMode mode);
/// Opens a read-only buffer as a FileStream, returning a stream that must be closed with `fsCloseStream`.
FileStream* fsOpenReadOnlyMemory(const void *buffer, size_t bufferLengthInBytes);
/// Opens a read-write buffer as a FileStream, returning a stream that must be closed with `fsCloseStream`.
FileStream* fsOpenReadWriteMemory(void *buffer, size_t bufferLengthInBytes);
/// Reads at most `bufferSizeInBytes` bytes from the file and copies them into outputBuffer.
/// Returns the number of bytes read.
size_t fsReadFromStream(FileStream* stream, void* outputBuffer, size_t bufferSizeInBytes);
size_t __fsScanFromStream(FileStream* stream, int* bytesRead, const char* format, ...);
/// Reads formatted data from `stream`, returning the items of the argument list successfully filled.
/// The second argument must be a pointer to an int that will contain the number of bytes read.
/// See `fscanf` in the C standard library for reference.
/// NOTE: this is defined as a macro so we can append %n to the format string, allowing us
/// to get the number of bytes read back from the underlying C function.
#define fsScanFromStream(S, bytesRead, FMT, ...) ( \
(__fsScanFromStream(S, bytesRead, FMT "%n", __VA_ARGS__, bytesRead)))
/// Reads at most `bufferSizeInBytes` bytes from sourceBuffer and writes them into the file.
/// Returns the number of bytes written.
size_t fsWriteToStream(FileStream* stream, const void* sourceBuffer, size_t byteCount);
/// Writes the C string pointed by `format` to the stream. If` format` includes format specifiers
/// (subsequences beginning with %), the additional arguments following format are formatted and
/// inserted in the resulting string replacing their respective specifiers.
/// Returns the total number of characters written.
/// See `fprintf` in the C standard library for reference.
size_t fsPrintToStream(FileStream* stream, const char* format, ...);
/// Seeks to the specified position in the file, using `baseOffset` as the reference offset.
bool fsSeekStream(FileStream* stream, SeekBaseOffset baseOffset, ssize_t seekOffset);
/// Gets the current seek position in the file.
ssize_t fsGetStreamSeekPosition(const FileStream* stream);
/// Gets the current size of the file. Returns -1 if the size is unknown or unavailable.
ssize_t fsGetStreamFileSize(const FileStream* stream);
/// Flushes all writes to the file stream to the underlying subsystem.
void fsFlushStream(FileStream* stream);
/// Returns whether the current seek position is at the end of the file stream.
bool fsStreamAtEnd(const FileStream* stream);
/// Closes and invalidates the file stream.
bool fsCloseStream(FileStream* stream);
// MARK: FileStream Typed Reads
int64_t fsReadFromStreamInt64(FileStream* stream);
int32_t fsReadFromStreamInt32(FileStream* stream);
int16_t fsReadFromStreamInt16(FileStream* stream);
int8_t fsReadFromStreamInt8(FileStream* stream);
uint64_t fsReadFromStreamUInt64(FileStream* stream);
uint32_t fsReadFromStreamUInt32(FileStream* stream);
uint16_t fsReadFromStreamUInt16(FileStream* stream);
uint8_t fsReadFromStreamUInt8(FileStream* stream);
bool fsReadFromStreamBool(FileStream* stream);
float fsReadFromStreamFloat(FileStream* stream);
double fsReadFromStreamDouble(FileStream* stream);
size_t fsReadFromStreamString(FileStream* stream, char* buffer, size_t maxLength);
size_t fsReadFromStreamLine(FileStream* stream, char* buffer, size_t maxLength);
// MARK: FileStream Typed Writes
bool fsWriteToStreamInt64(FileStream* stream, int64_t value);
bool fsWriteToStreamInt32(FileStream* stream, int32_t value);
bool fsWriteToStreamInt16(FileStream* stream, int16_t value);
bool fsWriteToStreamInt8(FileStream* stream, int8_t value);
bool fsWriteToStreamUInt64(FileStream* stream, uint64_t value);
bool fsWriteToStreamUInt32(FileStream* stream, uint32_t value);
bool fsWriteToStreamUInt16(FileStream* stream, uint16_t value);
bool fsWriteToStreamUInt8(FileStream* stream, uint8_t value);
bool fsWriteToStreamBool(FileStream* stream, bool value);
bool fsWriteToStreamFloat(FileStream* stream, float value);
bool fsWriteToStreamDouble(FileStream* stream, double value);
bool fsWriteToStreamString(FileStream* stream, const char* value);
bool fsWriteToStreamLine(FileStream* stream, const char* value);
// MARK: - FileWatcher
typedef struct FileWatcher FileWatcher;
typedef void (*FileWatcherCallback)(const Path* path, uint32_t action);
typedef enum FileWatcherEventMask {
FWE_MODIFIED = 1 << 0,
FWE_ACCESSED = 1 << 1,
FWE_CREATED = 1 << 2,
FWE_DELETED = 1 << 3,
} FileWatcherEventMask;
/// Creates a new FileWatcher that watches for changes specified by `eventMask` at `path` and calls `callback` when changes occur.
/// The return value must have `fsFreeFileWatcher` called to free it.
FileWatcher* fsCreateFileWatcher(const Path* path, FileWatcherEventMask eventMask, FileWatcherCallback callback);
/// Invalidates and frees `fileWatcher.
void fsFreeFileWatcher(FileWatcher* fileWatcher);
// MARK: - FileSystem
typedef enum FileSystemKind
{
FSK_SYSTEM = 0,
FSK_ZIP,
FSK_RESOURCE_BUNDLE,
FSK_DEFAULT = FSK_SYSTEM
} FileSystemKind;
FileSystem* fsGetSystemFileSystem(void);
FileSystemKind fsGetFileSystemKind(const FileSystem* fileSystem);
typedef enum FileSystemFlags {
FSF_CREATE_IF_NECESSARY = 1 << 0,
FSF_OVERWRITE = 1 << 1,
FSF_READ_ONLY = 1 << 2,
} FileSystemFlags;
/// Creates a new file-system with its root at rootPath.
/// If rootPath is a compressed zip file, the file system will be the contents of the zip file.
FileSystem* fsCreateFileSystemFromFileAtPath(const Path* rootPath, FileSystemFlags flags);
/// Invalidates and frees `fileSystem`. All paths created from `fileSystem` and all files opened from
/// those paths files are also invalidated but not freed.
void fsFreeFileSystem(FileSystem* fileSystem);
/// Returns true if the file system is read-only.
bool fsFileSystemIsReadOnly(const FileSystem* fileSystem);
#ifdef __cplusplus
} // extern "C"
#endif
// MARK: - C++ Extras
#ifdef __cplusplus
// These types are only defined in C++, so conditionalise them out unless we're importing this from a C++ source file.
float2 fsReadFromStreamFloat2(FileStream* stream);
float3 fsReadFromStreamFloat3(FileStream* stream);
float3 fsReadFromStreamPackedFloat3(FileStream* stream, float maxAbsCoord);
float4 fsReadFromStreamFloat4(FileStream* stream);
bool fsWriteToStreamFloat2(FileStream* stream, float2 value);
bool fsWriteToStreamFloat3(FileStream* stream, float3 value);
bool fsWriteToStreamPackedFloat3(FileStream* stream, float3 value, float maxAbsCoord);
bool fsWriteToStreamFloat4(FileStream* stream, float4 value);
template<typename T>
static inline T fsReadFromStreamType(FileStream* stream) {
T result;
fsReadFromStream(stream, &result, sizeof(T));
return result;
}
template<typename T>
static inline bool fsWriteToStreamType(FileStream* stream, T value) {
return fsWriteToStream(stream, &value, sizeof(T)) == sizeof(T);
}
/// PathHandle is a RAII wrapper for `Path`.
/// Any function that returns a `Path*` may have its result assigned to a `PathHandle`,
/// and that path will automatically be freed when the PathHandle goes out of scope.
class PathHandle {
private:
Path* pPath;
public:
inline PathHandle() : pPath(nullptr) {}
inline PathHandle(Path *path) : pPath(path) {}
inline PathHandle(const FileSystem* fileSystem, const char* absolutePathString) : pPath(fsCreatePath(fileSystem, absolutePathString)) {}
inline PathHandle(const PathHandle& other) : pPath(fsCopyPath(other.pPath)) {}
inline ~PathHandle() {
fsFreePath(pPath);
}
inline operator const Path*() const { return pPath; }
inline PathHandle& operator=( const PathHandle& other ) {
fsFreePath(pPath);
pPath = fsCopyPath(other.pPath);
return *this;
}
inline operator bool() const {
return pPath != nullptr;
}
inline bool operator==(const PathHandle& other) const {
return fsPathsEqual(pPath, other.pPath);
}
};
#endif // ifdef __cplusplus
#if TARGET_OS_IPHONE
void fsRegisterUTIForExtension(const char* uti, const char* extension);
#endif
#endif // ifndef IFileSystem_h
// We define a couple of extras for if EASTL's string type is defined.
// This is under a separate include guard in case this header is included transitively,
// EASTL is included, and then this header is included again.
#if defined(EASTL_STRING_H) && !defined(IFileSystem_h_STLString)
#define IFileSystem_h_STLString
eastl::string fsReadFromStreamSTLString(FileStream* stream);
eastl::string fsReadFromStreamSTLLine(FileStream* stream);
/// Forms an `eastl::string` from `pathComponent`
inline eastl::string fsPathComponentToString(PathComponent pathComponent) {
return eastl::string(pathComponent.buffer, pathComponent.length);
}
#endif // defined(EASTL_STRING_H) && !defined(IFileSystem_h_STLString)
#if defined(EASTL_VECTOR_H) && !defined(IFileSystem_h_STLVector)
#define IFileSystem_h_STLVector
/// Collects the results of calling `fsEnumerateFilesWithExtension` with `directory` and `extension` into an `eastl::vector<PathHandle>`.
eastl::vector<PathHandle> fsGetFilesWithExtension(const Path* directory, const char* extension);
/// Collects the results of calling `fsEnumerateSubdirectories` with `directory` into an `eastl::vector<PathHandle>`.
eastl::vector<PathHandle> fsGetSubDirectories(const Path* directory);
#endif // defined(EASTL_VECTOR_H) && !defined(IFileSystem_h_STLVector)