Skip to content

Commit

Permalink
Add c-typed functions for wrapper error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanielloNTIA committed Oct 31, 2024
1 parent e761301 commit 00cef96
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions include/ITS.ITU.PSeries.P2108/P2108.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -69,6 +72,7 @@ double TerrestrialStatisticalModelHelper(
const double f__ghz, const double d__km, const double p
);


} // namespace P2108
} // namespace PSeries
} // namespace ITU
Expand Down
2 changes: 0 additions & 2 deletions include/ITS.ITU.PSeries.P2108/ReturnCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ enum ReturnCode {
};
// clang-format on

std::string GetReturnStatus(int code);

} // namespace P2108
} // namespace PSeries
} // namespace ITU
Expand Down
35 changes: 34 additions & 1 deletion src/ReturnCodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstring> // for strcpy_s
#include <string> // for std::string
#include <unordered_map> // for std::unordered_map

Expand All @@ -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<ReturnCode, std::string> messages
= {{SUCCESS, "Successful execution"},
{ERROR31__FREQUENCY, "Frequency must be between 0.3 and 3 GHz"},
Expand Down Expand Up @@ -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());

Check failure on line 73 in src/ReturnCodes.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-latest / CMake 3.21

‘strcpy_s’ was not declared in this scope; did you mean ‘strcpy’?

Check failure on line 73 in src/ReturnCodes.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-latest / CMake latest

‘strcpy_s’ was not declared in this scope; did you mean ‘strcpy’?
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
Expand Down

0 comments on commit 00cef96

Please sign in to comment.