-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileManager.h
More file actions
122 lines (105 loc) · 3.11 KB
/
FileManager.h
File metadata and controls
122 lines (105 loc) · 3.11 KB
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
#ifndef FILEMANAGER_H
#define FILEMANAGER_H
#include <stdio.h>
#define MAX_QPATH 64 // max length of a quake game pathname
#define MAX_OSPATH 128 // max length of a filesystem pathname
// in memory
typedef struct {
char name[MAX_QPATH];
int filepos, filelen;
} packfile_t;
typedef struct pack_s {
char filename[MAX_OSPATH];
int handle;
int numfiles;
packfile_t *files;
} pack_t;
typedef struct searchpath_s {
char filename[MAX_OSPATH];
pack_t *pack; // only one of filename / pack will be used
struct searchpath_s *next;
} searchpath_t;
class File {
private:
FILE *obj;
char *path;
char openType[4];
public:
enum FileOpenType { READ, WRITE, APPEND };
File(const char *path, FileOpenType mode, bool isBinary);
~File();
bool isOpen();
long getLength();
size_t read(void *buffer, size_t size);
size_t write(const void *buffer, size_t size);
void seek(long position);
};
class FileManager {
public:
static searchpath_t *searchpaths;
/**
* Returns true if the filename passed in points to an existing file.
*/
static bool FileExists(const char *filename);
static int OpenFile(const char *filename, int *hndl);
static int FOpenFile(const char *filename, FILE **file);
static int FindFile(const char *filename, int *handle, FILE **file);
static void WriteFile(const char *filename, void *data, int len);
static void CloseFile(int h);
/**
* Converts any illegal file characeters into legal ones.
*
* @param data to check and fix
*/
static void MakeFilenameValid(char *data);
/**
* Copy string without extension.
*/
static void StripExtension(const char *in, char *out);
/**
* Return a pointer to the start of the file extension.
*/
static const char *FileExtension(const char *in);
/**
* If the path doesn't contain an extension .EXT then add the provided
* default.
*/
static void DefaultExtension(char *path, const char *extension);
static void FileBase(const char *in, char *out);
/**
* Console command to print out the search paths
*/
static void PathCmd(void);
/**
* Sets com_gamedir, adds the directory to the head of the path,
* then loads and adds pak1.pak pak2.pak ...
*/
static void AddGameDirectory(char *dir);
/**
* Add a pak to our list of places to search for files
*/
static void AddPackToPath(const char *pak);
/**
* Takes an explicit (not game tree related) path to a pak file.
*
* Loads the header and directory, adding the files at the beginning of the list
* so they override previous pack files.
*/
static pack_t *LoadPackFile(const char *packfile);
};
class SystemFileManager {
public:
static int FileLength(FILE *f);
static int FileOpenRead(const char *path, int *hndl);
static int FileOpenWrite(const char *path);
static void FileClose(int handle);
static void FileSeek(int handle, long pos);
static int FileRead(int handle, void *dest, size_t count);
static size_t FileWrite(int handle, const void *data, size_t count);
private:
static const int MAX_HANDLES = 10;
static File *handles[MAX_HANDLES];
static int findHandle();
static File *getFileForHandle(int handle);
};
#endif /* FILEMANAGER_H */