glibmm r676 - in trunk: . examples examples/keyfile



Author: murrayc
Date: Mon Jun 23 07:38:19 2008
New Revision: 676
URL: http://svn.gnome.org/viewvc/glibmm?rev=676&view=rev

Log:
2008-06-23  Murray Cumming  <murrayc murrayc com>

* configure.in:
* examples/Makefile.am:
* examples/keyfile/Makefile.am:
* examples/keyfile/example.ini:
* examples/keyfile/main.cc: Added a little KeyFile example. I am 
surprised that we do not seem to have one somewhere already.
I need to move all these glibmm examples into gtkmm-documentation some 
time.

Added:
   trunk/examples/keyfile/
   trunk/examples/keyfile/Makefile.am
      - copied, changed from r675, /trunk/examples/markup/Makefile.am
   trunk/examples/keyfile/example.ini
   trunk/examples/keyfile/main.cc
      - copied, changed from r675, /trunk/examples/options/main.cc
Modified:
   trunk/ChangeLog
   trunk/configure.in
   trunk/examples/Makefile.am

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Mon Jun 23 07:38:19 2008
@@ -308,6 +308,7 @@
 
   examples/Makefile
     examples/compose/Makefile
+    examples/keyfile/Makefile
     examples/markup/Makefile
     examples/options/Makefile
     examples/thread/Makefile

Modified: trunk/examples/Makefile.am
==============================================================================
--- trunk/examples/Makefile.am	(original)
+++ trunk/examples/Makefile.am	Mon Jun 23 07:38:19 2008
@@ -1,4 +1,4 @@
-example_dirs = child_watch compose iochannel_stream markup options regex thread
+example_dirs = child_watch compose iochannel_stream markup options regex thread keyfile
 
 # These use gtkmm stuff:
 # thread

Copied: trunk/examples/keyfile/Makefile.am (from r675, /trunk/examples/markup/Makefile.am)
==============================================================================
--- /trunk/examples/markup/Makefile.am	(original)
+++ trunk/examples/keyfile/Makefile.am	Mon Jun 23 07:38:19 2008
@@ -1,8 +1,7 @@
 include $(top_srcdir)/examples/Makefile.am_fragment
 
-EXTRA_DIST = test.xml
-
 #Build the executable, but don't install it.
-noinst_PROGRAMS = parser
-parser_SOURCES 	= parser.cc
+noinst_PROGRAMS = example
+example_SOURCES = main.cc
 
+EXTRA_DIST = example.ini

Added: trunk/examples/keyfile/example.ini
==============================================================================
--- (empty file)
+++ trunk/examples/keyfile/example.ini	Mon Jun 23 07:38:19 2008
@@ -0,0 +1,19 @@
+# this is just an example
+# there can be comments before the first group
+
+[First Group]
+
+Name=Key File Example\tthis value shows\nescaping
+
+# localized strings are stored in multiple key-value pairs
+Welcome=Hello
+Welcome[de]=Hallo
+Welcome[fr]=Bonjour
+Welcome[it]=Ciao
+
+[Another Group]
+
+Numbers=2;20;-200;0
+
+Booleans=true;false;true;true
+

Copied: trunk/examples/keyfile/main.cc (from r675, /trunk/examples/options/main.cc)
==============================================================================
--- /trunk/examples/options/main.cc	(original)
+++ trunk/examples/keyfile/main.cc	Mon Jun 23 07:38:19 2008
@@ -16,146 +16,82 @@
  */
 
 #include <glibmm.h>
-#include <iomanip>
 #include <iostream>
 
 
-class ExampleOptionGroup : public Glib::OptionGroup
-{ 
-public:
-  ExampleOptionGroup();
-
-  virtual bool on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group);
-  virtual bool on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group);
-  virtual void on_error(Glib::OptionContext& context, Glib::OptionGroup& group);
-  
-  //These int instances should live as long as the OptionGroup to which they are added, 
-  //and as long as the OptionContext to which those OptionGroups are added.
-  int m_arg_foo;
-  std::string m_arg_filename;
-  Glib::ustring m_arg_goo;
-  bool m_arg_boolean;
-  Glib::OptionGroup::vecustrings m_arg_list;
-  Glib::OptionGroup::vecustrings m_remaining_list;
-};
-
-ExampleOptionGroup::ExampleOptionGroup()
-: Glib::OptionGroup("example_group", "description of example group", "help description of example group"),
-  m_arg_foo(0), m_arg_boolean(false)
-{
-  Glib::OptionEntry entry1;
-  entry1.set_long_name("foo");
-  entry1.set_short_name('f');
-  entry1.set_description("The Foo");
-  add_entry(entry1, m_arg_foo);
-      
-  Glib::OptionEntry entry2;
-  entry2.set_long_name("file");
-  entry2.set_short_name('F');
-  entry2.set_description("The Filename");
-  add_entry_filename(entry2, m_arg_filename);
- 
-  Glib::OptionEntry entry3;
-  entry3.set_long_name("goo");
-  entry3.set_short_name('g');
-  entry3.set_description("The Goo");
-  m_arg_goo = "default-goo-value"; //We can choose a default to be used if the user doesn't specify this option.
-  add_entry(entry3, m_arg_goo);
-  
-  Glib::OptionEntry entry4;
-  entry4.set_long_name("activate_something");
-  entry4.set_description("Activate something");
-  add_entry(entry4, m_arg_boolean);
-  
-  Glib::OptionEntry entry5;
-  entry5.set_long_name("list");
-  entry5.set_short_name('l');
-  entry5.set_description("A List");
-  add_entry(entry5, m_arg_list);
-
-  Glib::OptionEntry entry_remaining;
-  entry_remaining.set_long_name(G_OPTION_REMAINING);
-  entry_remaining.set_arg_description(G_OPTION_REMAINING);
-
-  add_entry(entry_remaining, m_remaining_list);
-}
-
-bool ExampleOptionGroup::on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
-{
-  //This is called before the m_arg_* instances are given their values.
-  // You do not need to override this method. This is just here to show you how,
-  // in case you want to do any extra processing.
-  return Glib::OptionGroup::on_pre_parse(context, group);
-}
-
-bool ExampleOptionGroup::on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
-{
-  //This is called after the m_arg_* instances are given their values.
-  // You do not need to override this method. This is just here to show you how,
-  // in case you want to do any extra processing.
-  return Glib::OptionGroup::on_post_parse(context, group);
-}
-
-void ExampleOptionGroup::on_error(Glib::OptionContext& context, Glib::OptionGroup& group)
-{
-  Glib::OptionGroup::on_error(context, group);
-}
-  
-
-
 int main(int argc, char** argv)
 {
-  //This example should be executed like so:
-  //./example --foo=1 --bar=2 --goo=abc
-  //./example --help
+  // This example should be executed like so:
+  // ./example the_ini_file.ini
   
   Glib::init();
    
-  Glib::OptionContext context;
-  
-  ExampleOptionGroup group;
-  context.set_main_group(group);
-  
-  #ifdef GLIBMM_EXCEPTIONS_ENABLED
+  std::string filepath = "./example.ini";
+
+  Glib::KeyFile keyfile;
+
+  // An exception will be thrown if the file is not there, or if the file is incorrectly formatted:
   try
   {
-    context.parse(argc, argv);
+    const bool loaded = keyfile.load_from_file(filepath);
+    if(!loaded)
+      std::cerr << "Could not load keyfile." << std::endl;
   }
-  catch(const Glib::Error& ex)
+  catch(const Glib::FileError& ex)
   {
-    std::cout << "Exception: " << ex.what() << std::endl;
+    std::cerr << "Exception while loading key file: " << ex.what() << std::endl;
+    return -1;
   }
-  #else
-  std::auto_ptr<Glib::Error> ex;
-  context.parse(argc, argv, ex);
-  if(ex.get())
+  catch(const Glib::KeyFileError& ex)
   {
-    std::cout << "Exception: " << ex->what() << std::endl;
+    std::cerr << "Exception while loading key file: " << ex.what() << std::endl;
+    return -1;
   }
-  #endif //GLIBMM_EXCEPTIONS_ENABLED
 
-  std::cout << "parsed values: " << std::endl <<
-    "  foo = " << group.m_arg_foo << std::endl << 
-    "  filename = " << group.m_arg_filename << std::endl <<
-    "  activate_something = " << (group.m_arg_boolean ? "enabled" : "disabled") << std::endl <<
-    "  goo = " << group.m_arg_goo << std::endl;
-    
-  //This one shows the results of multiple instance of the same option, such as --list=1 --list=a --list=b
-  std::cout << "  list = ";
-  for(Glib::OptionGroup::vecustrings::const_iterator iter = group.m_arg_list.begin(); iter != group.m_arg_list.end(); ++iter)
+  // Try to get a value that is not in the file:
+  // An exception will be thrown if the value is not in the file:
+  try
   {
-    std::cout << *iter << ", ";
+    const Glib::ustring value = keyfile.get_value("somegroup", "somekey");
+    std::cout << "somekey value=" << value << std::endl;
+  }
+  catch(const Glib::KeyFileError& ex)
+  {
+    std::cerr << "Exception while getting value: " << ex.what() << std::endl;
+    //return -1;
   }
-  std::cout << std::endl;
 
-  //This one shows the remaining arguments on the command line, which had no name= form:
-  std::cout << "  remaining = ";
-  for(Glib::OptionGroup::vecustrings::const_iterator iter = group.m_remaining_list.begin(); iter != group.m_remaining_list.end(); ++iter)
+  // Try to get a value that is in the file:
+  // An exception will be thrown if the value is not in the file:
+  try
   {
-    std::cout << *iter << ", ";
+    const Glib::ustring value = keyfile.get_value("First Group", "Welcome");
+    std::cout << "Welcome value=" << value << std::endl;
   }
-  std::cout << std::endl;
- 
+  catch(const Glib::KeyFileError& ex)
+  {
+    std::cerr << "Exception while getting value: " << ex.what() << std::endl;
+    //return -1;
+  }
+
+  // Try to get a list of integers that is in the file:
+  // An exception will be thrown if the value is not in the file:
+  try
+  {
+    typedef std::list<int> type_list_integers;
+    const type_list_integers value_list = keyfile.get_integer_list("Another Group", "Numbers");
+    for(type_list_integers::const_iterator iter = value_list.begin(); iter != value_list.end(); ++iter)
+    {
+      const int value = *iter;
+      std::cout << "Number list value: item=" << value << std::endl;
+    }
+  }
+  catch(const Glib::KeyFileError& ex)
+  {
+    std::cerr << "Exception while getting list value: " << ex.what() << std::endl;
+    //return -1;
+  }
+
   return 0;
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]