]> git.xonotic.org Git - xonotic/gmqcc.git/blob - msvc.c
Mage inexact travel across constants.
[xonotic/gmqcc.git] / msvc.c
1 /*
2  * Copyright (C) 2012, 2013, 2014
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     return 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_getenv(const char *var) {
80     char  *buffer = (char *)platform_mem_allocate(GETENV_BUFFER);
81     size_t size;
82     getenv_s(&size, buffer, GETENV_BUFFER, var);
83     return buffer;
84 }
85
86 /*
87  * TODO: this isn't exactly 'accurate' for MSVC but it seems to work,
88  * at least to some extent.
89  */
90 int platform_vasprintf(char **dat, const char *fmt, va_list args) {
91     int   ret;
92     int   len;
93     char *tmp = NULL;
94
95     if ((len = _vscprintf(fmt, args)) < 0) {
96         *dat = NULL;
97         return -1;
98     }
99
100     tmp = (char*)mem_a(len + 1);
101     if ((ret = _vsnprintf_s(tmp, len+1, len+1, fmt, args)) != len) {
102         mem_d(tmp);
103         *dat = NULL;
104         return -1;
105     }
106     *dat = tmp;
107     return len;
108 }
109
110 char *platform_strcat(char *dest, const char *src) {
111     strcat_s(dest, strlen(src), src);
112     return dest;
113 }
114
115 char *platform_strncpy(char *dest, const char *src, size_t num) {
116     strncpy_s(dest, num, src, num);
117     return dest;
118 }
119
120 const char *platform_strerror(int err) {
121     char *buffer = (char*)platform_mem_allocate(STRERROR_BUFFER);
122     strerror_s(buffer, STRERROR_BUFFER, err);
123     return buffer;
124 }
125
126 FILE *platform_fopen(const char *filename, const char *mode) {
127     FILE *handle;
128     return (fopen_s(&handle, filename, mode) != 0) ? NULL : handle;
129 }
130
131 size_t platform_fread(void *ptr, size_t size, size_t count, FILE *stream) {
132     return fread_s(ptr, size, size, count, stream);
133 }
134
135 size_t platform_fwrite(const void *ptr, size_t size, size_t count, FILE *stream) {
136     return fwrite(ptr, size, count, stream);
137 }
138
139 int platform_fflush(FILE *stream) {
140     return fflush(stream);
141 }
142
143 int platform_vfprintf(FILE *stream, const char *format, va_list arg) {
144     return vfprintf_s(stream, format, arg);
145 }
146
147 int platform_fclose(FILE *stream) {
148     return fclose(stream);
149 }
150
151 int platform_ferror(FILE *stream) {
152     return ferror(stream);
153 }
154
155 int platform_fgetc(FILE *stream) {
156     return fgetc(stream);
157 }
158
159 int platform_fputs(const char *str, FILE *stream) {
160     return fputs(str, stream);
161 }
162
163 int platform_fseek(FILE *stream, long offset, int origin) {
164     return fseek(stream, offset, origin);
165 }
166
167 long platform_ftell(FILE *stream) {
168     return ftell(stream);
169 }
170
171 int platform_mkdir(const char *path, int mode) {
172     return mkdir(path, mode);
173 }
174
175 DIR *platform_opendir(const char *path) {
176     DIR *dir = (DIR*)mem_a(sizeof(DIR) + strlen(path));
177     if (!dir)
178         return NULL;
179
180     platform_strncpy(dir->dd_name, path, strlen(path));
181     return dir;
182 }
183
184 int platform_closedir(DIR *dir) {
185     FindClose((HANDLE)dir->dd_handle);
186     mem_d((void*)dir);
187     return 0;
188 }
189
190 struct dirent *platform_readdir(DIR *dir) {
191     WIN32_FIND_DATA info;
192     struct dirent  *data;
193     int             ret;
194
195     if (!dir->dd_handle) {
196         char *dirname;
197         if (*dir->dd_name) {
198             size_t n = strlen(dir->dd_name);
199             if ((dir = (char*)mem_a(n+5))) {
200                 platform_strncpy(dirname,     dir->dd_name, n);
201                 platform_strncpy(dirname + n, "\\*.*",      4);
202             }
203         } else {
204             if (!(dirname = util_strdup("\\*.*")))
205                 return NULL;
206         }
207
208         dir->dd_handle = (long)FindFirstFile(dirname, &info);
209         mem_d(dirname);
210         ret = !(!dir->dd_handle);
211     } else if (dir->dd_handle != -11) {
212         ret = FindNextFile((HANDLE)dir->dd_handle, &info);
213     } else {
214         ret = 0;
215     }
216
217     if (!ret)
218         return NULL;
219
220     if ((data = (struct dirent*)mem_a(sizeof(struct dirent)))) {
221         platform_strncpy(data->d_name, info.cFileName, FILENAME_MAX - 1);
222         data->d_name[FILENAME_MAX - 1] = '\0';
223         data->d_namelen                = strlen(data->d_name);
224     }
225
226     return data;
227 }
228
229 int platform_istty(int fd) {
230     return _isatty(fd);
231 }