OpenSync
0.22
|
00001 /* 00002 * libopensync - A synchronization framework 00003 * Copyright (C) 2004-2005 Armin Bauer <armin.bauer@opensync.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 * 00019 */ 00020 00021 #include "opensync.h" 00022 #include "opensync_internals.h" 00023 00033 00040 static const char *osync_error_name_from_type(OSyncErrorType type) 00041 { 00042 switch (type) { 00043 case OSYNC_NO_ERROR: 00044 return "NoError"; 00045 case OSYNC_ERROR_GENERIC: 00046 return "UnknownError"; 00047 case OSYNC_ERROR_IO_ERROR: 00048 return "IOError"; 00049 case OSYNC_ERROR_NOT_SUPPORTED: 00050 return "NotSupported"; 00051 case OSYNC_ERROR_TIMEOUT: 00052 return "Timeout"; 00053 case OSYNC_ERROR_DISCONNECTED: 00054 return "Disconnected"; 00055 case OSYNC_ERROR_FILE_NOT_FOUND: 00056 return "FileNotFound"; 00057 default: 00058 return "UnspecifiedError"; 00059 } 00060 } 00061 00070 void osync_error_set_vargs(OSyncError **error, OSyncErrorType type, const char *format, va_list args) 00071 { 00072 if (!error || !format) 00073 return; 00074 if (osync_error_is_set(error)) 00075 osync_error_free(error); 00076 osync_assert(osync_error_is_set(error) == FALSE); 00077 00078 char buffer[1024]; 00079 memset(buffer, 0, sizeof(buffer)); 00080 *error = g_malloc0(sizeof(OSyncError)); 00081 g_vsnprintf(buffer, 1024, format, args); 00082 00083 (*error)->message = g_strdup(buffer); 00084 (*error)->type = type; 00085 return; 00086 } 00087 00097 00098 00105 const char *osync_error_get_name(OSyncError **error) 00106 { 00107 osync_return_val_if_fail(error != NULL, NULL); 00108 if (!*error) 00109 return osync_error_name_from_type(OSYNC_NO_ERROR); 00110 return osync_error_name_from_type((*error)->type); 00111 } 00112 00118 void osync_error_free(OSyncError **error) 00119 { 00120 osync_return_if_fail(error != NULL); 00121 if (*error == NULL) 00122 return; 00123 00124 if ((*error)->message) 00125 g_free ((*error)->message); 00126 00127 g_free(*error); 00128 *error = NULL; 00129 } 00130 00137 osync_bool osync_error_is_set (OSyncError **error) 00138 { 00139 if (!error) 00140 return FALSE; 00141 00142 if (*error == NULL) 00143 return FALSE; 00144 00145 if ((*error)->type) 00146 return TRUE; 00147 00148 return FALSE; 00149 } 00150 00157 OSyncErrorType osync_error_get_type(OSyncError **error) 00158 { 00159 if (!osync_error_is_set(error)) 00160 return OSYNC_NO_ERROR; 00161 return (*error)->type; 00162 } 00163 00170 const char *osync_error_print(OSyncError **error) 00171 { 00172 if (!osync_error_is_set(error)) 00173 return NULL; 00174 return (*error)->message; 00175 } 00176 00187 void osync_error_update(OSyncError **error, const char *format, ...) 00188 { 00189 osync_return_if_fail(error != NULL); 00190 osync_return_if_fail(*error != NULL); 00191 00192 va_list args; 00193 va_start(args, format); 00194 00195 char buffer[1024]; 00196 memset(buffer, 0, sizeof(buffer)); 00197 g_vsnprintf(buffer, 1024, format, args); 00198 00199 g_free((*error)->message); 00200 (*error)->message = g_strdup(buffer); 00201 00202 va_end (args); 00203 } 00204 00212 void osync_error_duplicate(OSyncError **target, OSyncError **source) 00213 { 00214 if (!target) 00215 return; 00216 00217 osync_return_if_fail(osync_error_is_set(source)); 00218 00219 if (!osync_error_is_set(source)) { 00220 *target = NULL; 00221 return; 00222 } 00223 00224 *target = g_malloc0(sizeof(OSyncError)); 00225 (*target)->message = g_strdup((*source)->message); 00226 (*target)->type = (*source)->type; 00227 } 00228 00238 void osync_error_set(OSyncError **error, OSyncErrorType type, const char *format, ...) 00239 { 00240 va_list args; 00241 va_start(args, format); 00242 osync_error_set_vargs(error, type, format, args); 00243 va_end (args); 00244 } 00245 00252 void osync_error_set_type(OSyncError **error, OSyncErrorType type) 00253 { 00254 if (!error) 00255 return; 00256 00257 (*error)->type = type; 00258 return; 00259 } 00260