blob: f80a9d244c0f8f31978ea3575261fd1613fb8bc0 [file] [log] [blame]
Darin Petkov36a3ace2012-03-06 17:22:14 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovf7897bc2011-06-08 17:13:36 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Christopher Wiley5447d2e2013-03-19 17:46:03 -07005#include "shill/glib.h"
6
7#include <string>
8
Darin Petkov083047b2011-06-23 20:42:48 -07009#include <base/stringprintf.h>
10
Christopher Wiley5447d2e2013-03-19 17:46:03 -070011#include "shill/logging.h"
12
13using std::string;
Darin Petkovf7897bc2011-06-08 17:13:36 -070014
15namespace shill {
16
Christopher Wiley0f3eab32013-03-21 11:55:41 -070017GLib::GLib() {}
Darin Petkov3258a812011-06-23 11:28:45 -070018GLib::~GLib() {}
19
Darin Petkov083047b2011-06-23 20:42:48 -070020std::string GLib::ConvertErrorToMessage(GError *error) {
21 if (!error) {
22 return "Unknown GLib error.";
23 }
24 std::string message =
25 base::StringPrintf("GError(%d): %s", error->code, error->message);
26 g_error_free(error);
27 return message;
28}
29
Christopher Wiley5447d2e2013-03-19 17:46:03 -070030bool GLib::B64Decode(const string &input, string *output) {
31 CHECK(output);
32 gsize result_len = 0;
Christopher Wiley0f3eab32013-03-21 11:55:41 -070033 guchar *result = g_base64_decode(input.c_str(), &result_len);
Christopher Wiley5447d2e2013-03-19 17:46:03 -070034 if (!result) {
35 LOG(ERROR) << "Failed in encoding.";
36 return false;
37 }
38
39 if (!result_len) {
40 LOG(ERROR) << "Failed in encoding.";
41 Free(result);
42 return false;
43 }
44
45 output->assign(reinterpret_cast<char *>(result), result_len);
46 Free(result);
47 return true;
48}
49
50bool GLib::B64Encode(const string &input, string *output) {
51 CHECK(output);
Christopher Wiley0f3eab32013-03-21 11:55:41 -070052 gchar *result = g_base64_encode(
Christopher Wiley5447d2e2013-03-19 17:46:03 -070053 reinterpret_cast<const unsigned char *>(input.c_str()), input.length());
54 if (!result) {
55 LOG(ERROR) << "Failed in encoding.";
56 return false;
57 }
58
59 output->assign(result);
60 Free(result);
61 return true;
62}
63
Darin Petkov887f2982011-07-14 16:10:17 -070064void GLib::BusUnwatchName(guint watcher_id) {
65 g_bus_unwatch_name(watcher_id);
66}
67
68guint GLib::BusWatchName(GBusType bus_type,
69 const gchar *name,
70 GBusNameWatcherFlags flags,
71 GBusNameAppearedCallback name_appeared_handler,
72 GBusNameVanishedCallback name_vanished_handler,
73 gpointer user_data,
74 GDestroyNotify user_data_free_func) {
75 return g_bus_watch_name(bus_type,
76 name,
77 flags,
78 name_appeared_handler,
79 name_vanished_handler,
80 user_data,
81 user_data_free_func);
82}
83
Darin Petkov92c43902011-06-09 20:46:06 -070084guint GLib::ChildWatchAdd(GPid pid,
85 GChildWatchFunc function,
86 gpointer data) {
87 return g_child_watch_add(pid, function, data);
88}
89
Darin Petkov083047b2011-06-23 20:42:48 -070090void GLib::Free(gpointer mem) {
91 g_free(mem);
92}
93
94void GLib::KeyFileFree(GKeyFile *key_file) {
95 g_key_file_free(key_file);
96}
97
98gboolean GLib::KeyFileLoadFromFile(GKeyFile *key_file,
99 const gchar *file,
100 GKeyFileFlags flags,
101 GError **error) {
102 return g_key_file_load_from_file(key_file, file, flags, error);
103}
104
105gboolean GLib::KeyFileGetBoolean(GKeyFile *key_file,
106 const gchar *group_name,
107 const gchar *key,
108 GError **error) {
109 return g_key_file_get_boolean(key_file, group_name, key, error);
110}
111
112gchar **GLib::KeyFileGetGroups(GKeyFile *key_file,
113 gsize *length) {
114 return g_key_file_get_groups(key_file, length);
115}
116
117gint GLib::KeyFileGetInteger(GKeyFile *key_file,
118 const gchar *group_name,
119 const gchar *key,
120 GError **error) {
121 return g_key_file_get_integer(key_file, group_name, key, error);
122}
123
124gchar *GLib::KeyFileGetString(GKeyFile *key_file,
125 const gchar *group_name,
126 const gchar *key,
127 GError **error) {
128 return g_key_file_get_string(key_file, group_name, key, error);
129}
130
Darin Petkovb2841fd2011-06-30 12:54:12 -0700131gchar **GLib::KeyFileGetStringList(GKeyFile *key_file,
132 const gchar *group_name,
133 const gchar *key,
134 gsize *length,
135 GError **error) {
136 return g_key_file_get_string_list(key_file, group_name, key, length, error);
137}
138
Darin Petkov083047b2011-06-23 20:42:48 -0700139gboolean GLib::KeyFileHasGroup(GKeyFile *key_file,
140 const gchar *group_name) {
141 return g_key_file_has_group(key_file, group_name);
142}
143
Paul Stewarta41e38d2011-11-11 07:47:29 -0800144gboolean GLib::KeyFileHasKey(GKeyFile *key_file,
145 const gchar *group_name,
146 const gchar *key,
147 GError **error) {
148 return g_key_file_has_key(key_file, group_name, key, error);
149}
150
Darin Petkov083047b2011-06-23 20:42:48 -0700151GKeyFile *GLib::KeyFileNew() {
152 return g_key_file_new();
153}
154
155void GLib::KeyFileRemoveGroup(GKeyFile *key_file,
156 const gchar *group_name,
157 GError **error) {
158 g_key_file_remove_group(key_file, group_name, error);
159}
160
161void GLib::KeyFileRemoveKey(GKeyFile *key_file,
162 const gchar *group_name,
163 const gchar *key,
164 GError **error) {
165 g_key_file_remove_key(key_file, group_name, key, error);
166}
167
168void GLib::KeyFileSetBoolean(GKeyFile *key_file,
169 const gchar *group_name,
170 const gchar *key,
171 gboolean value) {
172 g_key_file_set_boolean(key_file, group_name, key, value);
173}
174
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700175gboolean GLib::KeyFileSetComment(GKeyFile *key_file,
176 const gchar *group_name,
177 const gchar *key,
178 const gchar *comment,
179 GError **error) {
180 return g_key_file_set_comment(key_file, group_name, key, comment, error);
181}
182
Darin Petkov083047b2011-06-23 20:42:48 -0700183void GLib::KeyFileSetInteger(GKeyFile *key_file,
184 const gchar *group_name,
185 const gchar *key,
186 gint value) {
187 g_key_file_set_integer(key_file, group_name, key, value);
188}
189
190void GLib::KeyFileSetString(GKeyFile *key_file,
191 const gchar *group_name,
192 const gchar *key,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800193 const gchar *value) {
194 g_key_file_set_string(key_file, group_name, key, value);
Darin Petkov083047b2011-06-23 20:42:48 -0700195}
196
Darin Petkovb2841fd2011-06-30 12:54:12 -0700197void GLib::KeyFileSetStringList(GKeyFile *key_file,
198 const gchar *group_name,
199 const gchar *key,
200 const gchar * const list[],
201 gsize length) {
202 g_key_file_set_string_list(key_file, group_name, key, list, length);
203}
204
Darin Petkov083047b2011-06-23 20:42:48 -0700205gchar *GLib::KeyFileToData(GKeyFile *key_file,
206 gsize *length,
207 GError **error) {
208 return g_key_file_to_data(key_file, length, error);
209}
210
Darin Petkov92c43902011-06-09 20:46:06 -0700211gboolean GLib::SourceRemove(guint tag) {
212 return g_source_remove(tag);
213}
214
Darin Petkovf7897bc2011-06-08 17:13:36 -0700215gboolean GLib::SpawnAsync(const gchar *working_directory,
216 gchar **argv,
217 gchar **envp,
218 GSpawnFlags flags,
219 GSpawnChildSetupFunc child_setup,
220 gpointer user_data,
221 GPid *child_pid,
222 GError **error) {
223 return g_spawn_async(working_directory,
224 argv,
225 envp,
226 flags,
227 child_setup,
228 user_data,
229 child_pid,
230 error);
231}
232
Darin Petkov92c43902011-06-09 20:46:06 -0700233void GLib::SpawnClosePID(GPid pid) {
234 g_spawn_close_pid(pid);
235}
236
Darin Petkov3c5e4dc2012-04-02 14:44:27 +0200237gboolean GLib::SpawnSync(const gchar *working_directory,
238 gchar **argv,
239 gchar **envp,
240 GSpawnFlags flags,
241 GSpawnChildSetupFunc child_setup,
242 gpointer user_data,
243 gchar **standard_output,
244 gchar **standard_error,
245 gint *exit_status,
246 GError **error) {
247 return g_spawn_sync(working_directory,
248 argv,
249 envp,
250 flags,
251 child_setup,
252 user_data,
253 standard_output,
254 standard_error,
255 exit_status,
256 error);
257}
258
Darin Petkov083047b2011-06-23 20:42:48 -0700259void GLib::Strfreev(gchar **str_array) {
260 g_strfreev(str_array);
261}
262
Darin Petkov887f2982011-07-14 16:10:17 -0700263void GLib::TypeInit() {
264 g_type_init();
265}
266
Darin Petkovf7897bc2011-06-08 17:13:36 -0700267} // namespace shill