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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
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 *Cvar::con_ansi = 0;
Cvar *Cvar::sv_dedicated = 0;
Cvar *Cvar::sv_private = 0;
Cvar *Cvar::sv_framerate = 0;
Cvar *Cvar::sv_name = 0;
Cvar *Cvar::net_host = 0;
Cvar *Cvar::net_port = 0;
Cvar *Cvar::net_maxclients = 0;
Cvar *Cvar::net_timeout = 0;
Cvar *Cvar::net_framerate = 0;
std::map<std::string, Cvar*> Cvar::registry;
Cvar::Cvar(const char *name, unsigned int flags)
{
cvar_flags = flags;
if (name)
cvar_name.assign(name);
else
cvar_name.clear();
cvar_info.clear();
}
void Cvar::set_info(const char *info)
{
if (info)
cvar_info.assign(info);
}
Cvar & Cvar::operator=(const std::string &other)
{
cvar_str = other;
std::stringstream s(cvar_str);
if (!(s >> cvar_value))
cvar_value = cvar_str.size();
return (*this);
}
Cvar & Cvar::operator=(const char *other)
{
return ((*this) = std::string(other));
}
Cvar & Cvar::operator=(float other)
{
std::stringstream s;
s << other;
s >> cvar_str;
cvar_value = other;
return (*this);
}
Cvar* Cvar::get(const char *name, const char *value, unsigned int flags)
{
Cvar *c = find(name);
if (c) {
//con_debug << "get " << name << " already exist with value " << cvar->str() << std::endl;
c->cvar_flags |= flags;
} else {
//con_debug << "get " << name << " " << value << std::endl;
c = new Cvar(name, flags);
registry[std::string(name)] = c;
(*c) = value;
}
c->cvar_flags |= flags;
return c;
}
Cvar* Cvar::get(const char *name, float value, unsigned int flags)
{
Cvar *c = find(name);
if (c) {
//con_debug << "get " << name << " already exist with value " << cvar->str() << std::endl;
} else {
//con_debug << "get " << name << " " << value << std::endl;
c = new Cvar(name, flags);
registry[std::string(name)] = c;
(*c) = value;
}
c->cvar_flags |= flags;
return c;
}
Cvar* Cvar::set(const char *name, const char *value, unsigned int flags)
{
Cvar *c = find(name);
if (!c) {
c = new Cvar(name, flags);
registry[std::string(name)] = c;
}
(*c) = value;
c->cvar_flags = flags;
//con_debug << "set " << name << " " << cvar->str() << std::endl;
return c;
}
Cvar* Cvar::set(const char *name, float value, unsigned int flags)
{
Cvar *c = find(name);
if (!c) {
c = new Cvar(name, flags);
registry[std::string(name)] = c;
}
(*c) = value;
c->cvar_flags = flags;
//con_debug << "set " << name << " " << cvar->str() << std::endl;
return c;
}
void Cvar::unset(std::string const &name)
{
Cvar *c = find(name);
if (c) {
con_debug << "unset " << name << std::endl;
registry.erase(name);
delete c;
}
}
void Cvar::unset(const char *name)
{
unset(std::string(name));
}
Cvar *Cvar::find(std::string const &name)
{
std::map<std::string, Cvar*>::iterator it = registry.find(name);
if (it == registry.end())
return 0;
else
return (*it).second;
}
Cvar *Cvar::find(const char *name)
{
return(find(std::string(name)));
}
void Cvar::list()
{
con_print << "Flags: A=Archive G=Game R=ReadOnly" << std::endl;
std::map<std::string, Cvar*>::iterator it;
for (it = registry.begin(); it != registry.end(); it++) {
std::string typeindicator;
if (((*it).second->flags() & Archive) == Archive)
typeindicator += 'A';
else
typeindicator += ' ';
if (((*it).second->flags() & Game) == Game)
typeindicator += 'G';
else
typeindicator += ' ';
if (((*it).second->flags() & ReadOnly) == ReadOnly)
typeindicator += 'R';
else
typeindicator += ' ';
con_print << " " << typeindicator <<
" " << (*it).first << " " << (*it).second->str() << " ^N" << (*it).second->info() << std::endl;
}
con_print << registry.size() << " registered variables" << std::endl;
}
}
|