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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/*
Copyright (c) 2008 Robin Vobruba <hoijui.quaero@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _OPTION_CPP
#define _OPTION_CPP
#include "Option.h"
#include "System/Util.h"
#include "System/Exceptions.h"
#include "Lua/LuaParser.h"
#include "Map/MapParser.h"
#include <assert.h>
static const char* Option_badKeyChars = " =;\r\n\t";
std::string option_getDefString(const Option& option) {
std::string def = "";
switch (option.typeCode) {
case opt_bool: {
def = option.boolDef ? "true" : "false";
break;
} case opt_list: {
def = option.listDef;
break;
} case opt_number: {
static const size_t fltString_sizeMax = 32;
char fltString[fltString_sizeMax];
SNPRINTF(fltString, fltString_sizeMax, "%f", option.numberDef);
def += fltString;
break;
} case opt_string: {
def = option.stringDef;
break;
} case opt_error: {
} case opt_section: {
} default: {
break;
}
}
return def;
}
static bool parseOption(const LuaTable& root, int index, Option& opt,
std::set<string>& optionsSet, CLogSubsystem& logSubsystem) {
const LuaTable& optTbl = root.SubTable(index);
if (!optTbl.IsValid()) {
logOutput.Print(logSubsystem,
"parseOption: subtable %d invalid", index);
return false;
}
// common options properties
opt.key = optTbl.GetString("key", "");
if (opt.key.empty()
|| (opt.key.find_first_of(Option_badKeyChars) != string::npos)) {
logOutput.Print(logSubsystem,
"parseOption: empty key or key contains bad characters");
return false;
}
opt.key = StringToLower(opt.key);
opt.scope = optTbl.GetString("scope", "scope");
if (opt.key.empty()
|| (opt.key.find_first_of(Option_badKeyChars) != string::npos)) {
logOutput.Print(logSubsystem,
"parseOption: empty key or key contains bad characters");
return false;
}
opt.scope = StringToLower(opt.scope);
if (optionsSet.find(opt.key) != optionsSet.end()) {
logOutput.Print(logSubsystem, "parseOption: key %s exists already",
opt.key.c_str());
return false;
}
opt.name = optTbl.GetString("name", opt.key);
if (opt.name.empty()) {
logOutput.Print(logSubsystem, "parseOption: %s: empty name",
opt.key.c_str());
return false;
}
opt.desc = optTbl.GetString("desc", opt.name);
opt.section = optTbl.GetString("section", "");
opt.style = optTbl.GetString("style", "");
opt.type = optTbl.GetString("type", "");
opt.type = StringToLower(opt.type);
// option type specific properties
if (opt.type == "bool") {
opt.typeCode = opt_bool;
opt.boolDef = optTbl.GetBool("def", false);
}
else if (opt.type == "number") {
opt.typeCode = opt_number;
opt.numberDef = optTbl.GetFloat("def", 0.0f);
opt.numberMin = optTbl.GetFloat("min", -1.0e30f);
opt.numberMax = optTbl.GetFloat("max", +1.0e30f);
opt.numberStep = optTbl.GetFloat("step", 0.0f);
}
else if (opt.type == "string") {
opt.typeCode = opt_string;
opt.stringDef = optTbl.GetString("def", "");
opt.stringMaxLen = optTbl.GetInt("maxlen", 0);
}
else if (opt.type == "list") {
opt.typeCode = opt_list;
const LuaTable& listTbl = optTbl.SubTable("items");
if (!listTbl.IsValid()) {
logOutput.Print(logSubsystem, "parseOption: %s: subtable items invalid", opt.key.c_str());
return false;
}
for (int i = 1; listTbl.KeyExists(i); i++) {
OptionListItem item;
// string format
item.key = listTbl.GetString(i, "");
if (!item.key.empty() &&
(item.key.find_first_of(Option_badKeyChars) == string::npos)) {
item.name = item.key;
item.desc = item.name;
opt.list.push_back(item);
continue;
}
// table format (name & desc)
const LuaTable& itemTbl = listTbl.SubTable(i);
if (!itemTbl.IsValid()) {
logOutput.Print(logSubsystem,
"parseOption: %s: subtable %d of subtable items invalid",
opt.key.c_str(), i);
break;
}
item.key = itemTbl.GetString("key", "");
if (item.key.empty() || (item.key.find_first_of(Option_badKeyChars) != string::npos)) {
logOutput.Print(logSubsystem,
"parseOption: %s: empty key or key contains bad characters",
opt.key.c_str());
return false;
}
item.key = StringToLower(item.key);
item.name = itemTbl.GetString("name", item.key);
if (item.name.empty()) {
logOutput.Print(logSubsystem, "parseOption: %s: empty name",
opt.key.c_str());
return false;
}
item.desc = itemTbl.GetString("desc", item.name);
opt.list.push_back(item);
}
if (opt.list.size() <= 0) {
logOutput.Print(logSubsystem, "parseOption: %s: empty list",
opt.key.c_str());
return false; // no empty lists
}
opt.listDef = optTbl.GetString("def", opt.list[0].name);
}
else if (opt.type == "section") {
opt.typeCode = opt_section;
}
else {
logOutput.Print(logSubsystem, "parseOption: %s: unknown type %s",
opt.key.c_str(), opt.type.c_str());
return false; // unknown type
}
optionsSet.insert(opt.key);
return true;
}
void parseOptions(
std::vector<Option>& options,
const std::string& fileName,
const std::string& fileModes,
const std::string& accessModes,
const std::string& mapName,
std::set<std::string>* optionsSet,
CLogSubsystem* logSubsystem) {
if (!logSubsystem) {
assert(logSubsystem);
}
LuaParser luaParser(fileName, fileModes, accessModes);
const string configName = MapParser::GetMapConfigName(mapName);
if (!mapName.empty() && !configName.empty()) {
luaParser.GetTable("Map");
luaParser.AddString("fileName", mapName);
luaParser.AddString("fullName", "maps/" + mapName);
luaParser.AddString("configFile", configName);
luaParser.EndTable();
}
if (!luaParser.Execute()) {
throw content_error("luaParser.Execute() failed: "
+ luaParser.GetErrorLog());
}
const LuaTable root = luaParser.GetRoot();
if (!root.IsValid()) {
throw content_error("root table invalid");
}
std::set<std::string>* myOptionsSet = NULL;
if (optionsSet == NULL) {
myOptionsSet = new std::set<std::string>();
} else {
myOptionsSet = optionsSet;
}
for (int index = 1; root.KeyExists(index); index++) {
Option opt;
if (parseOption(root, index, opt, *myOptionsSet, *logSubsystem)) {
options.push_back(opt);
}
}
if (optionsSet == NULL) {
delete myOptionsSet;
myOptionsSet = NULL;
}
}
std::vector<Option> parseOptions(
const std::string& fileName,
const std::string& fileModes,
const std::string& accessModes,
const std::string& mapName,
std::set<std::string>* optionsSet,
CLogSubsystem* logSubsystem) {
std::vector<Option> options;
parseOptions(options, fileName, fileModes, accessModes, mapName, optionsSet,
logSubsystem);
return options;
}
#endif // _OPTION_CPP
|