2 * Copyright (C) 2012, 2013
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
29 #define CTIME_BUFFER 64
30 #define GETENV_BUFFER 4096
31 #define STRERROR_BUFFER 128
33 static void **platform_mem_pool = NULL;
34 static void platform_mem_atexit() {
36 for (i = 0; i < vec_size(platform_mem_pool); i++)
37 mem_d(platform_mem_pool[i]);
38 vec_free(platform_mem_pool);
41 static void *platform_mem_allocate(size_t bytes) {
43 if (!platform_mem_pool) {
44 atexit(&platform_mem_atexit);
45 vec_push(platform_mem_pool, NULL);
49 vec_push(platform_mem_pool, mem);
54 int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg) {
55 vsnprintf_s(buffer, bytes, bytes, format, arg);
58 int platform_sscanf(const char *str, const char *format, ...) {
61 vsscanf_s(str, format, va);
65 const struct tm *platform_localtime(const time_t *timer) {
67 t = (struct tm*)platform_mem_allocate(sizeof(struct tm));
68 localtime_s(&t, timer);
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);
78 char *platform_strncat(char *dest, const char *src, size_t num) {
79 return strncat_s(dest, num, src, _TRUNCATE);
82 const char *platform_tmpnam(char *str) {
83 return tmpnam_s(str, L_tmpnam);
86 const char *platform_getenv(char *var) {
87 char *buffer = (char *)platform_mem_allocate(GETENV_BUFFER);
89 getenv_s(&size, buffer, GETENV_BUFFER, var);
93 int platform_snprintf(char *src, size_t bytes, const char *format, ...) {
98 rt = vsprintf_s(src, bytes, format, va);
105 * TODO: this isn't exactly 'accurate' for MSVC but it seems to work,
106 * at least to some extent.
108 int platform_vasprintf(char **dat, const char *fmt, va_list args) {
113 if ((len = _vscprintf(fmt, args)) < 0) {
118 tmp = (char*)mem_a(len + 1);
119 if ((ret = _vsnprintf_s(tmp, len+1, len+1, fmt, args)) != len) {
128 char *platform_strcat(char *dest, const char *src) {
129 strcat_s(dest, strlen(src), src);
133 char *platform_strncpy(char *dest, const char *src, size_t num) {
134 strncpy_s(dest, num, src, num);
138 const char *platform_strerror(int err) {
139 char *buffer = (char*)platform_mem_allocate(STRERROR_BUFFER);
140 strerror_s(buffer, STRERROR_BUFFER, err);
144 FILE *platform_fopen(const char *filename, const char *mode) {
146 return (fopen_s(&handle, filename, mode) != 0) ? NULL : handle;
149 size_t platform_fread(void *ptr, size_t size, size_t count, FILE *stream) {
150 return fread_s(ptr, size, size, count, stream);
153 size_t platform_fwrite(const void *ptr, size_t size, size_t count, FILE *stream) {
154 return fwrite(ptr, size, count, stream);
157 int platform_fflush(FILE *stream) {
158 return fflush(stream);
161 int platform_vfprintf(FILE *stream, const char *format, va_list arg) {
162 return vfprintf_s(stream, format, arg);
165 int platform_fclose(FILE *stream) {
166 return fclose(stream);
169 int platform_ferror(FILE *stream) {
170 return ferror(stream);
173 int platform_fgetc(FILE *stream) {
174 return fgetc(stream);
177 int platform_fputs(const char *str, FILE *stream) {
178 return fputs(str, stream);
181 int platform_fseek(FILE *stream, long offset, int origin) {
182 return fseek(stream, offset, origin);
185 long platform_ftell(FILE *stream) {
186 return ftell(stream);
189 int platform_mkdir(const char *path, int mode) {
190 return mkdir(path, mode);
193 DIR *platform_opendir(const char *path) {
194 return opendir(path);
197 int platform_closedir(DIR *dir) {
198 return closedir(dir);
201 struct dirent *platform_readdir(DIR *dir) {
205 int platform_istty(int fd) {