]> git.xonotic.org Git - xonotic/gmqcc.git/blob - msvc.c
More cleanup
[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 char *platform_strcat(char *dest, const char *src) {
105     strcat_s(dest, strlen(src), src);
106     return dest;
107 }
108
109 char *platform_strncpy(char *dest, const char *src, size_t num) {
110     strncpy_s(dest, num, src, num);
111     return dest;
112 }
113
114 const char *platform_strerror(int err) {
115     char *buffer = (char*)platform_mem_allocate(STRERROR_BUFFER);
116     strerror_s(buffer, STRERROR_BUFFER, err);
117     return buffer;
118 }
119
120 FILE *platform_fopen(const char *filename, const char *mode) {
121     FILE *handle;
122     return (fopen_s(&handle, filename, mode) != 0) ? NULL : handle;
123 }
124
125 size_t platform_fread(void *ptr, size_t size, size_t count, FILE *stream) {
126     return fread_s(ptr, size, size, count, stream);
127 }
128
129 size_t platform_fwrite(const void *ptr, size_t size, size_t count, FILE *stream) {
130     return fwrite(ptr, size, count, stream);
131 }
132
133 int platform_vfprintf(FILE *stream, const char *format, va_list arg) {
134     return vfprintf_s(stream, format, arg);
135 }
136
137 int platform_fclose(FILE *stream) {
138     return fclose(stream);
139 }
140
141 int platform_ferror(FILE *stream) {
142     return ferror(stream);
143 }
144
145 int platform_fgetc(FILE *stream) {
146     return fgetc(stream);
147 }
148
149 int platform_fputs(const char *str, FILE *stream) {
150     return fputs(str, stream);
151 }
152
153 int platform_fseek(FILE *stream, long offset, int origin) {
154     return fseek(stream, offset, origin);
155 }
156
157 long platform_ftell(FILE *stream) {
158     return ftell(stream);
159 }
160
161 int platform_mkdir(const char *path, int mode) {
162     return mkdir(path, mode);
163 }
164
165 DIR *platform_opendir(const char *path) {
166     return opendir(path);
167 }
168
169 int platform_closedir(DIR *dir) {
170     return closedir(dir);
171 }
172
173 struct dirent *platform_readdir(DIR *dir) {
174     return readdir(dir);
175 }
176
177 int platform_istty(int fd) {
178     return _istty(fd);
179 }