2 * Copyright (C) 2012, 2013
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * This is essentially a "wrapper" interface around standard C's IO
27 * library. There is two reason we implement this, 1) visual studio
28 * hearts for "secure" varations, as part of it's "Security Enhancements
29 * in the CRT" (http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx).
30 * 2) But one of the greater reasons is for the possibility of large file
31 * support in the future. I don't expect to reach the 2GB limit any
32 * time soon (mainly because that would be insane). But when it comes
33 * to adding support for some other larger IO tasks (in the test-suite,
34 * or even the QCVM we'll need it). There is also a third possibility of
35 * building .dat files directly from zip files (which would be very cool
36 * at least I think so).
41 * Visual Studio has security CRT features which I actually want to support
42 * if we ever port to Windows 8, and want GMQCC to be API safe.
44 * We handle them here, for all file-operations.
47 static void file_exception (
48 const wchar_t *expression,
49 const wchar_t *function,
54 wprintf(L"Invalid parameter dectected %s:%d %s [%s]\n", file, line, function, expression);
55 wprintf(L"Aborting ...\n");
59 static void file_init() {
60 static bool init = false;
65 _set_invalid_parameter_handler(&file_exception);
68 * Turnoff the message box for CRT asserations otherwise
69 * we don't get the error reported to the console as we should
72 _CrtSetReportMode(_CRT_ASSERT, 0);
77 FILE *fs_file_open(const char *filename, const char *mode) {
81 return (fopen_s(&handle, filename, mode) != 0) ? NULL : handle;
84 size_t fs_file_read(void *buffer, size_t size, size_t count, FILE *fp) {
86 return fread_s(buffer, size*count, size, count, fp);
89 int fs_file_printf(FILE *fp, const char *format, ...) {
95 rt = vfprintf_s(fp, format, va);
105 * All other compilers/platforms that don't restrict insane policies on
106 * IO for no aparent reason.
108 FILE *fs_file_open(const char *filename, const char *mode) {
109 return fopen(filename, mode);
112 size_t fs_file_read(void *buffer, size_t size, size_t count, FILE *fp) {
113 return fread(buffer, size, count, fp);
116 int fs_file_printf(FILE *fp, const char *format, ...) {
119 va_start(va, format);
120 rt = vfprintf(fp, format, va);
130 * These are implemented as just generic wrappers to keep consistency in
131 * the API. Not as macros though
133 void fs_file_close(FILE *fp) {
134 /* Invokes file_exception on windows if fp is null */
138 size_t fs_file_write (
144 /* Invokes file_exception on windows if fp is null */
145 return fwrite(buffer, size, count, fp);
148 int fs_file_error(FILE *fp) {
149 /* Invokes file_exception on windows if fp is null */
153 int fs_file_getc(FILE *fp) {
154 /* Invokes file_exception on windows if fp is null */
158 int fs_file_puts(FILE *fp, const char *str) {
159 /* Invokes file_exception on windows if fp is null */
160 return fputs(str, fp);
163 int fs_file_seek(FILE *fp, long int off, int whence) {
164 /* Invokes file_exception on windows if fp is null */
165 return fseek(fp, off, whence);
168 int fs_file_flush(FILE *fp) {
169 /* Invokes file_exception on windows if fp is null */
173 long int fs_file_tell(FILE *fp) {
174 /* Invokes file_exception on windows if fp is null */
179 * Implements libc getline for systems that don't have it, which is
180 * assmed all. This works the same as getline().
182 int fs_file_getline(char **lineptr, size_t *n, FILE *stream) {
187 if (!lineptr || !n || !stream)
190 if (!(*lineptr = (char*)mem_a((*n=64))))
198 int c = fs_file_getc(stream);
201 *n += (*n > 16) ? *n : 64;
202 chr = *n + *lineptr - pos;
203 if (!(*lineptr = (char*)mem_r(*lineptr,*n)))
205 pos = *n - chr + *lineptr;
223 return (ret = pos - *lineptr);
227 * Now we implement some directory functionality. Windows lacks dirent.h
228 * this is such a pisss off, we implement it here.
230 #if defined(_WIN32) && !defined(__MINGW32__)
231 DIR *fs_dir_open(const char *name) {
232 DIR *dir = (DIR*)mem_a(sizeof(DIR) + strlen(name));
236 strncpy(dir->dd_name, name, strlen(name));
240 int fs_dir_close(DIR *dir) {
241 FindClose((HANDLE)dir->dd_handle);
246 struct dirent *fs_dir_read(DIR *dir) {
247 WIN32_FIND_DATA info;
251 if (!dir->dd_handle) {
254 size_t n = strlen(dir->dd_name);
255 if ((dirname = (char*)mem_a(n + 5) /* 4 + 1 */)) {
256 strncpy(dirname, dir->dd_name, n);
257 strncpy(dirname + n, "\\*.*", 4); /* 4 + 1 */
260 if (!(dirname = util_strdup("\\*.*")))
264 dir->dd_handle = (long)FindFirstFile(dirname, &info);
266 rets = !(!dir->dd_handle);
267 } else if (dir->dd_handle != -11) {
268 rets = FindNextFile ((HANDLE)dir->dd_handle, &info);
276 if ((data = (struct dirent*)mem_a(sizeof(struct dirent)))) {
277 strncpy(data->d_name, info.cFileName, FILENAME_MAX - 1);
278 data->d_name[FILENAME_MAX - 1] = '\0'; /* terminate */
279 data->d_namlen = strlen(data->d_name);
284 int fs_dir_change(const char *path) {
285 return !SetCurrentDirectory(path);
288 int fs_dir_make(const char *path) {
289 return !CreateDirectory(path, NULL);
293 * Visual studio also lacks S_ISDIR for sys/stat.h, so we emulate this as well
294 * which is not hard at all.
297 # define S_ISDIR(X) ((X)&_S_IFDIR)
299 # if !defined(__MINGW32__)
300 # include <sys/stat.h> /* mkdir */
301 # include <unistd.h> /* chdir */
303 int fs_dir_make(const char *path) {
304 return mkdir(path, 0700);
307 int fs_dir_make(const char *path) {
310 # endif /*! !defined(__MINGW32__) */
312 DIR *fs_dir_open(const char *name) {
313 return opendir(name);
316 int fs_dir_close(DIR *dir) {
317 return closedir(dir);
320 struct dirent *fs_dir_read(DIR *dir) {
324 int fs_dir_change(const char *path) {
328 #endif /*! defined(_WIN32) && !defined(__MINGW32__) */