More enums.
[wrappi.git] / lib / wrappi.c
1 /* wrappi
2  * Copyright (C) 2011-2012 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24
25 #include "wrappi.h"
26 #include "internal.h"
27
28 wrap_h *
29 wrap_create (void)
30 {
31   struct wrap_h *w = calloc (1, sizeof *w);
32   if (w == NULL)
33     return NULL;
34
35   return w;
36 }
37
38 void
39 wrap_close (wrap_h *w)
40 {
41   free (w);
42 }
43
44 static void
45 set_error_buf (struct wrap_internal_h *w,
46                char *buf, int errnum, const char *func)
47 {
48   free (w->error);
49   w->error = buf;
50   w->errnum = errnum;
51
52   /* 'func' is always statically allocated, since it comes from
53    * C99 __func__, so this is safe.
54    */
55   if (STRPREFIX (func, "impl_"))
56     func += 5;
57   else if (STRPREFIX (func, "wrap_"))
58     func += 5;
59   w->error_func = func;
60
61   /* XXX This will be conditional on the user setting an error handler. */
62   fprintf (stderr, "wrappi: %s: %s\n", func, buf);
63 }
64
65 void
66 wrap_int_set_error (struct wrap_internal_h *w,
67                     const char *func,
68                     const char *fs, ...)
69 {
70   va_list args;
71   char *msg;
72   int err;
73
74   va_start (args, fs);
75   err = vasprintf (&msg, fs, args);
76   va_end (args);
77
78   if (err < 0) return;
79
80   set_error_buf (w, msg, 0, func);
81 }
82
83 void
84 wrap_int_set_error_errno (struct wrap_internal_h *w,
85                           int errnum,
86                           const char *func,
87                           const char *fs, ...)
88 {
89   va_list args;
90   char *msg;
91   int err;
92
93   va_start (args, fs);
94   err = vasprintf (&msg, fs, args);
95   va_end (args);
96
97   if (err < 0) return;
98
99   set_error_buf (w, msg, errnum, func);
100 }