From 00cef967ed2222661a0eab40755c57d479e78b18 Mon Sep 17 00:00:00 2001 From: Anthony Romaniello <66272872+aromanielloNTIA@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:59:42 -0400 Subject: [PATCH] Add c-typed functions for wrapper error handling --- include/ITS.ITU.PSeries.P2108/P2108.h | 4 +++ include/ITS.ITU.PSeries.P2108/ReturnCodes.h | 2 -- src/ReturnCodes.cpp | 35 ++++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/include/ITS.ITU.PSeries.P2108/P2108.h b/include/ITS.ITU.PSeries.P2108/P2108.h index 56a477f..01cb195 100644 --- a/include/ITS.ITU.PSeries.P2108/P2108.h +++ b/include/ITS.ITU.PSeries.P2108/P2108.h @@ -44,9 +44,12 @@ EXPORTED ReturnCode HeightGainTerminalCorrectionModel( const ClutterType clutter_type, double &A_h__db ); +EXPORTED char *GetReturnStatusCharArray(const int code); +EXPORTED void FreeReturnStatusCharArray(char *c_msg); //////////////////////////////////////////////////////////////////////////////// // Private Functions +std::string GetReturnStatus(const int code); double cot(const double x); double InverseComplementaryCumulativeDistribution(const double q); double Equation_2a(const double nu); @@ -69,6 +72,7 @@ double TerrestrialStatisticalModelHelper( const double f__ghz, const double d__km, const double p ); + } // namespace P2108 } // namespace PSeries } // namespace ITU diff --git a/include/ITS.ITU.PSeries.P2108/ReturnCodes.h b/include/ITS.ITU.PSeries.P2108/ReturnCodes.h index d408fd5..1bf32c1 100644 --- a/include/ITS.ITU.PSeries.P2108/ReturnCodes.h +++ b/include/ITS.ITU.PSeries.P2108/ReturnCodes.h @@ -37,8 +37,6 @@ enum ReturnCode { }; // clang-format on -std::string GetReturnStatus(int code); - } // namespace P2108 } // namespace PSeries } // namespace ITU diff --git a/src/ReturnCodes.cpp b/src/ReturnCodes.cpp index e77c737..08168f8 100644 --- a/src/ReturnCodes.cpp +++ b/src/ReturnCodes.cpp @@ -4,6 +4,17 @@ #include "ITS.ITU.PSeries.P2108/ReturnCodes.h" +#ifdef _WIN32 + // Ensure strcpy_s is available on Windows + #ifndef __STDC_LIB_EXT1__ + #define __STDC_LIB_EXT1__ + #endif + #ifndef __STDC_WANT_LIB_EXT1__ + #define __STDC_WANT_LIB_EXT1__ 1 + #endif +#endif + +#include // for strcpy_s #include // for std::string #include // for std::unordered_map @@ -18,7 +29,7 @@ namespace P2108 { * @param[in] code Integer return code. * @return A status message corresponding to the input code. ******************************************************************************/ -std::string GetReturnStatus(int code) { +std::string GetReturnStatus(const int code) { static const std::unordered_map messages = {{SUCCESS, "Successful execution"}, {ERROR31__FREQUENCY, "Frequency must be between 0.3 and 3 GHz"}, @@ -50,6 +61,28 @@ std::string GetReturnStatus(int code) { return msg; } +/******************************************************************************* + * Get an error message string (as C-style string) from a return code. + * + * @param[in] code Integer return code. + * @return A status message corresponding to the input code. + ******************************************************************************/ +char *GetReturnStatusCharArray(const int code) { + std::string msg = GetReturnStatus(code); + char *c_msg = new char[msg.size() + 1]; + strcpy_s(c_msg, msg.size() + 1, msg.c_str()); + return c_msg; +} + +/******************************************************************************* + * Free the memory allocated by GetReturnStatusCharArray + * + * @param[in] c_msg The status message C-style string to delete + ******************************************************************************/ +void FreeReturnStatusCharArray(char *c_msg) { + delete[] c_msg; +} + } // namespace P2108 } // namespace PSeries } // namespace ITU