diff options
author | Anders Carlsson <andersca@codefactory.se> | 2003-02-13 19:02:08 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@codefactory.se> | 2003-02-13 19:02:08 +0000 |
commit | b09fadc1145570d752e7f709bfc0cf8c486d5146 (patch) | |
tree | 306aae979a5484c1a5c8d2af61d3047190cf233e | |
parent | f28c36435215a56364681667045adcafecc791e6 (diff) |
2003-02-13 Anders Carlsson <andersca@codefactory.se>
* dbus/dbus-errors.c: (dbus_result_to_string), (dbus_error_init),
(dbus_error_free), (dbus_set_error_const), (dbus_set_error):
* dbus/dbus-errors.h:
Add DBusError structure.
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | dbus/dbus-errors.c | 99 | ||||
-rw-r--r-- | dbus/dbus-errors.h | 25 |
3 files changed, 130 insertions, 1 deletions
@@ -1,5 +1,12 @@ 2003-02-13 Anders Carlsson <andersca@codefactory.se> + * dbus/dbus-errors.c: (dbus_result_to_string), (dbus_error_init), + (dbus_error_free), (dbus_set_error_const), (dbus_set_error): + * dbus/dbus-errors.h: + Add DBusError structure. + +2003-02-13 Anders Carlsson <andersca@codefactory.se> + * test/data/valid-messages/standard-acquire-service.message: * test/data/valid-messages/standard-hello.message: * test/data/valid-messages/standard-list-services.message: diff --git a/dbus/dbus-errors.c b/dbus/dbus-errors.c index c70c1bc4..a00ba946 100644 --- a/dbus/dbus-errors.c +++ b/dbus/dbus-errors.c @@ -21,6 +21,9 @@ * */ #include "dbus-errors.h" +#include "dbus-internals.h" +#include <stdarg.h> +#include <stdio.h> /** * @defgroup DBusErrors Error reporting @@ -42,6 +45,14 @@ * @{ */ +typedef struct +{ + const char *name; /**< error name */ + char *message; /**< error message */ + + unsigned int const_message : 1; /** Message is not owned by DBusError */ +} DBusRealError; + /** * Set a result code at a result code location, * if code_address is not #NULL. @@ -112,4 +123,92 @@ dbus_result_to_string (DBusResultCode code) return "Invalid error code"; } +void +dbus_error_init (DBusError *error) +{ + DBusRealError *real; + + _dbus_assert (error != NULL); + + _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError)); + + real = (DBusRealError *)error; + + real->name = NULL; + real->message = NULL; + + real->const_message = TRUE; +} + +void +dbus_error_free (DBusError *error) +{ + DBusRealError *real; + + real = (DBusRealError *)error; + + if (!real->const_message) + dbus_free (real->message); +} + +void +dbus_set_error_const (DBusError *error, + const char *name, + const char *message) +{ + DBusRealError *real; + + if (error == NULL) + return; + + dbus_error_init (error); + + real = (DBusRealError *)error; + + real->name = name; + real->message = (char *)message; + real->const_message = TRUE; +} + +dbus_bool_t +dbus_set_error (DBusError *error, + const char *name, + const char *format, + ...) +{ + DBusRealError *real; + va_list args, args2; + int message_length; + char *message; + char c; + + if (error == NULL) + return TRUE; + + va_start (args, format); + + va_copy (args2, args); + + /* Measure the message length */ + message_length = vsnprintf (&c, 1,format, args) + 1; + + message = dbus_malloc (message_length); + + vsprintf (message, format, args2); + + if (!message) + return FALSE; + + va_end (args); + + dbus_error_init (error); + real = (DBusRealError *)error; + + real->name = name; + real->message = message; + real->const_message = FALSE; + + return TRUE; +} + /** @} */ diff --git a/dbus/dbus-errors.h b/dbus/dbus-errors.h index 29f847de..0a7348ff 100644 --- a/dbus/dbus-errors.h +++ b/dbus/dbus-errors.h @@ -28,9 +28,21 @@ #define DBUS_ERROR_H #include <dbus/dbus-macros.h> +#include <dbus/dbus-types.h> DBUS_BEGIN_DECLS; +typedef struct DBusError DBusError; + +struct DBusError +{ + const char *name; /**< error name */ + const char *message; /**< error message */ + + unsigned int dummy1 : 1; /**< placeholder */ + unsigned int dummy2 : 1; /**< placeholder */ +}; + typedef enum { DBUS_RESULT_SUCCESS, /**< Operation was successful. */ @@ -55,11 +67,22 @@ typedef enum DBUS_RESULT_FILE_NOT_FOUND /**< File doesn't exist */ } DBusResultCode; +void dbus_error_init (DBusError *error); +void dbus_error_free (DBusError *error); +dbus_bool_t dbus_set_error (DBusError *error, + const char *name, + const char *message, + ...); +void dbus_set_error_const (DBusError *error, + const char *name, + const char *message); + + + void dbus_set_result (DBusResultCode *code_address, DBusResultCode code); const char* dbus_result_to_string (DBusResultCode code); - DBUS_END_DECLS; #endif /* DBUS_ERROR_H */ |