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 | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 31 | guint GLib::ChildWatchAdd(GPid pid, |
| 32 | GChildWatchFunc function, |
| 33 | gpointer data) { |
| 34 | return g_child_watch_add(pid, function, data); |
| 35 | } |
| 36 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 37 | void GLib::Free(gpointer mem) { |
| 38 | g_free(mem); |
| 39 | } |
| 40 | |
| 41 | void GLib::KeyFileFree(GKeyFile *key_file) { |
| 42 | g_key_file_free(key_file); |
| 43 | } |
| 44 | |
| 45 | gboolean GLib::KeyFileLoadFromFile(GKeyFile *key_file, |
| 46 | const gchar *file, |
| 47 | GKeyFileFlags flags, |
| 48 | GError **error) { |
| 49 | return g_key_file_load_from_file(key_file, file, flags, error); |
| 50 | } |
| 51 | |
| 52 | gboolean GLib::KeyFileGetBoolean(GKeyFile *key_file, |
| 53 | const gchar *group_name, |
| 54 | const gchar *key, |
| 55 | GError **error) { |
| 56 | return g_key_file_get_boolean(key_file, group_name, key, error); |
| 57 | } |
| 58 | |
| 59 | gchar **GLib::KeyFileGetGroups(GKeyFile *key_file, |
| 60 | gsize *length) { |
| 61 | return g_key_file_get_groups(key_file, length); |
| 62 | } |
| 63 | |
| 64 | gint GLib::KeyFileGetInteger(GKeyFile *key_file, |
| 65 | const gchar *group_name, |
| 66 | const gchar *key, |
| 67 | GError **error) { |
| 68 | return g_key_file_get_integer(key_file, group_name, key, error); |
| 69 | } |
| 70 | |
| 71 | gchar *GLib::KeyFileGetString(GKeyFile *key_file, |
| 72 | const gchar *group_name, |
| 73 | const gchar *key, |
| 74 | GError **error) { |
| 75 | return g_key_file_get_string(key_file, group_name, key, error); |
| 76 | } |
| 77 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 78 | gchar **GLib::KeyFileGetStringList(GKeyFile *key_file, |
| 79 | const gchar *group_name, |
| 80 | const gchar *key, |
| 81 | gsize *length, |
| 82 | GError **error) { |
| 83 | return g_key_file_get_string_list(key_file, group_name, key, length, error); |
| 84 | } |
| 85 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 86 | gboolean GLib::KeyFileHasGroup(GKeyFile *key_file, |
| 87 | const gchar *group_name) { |
| 88 | return g_key_file_has_group(key_file, group_name); |
| 89 | } |
| 90 | |
| 91 | GKeyFile *GLib::KeyFileNew() { |
| 92 | return g_key_file_new(); |
| 93 | } |
| 94 | |
| 95 | void GLib::KeyFileRemoveGroup(GKeyFile *key_file, |
| 96 | const gchar *group_name, |
| 97 | GError **error) { |
| 98 | g_key_file_remove_group(key_file, group_name, error); |
| 99 | } |
| 100 | |
| 101 | void GLib::KeyFileRemoveKey(GKeyFile *key_file, |
| 102 | const gchar *group_name, |
| 103 | const gchar *key, |
| 104 | GError **error) { |
| 105 | g_key_file_remove_key(key_file, group_name, key, error); |
| 106 | } |
| 107 | |
| 108 | void GLib::KeyFileSetBoolean(GKeyFile *key_file, |
| 109 | const gchar *group_name, |
| 110 | const gchar *key, |
| 111 | gboolean value) { |
| 112 | g_key_file_set_boolean(key_file, group_name, key, value); |
| 113 | } |
| 114 | |
| 115 | void GLib::KeyFileSetInteger(GKeyFile *key_file, |
| 116 | const gchar *group_name, |
| 117 | const gchar *key, |
| 118 | gint value) { |
| 119 | g_key_file_set_integer(key_file, group_name, key, value); |
| 120 | } |
| 121 | |
| 122 | void GLib::KeyFileSetString(GKeyFile *key_file, |
| 123 | const gchar *group_name, |
| 124 | const gchar *key, |
| 125 | const gchar *string) { |
| 126 | g_key_file_set_string(key_file, group_name, key, string); |
| 127 | } |
| 128 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 129 | void GLib::KeyFileSetStringList(GKeyFile *key_file, |
| 130 | const gchar *group_name, |
| 131 | const gchar *key, |
| 132 | const gchar * const list[], |
| 133 | gsize length) { |
| 134 | g_key_file_set_string_list(key_file, group_name, key, list, length); |
| 135 | } |
| 136 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 137 | gchar *GLib::KeyFileToData(GKeyFile *key_file, |
| 138 | gsize *length, |
| 139 | GError **error) { |
| 140 | return g_key_file_to_data(key_file, length, error); |
| 141 | } |
| 142 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 143 | gboolean GLib::SourceRemove(guint tag) { |
| 144 | return g_source_remove(tag); |
| 145 | } |
| 146 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 147 | gboolean GLib::SpawnAsync(const gchar *working_directory, |
| 148 | gchar **argv, |
| 149 | gchar **envp, |
| 150 | GSpawnFlags flags, |
| 151 | GSpawnChildSetupFunc child_setup, |
| 152 | gpointer user_data, |
| 153 | GPid *child_pid, |
| 154 | GError **error) { |
| 155 | return g_spawn_async(working_directory, |
| 156 | argv, |
| 157 | envp, |
| 158 | flags, |
| 159 | child_setup, |
| 160 | user_data, |
| 161 | child_pid, |
| 162 | error); |
| 163 | } |
| 164 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 165 | void GLib::SpawnClosePID(GPid pid) { |
| 166 | g_spawn_close_pid(pid); |
| 167 | } |
| 168 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 169 | void GLib::Strfreev(gchar **str_array) { |
| 170 | g_strfreev(str_array); |
| 171 | } |
| 172 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 173 | } // namespace shill |