]> git.xonotic.org Git - xonotic/gmqcc.git/blob - msvc.c
Refactor some util/platform usage and extend file system file interface to accept...
[xonotic/gmqcc.git] / msvc.c
1 /*
2  * Copyright (C) 2012, 2013
3  *     Dale Weiler
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * SOFTWARE.
22  */
23 #define GMQCC_PLATFORM_HEADER
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "platform.h"
28
29 #define CTIME_BUFFER    64
30 #define GETENV_BUFFER   4096
31 #define STRERROR_BUFFER 128
32
33 static void **platform_mem_pool = NULL;
34 static void platform_mem_atexit() {
35     size_t i;
36     for (i = 0; i < vec_size(platform_mem_pool); i++)
37         mem_d(platform_mem_pool[i]);
38     vec_free(platform_mem_pool);
39 }
40
41 static void *platform_mem_allocate(size_t bytes) {
42     void *mem = NULL;
43     if (!platform_mem_pool) {
44         atexit(&platform_mem_atexit);
45         vec_push(platform_mem_pool, NULL);
46     }
47
48     mem = mem_a(bytes);
49     vec_push(platform_mem_pool, mem);
50
51     return mem;
52 }
53
54 int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg) {
55     vsnprintf_s(buffer, bytes, bytes, format, arg);
56 }
57
58 int platform_vsscanf(const char *str, const char *format, va_list va) {
59     return vsscanf_s(str, format, va);
60 }
61
62 const struct tm *platform_localtime(const time_t *timer) {
63     struct tm *t;
64     t = (struct tm*)platform_mem_allocate(sizeof(struct tm));
65     localtime_s(&t, timer);
66     return t;
67 }
68
69 const char *platform_ctime(const time_t *timer) {
70     char *buffer = (char *)platform_mem_allocate(CTIME_BUFFER);
71     ctime_s(buffer, CTIME_BUFFER, timer);
72     return buffer;
73 }
74
75 char *platform_strncat(char *dest, const char *src, size_t num) {
76     return strncat_s(dest, num, src, _TRUNCATE);
77 }
78
79 const char *platform_tmpnam(char *str) {
80     return tmpnam_s(str, L_tmpnam);
81 }
82
83 const char *platform_getenv(char *var) {
84     char  *buffer = (char *)platform_mem_allocate(GETENV_BUFFER);
85     size_t size;
86     getenv_s(&size, buffer, GETENV_BUFFER, var);
87     return buffer;
88 }
89
90 /*
91  * TODO: this isn't exactly 'accurate' for MSVC but it seems to work,
92  * at least to some extent.
93  */
94 int platform_vasprintf(char **dat, const char *fmt, va_list args) {
95     int   ret;
96     int   len;
97     char *tmp = NULL;
98
99     if ((len = _vscprintf(fmt, args)) < 0) {
100         *dat = NULL;
101         return -1;
102     }
103
104     tmp = (char*)mem_a(len + 1);
105     if ((ret = _vsnprintf_s(tmp, len+1, len+1, fmt, args)) != len) {
106         mem_d(tmp);
107         *dat = NULL;
108         return -1;
109     }
110     *dat = tmp;
111     return len;
112 }
113
114 char *platform_strcat(char *dest, const char *src) {
115     strcat_s(dest, strlen(src), src);
116     return dest;
117 }
118
119 char *platform_strncpy(char *dest, const char *src, size_t num) {
120     strncpy_s(dest, num, src, num);
121     return dest;
122 }
123
124 const char *platform_strerror(int err) {
125     char *buffer = (char*)platform_mem_allocate(STRERROR_BUFFER);
126     strerror_s(buffer, STRERROR_BUFFER, err);
127     return buffer;
128 }
129
130 FILE *platform_fopen(const char *filename, const char *mode) {
131     FILE *handle;
132     return (fopen_s(&handle, filename, mode) != 0) ? NULL : handle;
133 }
134
135 size_t platform_fread(void *ptr, size_t size, size_t count, FILE *stream) {
136     return fread_s(ptr, size, size, count, stream);
137 }
138
139 size_t platform_fwrite(const void *ptr, size_t size, size_t count, FILE *stream) {
140     return fwrite(ptr, size, count, stream);
141 }
142
143 int platform_fflush(FILE *stream) {
144     return fflush(stream);
145 }
146
147 int platform_vfprintf(FILE *stream, const char *format, va_list arg) {
148     return vfprintf_s(stream, format, arg);
149 }
150
151 int platform_fclose(FILE *stream) {
152     return fclose(stream);
153 }
154
155 int platform_ferror(FILE *stream) {
156     return ferror(stream);
157 }
158
159 int platform_fgetc(FILE *stream) {
160     return fgetc(stream);
161 }
162
163 int platform_fputs(const char *str, FILE *stream) {
164     return fputs(str, stream);
165 }
166
167 int platform_fseek(FILE *stream, long offset, int origin) {
168     return fseek(stream, offset, origin);
169 }
170
171 long platform_ftell(FILE *stream) {
172     return ftell(stream);
173 }
174
175 int platform_mkdir(const char *path, int mode) {
176     return mkdir(path, mode);
177 }
178
179 DIR *platform_opendir(const char *path) {
180     DIR *dir = (DIR*)mem_a(sizeof(DIR) + strlen(path));
181     if (!dir)
182         return NULL;
183
184     platform_strncpy(dir->dd_name, path, strlen(path));
185     return dir;
186 }
187
188 int platform_closedir(DIR *dir) {
189     FindClose((HANDLE)dir->dd_handle);
190     mem_d((void*)dir);
191     return 0;
192 }
193
194 struct dirent *platform_readdir(DIR *dir) {
195     WIN32_FIND_DATA info;
196     struct dirent  *data;
197     int             ret;
198
199     if (!dir->dd_handle) {
200         char *dirname;
201         if (*dir->dd_name) {
202             size_t n = strlen(dir->dd_name);
203             if ((dir = (char*)mem_a(n+5))) {
204                 platform_strncpy(dirname,     dir->dd_name, n);
205                 platform_strncpy(dirname + n, "\\*.*",      4);
206             }
207         } else {
208             if (!(dirname = util_strdup("\\*.*")))
209                 return NULL;
210         }
211
212         dir->dd_handle = (long)FindFirstFile(dirname, &info);
213         mem_d(dirname);
214         ret = !(!dir->dd_handle);
215     } else if (dir->dd_handle != -11) {
216         ret = FindNextFile((HANDLE)dir->dd_handle, &info);
217     } else {
218         ret = 0;
219     }
220
221     if (!ret)
222         return NULL;
223
224     if ((data = (struct dirent*)mem_a(sizeof(struct dirent)))) {
225         platform_strncpy(data->d_name, info.cFileName, FILENAME_MAX - 1);
226         data->d_name[FILENAME_MAX - 1] = '\0';
227         data->d_namelen                = strlen(data->d_name);
228     }
229
230     return data;
231 }
232
233 int platform_istty(int fd) {
234     return _istty(fd);
235 }