UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filesys.h
Go to the documentation of this file.
1 
6 /*
7 All original material Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 Copyright (C) 1997-2001 Id Software, Inc.
10 
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 See the GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 
26 */
27 
28 #pragma once
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include "../shared/ufotypes.h"
33 #include "../shared/cxx.h"
34 #define BASEDIRNAME "base"
35 
36 /* Maximum maps constant */
37 #define MAX_MAPS 400
38 
39 /* max length of the ufo virtual filesystem */
40 #define MAX_QPATH 64
41 
42 /* max length of a filesystem pathname
43  * windows + linux 256, macosx 32 */
44 #define MAX_OSPATH 256
45 
46 /* max files in a directory */
47 #define MAX_FILES 512
48 
49 /*
50 ==============================================================
51 FILESYSTEM
52 ==============================================================
53 */
54 typedef struct qFILE_s {
55  void* z;
56  FILE* f;
58  unsigned long filepos;
59  unsigned long size;
60 
61  inline FILE* getFile () const
62  {
63  return f;
64  }
65 } qFILE;
66 
67 typedef enum {
72 } fsMode_t;
73 
74 typedef enum {
78 } fsOrigin_t;
79 
81 typedef struct filelink_s {
82  struct filelink_s* next;
83  char* from;
85  char* to;
86 } filelink_t;
87 
88 typedef struct {
89  char name[MAX_QPATH];
90  unsigned long filepos;
91  unsigned long filelen;
92 } packfile_t;
93 
94 typedef struct pack_s {
97  int numfiles;
99 } pack_t;
100 
101 typedef struct searchpath_s {
104  bool write;
107 } searchpath_t;
108 
110 typedef enum {
112 } filemode_t;
113 
114 /*
115 ==============================================================
116 FILESYSTEM
117 ==============================================================
118 */
119 extern char* fs_maps[MAX_MAPS];
120 extern int fs_numInstalledMaps;
121 
122 /* directory searching */
123 #define SFF_ARCH 0x01
124 #define SFF_HIDDEN 0x02
125 #define SFF_RDONLY 0x04
126 #define SFF_SUBDIR 0x08
127 #define SFF_SYSTEM 0x10
128 
129 int FS_FileLength(qFILE* f);
130 int FS_Seek(qFILE* f, long offset, int origin);
131 int FS_WriteFile(const void* buffer, size_t len, const char* filename);
132 int FS_Write(const void* buffer, int len, qFILE* f);
133 int FS_Printf(qFILE* f, const char* msg, ...) __attribute__((format(__printf__, 2, 3)));
134 void FS_InitFilesystem(bool writeToHomeDir);
135 void FS_AddGameDirectory(const char* dir, bool write);
136 void FS_AddHomeAsGameDirectory(const char* dir, bool write);
137 void FS_RestartFilesystem(const char* gamedir);
138 const char* FS_Gamedir(void);
139 void FS_CreateOpenPipeFile(const char* filename, qFILE* f);
140 const char* FS_NextPath(const char* prevpath);
141 void FS_ExecAutoexec(void);
142 int FS_GetModList(struct linkedList_t** mods);
143 const char* FS_GetCwd(void);
144 void FS_NormPath(char* path);
145 bool FS_FileExists(const char* filename, ...) __attribute__((format(__printf__, 1, 2)));
146 
147 void FS_GetMaps(bool reset);
148 
149 int FS_OpenFile(const char* filename, qFILE* file, filemode_t mode);
150 void FS_CloseFile(qFILE* f);
151 
152 bool FS_RenameFile(const char* from, const char* to, bool relative);
153 void FS_RemoveFile(const char* osPath);
154 void FS_CopyFile(const char* fromOSPath, const char* toOSPath);
155 
156 /* note: this can't be called from another DLL, due to MS libc issues */
157 
158 int FS_LoadFile(const char* path, byte** buffer);
159 
160 /* a null buffer will just return the file length without loading */
161 /* a -1 length is not present */
162 
163 int FS_Read2(void* buffer, int len, qFILE* f, bool failOnEmptyRead);
164 int FS_Read(void* buffer, int len, qFILE* f);
165 /* properly handles partial reads */
166 
167 void FS_FreeFile(void* buffer);
168 
169 int FS_CheckFile(const char* fmt, ...) __attribute__((format(__printf__, 1, 2)));
170 
171 int FS_BuildFileList(const char* files);
172 const char* FS_NextFileFromFileList(const char* files);
173 char* FS_NextScriptHeader(const char* files, const char** name, const char** text);
174 void FS_CreatePath(const char* path);
175 
176 /* Make sure we have this available */
177 char** FS_ListFiles(const char* findname, int* numfiles, unsigned musthave, unsigned canthave);
178 
179 const char* FS_GetFileData(const char* files);
180 
184 void FS_Shutdown(void);
185 
186 class ScopedFile {
187 private:
189 public:
191  {
192  memset(&_file, 0, sizeof(_file));
193  }
195  {
196  FS_CloseFile(&_file);
197  }
198  inline qFILE* operator& ()
199  {
200  return &_file;
201  }
202  inline operator bool () const
203  {
204  return file() || zip();
205  }
206  inline bool file () const
207  {
208  return _file.f;
209  }
210  inline bool zip () const
211  {
212  return _file.z;
213  }
214  inline FILE* getFile () const
215  {
216  return _file.getFile();
217  }
218 };
void FS_CopyFile(const char *fromOSPath, const char *toOSPath)
Copy a fully specified file from one place to another.
Definition: files.cpp:1652
bool FS_FileExists(const char *filename,...) __attribute__((format(__printf__
bool write
Definition: filesys.h:104
void FS_ExecAutoexec(void)
bool zip() const
Definition: filesys.h:210
void FS_CreatePath(const char *path)
Creates any directories needed to store the given filename.
Definition: files.cpp:117
int FS_Read2(void *buffer, int len, qFILE *f, bool failOnEmptyRead)
Read a file into a given buffer in memory.
Definition: files.cpp:327
const char * FS_NextFileFromFileList(const char *files)
Returns the next file that is found in the virtual filesystem identified by the given file pattern...
Definition: files.cpp:1079
bool file() const
Definition: filesys.h:206
int FS_WriteFile(const void *buffer, size_t len, const char *filename)
Definition: files.cpp:1544
void FS_CreateOpenPipeFile(const char *filename, qFILE *f)
Definition: files.cpp:47
voidpf uLong int origin
Definition: ioapi.h:45
const char * FS_NextPath(const char *prevpath)
Allows enumerating all of the directories in the search path.
Definition: files.cpp:614
unsigned long filepos
Definition: filesys.h:90
struct searchpath_s * next
Definition: filesys.h:106
const char * filename
Definition: ioapi.h:41
#define FILE
Definition: test_webapi.cpp:30
const char * FS_Gamedir(void)
Called to find where to write a file (savegames, etc)
Definition: files.cpp:68
#define MAX_MAPS
Definition: filesys.h:37
char * FS_NextScriptHeader(const char *files, const char **name, const char **text)
Definition: files.cpp:1194
int void FS_InitFilesystem(bool writeToHomeDir)
Definition: files.cpp:888
#define MAX_OSPATH
Definition: filesys.h:44
const char * FS_GetCwd(void)
Return current working dir.
Definition: files.cpp:1568
Definition: filesys.h:94
FILE * getFile() const
Definition: filesys.h:214
int FS_OpenFile(const char *filename, qFILE *file, filemode_t mode)
Finds and opens the file in the search path.
Definition: files.cpp:162
#define __attribute__(x)
Definition: cxx.h:37
void FS_NormPath(char *path)
Convert operating systems path separators to ufo virtual filesystem separators (/) ...
Definition: files.cpp:83
static char findname[MAX_OSPATH]
Definition: win_shared.cpp:167
struct filelink_s filelink_t
Links one file onto another - like a symlink.
char ** FS_ListFiles(const char *findname, int *numfiles, unsigned musthave, unsigned canthave)
Builds a qsorted filelist.
Definition: files.cpp:562
int FS_GetModList(struct linkedList_t **mods)
Searches and builds a list of mod directories.
Definition: files.cpp:669
packfile_t * files
Definition: filesys.h:98
void FS_CloseFile(qFILE *f)
Closes a file handle.
int numfiles
Definition: filesys.h:97
int fs_numInstalledMaps
Definition: files.cpp:1312
ScopedFile()
Definition: filesys.h:190
void FS_RestartFilesystem(const char *gamedir)
Restart the filesystem (reload all pk3 files)
Definition: files.cpp:1641
struct qFILE_s qFILE
~ScopedFile()
Definition: filesys.h:194
void FS_Shutdown(void)
cleanup function
Definition: files.cpp:1602
qFILE _file
Definition: filesys.h:188
int FS_Read(void *buffer, int len, qFILE *f)
Definition: files.cpp:371
bool FS_RenameFile(const char *from, const char *to, bool relative)
Renames a file.
Definition: files.cpp:1707
bool void FS_GetMaps(bool reset)
File the fs_maps array with valid maps.
Definition: files.cpp:1373
void FS_RemoveFile(const char *osPath)
Definition: files.cpp:1690
qFILE handle
Definition: filesys.h:96
filemode_t
Definition: filesys.h:110
pack_t * pack
Definition: filesys.h:103
int FS_FileLength(qFILE *f)
Returns the size of a given file or -1 if no file is opened.
Definition: files.cpp:91
int FS_Printf(qFILE *f, const char *msg,...) __attribute__((format(__printf__
unsigned long filepos
Definition: filesys.h:58
struct pack_s pack_t
struct searchpath_s searchpath_t
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
char * fs_maps[MAX_MAPS]
Definition: files.cpp:1311
void FS_FreeFile(void *buffer)
Definition: files.cpp:411
#define MAX_QPATH
Definition: filesys.h:40
void FS_AddHomeAsGameDirectory(const char *dir, bool write)
Definition: files.cpp:655
QGL_EXTERN GLuint GLchar GLuint * len
Definition: r_gl.h:99
int FS_CheckFile(const char *fmt,...) __attribute__((format(__printf__
int FS_LoadFile(const char *path, byte **buffer)
Filenames are relative to the quake search path.
Definition: files.cpp:384
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
const char int mode
Definition: ioapi.h:41
FILE * getFile() const
Definition: filesys.h:61
const char * FS_GetFileData(const char *files)
Returns the buffer of a file.
Definition: files.cpp:1135
void FS_AddGameDirectory(const char *dir, bool write)
Adds the directory to the head of the search path.
Definition: files.cpp:495
voidpf uLong offset
Definition: ioapi.h:45
int FS_Write(const void *buffer, int len, qFILE *f)
Properly handles partial writes.
Definition: files.cpp:1511
char name[MAX_OSPATH]
Definition: filesys.h:57
fsMode_t
Definition: filesys.h:67
void * z
Definition: filesys.h:55
unsigned long filelen
Definition: filesys.h:91
uint8_t byte
Definition: ufotypes.h:34
int int FS_BuildFileList(const char *files)
Build a filelist.
Definition: files.cpp:960
fsOrigin_t
Definition: filesys.h:74
int FS_Seek(qFILE *f, long offset, int origin)
Sets the file position of the given file.
Definition: files.cpp:246
unsigned long size
Definition: filesys.h:59
void format(__printf__, 1, 2)))
FILE * f
Definition: filesys.h:56