blob: a462ac72597fa329d2f088616a310f3e10185e8a [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
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
Paul Stewarta41e38d2011-11-11 07:47:29 -0800111gboolean GLib::KeyFileHasKey(GKeyFile *key_file,
112 const gchar *group_name,
113 const gchar *key,
114 GError **error) {
115 return g_key_file_has_key(key_file, group_name, key, error);
116}
117
Darin Petkov083047b2011-06-23 20:42:48 -0700118GKeyFile *GLib::KeyFileNew() {
119 return g_key_file_new();
120}
121
122void GLib::KeyFileRemoveGroup(GKeyFile *key_file,
123 const gchar *group_name,
124 GError **error) {
125 g_key_file_remove_group(key_file, group_name, error);
126}
127
128void GLib::KeyFileRemoveKey(GKeyFile *key_file,
129 const gchar *group_name,
130 const gchar *key,
131 GError **error) {
132 g_key_file_remove_key(key_file, group_name, key, error);
133}
134
135void GLib::KeyFileSetBoolean(GKeyFile *key_file,
136 const gchar *group_name,
137 const gchar *key,
138 gboolean value) {
139 g_key_file_set_boolean(key_file, group_name, key, value);
140}
141
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700142gboolean GLib::KeyFileSetComment(GKeyFile *key_file,
143 const gchar *group_name,
144 const gchar *key,
145 const gchar *comment,
146 GError **error) {
147 return g_key_file_set_comment(key_file, group_name, key, comment, error);
148}
149
Darin Petkov083047b2011-06-23 20:42:48 -0700150void GLib::KeyFileSetInteger(GKeyFile *key_file,
151 const gchar *group_name,
152 const gchar *key,
153 gint value) {
154 g_key_file_set_integer(key_file, group_name, key, value);
155}
156
157void GLib::KeyFileSetString(GKeyFile *key_file,
158 const gchar *group_name,
159 const gchar *key,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800160 const gchar *value) {
161 g_key_file_set_string(key_file, group_name, key, value);
Darin Petkov083047b2011-06-23 20:42:48 -0700162}
163
Darin Petkovb2841fd2011-06-30 12:54:12 -0700164void GLib::KeyFileSetStringList(GKeyFile *key_file,
165 const gchar *group_name,
166 const gchar *key,
167 const gchar * const list[],
168 gsize length) {
169 g_key_file_set_string_list(key_file, group_name, key, list, length);
170}
171
Darin Petkov083047b2011-06-23 20:42:48 -0700172gchar *GLib::KeyFileToData(GKeyFile *key_file,
173 gsize *length,
174 GError **error) {
175 return g_key_file_to_data(key_file, length, error);
176}
177
Darin Petkov92c43902011-06-09 20:46:06 -0700178gboolean GLib::SourceRemove(guint tag) {
179 return g_source_remove(tag);
180}
181
Darin Petkovf7897bc2011-06-08 17:13:36 -0700182gboolean GLib::SpawnAsync(const gchar *working_directory,
183 gchar **argv,
184 gchar **envp,
185 GSpawnFlags flags,
186 GSpawnChildSetupFunc child_setup,
187 gpointer user_data,
188 GPid *child_pid,
189 GError **error) {
190 return g_spawn_async(working_directory,
191 argv,
192 envp,
193 flags,
194 child_setup,
195 user_data,
196 child_pid,
197 error);
198}
199
Darin Petkov36a3ace2012-03-06 17:22:14 +0100200gboolean GLib::SpawnAsyncWithPipesCWD(gchar **argv,
201 gchar **envp,
202 GSpawnFlags flags,
203 GSpawnChildSetupFunc child_setup,
204 gpointer user_data,
205 GPid *child_pid,
206 gint *standard_input,
207 gint *standard_output,
208 gint *standard_error,
209 GError **error) {
210 return g_spawn_async_with_pipes(NULL,
211 argv,
212 envp,
213 flags,
214 child_setup,
215 user_data,
216 child_pid,
217 standard_input,
218 standard_output,
219 standard_error,
220 error);
221}
222
Darin Petkov92c43902011-06-09 20:46:06 -0700223void GLib::SpawnClosePID(GPid pid) {
224 g_spawn_close_pid(pid);
225}
226
Darin Petkov3c5e4dc2012-04-02 14:44:27 +0200227gboolean GLib::SpawnSync(const gchar *working_directory,
228 gchar **argv,
229 gchar **envp,
230 GSpawnFlags flags,
231 GSpawnChildSetupFunc child_setup,
232 gpointer user_data,
233 gchar **standard_output,
234 gchar **standard_error,
235 gint *exit_status,
236 GError **error) {
237 return g_spawn_sync(working_directory,
238 argv,
239 envp,
240 flags,
241 child_setup,
242 user_data,
243 standard_output,
244 standard_error,
245 exit_status,
246 error);
247}
248
Darin Petkov083047b2011-06-23 20:42:48 -0700249void GLib::Strfreev(gchar **str_array) {
250 g_strfreev(str_array);
251}
252
Darin Petkov887f2982011-07-14 16:10:17 -0700253void GLib::TypeInit() {
254 g_type_init();
255}
256
Darin Petkovf7897bc2011-06-08 17:13:36 -0700257} // namespace shill