Library of Assembled Shared Sources
lass_errno.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2023 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "lass_errno.h"
45
46#include <errno.h>
47#include <string.h>
48
49#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
50# define NOMINMAX
51# define WIN32_LEAN_AND_MEAN
52# include <windows.h>
54#endif
55
56namespace lass
57{
58namespace util
59{
60namespace impl
61{
62
63/** returns CLIB errno
64 * @ingroup LassErrno
65 * @internal
66 */
68{
69 return errno;
70}
71
72/** sets CLIB errno to zero.
73 * @ingroup LassErrno
74 * @internal
75 */
77{
78 errno = 0;
79}
80
81/** returns message associated to an CLIB error code
82 * @ingroup LassErrno
83 * @internal
84 */
85const std::string lass_strerror(int errnum)
86{
87 const char* msg = 0;
88
89#if LASS_HAVE_FUNC_STRERROR_R
90
91 const size_t bufferLength = 256;
92 char buffer[bufferLength + 1];
93 errno = 0;
94# if LASS_HAVE_STRERROR_R_CHAR_P
95 msg = ::strerror_r(errnum, buffer, bufferLength);
96 if (errno != 0)
97 {
98 msg = "[no error message due to strerror_r failure]";
99 }
100# else
101 const int rc = ::strerror_r(errnum, buffer, bufferLength);
102 msg = (rc == 0) ? buffer : "[no error message due to strerror_r failure]";
103# endif
104 if (!msg)
105 {
106 msg = "[no error message due to strerror_r failure]";
107 }
108
109#elif LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
110
111 const size_t bufferLength = 256;
112 char buffer[bufferLength + 1];
113 const errno_t rc = strerror_s(buffer, bufferLength, errnum);
114 msg = (rc == 0) ? buffer : "[no error message due to strerror_s failure]";
115
116#endif
117
118 std::ostringstream stream;
119 stream << "errno " << errnum;
120 if (msg)
121 {
122 stream << ": " << msg;
123 }
124 return stream.str();
125}
126
127#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
128
129LASS_META_ASSERT(sizeof(DWORD) <= sizeof(unsigned), unsigned_should_be_big_enough_for_a_DWORD);
130
131/** returns GetLastError()
132 * @ingroup LassErrno
133 * @internal
134 * @arg only available on Windows platforms
135 */
136unsigned lass_GetLastError()
137{
138 return GetLastError();
139}
140
141/** returns message associated to an Win API error code.
142 * @ingroup LassErrno
143 * @internal
144 * @arg only available on Windows platforms
145 */
146const std::string lass_FormatMessage(unsigned error)
147{
148 LASS_ASSERT(static_cast<DWORD>(error) == error);
149 LASS_ASSERT(error != 0);
150
151 LPSTR buffer;
152 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
153 0, static_cast<DWORD>(error), 0, (LPTSTR) &buffer, 0, 0))
154 {
155 return "[no error message due to FormatMessage failure]";
156 }
157 std::string result = buffer;
158 LocalFree(buffer);
159 return result;
160}
161
162#endif
163
164}
165}
166}
167
168// EOF
const std::string lass_strerror(int errnum)
returns message associated to an CLIB error code
int lass_errno()
returns CLIB errno
void lass_reset_errno()
sets CLIB errno to zero.
#define LASS_META_ASSERT(expression, message)
complite time static check
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53