Typically this class is used to create anonymous temporaries, for example: if ((fd = open(file_path, O_RDONLY)) < 0) { Error("%s Can not open %s file : %s", module_name, file_path, Strerror(errno).c_str()); return nullptr; }
| 37 | // } |
| 38 | // |
| 39 | class Strerror |
| 40 | { |
| 41 | public: |
| 42 | Strerror(int err_num) |
| 43 | { |
| 44 | // Make sure there are no unused function warnings. |
| 45 | // |
| 46 | static_cast<void>(_handle(0)); |
| 47 | static_cast<void>(_handle(nullptr)); |
| 48 | |
| 49 | // Handle either GNU or XSI version of strerror_r(). |
| 50 | // |
| 51 | if (!_handle(strerror_r(err_num, _buf, sizeof(_buf)))) { |
| 52 | _c_str = "strerror_r() call failed"; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | char const * |
| 57 | c_str() const |
| 58 | { |
| 59 | return _c_str; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | char _buf[256]; |
| 64 | char const *_c_str; |
| 65 | |
| 66 | // For XSI-compliant strerror_r(). |
| 67 | // |
| 68 | bool |
| 69 | _handle(int retval) |
| 70 | { |
| 71 | if (0 != retval) { |
| 72 | return false; |
| 73 | } |
| 74 | _buf[sizeof(_buf) - 1] = '\0'; |
| 75 | _c_str = _buf; |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // For GNU-specific strerror_r(). |
| 80 | // |
| 81 | bool |
| 82 | _handle(char *retval) |
| 83 | { |
| 84 | _buf[sizeof(_buf) - 1] = '\0'; |
| 85 | _c_str = retval; |
| 86 | return retval != nullptr; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | } // end namespace ts |