vrheights

view src/game_var.cc @ 8:3f221bdc9bab

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