platform.h (2300B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2017 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #ifndef DC_PLATFORM_H 23 #define DC_PLATFORM_H 24 25 #include <stddef.h> 26 #include <stdarg.h> 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif /* __cplusplus */ 31 32 #if defined(__GNUC__) 33 #define DC_ATTR_FORMAT_PRINTF(a,b) __attribute__((format(printf, a, b))) 34 #define DC_ATTR_UNUSED __attribute__((unused)) 35 #else 36 #define DC_ATTR_FORMAT_PRINTF(a,b) 37 #define DC_ATTR_UNUSED 38 #endif 39 40 #ifdef _WIN32 41 #define DC_PRINTF_SIZE "%Iu" 42 #define DC_FORMAT_INT64 "%I64d" 43 #else 44 #define DC_PRINTF_SIZE "%zu" 45 #define DC_FORMAT_INT64 "%lld" 46 #endif 47 48 #ifdef _MSC_VER 49 #define strcasecmp _stricmp 50 #define strncasecmp _strnicmp 51 #define strdup _strdup 52 #if _MSC_VER < 1800 53 // The rint() function is only available in MSVC 2013 and later 54 // versions. Our replacement macro isn't entirely correct, because the 55 // rounding rules for halfway cases are slightly different (away from 56 // zero vs to even). But for our use-case, that's not a problem. 57 #define rint(x) ((x) >= 0.0 ? floor((x) + 0.5): ceil((x) - 0.5)) 58 #endif 59 #endif 60 61 int dc_platform_sleep(unsigned int milliseconds); 62 63 /* 64 * A wrapper for the vsnprintf function, which will always null terminate the 65 * string and returns a negative value if the destination buffer is too small. 66 */ 67 int dc_platform_snprintf (char *str, size_t size, const char *format, ...) DC_ATTR_FORMAT_PRINTF(3, 4); 68 int dc_platform_vsnprintf (char *str, size_t size, const char *format, va_list ap) DC_ATTR_FORMAT_PRINTF(3, 0); 69 70 #ifdef __cplusplus 71 } 72 #endif /* __cplusplus */ 73 #endif /* DC_PLATFORM_H */