-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cpp
More file actions
89 lines (69 loc) · 2 KB
/
Config.cpp
File metadata and controls
89 lines (69 loc) · 2 KB
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
#include "Config.h"
#include "Parsing.h"
#include <fstream>
#include <algorithm>
void Config::set_defaults()
{
vars["particle-ratio"] = "200";
vars["particle-size"] = "1";
vars["particle-opacity"] = "30";
vars["particle-behaviour"] = "orbit";
vars["prediction-length"] = "0";
vars["prediction-precision"] = "10";
vars["gravity-line"] = "0";
vars["velocity-arrow"] = "0";
vars["acceleration-arrow"] = "0";
vars["motion-blur"] = "0";
vars["nHighScores"] = "10";
vars["fps"] = "0";
// Values out of 128 for SDL_mixer.
vars["music-volume"] = "100";
vars["sfx-volume"] = "128";
}
Config::Config()
{
set_defaults();
}
Config::Config( const std::string& filename )
{
reload( filename );
}
bool Config::reload( const std::string& filename )
{
// To ensure nothing is left uninitialized.
set_defaults();
std::ifstream cfg( filename.c_str() );
std::string line;
while( std::getline( cfg, line ) )
{
rm_comments( &line );
rm_whitespace( &line );
if( line.size() <= 1 )
continue;
// Check for errors in var after checking it against the possible
// choices. That way, bad expression and unknown handle errors
// can be handled together.
Variable var = evaluate_expression( line );
if( vars.find(var.handle) != vars.end() ) {
vars[ var.handle ] = var.value;
} else {
static std::ofstream out( "config.error" );
out << "Unknown valName (" << var.handle << "}\n";
out << "Line: \"" << line << "\"\n\n";
}
}
return cfg;
}
std::string& Config::operator [] ( const std::string& handle )
{
return vars[ handle ];
}
const std::string& Config::operator [] ( const std::string& handle ) const
{
static const std::string ZILCH = "";
Vars::const_iterator it = vars.find( handle );
if( it != vars.end() )
return it->second;
else
return ZILCH;
}