Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

component_options.h

Go to the documentation of this file.
00001 /*
00002         ClanGUI, copyrights by various people. Have a look in the CREDITS file.
00003         
00004         This sourcecode is distributed using the Library GNU Public Licence,
00005         version 2 or (at your option) any later version. Please read LICENSE
00006         for details.
00007 */
00008 
00010 
00011 #ifndef header_styleoptions
00012 #define header_styleoptions
00013 
00014 #include "../Core/System/clanstring.h"
00015 #include "../Core/System/error.h"
00016 #include <map>
00017 #include <string>
00018 #include <stdio.h>
00019 
00020 class CL_ComponentOptions
00021 //: Sets the options for components
00022 {
00023 public:
00024         bool exists(const std::string &option_name) const
00025         {
00026                 return options.find(option_name) != options.end();
00027         }
00028 
00029         int count(const std::string &option_name) const
00030         {
00031                 return options.count(option_name);
00032         }
00033 
00034         // Getting the value of a unexisting option is undefined, so use exists 
00035         // first to check if the option exists
00036         const std::string &get_value(const std::string &option_name, int offset=0) const
00037         { 
00038                 std::multimap<std::string, std::string>::const_iterator it = options.find(option_name);
00039                 if (it == options.end())
00040                 {
00041                         char buf[100];
00042                         sprintf(buf, "Missing component option '%s'", option_name.c_str());
00043                         throw CL_Error(buf);
00044                 }
00045                 for (;offset>0;offset--)
00046                 {
00047                         it++;
00048                         if (it == options.end())
00049                         {
00050                                 char buf[100];
00051                                 sprintf(buf, "Missing component option '%s'", option_name.c_str());
00052                                 throw CL_Error(buf);
00053                         }
00054                 }
00055                 return (*it).second; 
00056         }
00057 
00058         int get_value_as_int(const std::string &option_name, int offset=0) const
00059         {
00060                 const std::string &val = get_value(option_name, offset);
00061                 return atoi(val.c_str()); 
00062         }
00063 
00064         bool get_value_as_bool(const std::string &option_name, int offset=0) const
00065         {
00066                 const std::string &val = get_value(option_name, offset);
00067                 CL_String s(val);
00068                 s.to_lower();
00069 
00070                 if(s == "yes" ||
00071                         s == "true" ||
00072                         s == "on" ||
00073                         s == "1")
00074                         return true;
00075                 else if(s == "no" ||
00076                         s == "false" ||
00077                         s == "off" ||
00078                         s == "0")
00079                         return false;
00080 
00081                 char buf[200];
00082                 sprintf(buf, "Invalid bool value '%s' in component option '%s'", 
00083                         val.c_str(),
00084                         option_name.c_str());
00085                 throw CL_Error(buf);
00086         }
00087         
00088         void add_option(const std::string &option_name, const std::string &option_value)
00089         {
00090                 options.insert(std::pair<std::string, std::string>(option_name, option_value));
00091         }
00092         
00093         void add_option_as_int(const std::string &option_name, int option_value)
00094         {
00095                 CL_String value; value << option_value;
00096                 add_option(option_name, value);
00097         }
00098         
00099         void add_option_as_bool(const std::string &option_name, bool option_value)
00100         {
00101                 CL_String value; value << (option_value ? "1" : "0");
00102                 add_option(option_name, value);
00103         }
00104         
00105         void clear()
00106         {
00107                         options.clear();
00108         }
00109 
00110         std::multimap<std::string, std::string> options;
00111 };
00112 
00113 #endif

Generated at Wed Apr 4 19:53:59 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001