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
25 #define CTIME_BUFFER 64
26 #define GETENV_BUFFER 4096
27 #define STRERROR_BUFFER 128
29 static void **platform_mem_pool = NULL;
30 static void platform_mem_atexit() {
32 for (i = 0; i < vec_size(platform_mem_pool); i++)
33 mem_d(platform_mem_pool[i]);
34 vec_free(platform_mem_pool);
37 static void *platform_mem_allocate(size_t bytes) {
39 if (!platform_mem_pool) {
40 atexit(&platform_mem_atexit);
41 vec_push(platform_mem_pool, NULL);
45 vec_push(platform_mem_pool, mem);
50 int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg) {
51 vsnprintf_s(buffer, bytes, bytes, format, arg);
54 int platform_sscanf(const char *str, const char *format, ...) {
57 vsscanf_s(str, format, va);
61 const struct tm *platform_localtime(const time_t *timer) {
63 t = (struct tm*)platform_mem_allocate(sizeof(struct tm));
64 localtime_s(&t, timer);
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);
74 char *platform_strncat(char *dest, const char *src, size_t num) {
75 return strncat_s(dest, num, src, _TRUNCATE);
78 const char *platform_tmpnam(char *str) {
79 return tmpnam_s(str, L_tmpnam);
82 const char *platform_getenv(char *var) {
83 char *buffer = (char *)platform_mem_allocate(GETENV_BUFFER);
85 getenv_s(&size, buffer, GETENV_BUFFER, var);
89 int platform_snprintf(char *src, size_t bytes, const char *format, ...) {
94 rt = vsprintf_s(src, bytes, format, va);
100 char *platform_strcat(char *dest, const char *src) {
101 strcat_s(dest, strlen(src), src);
105 char *platform_strncpy(char *dest, const char *src, size_t num) {
106 strncpy_s(dest, num, src, num);
110 const char *platform_strerror(int err) {
111 char *buffer = (char*)platform_mem_allocate(STRERROR_BUFFER);
112 strerror_s(buffer, STRERROR_BUFFER, err);