Get rid of string type and replace with a bool type
[fidl.git] / error.c
bloba9ec5b7e7ae16b6fb219179dcf990870ecb6fe23
1 /*
2 * QEMU Errors
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 */
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
16 #include "error.h"
18 static void *mallocz(size_t size)
20 void *ret;
22 ret = malloc(size);
23 if (ret) {
24 memset(ret, 0, size);
26 return ret;
29 Error *error_new(const char *domain,
30 int code,
31 const char *format,
32 ...)
34 Error *e;
35 va_list ap;
37 va_start(ap, format);
38 e = error_new_valist(domain, code, format, ap);
39 va_end(ap);
41 return e;
44 Error *error_new_valist(const char *domain,
45 int code,
46 const char *format,
47 va_list ap)
49 Error *e;
51 e = mallocz(sizeof(*e));
52 e->domain = strdup(domain);
53 e->code = code;
55 if (vasprintf(&e->message, format, ap) == -1) {
56 abort();
59 return e;
62 Error *error_new_literal(const char *domain,
63 int code,
64 const char *message)
66 Error *e;
68 e = mallocz(sizeof(*e));
69 e->domain = strdup(domain);
70 e->code = code;
71 e->message = strdup(message);
73 return e;
76 void error_set(Error **errp,
77 const char *domain,
78 int code,
79 const char *format,
80 ...)
82 if (errp) {
83 va_list ap;
85 va_start(ap, format);
86 *errp = error_new_valist(domain, code, format, ap);
87 va_end(ap);
91 bool error_is_set(Error **errp)
93 if (errp && *errp) {
94 return true;
96 return false;
99 void error_propagate(Error **errp,
100 Error *err)
102 if (errp) {
103 *errp = err;
104 } else {
105 error_free(err);
109 Error *error_copy(const Error *err)
111 return error_new_literal(err->domain,
112 err->code,
113 err->message);
116 void error_free(Error *e)
118 free(e->domain);
119 free(e->message);
120 free(e);