]> git.xonotic.org Git - xonotic/gmqcc.git/blob - msvc.c
2c6a3ed6656d28bc5d81f1983a2c00e1e6c91f85
[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 #include <string.h>
24 #include <stdlib.h>
25 #include <io.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_sscanf(const char *str, const char *format, ...) {
59     va_list va;
60     va_start(va, format);
61     vsscanf_s(str, format, va);
62     va_end(va);
63 }
64
65 const struct tm *platform_localtime(const time_t *timer) {
66     struct tm *t;
67     t = (struct tm*)platform_mem_allocate(sizeof(struct tm));
68     localtime_s(&t, timer);
69     return &t;
70 }
71
72 const char *platform_ctime(const time_t *timer) {
73     char *buffer = (char *)platform_mem_allocate(CTIME_BUFFER);
74     ctime_s(buffer, CTIME_BUFFER, timer);
75     return buffer;
76 }
77
78 char *platform_strncat(char *dest, const char *src, size_t num) {
79     return strncat_s(dest, num, src, _TRUNCATE);
80 }
81
82 const char *platform_tmpnam(char *str) {
83     return tmpnam_s(str, L_tmpnam);
84 }
85
86 const char *platform_getenv(char *var) {
87     char  *buffer = (char *)platform_mem_allocate(GETENV_BUFFER);
88     size_t size;
89     getenv_s(&size, buffer, GETENV_BUFFER, var);
90     return buffer;
91 }
92
93 int platform_snprintf(char *src, size_t bytes, const char *format, ...) {
94     int      rt;
95     va_list  va;
96     va_start(va, format);
97
98     rt = vsprintf_s(src, bytes, format, va);
99     va_end  (va);
100
101     return rt;
102 }
103
104 /*
105  * TODO: this isn't exactly 'accurate' for MSVC but it seems to work,
106  * at least to some extent.
107  */
108 int platform_vasprintf(char **dat, const char *fmt, va_list args) {
109     int   ret;
110     int   len;
111     char *tmp = NULL;
112
113     if ((len = _vscprintf(fmt, args)) < 0) {
114         *dat = NULL;
115         return -1;
116     }
117
118     tmp = (char*)mem_a(len + 1);
119     if ((ret = _vsnprintf_s(tmp, len+1, len+1, fmt, args)) != len) {
120         mem_d(tmp);
121         *dat = NULL;
122         return -1;
123     }
124     *dat = tmp;
125     return len;
126 }
127
128 char *platform_strcat(char *dest, const char *src) {
129     strcat_s(dest, strlen(src), src);
130     return dest;
131 }
132
133 char *platform_strncpy(char *dest, const char *src, size_t num) {
134     strncpy_s(dest, num, src, num);
135     return dest;
136 }
137
138 const char *platform_strerror(int err) {
139     char *buffer = (char*)platform_mem_allocate(STRERROR_BUFFER);
140     strerror_s(buffer, STRERROR_BUFFER, err);
141     return buffer;
142 }
143
144 FILE *platform_fopen(const char *filename, const char *mode) {
145     FILE *handle;
146     return (fopen_s(&handle, filename, mode) != 0) ? NULL : handle;
147 }
148
149 size_t platform_fread(void *ptr, size_t size, size_t count, FILE *stream) {
150     return fread_s(ptr, size, size, count, stream);
151 }
152
153 size_t platform_fwrite(const void *ptr, size_t size, size_t count, FILE *stream) {
154     return fwrite(ptr, size, count, stream);
155 }
156
157 int platform_fflush(FILE *stream) {
158     return fflush(stream);
159 }
160
161 int platform_vfprintf(FILE *stream, const char *format, va_list arg) {
162     return vfprintf_s(stream, format, arg);
163 }
164
165 int platform_fclose(FILE *stream) {
166     return fclose(stream);
167 }
168
169 int platform_ferror(FILE *stream) {
170     return ferror(stream);
171 }
172
173 int platform_fgetc(FILE *stream) {
174     return fgetc(stream);
175 }
176
177 int platform_fputs(const char *str, FILE *stream) {
178     return fputs(str, stream);
179 }
180
181 int platform_fseek(FILE *stream, long offset, int origin) {
182     return fseek(stream, offset, origin);
183 }
184
185 long platform_ftell(FILE *stream) {
186     return ftell(stream);
187 }
188
189 int platform_mkdir(const char *path, int mode) {
190     return mkdir(path, mode);
191 }
192
193 DIR *platform_opendir(const char *path) {
194     return opendir(path);
195 }
196
197 int platform_closedir(DIR *dir) {
198     return closedir(dir);
199 }
200
201 struct dirent *platform_readdir(DIR *dir) {
202     return readdir(dir);
203 }
204
205 int platform_istty(int fd) {
206     return _istty(fd);
207 }