Main Page   Modules   Data Structures   File List   Data Fields   Related Pages  

dbus-errors.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-errors.c Error reporting
00003  *
00004  * Copyright (C) 2002  Red Hat Inc.
00005  * Copyright (C) 2003  CodeFactory AB
00006  *
00007  * Licensed under the Academic Free License version 1.2
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 #include "dbus-errors.h"
00025 #include "dbus-internals.h"
00026 #include <stdarg.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 
00067 typedef struct
00068 {
00069   const char *name; 
00070   char *message; 
00072   unsigned int const_message : 1; 
00074   unsigned int dummy2 : 1; 
00075   unsigned int dummy3 : 1; 
00076   unsigned int dummy4 : 1; 
00077   unsigned int dummy5 : 1; 
00079   void *padding1; 
00081 } DBusRealError;
00082 
00091 static const char*
00092 message_from_error (const char *error)
00093 {
00094   if (strcmp (error, DBUS_ERROR_FAILED) == 0)
00095     return "Unknown error";
00096   else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
00097     return "Not enough memory available";
00098   else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
00099     return "Error reading or writing data";
00100   else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
00101     return "Could not parse address";
00102   else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
00103     return "Feature not supported";
00104   else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
00105     return "Resource limits exceeded";
00106   else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
00107     return "Permission denied";
00108   else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
00109     return "Could not authenticate to server";
00110   else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
00111     return "No server available at address";
00112   else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
00113     return "Connection timed out";
00114   else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
00115     return "Network unavailable";
00116   else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
00117     return "Address already in use";
00118   else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
00119     return "Disconnected.";
00120   else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
00121     return "Invalid argumemts.";
00122   else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
00123     return "Did not get a reply message.";
00124   else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
00125     return "File doesn't exist.";
00126   else
00127     return error;
00128 }
00129 
00137 void
00138 dbus_error_init (DBusError *error)
00139 {
00140   DBusRealError *real;
00141 
00142   _dbus_return_if_fail (error != NULL);
00143 
00144   _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
00145 
00146   real = (DBusRealError *)error;
00147   
00148   real->name = NULL;  
00149   real->message = NULL;
00150 
00151   real->const_message = TRUE;
00152 }
00153 
00160 void
00161 dbus_error_free (DBusError *error)
00162 {
00163   DBusRealError *real;
00164 
00165   _dbus_return_if_fail (error != NULL);
00166   
00167   real = (DBusRealError *)error;
00168 
00169   if (!real->const_message)
00170     dbus_free (real->message);
00171 
00172   dbus_error_init (error);
00173 }
00174 
00187 void
00188 dbus_set_error_const (DBusError  *error,
00189                       const char *name,
00190                       const char *message)
00191 {
00192   DBusRealError *real;
00193 
00194   _dbus_return_if_error_is_set (error);
00195   _dbus_return_if_fail (name != NULL);
00196   
00197   if (error == NULL)
00198     return;
00199 
00200   _dbus_assert (error->name == NULL);
00201   _dbus_assert (error->message == NULL);
00202 
00203   if (message == NULL)
00204     message = message_from_error (name);
00205   
00206   real = (DBusRealError *)error;
00207   
00208   real->name = name;
00209   real->message = (char *)message;
00210   real->const_message = TRUE;
00211 }
00212 
00223 void
00224 dbus_move_error (DBusError *src,
00225                  DBusError *dest)
00226 {
00227   _dbus_return_if_error_is_set (dest);
00228 
00229   if (dest)
00230     {
00231       dbus_error_free (dest);
00232       *dest = *src;
00233       dbus_error_init (src);
00234     }
00235   else
00236     dbus_error_free (src);
00237 }
00238 
00246 dbus_bool_t
00247 dbus_error_has_name (const DBusError *error,
00248                      const char      *name)
00249 {
00250   _dbus_return_val_if_fail (error != NULL, FALSE);
00251   _dbus_return_val_if_fail (name != NULL, FALSE);
00252 
00253   _dbus_assert ((error->name != NULL && error->message != NULL) ||
00254                 (error->name == NULL && error->message == NULL));
00255   
00256   if (error->name != NULL)
00257     {
00258       DBusString str1, str2;
00259       _dbus_string_init_const (&str1, error->name);
00260       _dbus_string_init_const (&str2, name);
00261       return _dbus_string_equal (&str1, &str2);
00262     }
00263   else
00264     return FALSE;
00265 }
00266 
00273 dbus_bool_t
00274 dbus_error_is_set (const DBusError *error)
00275 {
00276   _dbus_return_val_if_fail (error != NULL, FALSE);  
00277   _dbus_assert ((error->name != NULL && error->message != NULL) ||
00278                 (error->name == NULL && error->message == NULL));
00279   return error->name != NULL;
00280 }
00281 
00302 void
00303 dbus_set_error (DBusError  *error,
00304                 const char *name,
00305                 const char *format,
00306                 ...)
00307 {
00308   DBusRealError *real;
00309   va_list args;
00310   int message_length;
00311   char *message;
00312   char c;
00313 
00314   if (error == NULL)
00315     return;
00316 
00317   /* it's a bug to pile up errors */
00318   _dbus_return_if_error_is_set (error);
00319   _dbus_return_if_fail (name != NULL);
00320   
00321   _dbus_assert (error->name == NULL);
00322   _dbus_assert (error->message == NULL);
00323 
00324   if (format == NULL)
00325     format = message_from_error (name);
00326   
00327   va_start (args, format);
00328   /* Measure the message length */
00329   message_length = vsnprintf (&c, 1, format, args) + 1;
00330   va_end (args);
00331   
00332   message = dbus_malloc (message_length);
00333   
00334   if (!message)
00335     {
00336       dbus_set_error_const (error, DBUS_ERROR_NO_MEMORY, NULL);
00337       return;
00338     }
00339   
00340   va_start (args, format);  
00341   vsprintf (message, format, args);  
00342   va_end (args);
00343 
00344   real = (DBusRealError *)error;
00345   
00346   real->name = name;
00347   real->message = message;
00348   real->const_message = FALSE;
00349 }
00350 

Generated on Wed Oct 22 14:05:01 2003 for D-BUS by doxygen1.3-rc3