]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ansi.c
Constant folding for strings
[xonotic/gmqcc.git] / ansi.c
1 /*
2  * Copyright (C) 2012, 2013, 2014, 2015
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 #include "gmqcc.h"
29
30 int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg) {
31     return vsnprintf(buffer, bytes, format, arg);
32 }
33
34 int platform_vsscanf(const char *str, const char *format, va_list arg) {
35     return vsscanf(str, format, arg);
36 }
37
38 const struct tm *platform_localtime(const time_t *timer) {
39     return localtime(timer);
40 }
41
42 const char *platform_ctime(const time_t *timer) {
43     return ctime(timer);
44 }
45
46 char *platform_strncat(char *dest, const char *src, size_t num) {
47     return strncat(dest, src, num);
48 }
49
50 const char *platform_getenv(const char *var) {
51     return getenv(var);
52 }
53
54 int platform_vasprintf(char **dat, const char *fmt, va_list args) {
55     int     ret;
56     int     len;
57     char   *tmp = NULL;
58     char    buf[128];
59     va_list cpy;
60
61     va_copy(cpy, args);
62     len = vsnprintf(buf, sizeof(buf), fmt, cpy);
63     va_end (cpy);
64
65     if (len < 0)
66         return len;
67
68     if (len < (int)sizeof(buf)) {
69         *dat = util_strdup(buf);
70         return len;
71     }
72
73     tmp = (char*)mem_a(len + 1);
74     if ((ret = vsnprintf(tmp, len + 1, fmt, args)) != len) {
75         mem_d(tmp);
76         *dat = NULL;
77         return -1;
78     }
79
80     *dat = tmp;
81     return len;
82 }
83
84 char *platform_strcat(char *dest, const char *src) {
85     return strcat(dest, src);
86 }
87
88 char *platform_strncpy(char *dest, const char *src, size_t num) {
89     return strncpy(dest, src, num);
90 }
91
92 const char *platform_strerror(int err) {
93     return strerror(err);
94 }
95
96 FILE *platform_fopen(const char *filename, const char *mode) {
97     return fopen(filename, mode);
98 }
99
100 size_t platform_fread(void *ptr, size_t size, size_t count, FILE *stream) {
101     return fread(ptr, size, count, stream);
102 }
103
104 size_t platform_fwrite(const void *ptr, size_t size, size_t count, FILE *stream) {
105     return fwrite(ptr, size, count, stream);
106 }
107
108 int platform_fflush(FILE *stream) {
109     return fflush(stream);
110 }
111
112 int platform_vfprintf(FILE *stream, const char *format, va_list arg) {
113     return vfprintf(stream, format, arg);
114 }
115
116 int platform_fclose(FILE *stream) {
117     return fclose(stream);
118 }
119
120 int platform_ferror(FILE *stream) {
121     return ferror(stream);
122 }
123
124 int platform_fgetc(FILE *stream) {
125     return fgetc(stream);
126 }
127
128 int platform_fputs(const char *str, FILE *stream) {
129     return fputs(str, stream);
130 }
131
132 int platform_fseek(FILE *stream, long offset, int origin) {
133     return fseek(stream, offset, origin);
134 }
135
136 long platform_ftell(FILE *stream) {
137     return ftell(stream);
138 }
139
140 int platform_mkdir(const char *path, int mode) {
141     /*
142      * For some reason mingw32 just doesn't have a correct mkdir impl
143      * so we handle that here.
144      */
145 #   ifdef _WIN32
146     (void)mode;
147     return mkdir(path);
148 #   else
149     return mkdir(path, mode);
150 #   endif /*!_WIN32*/
151 }
152
153 DIR *platform_opendir(const char *path) {
154     return opendir(path);
155 }
156
157 int platform_closedir(DIR *dir) {
158     return closedir(dir);
159 }
160
161 struct dirent *platform_readdir(DIR *dir) {
162     return readdir(dir);
163 }
164
165 int platform_isatty(int fd) {
166     return isatty(fd);
167 }