diff --git a/base/error_string.cc b/base/error_string.cc new file mode 100644 index 0000000..6cb02c4 --- /dev/null +++ b/base/error_string.cc @@ -0,0 +1,39 @@ +#include "base/error_string.h" + +#include +#include + +#include + +using namespace std; + +//---------------------------------------------------------------- + +#ifdef STRERROR_R_CHAR_P + +string base::error_string(int err) +{ + char *ptr; + char buffer[128]; + + ptr = strerror_r(errno, buffer, sizeof(buffer)); + return string(ptr); +} + +#else + +string base::error_string(int err) +{ + int r; + char buffer[128]; + + r = strerror_r(errno, buffer, sizeof(buffer)); + if (r) + throw runtime_error("strerror_r failed"); + + return string(buffer); +} + +#endif + +//---------------------------------------------------------------- diff --git a/base/error_string.h b/base/error_string.h new file mode 100644 index 0000000..dd7549a --- /dev/null +++ b/base/error_string.h @@ -0,0 +1,16 @@ +#ifndef BASE_ERROR_STRING_H +#define BASE_ERROR_STRING_H + +#include + +//---------------------------------------------------------------- + +namespace base { + // There are a couple of version of strerror_r kicking around, so + // we wrap it. + std::string error_string(int err); +} + +//---------------------------------------------------------------- + +#endif