/* Copyright 2006 The gtkmm Development Team * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ namespace Glib { /**** Glib::KeyFile ********************************************************/ KeyFile::KeyFile() { gobject_ = g_key_file_new(); owns_gobject_ = true; } KeyFile::KeyFile(GKeyFile* castitem, bool takes_ownership) { gobject_ = castitem; owns_gobject_ = takes_ownership; } KeyFile::~KeyFile() { if (owns_gobject_) g_key_file_free(gobject_); } bool KeyFile::load_from_data(const Glib::ustring& data, KeyFileFlags flags) { GError* error = 0; const gboolean result = g_key_file_load_from_data( gobj(), data.c_str(), data.bytes(), static_cast(unsigned(flags)), &error); if(error) Glib::Error::throw_exception(error); return (result != 0); } bool KeyFile::load_from_data_dirs(const std::string& file, std::string& full_path, KeyFileFlags flags) { GError* error = 0; char* full_path_c = 0; const gboolean result = g_key_file_load_from_data_dirs( gobj(), file.c_str(), &full_path_c, static_cast(unsigned(flags)), &error); if(error) Glib::Error::throw_exception(error); if(full_path_c) full_path = Glib::ScopedPtr(full_path_c).get(); else full_path.erase(); return (result != 0); } Glib::ustring KeyFile::to_data() { GError* error = 0; char *const str = g_key_file_to_data(gobj(), 0, &error); if(error) Glib::Error::throw_exception(error); return Glib::convert_return_gchar_ptr_to_ustring(str); } Glib::ArrayHandle KeyFile::get_groups() const { gsize length = 0; char** const array = g_key_file_get_groups(const_cast(gobj()), &length); return Glib::ArrayHandle(array, length, Glib::OWNERSHIP_DEEP); } Glib::ArrayHandle KeyFile::get_keys(const Glib::ustring& group_name) const { gsize length = 0; GError* error = 0; char** const array = g_key_file_get_keys( const_cast(gobj()), (group_name.empty()) ? 0 : group_name.c_str(), &length, &error); if(error) Glib::Error::throw_exception(error); return Glib::ArrayHandle(array, length, Glib::OWNERSHIP_DEEP); } Glib::ustring KeyFile::get_locale_string(const Glib::ustring& group_name, const Glib::ustring& key) const { GError* error = 0; char *const str = g_key_file_get_locale_string( const_cast(gobj()), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), 0, &error); if(error) Glib::Error::throw_exception(error); return Glib::convert_return_gchar_ptr_to_ustring(str); } #ifdef GLIBMM_EXCEPTIONS_ENABLED int KeyFile::get_integer(const Glib::ustring& key) const #else int KeyFile::get_integer(const Glib::ustring& key, std::auto_ptr& error) const #endif //GLIBMM_EXCEPTIONS_ENABLED { GError* gerror = 0; const int value = g_key_file_get_integer(const_cast(gobj()), 0, key.c_str(), &gerror); if(gerror) #ifdef GLIBMM_EXCEPTIONS_ENABLED Glib::Error::throw_exception(gerror); #else error = Glib::Error::throw_exception(gerror); #endif return value; } #ifdef GLIBMM_EXCEPTIONS_ENABLED double KeyFile::get_double(const Glib::ustring& key) const #else double KeyFile::get_double(const Glib::ustring& key, std::auto_ptr& error) const #endif //GLIBMM_EXCEPTIONS_ENABLED { GError* gerror = 0; double retvalue = g_key_file_get_double(const_cast(gobj()), NULL, key.c_str(), &(gerror)); #ifdef GLIBMM_EXCEPTIONS_ENABLED if(gerror) ::Glib::Error::throw_exception(gerror); #else if(gerror) error = ::Glib::Error::throw_exception(gerror); #endif //GLIBMM_EXCEPTIONS_ENABLED return retvalue; } void KeyFile::set_double(const Glib::ustring& key, double value) { g_key_file_set_double(gobj(), 0, key.c_str(), value); } #ifdef GLIBMM_EXCEPTIONS_ENABLED # define GLIBMM_ERROR_ARG # define GLIBMM_THROW(err) if (err) Glib::Error::throw_exception(err) #else # define GLIBMM_ERROR_ARG , std::auto_ptr& error # define GLIBMM_THROW(err) if (err) error = Glib::Error::throw_exception(err) #endif Glib::ArrayHandle KeyFile::get_string_list(const Glib::ustring& group_name, const Glib::ustring& key GLIBMM_ERROR_ARG) const { gsize length = 0; GError* gerror = 0; char** const array = g_key_file_get_string_list( const_cast(gobj()), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), &length, &gerror); GLIBMM_THROW(gerror); return Glib::ArrayHandle(array, length, Glib::OWNERSHIP_DEEP); } Glib::ArrayHandle KeyFile::get_locale_string_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale GLIBMM_ERROR_ARG) const { gsize length = 0; GError* gerror = 0; char** const array = g_key_file_get_locale_string_list( const_cast(gobj()), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), locale.c_str(), &length, &gerror); GLIBMM_THROW(gerror); return Glib::ArrayHandle(array, length, Glib::OWNERSHIP_DEEP); } Glib::ArrayHandle KeyFile::get_boolean_list(const Glib::ustring& group_name, const Glib::ustring& key GLIBMM_ERROR_ARG) const { gsize length = 0; GError* gerror = 0; gboolean *const array = g_key_file_get_boolean_list( const_cast(gobj()), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), &length, &gerror); GLIBMM_THROW(gerror); return Glib::ArrayHandle(array, length, Glib::OWNERSHIP_SHALLOW); } Glib::ArrayHandle KeyFile::get_integer_list(const Glib::ustring& group_name, const Glib::ustring& key GLIBMM_ERROR_ARG) const { gsize length = 0; GError* gerror = 0; int *const array = g_key_file_get_integer_list( const_cast(gobj()), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), &length, &gerror); GLIBMM_THROW(gerror); return Glib::ArrayHandle(array, length, Glib::OWNERSHIP_SHALLOW); } Glib::ArrayHandle KeyFile::get_double_list(const Glib::ustring& group_name, const Glib::ustring& key GLIBMM_ERROR_ARG) const { gsize length = 0; GError* gerror = 0; double *const array = g_key_file_get_double_list(const_cast(gobj()), group_name.c_str(), key.c_str(), &length, &gerror); GLIBMM_THROW(gerror); return Glib::ArrayHandle(array, length, Glib::OWNERSHIP_SHALLOW); } void KeyFile::set_string_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle& list) { g_key_file_set_string_list(gobj(), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), list.data(), list.size()); } void KeyFile::set_locale_string_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale, const Glib::ArrayHandle& list) { g_key_file_set_locale_string_list(gobj(), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), locale.c_str(), list.data(), list.size()); } void KeyFile::set_integer_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle& list) { g_key_file_set_integer_list(gobj(), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), const_cast(list.data()), list.size()); } void KeyFile::set_double_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle& list) { g_key_file_set_double_list(gobj(), group_name.c_str(), key.c_str(), const_cast(list.data()), list.size()); } void KeyFile::set_boolean_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle& list) { g_key_file_set_boolean_list(gobj(), (group_name.empty()) ? 0 : group_name.c_str(), key.c_str(), const_cast(list.data()), list.size()); } #ifdef GLIBMM_EXCEPTIONS_ENABLED Glib::ustring KeyFile::get_comment() const #else Glib::ustring KeyFile::get_comment(std::auto_ptr& error) const #endif { GError* gerror = 0; char *const str = g_key_file_get_comment(const_cast(gobj()), 0, 0, &gerror); GLIBMM_THROW(gerror); return Glib::convert_return_gchar_ptr_to_ustring(str); } Glib::ustring KeyFile::get_comment(const Glib::ustring& group_name GLIBMM_ERROR_ARG) const { GError* gerror = 0; char *const str = g_key_file_get_comment(const_cast(gobj()), (group_name.empty()) ? 0 : group_name.c_str(), 0, &gerror); GLIBMM_THROW(gerror); return Glib::convert_return_gchar_ptr_to_ustring(str); } void KeyFile::set_comment(const Glib::ustring& comment GLIBMM_ERROR_ARG) { GError* gerror = 0; g_key_file_set_comment(gobj(), 0, 0, comment.c_str(), &gerror); GLIBMM_THROW(gerror); } void KeyFile::set_comment(const Glib::ustring& group_name, const Glib::ustring& comment GLIBMM_ERROR_ARG) { GError* gerror = 0; g_key_file_set_comment(gobj(), (group_name.empty()) ? 0 : group_name.c_str(), 0, comment.c_str(), &gerror); GLIBMM_THROW(gerror); } } // namespace Glib