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 | |
Christopher Wiley | 5447d2e | 2013-03-19 17:46:03 -0700 | [diff] [blame] | 5 | #include "shill/glib.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 9 | #include <base/stringprintf.h> |
| 10 | |
Christopher Wiley | 5447d2e | 2013-03-19 17:46:03 -0700 | [diff] [blame] | 11 | #include "shill/logging.h" |
| 12 | |
| 13 | using std::string; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 14 | |
| 15 | namespace shill { |
| 16 | |
Christopher Wiley | 0f3eab3 | 2013-03-21 11:55:41 -0700 | [diff] [blame] | 17 | GLib::GLib() {} |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 18 | GLib::~GLib() {} |
| 19 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 20 | std::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 Wiley | 5447d2e | 2013-03-19 17:46:03 -0700 | [diff] [blame] | 30 | bool GLib::B64Decode(const string &input, string *output) { |
| 31 | CHECK(output); |
| 32 | gsize result_len = 0; |
Christopher Wiley | 0f3eab3 | 2013-03-21 11:55:41 -0700 | [diff] [blame] | 33 | guchar *result = g_base64_decode(input.c_str(), &result_len); |
Christopher Wiley | 5447d2e | 2013-03-19 17:46:03 -0700 | [diff] [blame] | 34 | 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 | |
| 50 | bool GLib::B64Encode(const string &input, string *output) { |
| 51 | CHECK(output); |
Christopher Wiley | 0f3eab3 | 2013-03-21 11:55:41 -0700 | [diff] [blame] | 52 | gchar *result = g_base64_encode( |
Christopher Wiley | 5447d2e | 2013-03-19 17:46:03 -0700 | [diff] [blame] | 53 | 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 Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 64 | void GLib::BusUnwatchName(guint watcher_id) { |
| 65 | g_bus_unwatch_name(watcher_id); |
| 66 | } |
| 67 | |
| 68 | guint 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 Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 84 | guint GLib::ChildWatchAdd(GPid pid, |
| 85 | GChildWatchFunc function, |
| 86 | gpointer data) { |
| 87 | return g_child_watch_add(pid, function, data); |
| 88 | } |
| 89 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 90 | void GLib::Free(gpointer mem) { |
| 91 | g_free(mem); |
| 92 | } |
| 93 | |
| 94 | void GLib::KeyFileFree(GKeyFile *key_file) { |
| 95 | g_key_file_free(key_file); |
| 96 | } |
| 97 | |
| 98 | gboolean 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 | |
| 105 | gboolean 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 | |
| 112 | gchar **GLib::KeyFileGetGroups(GKeyFile *key_file, |
| 113 | gsize *length) { |
| 114 | return g_key_file_get_groups(key_file, length); |
| 115 | } |
| 116 | |
| 117 | gint 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 | |
| 124 | gchar *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 Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 131 | gchar **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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 139 | gboolean GLib::KeyFileHasGroup(GKeyFile *key_file, |
| 140 | const gchar *group_name) { |
| 141 | return g_key_file_has_group(key_file, group_name); |
| 142 | } |
| 143 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 144 | gboolean 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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 151 | GKeyFile *GLib::KeyFileNew() { |
| 152 | return g_key_file_new(); |
| 153 | } |
| 154 | |
| 155 | void 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 | |
| 161 | void 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 | |
| 168 | void 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 Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 175 | gboolean 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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 183 | void 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 | |
| 190 | void GLib::KeyFileSetString(GKeyFile *key_file, |
| 191 | const gchar *group_name, |
| 192 | const gchar *key, |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 193 | const gchar *value) { |
| 194 | g_key_file_set_string(key_file, group_name, key, value); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 197 | void 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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 205 | gchar *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 Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 211 | gboolean GLib::SourceRemove(guint tag) { |
| 212 | return g_source_remove(tag); |
| 213 | } |
| 214 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 215 | gboolean 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 Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 233 | void GLib::SpawnClosePID(GPid pid) { |
| 234 | g_spawn_close_pid(pid); |
| 235 | } |
| 236 | |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 237 | gboolean 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 Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 259 | void GLib::Strfreev(gchar **str_array) { |
| 260 | g_strfreev(str_array); |
| 261 | } |
| 262 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 263 | void GLib::TypeInit() { |
| 264 | g_type_init(); |
| 265 | } |
| 266 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 267 | } // namespace shill |