qpc/ports/posix/safe_std.h

109 lines
3.2 KiB
C
Raw Normal View History

/*============================================================================
* QP/C Real-Time Embedded Framework (RTEF)
* Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
2020-04-02 21:21:11 -04:00
*
* SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
2020-04-02 21:21:11 -04:00
*
* This software is dual-licensed under the terms of the open source GNU
* General Public License version 3 (or any later version), or alternatively,
* under the terms of one of the closed source Quantum Leaps commercial
* licenses.
2020-04-02 21:21:11 -04:00
*
* The terms of the open source GNU General Public License version 3
* can be found at: <www.gnu.org/licenses/gpl-3.0>
2020-04-02 21:21:11 -04:00
*
* The terms of the closed source Quantum Leaps commercial licenses
* can be found at: <www.state-machine.com/licensing>
*
* Redistributions in source code must retain this top-level comment block.
* Plagiarizing this software to sidestep the license obligations is illegal.
2020-04-02 21:21:11 -04:00
*
* Contact information:
* <www.state-machine.com>
2020-04-02 21:21:11 -04:00
* <info@state-machine.com>
============================================================================*/
/*!
* @date Last updated on: 2022-07-30
* @version Last updated for: @ref qpc_7_0_1
*
* @file
* @brief "safe" <stdio.h> and <string.h> facilities
2020-04-02 21:21:11 -04:00
*/
#ifndef SAFE_STD_H
#define SAFE_STD_H
#include <stdio.h>
#include <string.h>
/* portable "safe" facilities from <stdio.h> and <string.h> ................*/
#ifdef _WIN32 /* Windows OS? */
#define MEMMOVE_S(dest_, num_, src_, count_) \
memmove_s(dest_, num_, src_, count_)
2020-08-25 10:17:51 -04:00
#define STRNCPY_S(dest_, destsiz_, src_) \
strncpy_s(dest_, destsiz_, src_, _TRUNCATE)
2020-04-02 21:21:11 -04:00
#define STRCAT_S(dest_, destsiz_, src_) \
strcat_s(dest_, destsiz_, src_)
#define SNPRINTF_S(buf_, bufsiz_, format_, ...) \
_snprintf_s(buf_, bufsiz_, _TRUNCATE, format_, ##__VA_ARGS__)
#define PRINTF_S(format_, ...) \
printf_s(format_, ##__VA_ARGS__)
#define FPRINTF_S(fp_, format_, ...) \
fprintf_s(fp_, format_, ##__VA_ARGS__)
#ifdef _MSC_VER
#define FREAD_S(buf_, bufsiz_, elsiz_, count_, fp_) \
fread_s(buf_, bufsiz_, elsiz_, count_, fp_)
#else
#define FREAD_S(buf_, bufsiz_, elsiz_, count_, fp_) \
fread(buf_, elsiz_, count_, fp_)
#endif /* _MSC_VER */
#define FOPEN_S(fp_, fName_, mode_) \
if (fopen_s(&fp_, fName_, mode_) != 0) { \
fp_ = (FILE *)0; \
} else (void)0
#define LOCALTIME_S(tm_, time_) \
localtime_s(tm_, time_)
#else /* other OS (Linux, MacOS, etc.) .....................................*/
#define MEMMOVE_S(dest_, num_, src_, count_) \
memmove(dest_, src_, count_)
2020-08-25 10:17:51 -04:00
#define STRNCPY_S(dest_, destsiz_, src_) do { \
strncpy(dest_, src_, destsiz_); \
dest_[(destsiz_) - 1] = '\0'; \
} while (false)
2020-04-02 21:21:11 -04:00
#define STRCAT_S(dest_, destsiz_, src_) \
2020-08-24 16:41:40 -04:00
strcat(dest_, src_)
2020-04-02 21:21:11 -04:00
#define SNPRINTF_S(buf_, bufsiz_, format_, ...) \
snprintf(buf_, bufsiz_, format_, ##__VA_ARGS__)
#define PRINTF_S(format_, ...) \
printf(format_, ##__VA_ARGS__)
#define FPRINTF_S(fp_, format_, ...) \
fprintf(fp_, format_, ##__VA_ARGS__)
#define FREAD_S(buf_, bufsiz_, elsiz_, count_, fp_) \
fread(buf_, elsiz_, count_, fp_)
#define FOPEN_S(fp_, fName_, mode_) \
(fp_ = fopen(fName_, mode_))
#define LOCALTIME_S(tm_, time_) \
memcpy(tm_, localtime(time_), sizeof(struct tm))
#endif /* _WIN32 */
#endif /* SAFE_STD_H */