rev |
line source |
nuclear@8
|
1 #include <string.h>
|
nuclear@7
|
2 #include <map>
|
nuclear@7
|
3 #include "game_var.h"
|
nuclear@7
|
4
|
nuclear@7
|
5 static std::map<std::string, GameVariable> gvars;
|
nuclear@7
|
6
|
nuclear@7
|
7 GameVariable::GameVariable()
|
nuclear@7
|
8 {
|
nuclear@7
|
9 num_val = 0.0f;
|
nuclear@7
|
10 bool_val = false;
|
nuclear@7
|
11 }
|
nuclear@7
|
12
|
nuclear@7
|
13 std::string GameVariable::to_str() const
|
nuclear@7
|
14 {
|
nuclear@7
|
15 char buf[128];
|
nuclear@7
|
16
|
nuclear@7
|
17 switch(type) {
|
nuclear@7
|
18 case GameVariable::STR:
|
nuclear@7
|
19 return val;
|
nuclear@7
|
20 case GameVariable::BOOL:
|
nuclear@7
|
21 return bool_val ? "true" : "false";
|
nuclear@7
|
22 case GameVariable::NUMBER:
|
nuclear@7
|
23 sprintf(buf, "%g", num_val);
|
nuclear@7
|
24 return buf;
|
nuclear@7
|
25 }
|
nuclear@7
|
26 return "<unknown>";
|
nuclear@7
|
27 }
|
nuclear@7
|
28
|
nuclear@7
|
29 void set_gvar(const GameVariable &var)
|
nuclear@7
|
30 {
|
nuclear@7
|
31 gvars[var.name] = var;
|
nuclear@7
|
32 }
|
nuclear@7
|
33
|
nuclear@7
|
34 void set_gvar_str(const char *name, const char *val)
|
nuclear@7
|
35 {
|
nuclear@7
|
36 GameVariable v;
|
nuclear@7
|
37 v.name = name;
|
nuclear@7
|
38 v.val = val;
|
nuclear@7
|
39 v.type = GameVariable::STR;
|
nuclear@7
|
40 set_gvar(v);
|
nuclear@7
|
41 }
|
nuclear@7
|
42
|
nuclear@7
|
43 void set_gvar_num(const char *name, float val)
|
nuclear@7
|
44 {
|
nuclear@7
|
45 GameVariable v;
|
nuclear@7
|
46 v.name = name;
|
nuclear@7
|
47 v.num_val = val;
|
nuclear@7
|
48 v.type = GameVariable::NUMBER;
|
nuclear@7
|
49 set_gvar(v);
|
nuclear@7
|
50 }
|
nuclear@7
|
51
|
nuclear@7
|
52 void set_gvar_bool(const char *name, bool val)
|
nuclear@7
|
53 {
|
nuclear@7
|
54 GameVariable v;
|
nuclear@7
|
55 v.name = name;
|
nuclear@7
|
56 v.bool_val = val;
|
nuclear@7
|
57 v.type = GameVariable::BOOL;
|
nuclear@7
|
58 set_gvar(v);
|
nuclear@7
|
59 }
|
nuclear@7
|
60
|
nuclear@7
|
61 static char *strip_whitespace(char *s)
|
nuclear@7
|
62 {
|
nuclear@7
|
63 while(*s && isspace(*s)) ++s;
|
nuclear@7
|
64 if(*s == 0) return s;
|
nuclear@7
|
65
|
nuclear@7
|
66 char *endp = s + strlen(s) - 1;
|
nuclear@7
|
67 while(endp > s && isspace(*endp)) {
|
nuclear@7
|
68 *endp-- = 0;
|
nuclear@7
|
69 }
|
nuclear@7
|
70 return s;
|
nuclear@7
|
71 }
|
nuclear@7
|
72
|
nuclear@7
|
73 static int strccmp(const char *a, const char *b)
|
nuclear@7
|
74 {
|
nuclear@7
|
75 while(*a && *b) {
|
nuclear@7
|
76 int diff = tolower(*a) - tolower(*b);
|
nuclear@7
|
77 if(diff != 0) return diff;
|
nuclear@7
|
78 ++a;
|
nuclear@7
|
79 ++b;
|
nuclear@7
|
80 }
|
nuclear@7
|
81 if(*a == 0 && *b == 0) return 0;
|
nuclear@7
|
82 return *a == 0 ? -1 : 1;
|
nuclear@7
|
83 }
|
nuclear@7
|
84
|
nuclear@11
|
85 int set_gvar_parse(const char *name, const char *val)
|
nuclear@7
|
86 {
|
nuclear@7
|
87 char *endp, *valstr = (char*)alloca(strlen(val) + 1);
|
nuclear@7
|
88 strcpy(valstr, val);
|
nuclear@7
|
89 valstr = strip_whitespace(valstr);
|
nuclear@7
|
90
|
nuclear@7
|
91 float fval = strtod(valstr, &endp);
|
nuclear@7
|
92 if(endp != valstr && *endp == 0) {
|
nuclear@7
|
93 set_gvar_num(name, fval);
|
nuclear@11
|
94 return GameVariable::NUMBER;
|
nuclear@7
|
95 }
|
nuclear@7
|
96
|
nuclear@7
|
97 if(strccmp(valstr, "true") == 0 || strccmp(valstr, "yes") == 0) {
|
nuclear@7
|
98 set_gvar_bool(name, true);
|
nuclear@11
|
99 return GameVariable::BOOL;
|
nuclear@7
|
100 }
|
nuclear@7
|
101 if(strccmp(valstr, "false") == 0 || strccmp(valstr, "no") == 0) {
|
nuclear@7
|
102 set_gvar_bool(name, false);
|
nuclear@11
|
103 return GameVariable::BOOL;
|
nuclear@7
|
104 }
|
nuclear@11
|
105 set_gvar_str(name, val);
|
nuclear@11
|
106 return GameVariable::STR;
|
nuclear@7
|
107 }
|
nuclear@7
|
108
|
nuclear@7
|
109 GameVariable &get_gvar(const char *name)
|
nuclear@7
|
110 {
|
nuclear@7
|
111 return gvars[name];
|
nuclear@7
|
112 }
|
nuclear@7
|
113
|
nuclear@7
|
114 const char *get_gvar_str(const char *name)
|
nuclear@7
|
115 {
|
nuclear@7
|
116 return gvars[name].val.c_str();
|
nuclear@7
|
117 }
|
nuclear@7
|
118
|
nuclear@7
|
119 float get_gvar_num(const char *name)
|
nuclear@7
|
120 {
|
nuclear@7
|
121 return gvars[name].num_val;
|
nuclear@7
|
122 }
|
nuclear@7
|
123
|
nuclear@7
|
124 bool get_gvar_bool(const char *name)
|
nuclear@7
|
125 {
|
nuclear@7
|
126 return gvars[name].bool_val;
|
nuclear@7
|
127 }
|
nuclear@7
|
128
|
nuclear@7
|
129 bool have_gvar(const char *name)
|
nuclear@7
|
130 {
|
nuclear@7
|
131 return gvars.find(std::string(name)) != gvars.end();
|
nuclear@7
|
132 }
|
nuclear@7
|
133
|
nuclear@7
|
134 std::list<std::string> get_gvar_list()
|
nuclear@7
|
135 {
|
nuclear@7
|
136 std::list<std::string> res;
|
nuclear@7
|
137
|
nuclear@7
|
138 std::map<std::string, GameVariable>::const_iterator it = gvars.begin();
|
nuclear@7
|
139 while(it != gvars.end()) {
|
nuclear@7
|
140 res.push_back((it++)->first);
|
nuclear@7
|
141 }
|
nuclear@7
|
142 return res;
|
nuclear@8
|
143 }
|