| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: setup.cpp,v 1.56 2009-05-27 09:15:42 qbix79 Exp $ */ |
| 20 | |
| 21 | #include "dosbox.h" |
| 22 | #include "cross.h" |
| 23 | #include "setup.h" |
| 24 | #include "control.h" |
| 25 | #include "support.h" |
| 26 | #include <fstream> |
| 27 | #include <string> |
| 28 | #include <sstream> |
| 29 | #include <list> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | using namespace std; |
| 34 | static std::string current_config_dir; // Set by parseconfigfile so Prop_path can use it to construct the realpath |
| 35 | void Value::destroy() throw(){ |
| 36 | if (type == V_STRING) delete _string; |
| 37 | } |
| 38 | |
| 39 | Value& Value::copy(Value const& in) { |
| 40 | if (this != &in) { //Selfassigment! |
| 41 | if(type != V_NONE && type != in.type) throw WrongType(); |
| 42 | destroy(); |
| 43 | plaincopy(in); |
| 44 | } |
| 45 | return *this; |
| 46 | } |
| 47 | |
| 48 | void Value::plaincopy(Value const& in) throw(){ |
| 49 | type = in.type; |
| 50 | _int = in._int; |
| 51 | _double = in._double; |
| 52 | _bool = in._bool; |
| 53 | _hex = in._hex; |
| 54 | if(type == V_STRING) _string = new string(*in._string); |
| 55 | } |
| 56 | |
| 57 | Value::operator bool () const { |
| 58 | if(type != V_BOOL) throw WrongType(); |
| 59 | return _bool; |
| 60 | } |
| 61 | |
| 62 | Value::operator Hex () const { |
| 63 | if(type != V_HEX) throw WrongType(); |
| 64 | return _hex; |
| 65 | } |
| 66 | |
| 67 | Value::operator int () const { |
| 68 | if(type != V_INT) throw WrongType(); |
| 69 | return _int; |
| 70 | } |
| 71 | |
| 72 | Value::operator double () const { |
| 73 | if(type != V_DOUBLE) throw WrongType(); |
| 74 | return _double; |
| 75 | } |
| 76 | |
| 77 | Value::operator char const* () const { |
| 78 | if(type != V_STRING) throw WrongType(); |
| 79 | return _string->c_str(); |
| 80 | } |
| 81 | |
| 82 | bool Value::operator==(Value const& other) { |
| 83 | if(this == &other) return true; |
| 84 | if(type != other.type) return false; |
| 85 | switch(type){ |
| 86 | case V_BOOL: |
| 87 | if(_bool == other._bool) return true; |
| 88 | break; |
| 89 | case V_INT: |
| 90 | if(_int == other._int) return true; |
| 91 | break; |
| 92 | case V_HEX: |
| 93 | if(_hex == other._hex) return true; |
| 94 | break; |
| 95 | case V_DOUBLE: |
| 96 | if(_double == other._double) return true; |
| 97 | break; |
| 98 | case V_STRING: |
| 99 | if((*_string) == (*other._string)) return true; |
| 100 | break; |
| 101 | default: |
| 102 | E_Exit("comparing stuff that doesn't make sense"); |
| 103 | break; |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | void Value::SetValue(string const& in,Etype _type) { |
| 108 | /* Throw exception if the current type isn't the wanted type |
| 109 | * Unless the wanted type is current. |
| 110 | */ |
| 111 | if(_type == V_CURRENT && type == V_NONE) throw WrongType(); |
| 112 | if(_type != V_CURRENT) { |
| 113 | if(type != V_NONE && type != _type) throw WrongType(); |
| 114 | type = _type; |
| 115 | } |
| 116 | switch(type){ |
| 117 | case V_HEX: |
| 118 | set_hex(in); |
| 119 | break; |
| 120 | case V_INT: |
| 121 | set_int(in); |
| 122 | break; |
| 123 | case V_BOOL: |
| 124 | set_bool(in); |
| 125 | break; |
| 126 | case V_STRING: |
| 127 | set_string(in); |
| 128 | break; |
| 129 | case V_DOUBLE: |
| 130 | set_double(in); |
| 131 | break; |
| 132 | |
| 133 | case V_NONE: |
| 134 | case V_CURRENT: |
| 135 | default: |
| 136 | /* Shouldn't happen!/Unhandled */ |
| 137 | throw WrongType(); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void Value::set_hex(std::string const& in) { |
| 143 | istringstream input(in); |
| 144 | input.flags(ios::hex); |
| 145 | int result = 0; |
| 146 | input >> result; |
| 147 | _hex = result; |
| 148 | } |
| 149 | |
| 150 | void Value::set_int(string const &in) { |
| 151 | istringstream input(in); |
| 152 | int result = 0; |
| 153 | input >> result; |
| 154 | _int = result; |
| 155 | } |
| 156 | void Value::set_double(string const &in) { |
| 157 | istringstream input(in); |
| 158 | double result = 0; |
| 159 | input >> result; |
| 160 | _double = result; |
| 161 | } |
| 162 | |
| 163 | void Value::set_bool(string const &in) { |
| 164 | istringstream input(in); |
| 165 | string result; |
| 166 | input >> result; |
| 167 | _bool = true; |
| 168 | lowcase(result); |
| 169 | /* valid false entries: 0 ,d*, f*, off everything else gets true */ |
| 170 | if( !result.size() ) return; |
| 171 | if(result[0] == '0' || result[0] == 'd' || result[0] == 'f' || result == "off") |
| 172 | _bool = false; |
| 173 | } |
| 174 | |
| 175 | void Value::set_string(string const & in) { |
| 176 | if(!_string) _string = new string(); |
| 177 | _string->assign(in); |
| 178 | } |
| 179 | |
| 180 | string Value::ToString() const { |
| 181 | ostringstream oss; |
| 182 | switch(type) { |
| 183 | case V_HEX: |
| 184 | oss.flags(ios::hex); |
| 185 | oss << _hex; |
| 186 | break; |
| 187 | case V_INT: |
| 188 | oss << _int; |
| 189 | break; |
| 190 | case V_BOOL: |
| 191 | oss << boolalpha << _bool; |
| 192 | break; |
| 193 | case V_STRING: |
| 194 | oss << *_string; |
| 195 | break; |
| 196 | case V_DOUBLE: |
| 197 | oss.precision(2); |
| 198 | oss << fixed << _double; |
| 199 | break; |
| 200 | case V_NONE: |
| 201 | case V_CURRENT: |
| 202 | default: |
| 203 | E_Exit("ToString messed up ?"); |
| 204 | break; |
| 205 | } |
| 206 | return oss.str(); |
| 207 | } |
| 208 | |
| 209 | bool Property::CheckValue(Value const& in, bool warn){ |
| 210 | if(suggested_values.empty()) return true; |
| 211 | for(iter it = suggested_values.begin();it != suggested_values.end();it++) { |
| 212 | if ( (*it) == in) { //Match! |
| 213 | return true; |
| 214 | } |
| 215 | } |
| 216 | if(warn) LOG_MSG("\"%s\" is not a valid value for variable: %s.\nIt might now be reset to the default value: %s",in.ToString().c_str(),propname.c_str(),default_value.ToString().c_str()); |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | void Property::Set_help(string const& in) { |
| 221 | string result = string("CONFIG_") + propname; |
| 222 | upcase(result); |
| 223 | MSG_Add(result.c_str(),in.c_str()); |
| 224 | } |
| 225 | |
| 226 | char const* Property::Get_help() { |
| 227 | string result = string("CONFIG_") + propname; |
| 228 | upcase(result); |
| 229 | return MSG_Get(result.c_str()); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | bool Prop_int::CheckValue(Value const& in, bool warn) { |
| 234 | if(suggested_values.empty() && Property::CheckValue(in,warn)) return true; |
| 235 | //No >= and <= in Value type and == is ambigious |
| 236 | int mi = min; |
| 237 | int ma = max; |
| 238 | int va = static_cast<int>(Value(in)); |
| 239 | if(mi == -1 && ma == -1) return true; |
| 240 | if (va >= mi && va <= ma) return true; |
| 241 | if(warn) LOG_MSG("%s lies outside the range %s-%s for variable: %s.\nIt might now be reset to the default value: %s",in.ToString().c_str(),min.ToString().c_str(),max.ToString().c_str(),propname.c_str(),default_value.ToString().c_str()); |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | void Prop_double::SetValue(std::string const& input){ |
| 246 | Value val(input,Value::V_DOUBLE); |
| 247 | SetVal(val,false,true); |
| 248 | } |
| 249 | |
| 250 | //void Property::SetValue(char* input){ |
| 251 | // value.SetValue(input, Value::V_CURRENT); |
| 252 | //} |
| 253 | void Prop_int::SetValue(std::string const& input){; |
| 254 | Value val(input,Value::V_INT); |
| 255 | SetVal(val,false,true); |
| 256 | } |
| 257 | |
| 258 | void Prop_string::SetValue(std::string const& input){ |
| 259 | //Special version for lowcase stuff |
| 260 | std::string temp(input); |
| 261 | //suggested values always case insensitive. |
| 262 | //If there are none then it can be paths and such which are case sensitive |
| 263 | if(!suggested_values.empty()) lowcase(temp); |
| 264 | Value val(temp,Value::V_STRING); |
| 265 | SetVal(val,false,true); |
| 266 | } |
| 267 | bool Prop_string::CheckValue(Value const& in, bool warn){ |
| 268 | if(suggested_values.empty()) return true; |
| 269 | for(iter it = suggested_values.begin();it != suggested_values.end();it++) { |
| 270 | if ( (*it) == in) { //Match! |
| 271 | return true; |
| 272 | } |
| 273 | if((*it).ToString() == "%u") { |
| 274 | Bitu value; |
| 275 | if(sscanf(in.ToString().c_str(),"%u",&value) == 1) { |
| 276 | return true; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | if(warn) LOG_MSG("\"%s\" is not a valid value for variable: %s.\nIt might now be reset it to default value: %s",in.ToString().c_str(),propname.c_str(),default_value.ToString().c_str()); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | void Prop_path::SetValue(std::string const& input){ |
| 285 | //Special version to merge realpath with it |
| 286 | |
| 287 | Value val(input,Value::V_STRING); |
| 288 | SetVal(val,false,true); |
| 289 | |
| 290 | if(input.empty()) { |
| 291 | realpath = ""; |
| 292 | return; |
| 293 | } |
| 294 | std::string workcopy(input); |
| 295 | Cross::ResolveHomedir(workcopy); //Parse ~ and friends |
| 296 | //Prepend config directory in it exists. Check for absolute paths later |
| 297 | if( current_config_dir.empty()) realpath = workcopy; |
| 298 | else realpath = current_config_dir + CROSS_FILESPLIT + workcopy; |
| 299 | //Absolute paths |
| 300 | #if defined (WIN32) || defined(OS2) |
| 301 | if( workcopy.size() > 2 && workcopy[1] == ':' ) realpath = workcopy; |
| 302 | #else |
| 303 | if( workcopy.size() > 1 && workcopy[0] == '/' ) realpath = workcopy; |
| 304 | #endif |
| 305 | } |
| 306 | |
| 307 | void Prop_bool::SetValue(std::string const& input){ |
| 308 | value.SetValue(input,Value::V_BOOL); |
| 309 | } |
| 310 | |
| 311 | void Prop_hex::SetValue(std::string const& input){ |
| 312 | Value val(input,Value::V_HEX); |
| 313 | SetVal(val,false,true); |
| 314 | } |
| 315 | |
| 316 | void Prop_multival::make_default_value(){ |
| 317 | Bitu i = 1; |
| 318 | Property *p = section->Get_prop(0); |
| 319 | if(!p) return; |
| 320 | |
| 321 | std::string result = p->Get_Default_Value().ToString(); |
| 322 | while( (p = section->Get_prop(i++)) ) { |
| 323 | std::string props = p->Get_Default_Value().ToString(); |
| 324 | if(props == "") continue; |
| 325 | result += seperator; result += props; |
| 326 | } |
| 327 | Value val(result,Value::V_STRING); |
| 328 | SetVal(val,false,true); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | |
| 333 | //TODO checkvalue stuff |
| 334 | void Prop_multival_remain::SetValue(std::string const& input) { |
| 335 | Value val(input,Value::V_STRING); |
| 336 | SetVal(val,false,true); |
| 337 | |
| 338 | std::string local(input); |
| 339 | int i = 0,number_of_properties = 0; |
| 340 | Property *p = section->Get_prop(0); |
| 341 | //No properties in this section. do nothing |
| 342 | if(!p) return; |
| 343 | |
| 344 | while( (section->Get_prop(number_of_properties)) ) |
| 345 | number_of_properties++; |
| 346 | |
| 347 | string::size_type loc = string::npos; |
| 348 | while( (p = section->Get_prop(i++)) ) { |
| 349 | //trim leading seperators |
| 350 | loc = local.find_first_not_of(seperator); |
| 351 | if(loc != string::npos) local.erase(0,loc); |
| 352 | loc = local.find_first_of(seperator); |
| 353 | string in = "";//default value |
| 354 | /* when i == number_of_properties add the total line. (makes more then |
| 355 | * one string argument possible for parameters of cpu) */ |
| 356 | if(loc != string::npos && i < number_of_properties) { //seperator found |
| 357 | in = local.substr(0,loc); |
| 358 | local.erase(0,loc+1); |
| 359 | } else if(local.size()) { //last argument or last property |
| 360 | in = local; |
| 361 | local = ""; |
| 362 | } |
| 363 | //Test Value. If it fails set default |
| 364 | Value valtest (in,p->Get_type()); |
| 365 | if(!p->CheckValue(valtest,true)) { |
| 366 | make_default_value(); |
| 367 | return; |
| 368 | } |
| 369 | p->SetValue(in); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | //TODO checkvalue stuff |
| 374 | void Prop_multival::SetValue(std::string const& input) { |
| 375 | Value val(input,Value::V_STRING); |
| 376 | SetVal(val,false,true); |
| 377 | |
| 378 | std::string local(input); |
| 379 | int i = 0; |
| 380 | Property *p = section->Get_prop(0); |
| 381 | //No properties in this section. do nothing |
| 382 | if(!p) return; |
| 383 | string::size_type loc = string::npos; |
| 384 | while( (p = section->Get_prop(i++)) ) { |
| 385 | //trim leading seperators |
| 386 | loc = local.find_first_not_of(seperator); |
| 387 | if(loc != string::npos) local.erase(0,loc); |
| 388 | loc = local.find_first_of(seperator); |
| 389 | string in = "";//default value |
| 390 | if(loc != string::npos) { //seperator found |
| 391 | in = local.substr(0,loc); |
| 392 | local.erase(0,loc+1); |
| 393 | } else if(local.size()) { //last argument |
| 394 | in = local; |
| 395 | local = ""; |
| 396 | } |
| 397 | //Test Value. If it fails set default |
| 398 | Value valtest (in,p->Get_type()); |
| 399 | if(!p->CheckValue(valtest,true)) { |
| 400 | make_default_value(); |
| 401 | return; |
| 402 | } |
| 403 | p->SetValue(in); |
| 404 | |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | const std::vector<Value>& Property::GetValues() const { |
| 409 | return suggested_values; |
| 410 | } |
| 411 | const std::vector<Value>& Prop_multival::GetValues() const |
| 412 | { |
| 413 | Property *p = section->Get_prop(0); |
| 414 | //No properties in this section. do nothing |
| 415 | if(!p) return suggested_values; |
| 416 | int i =0; |
| 417 | while( (p = section->Get_prop(i++)) ) { |
| 418 | std::vector<Value> v = p->GetValues(); |
| 419 | if(!v.empty()) return p->GetValues(); |
| 420 | } |
| 421 | return suggested_values; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | void Section_prop::Add_double(char const * const _propname, double _value) { |
| 426 | Property* test=new Prop_double(_propname,_value); |
| 427 | properties.push_back(test); |
| 428 | }*/ |
| 429 | |
| 430 | void Property::Set_values(const char * const *in) { |
| 431 | Value::Etype type = default_value.type; |
| 432 | int i = 0; |
| 433 | while (in[i]) { |
| 434 | Value val(in[i],type); |
| 435 | suggested_values.push_back(val); |
| 436 | i++; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | Prop_int* Section_prop::Add_int(string const& _propname, Property::Changeable::Value when, int _value) { |
| 441 | Prop_int* test=new Prop_int(_propname,when,_value); |
| 442 | properties.push_back(test); |
| 443 | return test; |
| 444 | } |
| 445 | |
| 446 | Prop_string* Section_prop::Add_string(string const& _propname, Property::Changeable::Value when, char const * const _value) { |
| 447 | Prop_string* test=new Prop_string(_propname,when,_value); |
| 448 | properties.push_back(test); |
| 449 | return test; |
| 450 | } |
| 451 | |
| 452 | Prop_path* Section_prop::Add_path(string const& _propname, Property::Changeable::Value when, char const * const _value) { |
| 453 | Prop_path* test=new Prop_path(_propname,when,_value); |
| 454 | properties.push_back(test); |
| 455 | return test; |
| 456 | } |
| 457 | |
| 458 | Prop_bool* Section_prop::Add_bool(string const& _propname, Property::Changeable::Value when, bool _value) { |
| 459 | Prop_bool* test=new Prop_bool(_propname,when,_value); |
| 460 | properties.push_back(test); |
| 461 | return test; |
| 462 | } |
| 463 | Prop_hex* Section_prop::Add_hex(string const& _propname, Property::Changeable::Value when, Hex _value) { |
| 464 | Prop_hex* test=new Prop_hex(_propname,when,_value); |
| 465 | properties.push_back(test); |
| 466 | return test; |
| 467 | } |
| 468 | Prop_multival* Section_prop::Add_multi(std::string const& _propname, Property::Changeable::Value when,std::string const& sep) { |
| 469 | Prop_multival* test = new Prop_multival(_propname,when,sep); |
| 470 | properties.push_back(test); |
| 471 | return test; |
| 472 | } |
| 473 | Prop_multival_remain* Section_prop::Add_multiremain(std::string const& _propname, Property::Changeable::Value when,std::string const& sep) { |
| 474 | Prop_multival_remain* test = new Prop_multival_remain(_propname,when,sep); |
| 475 | properties.push_back(test); |
| 476 | return test; |
| 477 | } |
| 478 | |
| 479 | int Section_prop::Get_int(string const&_propname) const { |
| 480 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 481 | if((*tel)->propname==_propname){ |
| 482 | return ((*tel)->GetValue()); |
| 483 | } |
| 484 | } |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | bool Section_prop::Get_bool(string const& _propname) const { |
| 489 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 490 | if((*tel)->propname==_propname){ |
| 491 | return ((*tel)->GetValue()); |
| 492 | } |
| 493 | } |
| 494 | return false; |
| 495 | } |
| 496 | double Section_prop::Get_double(string const& _propname) const { |
| 497 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 498 | if((*tel)->propname==_propname){ |
| 499 | return ((*tel)->GetValue()); |
| 500 | } |
| 501 | } |
| 502 | return 0.0; |
| 503 | } |
| 504 | |
| 505 | Prop_path* Section_prop::Get_path(string const& _propname) const { |
| 506 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 507 | if((*tel)->propname==_propname){ |
| 508 | Prop_path* val = dynamic_cast<Prop_path*>((*tel)); |
| 509 | if(val) return val; else return NULL; |
| 510 | } |
| 511 | } |
| 512 | return NULL; |
| 513 | } |
| 514 | |
| 515 | Prop_multival* Section_prop::Get_multival(string const& _propname) const { |
| 516 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 517 | if((*tel)->propname==_propname){ |
| 518 | Prop_multival* val = dynamic_cast<Prop_multival*>((*tel)); |
| 519 | if(val) return val; else return NULL; |
| 520 | } |
| 521 | } |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | Prop_multival_remain* Section_prop::Get_multivalremain(string const& _propname) const { |
| 526 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 527 | if((*tel)->propname==_propname){ |
| 528 | Prop_multival_remain* val = dynamic_cast<Prop_multival_remain*>((*tel)); |
| 529 | if(val) return val; else return NULL; |
| 530 | } |
| 531 | } |
| 532 | return NULL; |
| 533 | } |
| 534 | Property* Section_prop::Get_prop(int index){ |
| 535 | for(it tel=properties.begin();tel!=properties.end();tel++){ |
| 536 | if(!index--) return (*tel); |
| 537 | } |
| 538 | return NULL; |
| 539 | } |
| 540 | |
| 541 | const char* Section_prop::Get_string(string const& _propname) const { |
| 542 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 543 | if((*tel)->propname==_propname){ |
| 544 | return ((*tel)->GetValue()); |
| 545 | } |
| 546 | } |
| 547 | return ""; |
| 548 | } |
| 549 | Hex Section_prop::Get_hex(string const& _propname) const { |
| 550 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 551 | if((*tel)->propname==_propname){ |
| 552 | return ((*tel)->GetValue()); |
| 553 | } |
| 554 | } |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | void trim(string& in) { |
| 559 | string::size_type loc = in.find_first_not_of(" \r\t\f\n"); |
| 560 | if(loc != string::npos) in.erase(0,loc); |
| 561 | loc = in.find_last_not_of(" \r\t\f\n"); |
| 562 | if(loc != string::npos) in.erase(loc+1); |
| 563 | } |
| 564 | |
| 565 | //TODO double c_str |
| 566 | void Section_prop::HandleInputline(string const& gegevens){ |
| 567 | string str1 = gegevens; |
| 568 | string::size_type loc = str1.find('='); |
| 569 | if(loc == string::npos) return; |
| 570 | string name = str1.substr(0,loc); |
| 571 | string val = str1.substr(loc + 1); |
| 572 | /* trim the results incase there were spaces somewhere */ |
| 573 | trim(name);trim(val); |
| 574 | for(it tel=properties.begin();tel!=properties.end();tel++){ |
| 575 | if(!strcasecmp((*tel)->propname.c_str(),name.c_str())){ |
| 576 | (*tel)->SetValue(val); |
| 577 | return; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | void Section_prop::PrintData(FILE* outfile) const { |
| 583 | /* Now print out the individual section entries */ |
| 584 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 585 | fprintf(outfile,"%s=%s\n",(*tel)->propname.c_str(),(*tel)->GetValue().ToString().c_str()); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | //TODO geen noodzaak voor 2 keer c_str |
| 590 | string Section_prop::GetPropValue(string const& _property) const{ |
| 591 | for(const_it tel=properties.begin();tel!=properties.end();tel++){ |
| 592 | if(!strcasecmp((*tel)->propname.c_str(),_property.c_str())){ |
| 593 | return (*tel)->GetValue().ToString(); |
| 594 | } |
| 595 | } |
| 596 | return NO_SUCH_PROPERTY; |
| 597 | } |
| 598 | |
| 599 | void Section_line::HandleInputline(string const& line){ |
| 600 | data+=line; |
| 601 | data+="\n"; |
| 602 | } |
| 603 | |
| 604 | void Section_line::PrintData(FILE* outfile) const { |
| 605 | fprintf(outfile,"%s",data.c_str()); |
| 606 | } |
| 607 | |
| 608 | string Section_line::GetPropValue(string const& /* _property*/) const { |
| 609 | return NO_SUCH_PROPERTY; |
| 610 | } |
| 611 | |
| 612 | bool Config::PrintConfig(char const * const configfilename) const { |
| 613 | char temp[50];char helpline[256]; |
| 614 | FILE* outfile=fopen(configfilename,"w+t"); |
| 615 | if(outfile==NULL) return false; |
| 616 | |
| 617 | /* Print start of configfile and add an return to improve readibility. */ |
| 618 | fprintf(outfile,MSG_Get("CONFIGFILE_INTRO"),VERSION); |
| 619 | fprintf(outfile,"\n"); |
| 620 | for (const_it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){ |
| 621 | /* Print out the Section header */ |
| 622 | Section_prop *sec = dynamic_cast<Section_prop *>(*tel); |
| 623 | strcpy(temp,(*tel)->GetName()); |
| 624 | lowcase(temp); |
| 625 | fprintf(outfile,"[%s]\n",temp); |
| 626 | |
| 627 | if (sec) { |
| 628 | Property *p; |
| 629 | size_t i = 0, maxwidth = 0; |
| 630 | while ((p = sec->Get_prop(i++))) { |
| 631 | size_t w = strlen(p->propname.c_str()); |
| 632 | if (w > maxwidth) maxwidth = w; |
| 633 | } |
| 634 | i=0; |
| 635 | char prefix[80]; |
| 636 | snprintf(prefix,80, "\n# %*s ", maxwidth, ""); |
| 637 | while ((p = sec->Get_prop(i++))) { |
| 638 | std::string help = p->Get_help(); |
| 639 | std::string::size_type pos = std::string::npos; |
| 640 | while ((pos = help.find("\n", pos+1)) != std::string::npos) { |
| 641 | help.replace(pos, 1, prefix); |
| 642 | } |
| 643 | |
| 644 | fprintf(outfile, "# %*s: %s", maxwidth, p->propname.c_str(), help.c_str()); |
| 645 | |
| 646 | std::vector<Value> values = p->GetValues(); |
| 647 | if (!values.empty()) { |
| 648 | fprintf(outfile, "%s%s:", prefix, MSG_Get("CONFIG_SUGGESTED_VALUES")); |
| 649 | std::vector<Value>::iterator it = values.begin(); |
| 650 | while (it != values.end()) { |
| 651 | if((*it).ToString() != "%u") { //Hack hack hack. else we need to modify GetValues, but that one is const... |
| 652 | if (it != values.begin()) fputs(",", outfile); |
| 653 | fprintf(outfile, " %s", (*it).ToString().c_str()); |
| 654 | } |
| 655 | ++it; |
| 656 | } |
| 657 | fprintf(outfile,"."); |
| 658 | } |
| 659 | fprintf(outfile, "\n"); |
| 660 | } |
| 661 | } else { |
| 662 | upcase(temp); |
| 663 | strcat(temp,"_CONFIGFILE_HELP"); |
| 664 | const char * helpstr=MSG_Get(temp); |
| 665 | char * helpwrite=helpline; |
| 666 | while (*helpstr) { |
| 667 | *helpwrite++=*helpstr; |
| 668 | if (*helpstr == '\n') { |
| 669 | *helpwrite=0; |
| 670 | fprintf(outfile,"# %s",helpline); |
| 671 | helpwrite=helpline; |
| 672 | } |
| 673 | helpstr++; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | fprintf(outfile,"\n"); |
| 678 | (*tel)->PrintData(outfile); |
| 679 | fprintf(outfile,"\n"); /* Always an empty line between sections */ |
| 680 | } |
| 681 | fclose(outfile); |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | Section_prop* Config::AddSection_prop(char const * const _name,void (*_initfunction)(Section*),bool canchange){ |
| 687 | Section_prop* blah = new Section_prop(_name); |
| 688 | blah->AddInitFunction(_initfunction,canchange); |
| 689 | sectionlist.push_back(blah); |
| 690 | return blah; |
| 691 | } |
| 692 | |
| 693 | Section_prop::~Section_prop() { |
| 694 | //ExecuteDestroy should be here else the destroy functions use destroyed properties |
| 695 | ExecuteDestroy(true); |
| 696 | /* Delete properties themself (properties stores the pointer of a prop */ |
| 697 | for(it prop = properties.begin(); prop != properties.end(); prop++) |
| 698 | delete (*prop); |
| 699 | } |
| 700 | |
| 701 | |
| 702 | Section_line* Config::AddSection_line(char const * const _name,void (*_initfunction)(Section*)){ |
| 703 | Section_line* blah = new Section_line(_name); |
| 704 | blah->AddInitFunction(_initfunction); |
| 705 | sectionlist.push_back(blah); |
| 706 | return blah; |
| 707 | } |
| 708 | |
| 709 | |
| 710 | void Config::Init() { |
| 711 | for (const_it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){ |
| 712 | (*tel)->ExecuteInit(); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | void Section::AddInitFunction(SectionFunction func,bool canchange) { |
| 717 | initfunctions.push_back(Function_wrapper(func,canchange)); |
| 718 | } |
| 719 | |
| 720 | void Section::AddDestroyFunction(SectionFunction func,bool canchange) { |
| 721 | destroyfunctions.push_front(Function_wrapper(func,canchange)); |
| 722 | } |
| 723 | |
| 724 | |
| 725 | void Section::ExecuteInit(bool initall) { |
| 726 | typedef std::list<Function_wrapper>::iterator func_it; |
| 727 | for (func_it tel=initfunctions.begin(); tel!=initfunctions.end(); tel++) { |
| 728 | if(initall || (*tel).canchange) (*tel).function(this); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | void Section::ExecuteDestroy(bool destroyall) { |
| 733 | typedef std::list<Function_wrapper>::iterator func_it; |
| 734 | for (func_it tel=destroyfunctions.begin(); tel!=destroyfunctions.end(); ) { |
| 735 | if(destroyall || (*tel).canchange) { |
| 736 | (*tel).function(this); |
| 737 | tel=destroyfunctions.erase(tel); //Remove destroyfunction once used |
| 738 | } else tel++; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | Config::~Config() { |
| 743 | reverse_it cnt=sectionlist.rbegin(); |
| 744 | while (cnt!=sectionlist.rend()) { |
| 745 | delete (*cnt); |
| 746 | cnt++; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | Section* Config::GetSection(int index){ |
| 751 | for (it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){ |
| 752 | if (!index--) return (*tel); |
| 753 | } |
| 754 | return NULL; |
| 755 | } |
| 756 | //c_str() 2x |
| 757 | Section* Config::GetSection(string const& _sectionname) const{ |
| 758 | for (const_it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){ |
| 759 | if (!strcasecmp((*tel)->GetName(),_sectionname.c_str())) return (*tel); |
| 760 | } |
| 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | Section* Config::GetSectionFromProperty(char const * const prop) const{ |
| 765 | for (const_it tel=sectionlist.begin(); tel!=sectionlist.end(); tel++){ |
| 766 | if ((*tel)->GetPropValue(prop) != NO_SUCH_PROPERTY) return (*tel); |
| 767 | } |
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | |
| 772 | bool Config::ParseConfigFile(char const * const configfilename){ |
| 773 | static bool first_configfile = true; |
| 774 | ifstream in(configfilename); |
| 775 | if (!in) return false; |
| 776 | const char * settings_type = first_configfile?"primary":"additional"; |
| 777 | first_configfile = false; |
| 778 | LOG_MSG("CONFIG:Loading %s settings from config file %s", settings_type,configfilename); |
| 779 | |
| 780 | //Get directory from configfilename, used with relative paths. |
| 781 | current_config_dir=configfilename; |
| 782 | std::string::size_type pos = current_config_dir.rfind(CROSS_FILESPLIT); |
| 783 | if(pos == std::string::npos) pos = 0; //No directory then erase string |
| 784 | current_config_dir.erase(pos); |
| 785 | |
| 786 | string gegevens; |
| 787 | Section* currentsection = NULL; |
| 788 | Section* testsec = NULL; |
| 789 | while (getline(in,gegevens)) { |
| 790 | |
| 791 | /* strip leading/trailing whitespace */ |
| 792 | trim(gegevens); |
| 793 | if(!gegevens.size()) continue; |
| 794 | |
| 795 | switch(gegevens[0]){ |
| 796 | case '%': |
| 797 | case '\0': |
| 798 | case '#': |
| 799 | case ' ': |
| 800 | case '\n': |
| 801 | continue; |
| 802 | break; |
| 803 | case '[': |
| 804 | { |
| 805 | string::size_type loc = gegevens.find(']'); |
| 806 | if(loc == string::npos) continue; |
| 807 | gegevens.erase(loc); |
| 808 | testsec = GetSection(gegevens.substr(1)); |
| 809 | if(testsec != NULL ) currentsection = testsec; |
| 810 | testsec = NULL; |
| 811 | } |
| 812 | break; |
| 813 | default: |
| 814 | try { |
| 815 | if(currentsection) currentsection->HandleInputline(gegevens); |
| 816 | } catch(const char* message) { |
| 817 | message=0; |
| 818 | //EXIT with message |
| 819 | } |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | current_config_dir.clear();//So internal changes don't use the path information |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | void Config::ParseEnv(char ** envp) { |
| 828 | for(char** env=envp; *env;env++) { |
| 829 | char copy[1024]; |
| 830 | safe_strncpy(copy,*env,1024); |
| 831 | if(strncasecmp(copy,"DOSBOX_",7)) |
| 832 | continue; |
| 833 | char* sec_name = ©[7]; |
| 834 | if(!(*sec_name)) |
| 835 | continue; |
| 836 | char* prop_name = strrchr(sec_name,'_'); |
| 837 | if(!prop_name || !(*prop_name)) |
| 838 | continue; |
| 839 | *prop_name++=0; |
| 840 | Section* sect = GetSection(sec_name); |
| 841 | if(!sect) |
| 842 | continue; |
| 843 | sect->HandleInputline(prop_name); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | void Config::SetStartUp(void (*_function)(void)) { |
| 848 | _start_function=_function; |
| 849 | } |
| 850 | |
| 851 | |
| 852 | void Config::StartUp(void) { |
| 853 | (*_start_function)(); |
| 854 | } |
| 855 | |
| 856 | bool CommandLine::FindExist(char const * const name,bool remove) { |
| 857 | cmd_it it; |
| 858 | if (!(FindEntry(name,it,false))) return false; |
| 859 | if (remove) cmds.erase(it); |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | bool CommandLine::FindHex(char const * const name,int & value,bool remove) { |
| 864 | cmd_it it,it_next; |
| 865 | if (!(FindEntry(name,it,true))) return false; |
| 866 | it_next=it;it_next++; |
| 867 | sscanf((*it_next).c_str(),"%X",&value); |
| 868 | if (remove) cmds.erase(it,++it_next); |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | bool CommandLine::FindInt(char const * const name,int & value,bool remove) { |
| 873 | cmd_it it,it_next; |
| 874 | if (!(FindEntry(name,it,true))) return false; |
| 875 | it_next=it;it_next++; |
| 876 | value=atoi((*it_next).c_str()); |
| 877 | if (remove) cmds.erase(it,++it_next); |
| 878 | return true; |
| 879 | } |
| 880 | |
| 881 | bool CommandLine::FindString(char const * const name,std::string & value,bool remove) { |
| 882 | cmd_it it,it_next; |
| 883 | if (!(FindEntry(name,it,true))) return false; |
| 884 | it_next=it;it_next++; |
| 885 | value=*it_next; |
| 886 | if (remove) cmds.erase(it,++it_next); |
| 887 | return true; |
| 888 | } |
| 889 | |
| 890 | bool CommandLine::FindCommand(unsigned int which,std::string & value) { |
| 891 | if (which<1) return false; |
| 892 | if (which>cmds.size()) return false; |
| 893 | cmd_it it=cmds.begin(); |
| 894 | for (;which>1;which--) it++; |
| 895 | value=(*it); |
| 896 | return true; |
| 897 | } |
| 898 | |
| 899 | bool CommandLine::FindEntry(char const * const name,cmd_it & it,bool neednext) { |
| 900 | for (it=cmds.begin();it!=cmds.end();it++) { |
| 901 | if (!strcasecmp((*it).c_str(),name)) { |
| 902 | cmd_it itnext=it;itnext++; |
| 903 | if (neednext && (itnext==cmds.end())) return false; |
| 904 | return true; |
| 905 | } |
| 906 | } |
| 907 | return false; |
| 908 | } |
| 909 | |
| 910 | bool CommandLine::FindStringBegin(char const* const begin,std::string & value, bool remove) { |
| 911 | size_t len = strlen(begin); |
| 912 | for (cmd_it it=cmds.begin();it!=cmds.end();it++) { |
| 913 | if (strncmp(begin,(*it).c_str(),len)==0) { |
| 914 | value=((*it).c_str() + len); |
| 915 | if (remove) cmds.erase(it); |
| 916 | return true; |
| 917 | } |
| 918 | } |
| 919 | return false; |
| 920 | } |
| 921 | |
| 922 | bool CommandLine::FindStringRemain(char const * const name,std::string & value) { |
| 923 | cmd_it it;value=""; |
| 924 | if (!FindEntry(name,it)) return false; |
| 925 | it++; |
| 926 | for (;it!=cmds.end();it++) { |
| 927 | value+=" "; |
| 928 | value+=(*it); |
| 929 | } |
| 930 | return true; |
| 931 | } |
| 932 | |
| 933 | bool CommandLine::GetStringRemain(std::string & value) { |
| 934 | if(!cmds.size()) return false; |
| 935 | |
| 936 | cmd_it it=cmds.begin();value=(*it++); |
| 937 | for(;it != cmds.end();it++) { |
| 938 | value+=" "; |
| 939 | value+=(*it); |
| 940 | } |
| 941 | return true; |
| 942 | } |
| 943 | |
| 944 | |
| 945 | unsigned int CommandLine::GetCount(void) { |
| 946 | return (unsigned int)cmds.size(); |
| 947 | } |
| 948 | |
| 949 | CommandLine::CommandLine(int argc,char const * const argv[]) { |
| 950 | if (argc>0) { |
| 951 | file_name=argv[0]; |
| 952 | } |
| 953 | int i=1; |
| 954 | while (i<argc) { |
| 955 | cmds.push_back(argv[i]); |
| 956 | i++; |
| 957 | } |
| 958 | } |
| 959 | Bit16u CommandLine::Get_arglength() { |
| 960 | if(cmds.empty()) return 0; |
| 961 | Bit16u i=1; |
| 962 | for(cmd_it it=cmds.begin();it != cmds.end();it++) |
| 963 | i+=(*it).size() + 1; |
| 964 | return --i; |
| 965 | } |
| 966 | |
| 967 | |
| 968 | CommandLine::CommandLine(char const * const name,char const * const cmdline) { |
| 969 | if (name) file_name=name; |
| 970 | /* Parse the cmds and put them in the list */ |
| 971 | bool inword,inquote;char c; |
| 972 | inword=false;inquote=false; |
| 973 | std::string str; |
| 974 | const char * c_cmdline=cmdline; |
| 975 | while ((c=*c_cmdline)!=0) { |
| 976 | if (inquote) { |
| 977 | if (c!='"') str+=c; |
| 978 | else { |
| 979 | inquote=false; |
| 980 | cmds.push_back(str); |
| 981 | str.erase(); |
| 982 | } |
| 983 | }else if (inword) { |
| 984 | if (c!=' ') str+=c; |
| 985 | else { |
| 986 | inword=false; |
| 987 | cmds.push_back(str); |
| 988 | str.erase(); |
| 989 | } |
| 990 | } |
| 991 | else if (c=='"') { inquote=true;} |
| 992 | else if (c!=' ') { str+=c;inword=true;} |
| 993 | c_cmdline++; |
| 994 | } |
| 995 | if (inword || inquote) cmds.push_back(str); |
| 996 | } |
| 997 | |
| 998 | void CommandLine::Shift(unsigned int amount) { |
| 999 | while(amount--) { |
| 1000 | file_name = cmds.size()?(*(cmds.begin())):""; |
| 1001 | if(cmds.size()) cmds.erase(cmds.begin()); |
| 1002 | } |
| 1003 | } |