* Fix another filename casing error.
[matthijs/ABM2.git] / ABM2 / Engine / Settings.cpp
1 // CmdLineParser.cpp: implementation of the CSettingsParser class.\r
2 //\r
3 //////////////////////////////////////////////////////////////////////\r
4 \r
5 #include "engine.h"\r
6 #include "Settings.h"\r
7 \r
8 //////////////////////////////////////////////////////////////////////\r
9 // Construction/Destruction\r
10 //////////////////////////////////////////////////////////////////////\r
11 \r
12 CSettingsManager::CSettingsManager()\r
13 {\r
14         settingMap.clear();\r
15         CreateStandardSettings();\r
16 }\r
17 \r
18 CSettingsManager::~CSettingsManager()\r
19 {\r
20         DestroyStandardSettings();\r
21 }\r
22 \r
23 void CSettingsManager::RegisterVariable(std::string &name, CMMPointer<BaseDator> &var)\r
24 {\r
25         settingMap[name]=var;\r
26 }\r
27 \r
28 void CSettingsManager::SetVariable(std::string &name, std::string &value, int bias)\r
29 {\r
30         if(!settingMap[name])return; //setting doesn't exist\r
31         if(settingMap[name]->hasMultipleValues())\r
32         {\r
33                 std::list<std::string> valueList;\r
34                 valueList.clear();\r
35 \r
36                 //check for semicolon-seperated values\r
37                 if(value.find(';')!=-1)\r
38                 {\r
39                         //split the string into semicolor-seperated chunks\r
40                         int first=0, last;\r
41                         while((last=value.find(';',first))!=-1)\r
42                         {\r
43                                 valueList.push_back(value.substr(first,last-first));\r
44                                 first=last+1;\r
45                         }\r
46                         valueList.push_back(value.substr(first));\r
47                 }else{\r
48                         valueList.push_back(value);\r
49                 }\r
50 \r
51                 for(std::list<std::string>::iterator it=valueList.begin(); it!=valueList.end(); it++)\r
52                 {\r
53                         if(bias>0)\r
54                         {\r
55                                 (*settingMap[name])+=(*it);\r
56                         }else if(bias<0)\r
57                         {\r
58                                 (*settingMap[name])-=(*it);\r
59                         }else{\r
60                                 (*settingMap[name])=(*it);\r
61                         }\r
62                 }\r
63         }else{\r
64                 //just assign the value\r
65                 (*settingMap[name])=value;\r
66         }\r
67 }\r
68 \r
69 void CSettingsManager::ParseSetting(std::string str)\r
70 {\r
71         int bias=0; std::string name, value;\r
72         //test for bias\r
73         if((str[0]=='+')||(str[0]=='-'))\r
74         {\r
75                 bias=((str[0]=='+')*2)-1; //+ maps to 1*2-1=1, - maps to 0*2-1=-1\r
76                 str=str.substr(1); //remove the first character from the string\r
77         }\r
78         //test for '='\r
79         int eqPos=str.find('=');\r
80         if(eqPos!=-1)\r
81         {\r
82                 //there's an = sign in there\r
83                 //so split either side of it\r
84                 name=str.substr(0,eqPos);\r
85                 value=str.substr(eqPos+1);\r
86         }else{\r
87                 //there's no equal sign\r
88                 //we use the bias to construct a boolean value\r
89                 //so that flags can be +flag (mapping to flag=1) or -flag (mapping to flag=0)\r
90                 name=str;\r
91                 char szBuf[5];\r
92                 sprintf_s(szBuf,5,"%i",(bias+1)/2);\r
93                 value=szBuf;\r
94         }\r
95         //set the variable\r
96         SetVariable(name,value,bias);\r
97 }\r
98 \r
99 void CSettingsManager::ParseFile(std::string filename)\r
100 {\r
101         std::ifstream in(filename.c_str());\r
102         if(!in.is_open())return; //couldn't open\r
103         while(!in.eof())\r
104         {\r
105                 char szBuf[1024];\r
106                 in.getline(szBuf,1024);\r
107                 ParseSetting(szBuf);\r
108         }\r
109 }\r
110 \r
111 #define SETTING(type, target, var, name) target=new Dator<type>(var); RegisterVariable(std::string(name),CMMPointer<BaseDator>(target));\r
112 #define LIST(type, target, var, name) target=new ListDator<type>(var); RegisterVariable(std::string(name),CMMPointer<BaseDator>(target));\r
113 \r
114 void CSettingsManager::CreateStandardSettings()\r
115 {\r
116         SETTING(int,    CVideoUpdate::screenWidth,              CVideoUpdate::scrWidth,         "screenX"       );\r
117         SETTING(int,    CVideoUpdate::screenHeight,             CVideoUpdate::scrHeight,        "screenY"       );\r
118         SETTING(int,    CVideoUpdate::screenBPP,                CVideoUpdate::scrBPP,           "screenBPP"     );\r
119 }\r
120 \r
121 void CSettingsManager::DestroyStandardSettings()\r
122 {\r
123         CVideoUpdate::screenWidth       = 0;\r
124         CVideoUpdate::screenHeight      = 0;\r
125         CVideoUpdate::screenBPP         = 0;\r
126 \r
127 }\r