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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*
* libopensync - A synchronization framework
* Copyright (C) 2004-2005 Armin Bauer <armin.bauer@opensync.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "opensync.h"
#include "opensync_internals.h"
/**
* @defgroup OSyncErrorPrivateAPI OpenSync Error Internals
* @ingroup OSyncPrivate
* @brief The public API of opensync
*
* This gives you an insight in the public API of opensync.
*
*/
/*@{*/
/*! @brief Translate a error type into something human readable
*
* @param type The error type to look up
* @returns The name of the error type
*
*/
static const char *osync_error_name_from_type(OSyncErrorType type)
{
switch (type) {
case OSYNC_NO_ERROR:
return "NoError";
case OSYNC_ERROR_GENERIC:
return "UnknownError";
case OSYNC_ERROR_IO_ERROR:
return "IOError";
case OSYNC_ERROR_NOT_SUPPORTED:
return "NotSupported";
case OSYNC_ERROR_TIMEOUT:
return "Timeout";
case OSYNC_ERROR_DISCONNECTED:
return "Disconnected";
case OSYNC_ERROR_FILE_NOT_FOUND:
return "FileNotFound";
default:
return "UnspecifiedError";
}
}
/*! @brief Sets a error from a va_list
*
* @param error A pointer to a error struct
* @param type The type to set
* @param format The message
* @param args The arguments to the message
*
*/
void osync_error_set_vargs(OSyncError **error, OSyncErrorType type, const char *format, va_list args)
{
if (!error || !format)
return;
if (osync_error_is_set(error))
osync_error_free(error);
osync_assert(osync_error_is_set(error) == FALSE);
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
*error = g_malloc0(sizeof(OSyncError));
g_vsnprintf(buffer, 1024, format, args);
(*error)->message = g_strdup(buffer);
(*error)->type = type;
return;
}
/*@}*/
/**
* @defgroup OSyncErrorAPI OpenSync Errors
* @ingroup OSyncPublic
* @brief OpenSync's error reporting facilities
*
*/
/*@{*/
/*! @brief This will return a string describing the type of the error
*
* @param error A pointer to a error struct
* @returns The description, NULL on error
*
*/
const char *osync_error_get_name(OSyncError **error)
{
osync_return_val_if_fail(error != NULL, NULL);
if (!*error)
return osync_error_name_from_type(OSYNC_NO_ERROR);
return osync_error_name_from_type((*error)->type);
}
/*! @brief Frees the error so it can be reused
*
* @param error A pointer to a error struct to free
*
*/
void osync_error_free(OSyncError **error)
{
osync_return_if_fail(error != NULL);
if (*error == NULL)
return;
if ((*error)->message)
g_free ((*error)->message);
g_free(*error);
*error = NULL;
}
/*! @brief Checks if the error is set
*
* @param error A pointer to a error struct to check
* @returns TRUE if the error is set, FALSE otherwise
*
*/
osync_bool osync_error_is_set (OSyncError **error)
{
if (!error)
return FALSE;
if (*error == NULL)
return FALSE;
if ((*error)->type)
return TRUE;
return FALSE;
}
/*! @brief Returns the type of the error
*
* @param error The error
* @returns The type of the error or OSYNC_NO_ERROR if no error
*
*/
OSyncErrorType osync_error_get_type(OSyncError **error)
{
if (!osync_error_is_set(error))
return OSYNC_NO_ERROR;
return (*error)->type;
}
/*! @brief Returns the message of the error
*
* @param error The error to print
* @returns The message of the error or NULL if no error
*
*/
const char *osync_error_print(OSyncError **error)
{
if (!osync_error_is_set(error))
return NULL;
return (*error)->message;
}
/*! @brief Updates the error message
*
* You can use this function to update the error message on
* a error. You can use the old error->message as a parameter
* for this function.
*
* @param error A pointer to a error struct to update
* @param format The new message
*
*/
void osync_error_update(OSyncError **error, const char *format, ...)
{
osync_return_if_fail(error != NULL);
osync_return_if_fail(*error != NULL);
va_list args;
va_start(args, format);
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
g_vsnprintf(buffer, 1024, format, args);
g_free((*error)->message);
(*error)->message = g_strdup(buffer);
va_end (args);
}
/*! @brief Duplicates the error into the target
*
*
* @param target The target error to update
* @param source The source error which to duplicate
*
*/
void osync_error_duplicate(OSyncError **target, OSyncError **source)
{
if (!target)
return;
osync_return_if_fail(osync_error_is_set(source));
if (!osync_error_is_set(source)) {
*target = NULL;
return;
}
*target = g_malloc0(sizeof(OSyncError));
(*target)->message = g_strdup((*source)->message);
(*target)->type = (*source)->type;
}
/*! @brief Sets the error
*
* You can use this function to set the error to the given type and message
*
* @param error A pointer to a error struct to set
* @param type The Error type to set
* @param format The message
*
*/
void osync_error_set(OSyncError **error, OSyncErrorType type, const char *format, ...)
{
va_list args;
va_start(args, format);
osync_error_set_vargs(error, type, format, args);
va_end (args);
}
/*! @brief Sets the type of an error
*
* @param error A pointer to a error struct to set
* @param type The Error type to set
*
*/
void osync_error_set_type(OSyncError **error, OSyncErrorType type)
{
if (!error)
return;
(*error)->type = type;
return;
}
/*@}*/
|