Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 5 | #include <base/stringprintf.h> |
| 6 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 7 | #include "shill/glib.h" |
| 8 | |
| 9 | namespace shill { |
| 10 | |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 11 | GLib::~GLib() {} |
| 12 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 13 | std::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 Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 23 | guchar *GLib::Base64Decode(const gchar *text, gsize *out_len) { |
| 24 | return g_base64_decode(text, out_len); |
| 25 | } |
| 26 | |
| 27 | gchar *GLib::Base64Encode(const guchar *data, gsize len) { |
| 28 | return g_base64_encode(data, len); |
| 29 | } |
| 30 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 31 | void GLib::BusUnwatchName(guint watcher_id) { |
| 32 | g_bus_unwatch_name(watcher_id); |
| 33 | } |
| 34 | |
| 35 | guint 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 Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 51 | guint GLib::ChildWatchAdd(GPid pid, |
| 52 | GChildWatchFunc function, |
| 53 | gpointer data) { |
| 54 | return g_child_watch_add(pid, function, data); |
| 55 | } |
| 56 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 57 | void GLib::Free(gpointer mem) { |
| 58 | g_free(mem); |
| 59 | } |
| 60 | |
| 61 | void GLib::KeyFileFree(GKeyFile *key_file) { |
| 62 | g_key_file_free(key_file); |
| 63 | } |
| 64 | |
| 65 | gboolean 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 | |
| 72 | gboolean 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 | |
| 79 | gchar **GLib::KeyFileGetGroups(GKeyFile *key_file, |
| 80 | gsize *length) { |
| 81 | return g_key_file_get_groups(key_file, length); |
| 82 | } |
| 83 | |
| 84 | gint 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 | |
| 91 | gchar *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 Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 98 | gchar **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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 106 | gboolean GLib::KeyFileHasGroup(GKeyFile *key_file, |
| 107 | const gchar *group_name) { |
| 108 | return g_key_file_has_group(key_file, group_name); |
| 109 | } |
| 110 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 111 | gboolean 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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 118 | GKeyFile *GLib::KeyFileNew() { |
| 119 | return g_key_file_new(); |
| 120 | } |
| 121 | |
| 122 | void 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 | |
| 128 | void 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 | |
| 135 | void 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 Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 142 | gboolean 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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 150 | void 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 | |
| 157 | void GLib::KeyFileSetString(GKeyFile *key_file, |
| 158 | const gchar *group_name, |
| 159 | const gchar *key, |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 160 | const gchar *value) { |
| 161 | g_key_file_set_string(key_file, group_name, key, value); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 164 | void 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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 172 | gchar *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 Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 178 | gboolean GLib::SourceRemove(guint tag) { |
| 179 | return g_source_remove(tag); |
| 180 | } |
| 181 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 182 | gboolean 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 Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 200 | void GLib::SpawnClosePID(GPid pid) { |
| 201 | g_spawn_close_pid(pid); |
| 202 | } |
| 203 | |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 204 | gboolean GLib::SpawnSync(const gchar *working_directory, |
| 205 | gchar **argv, |
| 206 | gchar **envp, |
| 207 | GSpawnFlags flags, |
| 208 | GSpawnChildSetupFunc child_setup, |
| 209 | gpointer user_data, |
| 210 | gchar **standard_output, |
| 211 | gchar **standard_error, |
| 212 | gint *exit_status, |
| 213 | GError **error) { |
| 214 | return g_spawn_sync(working_directory, |
| 215 | argv, |
| 216 | envp, |
| 217 | flags, |
| 218 | child_setup, |
| 219 | user_data, |
| 220 | standard_output, |
| 221 | standard_error, |
| 222 | exit_status, |
| 223 | error); |
| 224 | } |
| 225 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 226 | void GLib::Strfreev(gchar **str_array) { |
| 227 | g_strfreev(str_array); |
| 228 | } |
| 229 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 230 | void GLib::TypeInit() { |
| 231 | g_type_init(); |
| 232 | } |
| 233 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 234 | } // namespace shill |