Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 111 | GKeyFile *GLib::KeyFileNew() { |
| 112 | return g_key_file_new(); |
| 113 | } |
| 114 | |
| 115 | void GLib::KeyFileRemoveGroup(GKeyFile *key_file, |
| 116 | const gchar *group_name, |
| 117 | GError **error) { |
| 118 | g_key_file_remove_group(key_file, group_name, error); |
| 119 | } |
| 120 | |
| 121 | void GLib::KeyFileRemoveKey(GKeyFile *key_file, |
| 122 | const gchar *group_name, |
| 123 | const gchar *key, |
| 124 | GError **error) { |
| 125 | g_key_file_remove_key(key_file, group_name, key, error); |
| 126 | } |
| 127 | |
| 128 | void GLib::KeyFileSetBoolean(GKeyFile *key_file, |
| 129 | const gchar *group_name, |
| 130 | const gchar *key, |
| 131 | gboolean value) { |
| 132 | g_key_file_set_boolean(key_file, group_name, key, value); |
| 133 | } |
| 134 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 135 | gboolean GLib::KeyFileSetComment(GKeyFile *key_file, |
| 136 | const gchar *group_name, |
| 137 | const gchar *key, |
| 138 | const gchar *comment, |
| 139 | GError **error) { |
| 140 | return g_key_file_set_comment(key_file, group_name, key, comment, error); |
| 141 | } |
| 142 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 143 | void GLib::KeyFileSetInteger(GKeyFile *key_file, |
| 144 | const gchar *group_name, |
| 145 | const gchar *key, |
| 146 | gint value) { |
| 147 | g_key_file_set_integer(key_file, group_name, key, value); |
| 148 | } |
| 149 | |
| 150 | void GLib::KeyFileSetString(GKeyFile *key_file, |
| 151 | const gchar *group_name, |
| 152 | const gchar *key, |
| 153 | const gchar *string) { |
| 154 | g_key_file_set_string(key_file, group_name, key, string); |
| 155 | } |
| 156 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 157 | void GLib::KeyFileSetStringList(GKeyFile *key_file, |
| 158 | const gchar *group_name, |
| 159 | const gchar *key, |
| 160 | const gchar * const list[], |
| 161 | gsize length) { |
| 162 | g_key_file_set_string_list(key_file, group_name, key, list, length); |
| 163 | } |
| 164 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 165 | gchar *GLib::KeyFileToData(GKeyFile *key_file, |
| 166 | gsize *length, |
| 167 | GError **error) { |
| 168 | return g_key_file_to_data(key_file, length, error); |
| 169 | } |
| 170 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 171 | gboolean GLib::SourceRemove(guint tag) { |
| 172 | return g_source_remove(tag); |
| 173 | } |
| 174 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 175 | gboolean GLib::SpawnAsync(const gchar *working_directory, |
| 176 | gchar **argv, |
| 177 | gchar **envp, |
| 178 | GSpawnFlags flags, |
| 179 | GSpawnChildSetupFunc child_setup, |
| 180 | gpointer user_data, |
| 181 | GPid *child_pid, |
| 182 | GError **error) { |
| 183 | return g_spawn_async(working_directory, |
| 184 | argv, |
| 185 | envp, |
| 186 | flags, |
| 187 | child_setup, |
| 188 | user_data, |
| 189 | child_pid, |
| 190 | error); |
| 191 | } |
| 192 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 193 | void GLib::SpawnClosePID(GPid pid) { |
| 194 | g_spawn_close_pid(pid); |
| 195 | } |
| 196 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 197 | void GLib::Strfreev(gchar **str_array) { |
| 198 | g_strfreev(str_array); |
| 199 | } |
| 200 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 201 | void GLib::TypeInit() { |
| 202 | g_type_init(); |
| 203 | } |
| 204 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 205 | } // namespace shill |