File: component_options.h

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (160 lines) | stat: -rw-r--r-- 3,894 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
	$Id: component_options.h,v 1.34 2002/01/06 19:12:28 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	See http://www.clanlib.org
	------------------------------------------------------------------------
*/

//! clanGUI="Framework"
//! header=gui.h

#ifndef header_styleoptions
#define header_styleoptions

#include "../Core/System/clanstring.h"
#include "../Core/System/error.h"
#include <map>
#include <string>
#include <stdio.h>

//: Sets the options for components
class CL_ComponentOptions
{
public:
//! Attributes:
	typedef std::multimap<std::string, std::string> options_t;
	typedef options_t::iterator iterator;
	typedef options_t::const_iterator const_iterator;

	//: Exists
	bool exists(const std::string &option_name) const
	{
		CL_String lc_name(option_name); lc_name.to_lower();
		return options.find(lc_name) != options.end();
	}

	//: Count
	int count(const std::string &option_name) const
	{
		CL_String lc_name(option_name); lc_name.to_lower();
		return options.count(lc_name);
	}

	void remove_option(const std::string &option_name)
	{
		CL_String lc_name(option_name); lc_name.to_lower();
		std::multimap<std::string, std::string>::iterator it = options.find(lc_name);
		while (it != options.end())
		{
			if (it->first != option_name) break;
			std::multimap<std::string, std::string>::iterator it2 = it;
			++it;
			options.erase(it2);
		}
	}

	//: Getting the value of a unexisting option is undefined, so use the 'exists' function
	//: first to check if the option exists
	const std::string &get_value(const std::string &option_name, int offset=0) const
	{ 
		CL_String lc_name(option_name); lc_name.to_lower();
		std::multimap<std::string, std::string>::const_iterator it = options.find(lc_name);
		if (it == options.end())
		{
			char buf[100];
			sprintf(buf, "Missing component option '%s'", option_name.c_str());
			cl_throw_error(buf);
		}
		for (;offset>0;offset--)
		{
			it++;
			if (it == options.end())
			{
				char buf[100];
				sprintf(buf, "Missing component option '%s'", option_name.c_str());
				cl_throw_error(buf);
			}
		}
		return (*it).second; 
	}

	const std::string &operator[](const std::string &option_name) const
	{
		return get_value(option_name, 0);
	}

	//: Get value as int
	int get_value_as_int(const std::string &option_name, int offset=0) const
	{
		const std::string &val = get_value(option_name, offset);
		return atoi(val.c_str()); 
	}

	//: Get value as bool
	bool get_value_as_bool(const std::string &option_name, int offset=0) const
	{
		const std::string &val = get_value(option_name, offset);
		CL_String s(val);
		s.to_lower();

		if(s == "yes" ||
			s == "true" ||
			s == "on" ||
			s == "1")
			return true;
		else if(s == "no" ||
			s == "false" ||
			s == "off" ||
			s == "0")
			return false;

		char buf[200];
		sprintf(
			buf,
			"Invalid bool value '%s' in component option '%s'", 
			val.c_str(),
			option_name.c_str());
		cl_throw_error(buf);
		return false;
	}
	
//! Operations:
	//: Add option
	void add_option(const std::string &option_name, const std::string &option_value)
	{
		CL_String lc_name(option_name); lc_name.to_lower();
		options.insert(options_t::value_type(lc_name, option_value));
	}
	
	//: Add option
	void add_option(const std::string &option_name, int option_value)
	{
		CL_String value; value << option_value;
		add_option(option_name, value);
	}
	
	//: Clear
	void clear()
	{
		options.clear();
	}

	iterator begin() { return options.begin(); }
	iterator end() { return options.end(); }

	const_iterator begin() const { return options.begin(); }
	const_iterator end() const { return options.end(); }

	//: Options
	options_t options;
};

#endif