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 | |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 17 | GLib::~GLib() {} |
| 18 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 19 | std::string GLib::ConvertErrorToMessage(GError *error) { |
| 20 | if (!error) { |
| 21 | return "Unknown GLib error."; |
| 22 | } |
| 23 | std::string message = |
| 24 | base::StringPrintf("GError(%d): %s", error->code, error->message); |
| 25 | g_error_free(error); |
| 26 | return message; |
| 27 | } |
| 28 | |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 29 | guchar *GLib::Base64Decode(const gchar *text, gsize *out_len) { |
| 30 | return g_base64_decode(text, out_len); |
| 31 | } |
| 32 | |
| 33 | gchar *GLib::Base64Encode(const guchar *data, gsize len) { |
| 34 | return g_base64_encode(data, len); |
| 35 | } |
| 36 | |
Christopher Wiley | 5447d2e | 2013-03-19 17:46:03 -0700 | [diff] [blame^] | 37 | bool GLib::B64Decode(const string &input, string *output) { |
| 38 | CHECK(output); |
| 39 | gsize result_len = 0; |
| 40 | guchar *result = Base64Decode(input.c_str(), &result_len); |
| 41 | if (!result) { |
| 42 | LOG(ERROR) << "Failed in encoding."; |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | if (!result_len) { |
| 47 | LOG(ERROR) << "Failed in encoding."; |
| 48 | Free(result); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | output->assign(reinterpret_cast<char *>(result), result_len); |
| 53 | Free(result); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool GLib::B64Encode(const string &input, string *output) { |
| 58 | CHECK(output); |
| 59 | gchar *result = Base64Encode( |
| 60 | reinterpret_cast<const unsigned char *>(input.c_str()), input.length()); |
| 61 | if (!result) { |
| 62 | LOG(ERROR) << "Failed in encoding."; |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | output->assign(result); |
| 67 | Free(result); |
| 68 | return true; |
| 69 | } |
| 70 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 71 | void GLib::BusUnwatchName(guint watcher_id) { |
| 72 | g_bus_unwatch_name(watcher_id); |
| 73 | } |
| 74 | |
| 75 | guint GLib::BusWatchName(GBusType bus_type, |
| 76 | const gchar *name, |
| 77 | GBusNameWatcherFlags flags, |
| 78 | GBusNameAppearedCallback name_appeared_handler, |
| 79 | GBusNameVanishedCallback name_vanished_handler, |
| 80 | gpointer user_data, |
| 81 | GDestroyNotify user_data_free_func) { |
| 82 | return g_bus_watch_name(bus_type, |
| 83 | name, |
| 84 | flags, |
| 85 | name_appeared_handler, |
| 86 | name_vanished_handler, |
| 87 | user_data, |
| 88 | user_data_free_func); |
| 89 | } |
| 90 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 91 | guint GLib::ChildWatchAdd(GPid pid, |
| 92 | GChildWatchFunc function, |
| 93 | gpointer data) { |
| 94 | return g_child_watch_add(pid, function, data); |
| 95 | } |
| 96 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 97 | void GLib::Free(gpointer mem) { |
| 98 | g_free(mem); |
| 99 | } |
| 100 | |
| 101 | void GLib::KeyFileFree(GKeyFile *key_file) { |
| 102 | g_key_file_free(key_file); |
| 103 | } |
| 104 | |
| 105 | gboolean GLib::KeyFileLoadFromFile(GKeyFile *key_file, |
| 106 | const gchar *file, |
| 107 | GKeyFileFlags flags, |
| 108 | GError **error) { |
| 109 | return g_key_file_load_from_file(key_file, file, flags, error); |
| 110 | } |
| 111 | |
| 112 | gboolean GLib::KeyFileGetBoolean(GKeyFile *key_file, |
| 113 | const gchar *group_name, |
| 114 | const gchar *key, |
| 115 | GError **error) { |
| 116 | return g_key_file_get_boolean(key_file, group_name, key, error); |
| 117 | } |
| 118 | |
| 119 | gchar **GLib::KeyFileGetGroups(GKeyFile *key_file, |
| 120 | gsize *length) { |
| 121 | return g_key_file_get_groups(key_file, length); |
| 122 | } |
| 123 | |
| 124 | gint GLib::KeyFileGetInteger(GKeyFile *key_file, |
| 125 | const gchar *group_name, |
| 126 | const gchar *key, |
| 127 | GError **error) { |
| 128 | return g_key_file_get_integer(key_file, group_name, key, error); |
| 129 | } |
| 130 | |
| 131 | gchar *GLib::KeyFileGetString(GKeyFile *key_file, |
| 132 | const gchar *group_name, |
| 133 | const gchar *key, |
| 134 | GError **error) { |
| 135 | return g_key_file_get_string(key_file, group_name, key, error); |
| 136 | } |
| 137 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 138 | gchar **GLib::KeyFileGetStringList(GKeyFile *key_file, |
| 139 | const gchar *group_name, |
| 140 | const gchar *key, |
| 141 | gsize *length, |
| 142 | GError **error) { |
| 143 | return g_key_file_get_string_list(key_file, group_name, key, length, error); |
| 144 | } |
| 145 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 146 | gboolean GLib::KeyFileHasGroup(GKeyFile *key_file, |
| 147 | const gchar *group_name) { |
| 148 | return g_key_file_has_group(key_file, group_name); |
| 149 | } |
| 150 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 151 | gboolean GLib::KeyFileHasKey(GKeyFile *key_file, |
| 152 | const gchar *group_name, |
| 153 | const gchar *key, |
| 154 | GError **error) { |
| 155 | return g_key_file_has_key(key_file, group_name, key, error); |
| 156 | } |
| 157 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 158 | GKeyFile *GLib::KeyFileNew() { |
| 159 | return g_key_file_new(); |
| 160 | } |
| 161 | |
| 162 | void GLib::KeyFileRemoveGroup(GKeyFile *key_file, |
| 163 | const gchar *group_name, |
| 164 | GError **error) { |
| 165 | g_key_file_remove_group(key_file, group_name, error); |
| 166 | } |
| 167 | |
| 168 | void GLib::KeyFileRemoveKey(GKeyFile *key_file, |
| 169 | const gchar *group_name, |
| 170 | const gchar *key, |
| 171 | GError **error) { |
| 172 | g_key_file_remove_key(key_file, group_name, key, error); |
| 173 | } |
| 174 | |
| 175 | void GLib::KeyFileSetBoolean(GKeyFile *key_file, |
| 176 | const gchar *group_name, |
| 177 | const gchar *key, |
| 178 | gboolean value) { |
| 179 | g_key_file_set_boolean(key_file, group_name, key, value); |
| 180 | } |
| 181 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 182 | gboolean GLib::KeyFileSetComment(GKeyFile *key_file, |
| 183 | const gchar *group_name, |
| 184 | const gchar *key, |
| 185 | const gchar *comment, |
| 186 | GError **error) { |
| 187 | return g_key_file_set_comment(key_file, group_name, key, comment, error); |
| 188 | } |
| 189 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 190 | void GLib::KeyFileSetInteger(GKeyFile *key_file, |
| 191 | const gchar *group_name, |
| 192 | const gchar *key, |
| 193 | gint value) { |
| 194 | g_key_file_set_integer(key_file, group_name, key, value); |
| 195 | } |
| 196 | |
| 197 | void GLib::KeyFileSetString(GKeyFile *key_file, |
| 198 | const gchar *group_name, |
| 199 | const gchar *key, |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 200 | const gchar *value) { |
| 201 | g_key_file_set_string(key_file, group_name, key, value); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 204 | void GLib::KeyFileSetStringList(GKeyFile *key_file, |
| 205 | const gchar *group_name, |
| 206 | const gchar *key, |
| 207 | const gchar * const list[], |
| 208 | gsize length) { |
| 209 | g_key_file_set_string_list(key_file, group_name, key, list, length); |
| 210 | } |
| 211 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 212 | gchar *GLib::KeyFileToData(GKeyFile *key_file, |
| 213 | gsize *length, |
| 214 | GError **error) { |
| 215 | return g_key_file_to_data(key_file, length, error); |
| 216 | } |
| 217 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 218 | gboolean GLib::SourceRemove(guint tag) { |
| 219 | return g_source_remove(tag); |
| 220 | } |
| 221 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 222 | gboolean GLib::SpawnAsync(const gchar *working_directory, |
| 223 | gchar **argv, |
| 224 | gchar **envp, |
| 225 | GSpawnFlags flags, |
| 226 | GSpawnChildSetupFunc child_setup, |
| 227 | gpointer user_data, |
| 228 | GPid *child_pid, |
| 229 | GError **error) { |
| 230 | return g_spawn_async(working_directory, |
| 231 | argv, |
| 232 | envp, |
| 233 | flags, |
| 234 | child_setup, |
| 235 | user_data, |
| 236 | child_pid, |
| 237 | error); |
| 238 | } |
| 239 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 240 | void GLib::SpawnClosePID(GPid pid) { |
| 241 | g_spawn_close_pid(pid); |
| 242 | } |
| 243 | |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 244 | gboolean GLib::SpawnSync(const gchar *working_directory, |
| 245 | gchar **argv, |
| 246 | gchar **envp, |
| 247 | GSpawnFlags flags, |
| 248 | GSpawnChildSetupFunc child_setup, |
| 249 | gpointer user_data, |
| 250 | gchar **standard_output, |
| 251 | gchar **standard_error, |
| 252 | gint *exit_status, |
| 253 | GError **error) { |
| 254 | return g_spawn_sync(working_directory, |
| 255 | argv, |
| 256 | envp, |
| 257 | flags, |
| 258 | child_setup, |
| 259 | user_data, |
| 260 | standard_output, |
| 261 | standard_error, |
| 262 | exit_status, |
| 263 | error); |
| 264 | } |
| 265 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 266 | void GLib::Strfreev(gchar **str_array) { |
| 267 | g_strfreev(str_array); |
| 268 | } |
| 269 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 270 | void GLib::TypeInit() { |
| 271 | g_type_init(); |
| 272 | } |
| 273 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 274 | } // namespace shill |