Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ce31a49770b653174c33b81abb0fbc886297bdef (plain)
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
161
162
163
164
165
166
167
168
169
/*
   core/cvar.cc
   This file is part of the Osirion project and is distributed under 
   the terms of the GNU General Public License version 2 
*/

#include "core/cvar.h"
#include "sys/sys.h"

#include <map>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>


namespace core {

Cvar_t::Cvar_t(unsigned int cvarflags)
{
	cvar_flags = cvarflags;
}

Cvar_t & Cvar_t::operator=(const std::string &other)
{
	cvar_text = other;
	std::stringstream s(cvar_text);
	if (!(s >> cvar_value))
		cvar_value = cvar_text.size();
	return (*this);
}

Cvar_t & Cvar_t::operator=(const char *other)
{
	return ((*this) = std::string(other));
}

Cvar_t & Cvar_t::operator=(int other)
{
	std::stringstream s;
	s << other;
	s >> cvar_text;
	cvar_value = (float) other;
	return (*this);
}

Cvar_t & Cvar_t::operator=(float other)
{
	std::stringstream s;
	s << other;
	s >> cvar_text;
	cvar_value = other;
	return (*this);
}

unsigned int Cvar_t::flags() const 
{ 
	return(cvar_flags);
}

float Cvar_t::value() const 
{ 
	return(cvar_value); 
}

const std::string &Cvar_t::text() const 
{ 
	return(cvar_text); 
}

namespace cvar
{

std::map<std::string, Cvar> registry;

Cvar get(const char *name, const char *value, int flags)
{
	Cvar c = find(name);
	if (c) {
		//con_debug << "cvar::get " << name << " already exist with value " << cvar->text() << std::endl;
	} else {
		//con_debug << "cvar::get " << name << " " << value << std::endl;
		c = new Cvar_t(flags);
		registry[std::string(name)] = c;
		(*c) = value;
	}
	return c;
}

Cvar get(const char *name, float value, int flags)
{
	Cvar c = find(name);
	if (c) {
		//con_debug << "cvar::get " << name << " already exist with value " << cvar->text() << std::endl;
	} else {
		//con_debug << "cvar::get " << name << " " << value << std::endl;
		c = new Cvar_t(flags);
		registry[std::string(name)] = c;
		(*c) = value;
	}
	return c;
}

Cvar set(const char *name, const char *value, int flags)
{
	Cvar c = find(name);
	if (!c) {
		c = new Cvar_t(flags);
		registry[std::string(name)] = c;
	}
	(*c) = value;
	//con_debug << "cvar::set " << name << " " << cvar->text() << std::endl;
	return c;
}

Cvar set(const char *name, float value, int flags)
{
	Cvar c = find(name);
	if (!c) {
		c = new Cvar_t(flags);
		registry[std::string(name)] = c;
	}
	(*c) = value;
	//con_debug << "cvar::set " << name << " " << cvar->text() << std::endl;
	return c;
}

void unset(const std::string &name)
{
	Cvar c = find(name);
	if (c) {
		con_debug << "cvar::unset " << name << std::endl;
		registry.erase(name);
		delete c;
	}
}

void unset(const char *name)
{
	unset(std::string(name));
}

Cvar find(const std::string &name)
{
	std::map<std::string, Cvar>::iterator it = registry.find(name);
	if (it == registry.end())
		return 0;
	else
		return (*it).second;
}

Cvar find(const char *name)
{
	return(find(std::string(name)));
}

void list()
{
	std::map<std::string, Cvar>::iterator it;	
	for (it = registry.begin(); it != registry.end(); it++) {
		con_print << "  "<< (*it).first << " " << (*it).second->text() << std::endl;
	}
	con_print << registry.size() << " registered variables." << std::endl;
}

} // namespace cvar

} // namespace core