-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsimplescim_error_string.cpp
177 lines (152 loc) · 4.19 KB
/
simplescim_error_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* This file is part of the EGIL SCIM client.
*
* Copyright (C) 2017-2019 Föreningen Sambruk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "simplescim_error_string.hpp"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#ifdef _WIN32
// We use strerror instead of strerror_s as MSVC thinks we should use
#pragma warning( disable : 4996 )
#endif
static int prefix_present = 0;
static char prefix_buffer[1024];
static int message_present = 0;
static char message_buffer[1024];
static char error_string_buffer[2051];
static const char *no_error = "No error";
bool has_errors_to_print() {
return prefix_present || message_present;
}
/**
* Returns an error string that contains the latest error
* message generated by SimpleSCIM or "No error" if no
* error string has been set.
*/
const char *simplescim_error_string_get() {
if (!prefix_present && !message_present) {
return no_error;
}
if (!prefix_present) {
return message_buffer;
}
if (!message_present) {
return prefix_buffer;
}
snprintf(error_string_buffer,
sizeof(error_string_buffer),
"%s: %s",
prefix_buffer,
message_buffer);
return error_string_buffer;
}
/**
* Sets the prefix of the error string with 'prefix' as the
* format string. If a message has been set, the prefix
* will appear before the message with a colon and a space
* appended. If no message has been set, the prefix will
* appear as is. nullptr signifies no prefix.
*/
void simplescim_error_string_set_prefix(const char *prefix, ...) {
va_list ap;
if (prefix == nullptr) {
prefix_present = 0;
} else {
prefix_present = 1;
va_start(ap, prefix);
vsnprintf(prefix_buffer,
sizeof(prefix_buffer),
prefix,
ap);
va_end(ap);
}
}
/**
* Sets the message of the error string with 'message' as
* the format string. nullptr signifies no message.
*/
void simplescim_error_string_set_message(const char *message, ...) {
va_list ap;
if (message == nullptr) {
message_present = 0;
} else {
message_present = 1;
va_start(ap, message);
vsnprintf(message_buffer,
sizeof(message_buffer),
message,
ap);
va_end(ap);
}
}
/**
* Sets the prefix of the error string with 'prefix' as the
* format string and sets the message of the error string
* to strerror(errno). nullptr signifies no prefix.
*/
void simplescim_error_string_set_errno(const char *prefix, ...) {
va_list ap;
int tmp_errno;
tmp_errno = errno;
/* Set prefix */
if (prefix == nullptr) {
prefix_present = 0;
} else {
prefix_present = 1;
va_start(ap, prefix);
vsnprintf(prefix_buffer,
sizeof(prefix_buffer),
prefix,
ap);
va_end(ap);
}
/* Set message */
message_present = 1;
snprintf(message_buffer,
sizeof(message_buffer),
"%s",
strerror(tmp_errno));
}
/**
* Sets the prefix of the error string to 'prefix' and sets
* the message of the error string to 'message'.
* 'prefix' == nullptr signifies no prefix and
* 'message' == nullptr signifies no message.
*/
void simplescim_error_string_set(const char *prefix,
const char *message) {
/* Set prefix */
if (prefix == nullptr) {
prefix_present = 0;
} else {
prefix_present = 1;
snprintf(prefix_buffer,
sizeof(prefix_buffer),
"%s",
prefix);
}
/* Set message */
if (message == nullptr) {
message_present = 0;
} else {
message_present = 1;
snprintf(message_buffer,
sizeof(message_buffer),
"%s",
message);
}
}