-
Notifications
You must be signed in to change notification settings - Fork 2
/
Settings.cpp
127 lines (113 loc) · 3.19 KB
/
Settings.cpp
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
// CmdLineParser.cpp: implementation of the CSettingsParser class.
//
//////////////////////////////////////////////////////////////////////
#include "engine.h"
#include "settings.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSettingsManager::CSettingsManager()
{
settingMap.clear();
CreateStandardSettings();
}
CSettingsManager::~CSettingsManager()
{
DestroyStandardSettings();
}
void CSettingsManager::RegisterVariable(std::string name, CMMPointer<BaseDator> var)
{
settingMap[name]=var;
}
void CSettingsManager::SetVariable(std::string &name, std::string &value, int bias)
{
if(!settingMap[name])return; //setting doesn't exist
if(settingMap[name]->hasMultipleValues())
{
std::list<std::string> valueList;
valueList.clear();
//check for semicolon-seperated values
if(value.find(';')!=-1)
{
//split the string into semicolor-seperated chunks
int first=0, last;
while((last=value.find(';',first))!=-1)
{
valueList.push_back(value.substr(first,last-first));
first=last+1;
}
valueList.push_back(value.substr(first));
}else{
valueList.push_back(value);
}
for(std::list<std::string>::iterator it=valueList.begin(); it!=valueList.end(); it++)
{
if(bias>0)
{
(*settingMap[name])+=(*it);
}else if(bias<0)
{
(*settingMap[name])-=(*it);
}else{
(*settingMap[name])=(*it);
}
}
}else{
//just assign the value
(*settingMap[name])=value;
}
}
void CSettingsManager::ParseSetting(std::string str)
{
int bias=0; std::string name, value;
//test for bias
if((str[0]=='+')||(str[0]=='-'))
{
bias=((str[0]=='+')*2)-1; //+ maps to 1*2-1=1, - maps to 0*2-1=-1
str=str.substr(1); //remove the first character from the string
}
//test for '='
int eqPos=str.find('=');
if(eqPos!=-1)
{
//there's an = sign in there
//so split either side of it
name=str.substr(0,eqPos);
value=str.substr(eqPos+1);
}else{
//there's no equal sign
//we use the bias to construct a boolean value
//so that flags can be +flag (mapping to flag=1) or -flag (mapping to flag=0)
name=str;
char szBuf[5];
sprintf(szBuf,"%i",(bias+1)/2);
value=szBuf;
}
//set the variable
SetVariable(name,value,bias);
}
void CSettingsManager::ParseFile(std::string filename)
{
std::ifstream in(filename.c_str());
if(!in.is_open())return; //couldn't open
while(!in.eof())
{
char szBuf[1024];
in.getline(szBuf,1024);
ParseSetting(szBuf);
}
}
#define SETTING(type, target, var, name) target=new Dator<type>(var); RegisterVariable(std::string(name),CMMPointer<BaseDator>(target));
#define LIST(type, target, var, name) target=new ListDator<type>(var); RegisterVariable(std::string(name),CMMPointer<BaseDator>(target));
void CSettingsManager::CreateStandardSettings()
{
SETTING(int, CVideoUpdate::screenWidth, CVideoUpdate::scrWidth, "screenX" );
SETTING(int, CVideoUpdate::screenHeight, CVideoUpdate::scrHeight, "screenY" );
SETTING(int, CVideoUpdate::screenBPP, CVideoUpdate::scrBPP, "screenBPP" );
}
void CSettingsManager::DestroyStandardSettings()
{
CVideoUpdate::screenWidth = 0;
CVideoUpdate::screenHeight = 0;
CVideoUpdate::screenBPP = 0;
}