Add skeleton for web service library
[platform/upstream/connman.git] / gweb / gweb.c
1 /*
2  *
3  *  Web service library with GLib integration
4  *
5  *  Copyright (C) 2009-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "gweb.h"
30
31 struct _GWeb {
32         gint ref_count;
33
34         GWebDebugFunc debug_func;
35         gpointer debug_data;
36 };
37
38 static inline void debug(GWeb *web, const char *format, ...)
39 {
40         char str[256];
41         va_list ap;
42
43         if (web->debug_func == NULL)
44                 return;
45
46         va_start(ap, format);
47
48         if (vsnprintf(str, sizeof(str), format, ap) > 0)
49                 web->debug_func(str, web->debug_data);
50
51         va_end(ap);
52 }
53
54 GWeb *g_web_new(void)
55 {
56         GWeb *web;
57
58         web = g_try_new0(GWeb, 1);
59         if (web == NULL)
60                 return NULL;
61
62         web->ref_count = 1;
63
64         return web;
65 }
66
67 GWeb *g_web_ref(GWeb *web)
68 {
69         if (web == NULL)
70                 return NULL;
71
72         g_atomic_int_inc(&web->ref_count);
73
74         return web;
75 }
76
77 void g_web_unref(GWeb *web)
78 {
79         if (web == NULL)
80                 return;
81
82         if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
83                 return;
84
85         g_free(web);
86 }
87
88 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
89 {
90         if (web == NULL)
91                 return;
92
93         web->debug_func = func;
94         web->debug_data = user_data;
95 }