vrheights
view src/game_var.cc @ 15:ffb62c8db542
added missing makefile
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 30 Oct 2015 05:40:22 +0200 |
parents | 537db3079134 |
children |
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 int 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 GameVariable::NUMBER;
95 }
97 if(strccmp(valstr, "true") == 0 || strccmp(valstr, "yes") == 0) {
98 set_gvar_bool(name, true);
99 return GameVariable::BOOL;
100 }
101 if(strccmp(valstr, "false") == 0 || strccmp(valstr, "no") == 0) {
102 set_gvar_bool(name, false);
103 return GameVariable::BOOL;
104 }
105 set_gvar_str(name, val);
106 return GameVariable::STR;
107 }
109 GameVariable &get_gvar(const char *name)
110 {
111 return gvars[name];
112 }
114 const char *get_gvar_str(const char *name)
115 {
116 return gvars[name].val.c_str();
117 }
119 float get_gvar_num(const char *name)
120 {
121 return gvars[name].num_val;
122 }
124 bool get_gvar_bool(const char *name)
125 {
126 return gvars[name].bool_val;
127 }
129 bool have_gvar(const char *name)
130 {
131 return gvars.find(std::string(name)) != gvars.end();
132 }
134 std::list<std::string> get_gvar_list()
135 {
136 std::list<std::string> res;
138 std::map<std::string, GameVariable>::const_iterator it = gvars.begin();
139 while(it != gvars.end()) {
140 res.push_back((it++)->first);
141 }
142 return res;
143 }