Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
2c6372d7
Commit
2c6372d7
authored
Apr 30, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a portable thread-safe version of strerror.
parent
5019b6b0
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
1 deletion
+46
-1
format-test.cc
format-test.cc
+16
-0
format.cc
format.cc
+20
-0
format.h
format.h
+10
-1
No files found.
format-test.cc
View file @
2c6372d7
...
...
@@ -248,6 +248,22 @@ TEST(UtilTest, UTF8ToUTF16) {
// TODO: test UTF16ToUTF8::Convert
#endif // _WIN32
TEST
(
UtilTest
,
StrError
)
{
using
fmt
::
internal
::
StrError
;
EXPECT_DEBUG_DEATH
(
StrError
(
EDOM
,
0
,
0
),
"Assertion"
);
char
buffer
[
BUFFER_SIZE
];
EXPECT_DEBUG_DEATH
(
StrError
(
EDOM
,
buffer
,
0
),
"Assertion"
);
buffer
[
0
]
=
'x'
;
const
char
*
message
=
StrError
(
-
1
,
buffer
,
1
);
EXPECT_EQ
(
ERANGE
,
errno
);
EXPECT_STREQ
(
""
,
message
);
message
=
StrError
(
-
1
,
buffer
,
BUFFER_SIZE
);
EXPECT_EQ
(
0
,
errno
);
EXPECT_GE
(
BUFFER_SIZE
-
1
,
std
::
strlen
(
message
));
message
=
StrError
(
-
1
,
buffer
,
std
::
strlen
(
message
));
EXPECT_EQ
(
ERANGE
,
errno
);
}
class
TestString
{
private:
std
::
string
value_
;
...
...
format.cc
View file @
2c6372d7
...
...
@@ -204,6 +204,26 @@ int fmt::internal::UTF16ToUTF8::Convert(fmt::WStringRef s) {
#endif
char
*
fmt
::
internal
::
StrError
(
int
error_code
,
char
*
buffer
,
std
::
size_t
buffer_size
)
{
assert
(
buffer
!=
0
&&
buffer_size
!=
0
);
errno
=
0
;
#ifdef _GNU_SOURCE
char
*
message
=
strerror_r
(
error_code
,
buffer
,
buffer_size
);
if
(
message
==
buffer
&&
strlen
(
buffer
)
==
buffer_size
-
1
)
errno
=
ERANGE
;
// The buffer is full so the message is probably truncated.
return
message
;
#elif _WIN32
errno
=
strerror_s
(
buffer
,
buflen
,
error_code
);
if
(
errno
==
0
&&
std
::
strlen
(
buffer
)
==
buffer_size
-
1
)
errno
=
ERANGE
;
// The buffer is full so the message is probably truncated.
return
buffer
;
#else
strerror_r
(
error_code
,
buffer
,
buffer_size
);
return
buffer
;
#endif
}
void
fmt
::
internal
::
FormatSystemErrorMessage
(
fmt
::
Writer
&
out
,
int
error_code
,
fmt
::
StringRef
message
)
{
#ifndef _WIN32
...
...
format.h
View file @
2c6372d7
...
...
@@ -489,6 +489,16 @@ class UTF16ToUTF8 {
};
#endif
// Portable thread-safe version of strerror.
// Returns a pointer to a string describing the error code. This can be
// either a pointer to a string stored in buffer, or a pointer to some
// static immutable string. Sets errno to one of the following values:
// 0 - success
// ERANGE - buffer is not large enough to store the error message
// other - failure
// Buffer should be at least of size 1.
char
*
StrError
(
int
error_code
,
char
*
buffer
,
std
::
size_t
buffer_size
);
// Formats a system error message writing the output to out.
void
FormatSystemErrorMessage
(
Writer
&
out
,
int
error_code
,
StringRef
message
);
...
...
@@ -1521,7 +1531,6 @@ inline Formatter<NullSink, wchar_t> Format(WStringRef format) {
return
f
;
}
/**
A sink that gets the system error message corresponding to the error code
and throws SystemError.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment