blob: b06cfb5093437cff76b4a17ac3be5674d3e33a3e [file] [log] [blame]
Darin Petkovf7897bc2011-06-08 17:13:36 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkov083047b2011-06-23 20:42:48 -07005#include <base/stringprintf.h>
6
Darin Petkovf7897bc2011-06-08 17:13:36 -07007#include "shill/glib.h"
8
9namespace shill {
10
Darin Petkov3258a812011-06-23 11:28:45 -070011GLib::~GLib() {}
12
Darin Petkov083047b2011-06-23 20:42:48 -070013std::string GLib::ConvertErrorToMessage(GError *error) {
14 if (!error) {
15 return "Unknown GLib error.";
16 }
17 std::string message =
18 base::StringPrintf("GError(%d): %s", error->code, error->message);
19 g_error_free(error);
20 return message;
21}
22
Darin Petkov823c47e2011-06-27 16:15:35 -070023guchar *GLib::Base64Decode(const gchar *text, gsize *out_len) {
24 return g_base64_decode(text, out_len);
25}
26
27gchar *GLib::Base64Encode(const guchar *data, gsize len) {
28 return g_base64_encode(data, len);
29}
30
Darin Petkov887f2982011-07-14 16:10:17 -070031void GLib::BusUnwatchName(guint watcher_id) {
32 g_bus_unwatch_name(watcher_id);
33}
34
35guint GLib::BusWatchName(GBusType bus_type,
36 const gchar *name,
37 GBusNameWatcherFlags flags,
38 GBusNameAppearedCallback name_appeared_handler,
39 GBusNameVanishedCallback name_vanished_handler,
40 gpointer user_data,
41 GDestroyNotify user_data_free_func) {
42 return g_bus_watch_name(bus_type,
43 name,
44 flags,
45 name_appeared_handler,
46 name_vanished_handler,
47 user_data,
48 user_data_free_func);
49}
50
Darin Petkov92c43902011-06-09 20:46:06 -070051guint GLib::ChildWatchAdd(GPid pid,
52 GChildWatchFunc function,
53 gpointer data) {
54 return g_child_watch_add(pid, function, data);
55}
56
Darin Petkov083047b2011-06-23 20:42:48 -070057void GLib::Free(gpointer mem) {
58 g_free(mem);
59}
60
61void GLib::KeyFileFree(GKeyFile *key_file) {
62 g_key_file_free(key_file);
63}
64
65gboolean GLib::KeyFileLoadFromFile(GKeyFile *key_file,
66 const gchar *file,
67 GKeyFileFlags flags,
68 GError **error) {
69 return g_key_file_load_from_file(key_file, file, flags, error);
70}
71
72gboolean GLib::KeyFileGetBoolean(GKeyFile *key_file,
73 const gchar *group_name,
74 const gchar *key,
75 GError **error) {
76 return g_key_file_get_boolean(key_file, group_name, key, error);
77}
78
79gchar **GLib::KeyFileGetGroups(GKeyFile *key_file,
80 gsize *length) {
81 return g_key_file_get_groups(key_file, length);
82}
83
84gint GLib::KeyFileGetInteger(GKeyFile *key_file,
85 const gchar *group_name,
86 const gchar *key,
87 GError **error) {
88 return g_key_file_get_integer(key_file, group_name, key, error);
89}
90
91gchar *GLib::KeyFileGetString(GKeyFile *key_file,
92 const gchar *group_name,
93 const gchar *key,
94 GError **error) {
95 return g_key_file_get_string(key_file, group_name, key, error);
96}
97
Darin Petkovb2841fd2011-06-30 12:54:12 -070098gchar **GLib::KeyFileGetStringList(GKeyFile *key_file,
99 const gchar *group_name,
100 const gchar *key,
101 gsize *length,
102 GError **error) {
103 return g_key_file_get_string_list(key_file, group_name, key, length, error);
104}
105
Darin Petkov083047b2011-06-23 20:42:48 -0700106gboolean GLib::KeyFileHasGroup(GKeyFile *key_file,
107 const gchar *group_name) {
108 return g_key_file_has_group(key_file, group_name);
109}
110
111GKeyFile *GLib::KeyFileNew() {
112 return g_key_file_new();
113}
114
115void GLib::KeyFileRemoveGroup(GKeyFile *key_file,
116 const gchar *group_name,
117 GError **error) {
118 g_key_file_remove_group(key_file, group_name, error);
119}
120
121void GLib::KeyFileRemoveKey(GKeyFile *key_file,
122 const gchar *group_name,
123 const gchar *key,
124 GError **error) {
125 g_key_file_remove_key(key_file, group_name, key, error);
126}
127
128void GLib::KeyFileSetBoolean(GKeyFile *key_file,
129 const gchar *group_name,
130 const gchar *key,
131 gboolean value) {
132 g_key_file_set_boolean(key_file, group_name, key, value);
133}
134
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700135gboolean GLib::KeyFileSetComment(GKeyFile *key_file,
136 const gchar *group_name,
137 const gchar *key,
138 const gchar *comment,
139 GError **error) {
140 return g_key_file_set_comment(key_file, group_name, key, comment, error);
141}
142
Darin Petkov083047b2011-06-23 20:42:48 -0700143void GLib::KeyFileSetInteger(GKeyFile *key_file,
144 const gchar *group_name,
145 const gchar *key,
146 gint value) {
147 g_key_file_set_integer(key_file, group_name, key, value);
148}
149
150void GLib::KeyFileSetString(GKeyFile *key_file,
151 const gchar *group_name,
152 const gchar *key,
153 const gchar *string) {
154 g_key_file_set_string(key_file, group_name, key, string);
155}
156
Darin Petkovb2841fd2011-06-30 12:54:12 -0700157void GLib::KeyFileSetStringList(GKeyFile *key_file,
158 const gchar *group_name,
159 const gchar *key,
160 const gchar * const list[],
161 gsize length) {
162 g_key_file_set_string_list(key_file, group_name, key, list, length);
163}
164
Darin Petkov083047b2011-06-23 20:42:48 -0700165gchar *GLib::KeyFileToData(GKeyFile *key_file,
166 gsize *length,
167 GError **error) {
168 return g_key_file_to_data(key_file, length, error);
169}
170
Darin Petkov92c43902011-06-09 20:46:06 -0700171gboolean GLib::SourceRemove(guint tag) {
172 return g_source_remove(tag);
173}
174
Darin Petkovf7897bc2011-06-08 17:13:36 -0700175gboolean GLib::SpawnAsync(const gchar *working_directory,
176 gchar **argv,
177 gchar **envp,
178 GSpawnFlags flags,
179 GSpawnChildSetupFunc child_setup,
180 gpointer user_data,
181 GPid *child_pid,
182 GError **error) {
183 return g_spawn_async(working_directory,
184 argv,
185 envp,
186 flags,
187 child_setup,
188 user_data,
189 child_pid,
190 error);
191}
192
Darin Petkov92c43902011-06-09 20:46:06 -0700193void GLib::SpawnClosePID(GPid pid) {
194 g_spawn_close_pid(pid);
195}
196
Darin Petkov083047b2011-06-23 20:42:48 -0700197void GLib::Strfreev(gchar **str_array) {
198 g_strfreev(str_array);
199}
200
Darin Petkov887f2982011-07-14 16:10:17 -0700201void GLib::TypeInit() {
202 g_type_init();
203}
204
Darin Petkovf7897bc2011-06-08 17:13:36 -0700205} // namespace shill