]> git.xonotic.org Git - xonotic/gmqcc.git/blob - msvc.c
Initial platform / compiler specific code refactoring.
[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 "gmqcc.h"
24
25 #define CTIME_BUFFER    64
26 #define GETENV_BUFFER   4096
27 #define STRERROR_BUFFER 128
28
29 static void **platform_mem_pool = NULL;
30 static void platform_mem_atexit() {
31     size_t i;
32     for (i = 0; i < vec_size(platform_mem_pool); i++)
33         mem_d(platform_mem_pool[i]);
34     vec_free(platform_mem_pool);
35 }
36
37 static void *platform_mem_allocate(size_t bytes) {
38     void *mem = NULL;
39     if (!platform_mem_pool) {
40         atexit(&platform_mem_atexit);
41         vec_push(platform_mem_pool, NULL);
42     }
43
44     mem = mem_a(bytes);
45     vec_push(platform_mem_pool, mem);
46
47     return mem;
48 }
49
50 int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg) {
51     vsnprintf_s(buffer, bytes, bytes, format, arg);
52 }
53
54 int platform_sscanf(const char *str, const char *format, ...) {
55     va_list va;
56     va_start(va, format);
57     vsscanf_s(str, format, va);
58     va_end(va);
59 }
60
61 const struct tm *platform_localtime(const time_t *timer) {
62     struct tm *t;
63     t = (struct tm*)platform_mem_allocate(sizeof(struct tm));
64     localtime_s(&t, timer);
65     return &t;
66 }
67
68 const char *platform_ctime(const time_t *timer) {
69     char *buffer = (char *)platform_mem_allocate(CTIME_BUFFER);
70     ctime_s(buffer, CTIME_BUFFER, timer);
71     return buffer;
72 }
73
74 char *platform_strncat(char *dest, const char *src, size_t num) {
75     return strncat_s(dest, num, src, _TRUNCATE);
76 }
77
78 const char *platform_tmpnam(char *str) {
79     return tmpnam_s(str, L_tmpnam);
80 }
81
82 const char *platform_getenv(char *var) {
83     char  *buffer = (char *)platform_mem_allocate(GETENV_BUFFER);
84     size_t size;
85     getenv_s(&size, buffer, GETENV_BUFFER, var);
86     return buffer;
87 }
88
89 int platform_snprintf(char *src, size_t bytes, const char *format, ...) {
90     int      rt;
91     va_list  va;
92     va_start(va, format);
93
94     rt = vsprintf_s(src, bytes, format, va);
95     va_end  (va);
96
97     return rt;
98 }
99
100 char *platform_strcat(char *dest, const char *src) {
101     strcat_s(dest, strlen(src), src);
102     return dest;
103 }
104
105 char *platform_strncpy(char *dest, const char *src, size_t num) {
106     strncpy_s(dest, num, src, num);
107     return dest;
108 }
109
110 const char *platform_strerror(int err) {
111     char *buffer = (char*)platform_mem_allocate(STRERROR_BUFFER);
112     strerror_s(buffer, STRERROR_BUFFER, err);
113     return buffer;
114 }