Changeset 2116 for box/trunk/lib/common
- Timestamp:
- 28/03/2008 22:59:28 (4 years ago)
- Location:
- box/trunk
- Files:
-
- 4 edited
-
. (modified) (1 prop)
-
lib/common (modified) (1 prop)
-
lib/common/Configuration.cpp (modified) (22 diffs)
-
lib/common/Configuration.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
box/trunk
- Property svn:ignore
-
old new 11 11 release 12 12 runtest.pl 13 .hg
-
- Property svn:ignore
-
box/trunk/lib/common
- Property svn:ignore
-
old new 1 autogen_CommonException.cpp 2 autogen_CommonException.h 3 autogen_ConversionException.cpp 4 autogen_ConversionException.h 1 autogen_* 5 2 BoxConfig.h 6 3 BoxConfig.h.in 7 4 Makefile 8 5 makeexception.pl 6 BoxPortsAndFiles.h
-
- Property svn:ignore
-
box/trunk/lib/common/Configuration.cpp
r1865 r2116 12 12 #include <stdlib.h> 13 13 #include <limits.h> 14 15 #include <sstream> 14 16 15 17 #include "Configuration.h" … … 30 32 static const bool sValueBooleanValue[] = {true, true, false, false}; 31 33 32 34 ConfigurationVerifyKey::ConfigurationVerifyKey 35 ( 36 std::string name, 37 int flags, 38 void *testFunction 39 ) 40 : mName(name), 41 mHasDefaultValue(false), 42 mFlags(flags), 43 mTestFunction(testFunction) 44 { } 45 46 // to allow passing NULL for default ListenAddresses 47 48 ConfigurationVerifyKey::ConfigurationVerifyKey 49 ( 50 std::string name, 51 int flags, 52 NoDefaultValue_t t, 53 void *testFunction 54 ) 55 : mName(name), 56 mHasDefaultValue(false), 57 mFlags(flags), 58 mTestFunction(testFunction) 59 { } 60 61 ConfigurationVerifyKey::ConfigurationVerifyKey 62 ( 63 std::string name, 64 int flags, 65 std::string defaultValue, 66 void *testFunction 67 ) 68 : mName(name), 69 mDefaultValue(defaultValue), 70 mHasDefaultValue(true), 71 mFlags(flags), 72 mTestFunction(testFunction) 73 { } 74 75 ConfigurationVerifyKey::ConfigurationVerifyKey 76 ( 77 std::string name, 78 int flags, 79 const char *defaultValue, 80 void *testFunction 81 ) 82 : mName(name), 83 mDefaultValue(defaultValue), 84 mHasDefaultValue(true), 85 mFlags(flags), 86 mTestFunction(testFunction) 87 { } 88 89 ConfigurationVerifyKey::ConfigurationVerifyKey 90 ( 91 std::string name, 92 int flags, 93 int defaultValue, 94 void *testFunction 95 ) 96 : mName(name), 97 mHasDefaultValue(true), 98 mFlags(flags), 99 mTestFunction(testFunction) 100 { 101 ASSERT(flags & ConfigTest_IsInt); 102 std::ostringstream val; 103 val << defaultValue; 104 mDefaultValue = val.str(); 105 } 106 107 ConfigurationVerifyKey::ConfigurationVerifyKey 108 ( 109 std::string name, 110 int flags, 111 bool defaultValue, 112 void *testFunction 113 ) 114 : mName(name), 115 mHasDefaultValue(true), 116 mFlags(flags), 117 mTestFunction(testFunction) 118 { 119 ASSERT(flags & ConfigTest_IsBool); 120 mDefaultValue = defaultValue ? "yes" : "no"; 121 } 122 123 ConfigurationVerifyKey::ConfigurationVerifyKey 124 ( 125 const ConfigurationVerifyKey& rToCopy 126 ) 127 : mName(rToCopy.mName), 128 mDefaultValue(rToCopy.mDefaultValue), 129 mHasDefaultValue(rToCopy.mHasDefaultValue), 130 mFlags(rToCopy.mFlags), 131 mTestFunction(rToCopy.mTestFunction) 132 { } 33 133 34 134 // -------------------------------------------------------------------------- … … 121 221 if(!Verify(*pconfig, *pVerify, std::string(), rErrorMsg)) 122 222 { 123 //TRACE1("Error message from Verify: %s", rErrorMsg.c_str());124 TRACE0("Error at Configuration::Verify\n");223 BOX_ERROR("Error verifying configuration: " << 224 rErrorMsg); 125 225 delete pconfig; 126 226 pconfig = 0; … … 190 290 else 191 291 { 192 rErrorMsg += "Unexpected start block in " + rConfig.mName + "\n"; 292 rErrorMsg += "Unexpected start block in " + 293 rConfig.mName + "\n"; 193 294 } 194 295 } … … 291 392 // 292 393 // Function 293 // Name: Configuration::KeyExists(const char *)394 // Name: Configuration::KeyExists(const std::string&) 294 395 // Purpose: Checks to see if a key exists 295 396 // Created: 2003/07/23 296 397 // 297 398 // -------------------------------------------------------------------------- 298 bool Configuration::KeyExists(const char *pKeyName) const 299 { 300 if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 301 302 return mKeys.find(pKeyName) != mKeys.end(); 303 } 304 305 306 // -------------------------------------------------------------------------- 307 // 308 // Function 309 // Name: Configuration::GetKeyValue(const char *) 399 bool Configuration::KeyExists(const std::string& rKeyName) const 400 { 401 return mKeys.find(rKeyName) != mKeys.end(); 402 } 403 404 405 // -------------------------------------------------------------------------- 406 // 407 // Function 408 // Name: Configuration::GetKeyValue(const std::string&) 310 409 // Purpose: Returns the value of a configuration variable 311 410 // Created: 2003/07/23 312 411 // 313 412 // -------------------------------------------------------------------------- 314 const std::string &Configuration::GetKeyValue(const char *pKeyName) const 315 { 316 if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 317 318 std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); 413 const std::string &Configuration::GetKeyValue(const std::string& rKeyName) const 414 { 415 std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); 319 416 320 417 if(i == mKeys.end()) 321 418 { 322 BOX_ERROR("Missing configuration key: " << pKeyName);419 BOX_ERROR("Missing configuration key: " << rKeyName); 323 420 THROW_EXCEPTION(CommonException, ConfigNoKey) 324 421 } … … 333 430 // 334 431 // Function 335 // Name: Configuration::GetKeyValueInt(const char *)432 // Name: Configuration::GetKeyValueInt(const std::string& rKeyName) 336 433 // Purpose: Gets a key value as an integer 337 434 // Created: 2003/07/23 338 435 // 339 436 // -------------------------------------------------------------------------- 340 int Configuration::GetKeyValueInt(const char *pKeyName) const 341 { 342 if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 343 344 std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); 437 int Configuration::GetKeyValueInt(const std::string& rKeyName) const 438 { 439 std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); 345 440 346 441 if(i == mKeys.end()) … … 363 458 // 364 459 // Function 365 // Name: Configuration::GetKeyValueBool(const char *) const460 // Name: Configuration::GetKeyValueBool(const std::string&) 366 461 // Purpose: Gets a key value as a boolean 367 462 // Created: 17/2/04 368 463 // 369 464 // -------------------------------------------------------------------------- 370 bool Configuration::GetKeyValueBool(const char *pKeyName) const 371 { 372 if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 373 374 std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); 465 bool Configuration::GetKeyValueBool(const std::string& rKeyName) const 466 { 467 std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); 375 468 376 469 if(i == mKeys.end()) … … 429 522 // 430 523 // Function 431 // Name: Configuration::SubConfigurationExists(const char *) 524 // Name: Configuration::SubConfigurationExists(const 525 // std::string&) 432 526 // Purpose: Checks to see if a sub configuration exists 433 527 // Created: 2003/07/23 434 528 // 435 529 // -------------------------------------------------------------------------- 436 bool Configuration::SubConfigurationExists(const char *pSubName) const 437 { 438 if(pSubName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 439 530 bool Configuration::SubConfigurationExists(const std::string& rSubName) const 531 { 440 532 // Attempt to find it... 441 533 std::list<std::pair<std::string, Configuration> >::const_iterator i(mSubConfigurations.begin()); … … 444 536 { 445 537 // This the one? 446 if(i->first == pSubName)538 if(i->first == rSubName) 447 539 { 448 540 // Yes. … … 459 551 // 460 552 // Function 461 // Name: Configuration::GetSubConfiguration(const char *) 553 // Name: Configuration::GetSubConfiguration(const 554 // std::string&) 462 555 // Purpose: Gets a sub configuration 463 556 // Created: 2003/07/23 464 557 // 465 558 // -------------------------------------------------------------------------- 466 const Configuration &Configuration::GetSubConfiguration(const char *pSubName) const 467 { 468 if(pSubName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} 469 559 const Configuration &Configuration::GetSubConfiguration(const std::string& 560 rSubName) const 561 { 470 562 // Attempt to find it... 471 563 std::list<std::pair<std::string, Configuration> >::const_iterator i(mSubConfigurations.begin()); … … 474 566 { 475 567 // This the one? 476 if(i->first == pSubName)568 if(i->first == rSubName) 477 569 { 478 570 // Yes. … … 529 621 { 530 622 // Can the key be found? 531 ASSERT(pvkey->mpName); 532 if(rConfig.KeyExists(pvkey->mpName)) 623 if(rConfig.KeyExists(pvkey->Name())) 533 624 { 534 625 // Get value 535 const std::string &rval = rConfig.GetKeyValue(pvkey-> mpName);626 const std::string &rval = rConfig.GetKeyValue(pvkey->Name()); 536 627 const char *val = rval.c_str(); 537 628 538 629 // Check it's a number? 539 if((pvkey-> Tests& ConfigTest_IsInt) == ConfigTest_IsInt)630 if((pvkey->Flags() & ConfigTest_IsInt) == ConfigTest_IsInt) 540 631 { 541 632 // Test it... … … 546 637 // not a good value 547 638 ok = false; 548 rErrorMsg += rLevel + rConfig.mName + "." + pvkey->mpName+ " (key) is not a valid integer.\n";639 rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is not a valid integer.\n"; 549 640 } 550 641 } 551 642 552 643 // Check it's a bool? 553 if((pvkey-> Tests& ConfigTest_IsBool) == ConfigTest_IsBool)644 if((pvkey->Flags() & ConfigTest_IsBool) == ConfigTest_IsBool) 554 645 { 555 646 // See if it's one of the allowed strings. … … 569 660 { 570 661 ok = false; 571 rErrorMsg += rLevel + rConfig.mName + "." + pvkey->mpName+ " (key) is not a valid boolean value.\n";662 rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is not a valid boolean value.\n"; 572 663 } 573 664 } 574 665 575 666 // Check for multi valued statments where they're not allowed 576 if((pvkey-> Tests& ConfigTest_MultiValueAllowed) == 0)667 if((pvkey->Flags() & ConfigTest_MultiValueAllowed) == 0) 577 668 { 578 669 // Check to see if this key is a multi-value -- it shouldn't be … … 580 671 { 581 672 ok = false; 582 rErrorMsg += rLevel + rConfig.mName +"." + pvkey-> mpName+ " (key) multi value not allowed (duplicated key?).\n";673 rErrorMsg += rLevel + rConfig.mName +"." + pvkey->Name() + " (key) multi value not allowed (duplicated key?).\n"; 583 674 } 584 675 } … … 587 678 { 588 679 // Is it required to exist? 589 if((pvkey-> Tests& ConfigTest_Exists) == ConfigTest_Exists)680 if((pvkey->Flags() & ConfigTest_Exists) == ConfigTest_Exists) 590 681 { 591 682 // Should exist, but doesn't. 592 683 ok = false; 593 rErrorMsg += rLevel + rConfig.mName + "." + pvkey->mpName + " (key) is missing.\n"; 594 } 595 else if(pvkey->mpDefaultValue) 596 { 597 rConfig.mKeys[std::string(pvkey->mpName)] = std::string(pvkey->mpDefaultValue); 684 rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is missing.\n"; 685 } 686 else if(pvkey->HasDefaultValue()) 687 { 688 rConfig.mKeys[pvkey->Name()] = 689 pvkey->DefaultValue(); 598 690 } 599 691 } 600 692 601 if((pvkey-> Tests& ConfigTest_LastEntry) == ConfigTest_LastEntry)693 if((pvkey->Flags() & ConfigTest_LastEntry) == ConfigTest_LastEntry) 602 694 { 603 695 // No more! … … 619 711 while(scan) 620 712 { 621 if(scan-> mpName== i->first)713 if(scan->Name() == i->first) 622 714 { 623 715 found = true; … … 626 718 627 719 // Next? 628 if((scan-> Tests& ConfigTest_LastEntry) == ConfigTest_LastEntry)720 if((scan->Flags() & ConfigTest_LastEntry) == ConfigTest_LastEntry) 629 721 { 630 722 break; … … 651 743 while(scan) 652 744 { 653 ASSERT(scan->mpName); 654 if(scan->mpName[0] == '*') 745 if(scan->mName.length() > 0 && scan->mName[0] == '*') 655 746 { 656 747 wildcardverify = scan; … … 660 751 if((scan->Tests & ConfigTest_Exists) == ConfigTest_Exists) 661 752 { 662 if(scan->mpName[0] == '*') 753 if(scan->mName.length() > 0 && 754 scan->mName[0] == '*') 663 755 { 664 756 // Check something exists … … 673 765 { 674 766 // Check real thing exists 675 if(!rConfig.SubConfigurationExists(scan->m pName))767 if(!rConfig.SubConfigurationExists(scan->mName)) 676 768 { 677 769 // Should exist, but doesn't. 678 770 ok = false; 679 rErrorMsg += rLevel + rConfig.mName + "." + scan->m pName + " (block) is missing.\n";771 rErrorMsg += rLevel + rConfig.mName + "." + scan->mName + " (block) is missing.\n"; 680 772 } 681 773 } … … 702 794 while(scan) 703 795 { 704 if(s trcmp(scan->mpName, name) == 0)796 if(scan->mName == name) 705 797 { 706 798 // found it! -
box/trunk/lib/common/Configuration.h
r1776 r2116 30 30 { 31 31 public: 32 const char *mpName; // "*" for all other keys (not implemented yet) 33 const char *mpDefaultValue; // default for when it's not present 34 int Tests; 35 void *TestFunction; // set to zero for now, will implement later 32 typedef enum 33 { 34 NoDefaultValue = 1 35 } NoDefaultValue_t; 36 37 ConfigurationVerifyKey(std::string name, int flags, 38 void *testFunction = NULL); 39 // to allow passing ConfigurationVerifyKey::NoDefaultValue 40 // for default ListenAddresses 41 ConfigurationVerifyKey(std::string name, int flags, 42 NoDefaultValue_t t, void *testFunction = NULL); 43 ConfigurationVerifyKey(std::string name, int flags, 44 std::string defaultValue, void *testFunction = NULL); 45 ConfigurationVerifyKey(std::string name, int flags, 46 const char* defaultValue, void *testFunction = NULL); 47 ConfigurationVerifyKey(std::string name, int flags, 48 int defaultValue, void *testFunction = NULL); 49 ConfigurationVerifyKey(std::string name, int flags, 50 bool defaultValue, void *testFunction = NULL); 51 const std::string& Name() const { return mName; } 52 const std::string& DefaultValue() const { return mDefaultValue; } 53 const bool HasDefaultValue() const { return mHasDefaultValue; } 54 const int Flags() const { return mFlags; } 55 const void* TestFunction() const { return mTestFunction; } 56 ConfigurationVerifyKey(const ConfigurationVerifyKey& rToCopy); 57 58 private: 59 ConfigurationVerifyKey& operator=(const ConfigurationVerifyKey& 60 noAssign); 61 62 std::string mName; // "*" for all other keys (not implemented yet) 63 std::string mDefaultValue; // default for when it's not present 64 bool mHasDefaultValue; 65 int mFlags; 66 void *mTestFunction; // set to zero for now, will implement later 36 67 }; 37 68 … … 39 70 { 40 71 public: 41 const char *mpName;// "*" for all other sub config names72 std::string mName; // "*" for all other sub config names 42 73 const ConfigurationVerify *mpSubConfigurations; 43 74 const ConfigurationVerifyKey *mpKeys; 44 75 int Tests; 45 void *TestFunction; // set to zero for now, will implement later76 void *TestFunction; // set to zero for now, will implement later 46 77 }; 47 78 … … 80 111 { return LoadAndVerify(rFilename, 0, rErrorMsg); } 81 112 82 bool KeyExists(const char *pKeyName) const;83 const std::string &GetKeyValue(const char *pKeyName) const;84 int GetKeyValueInt(const char *pKeyName) const;85 bool GetKeyValueBool(const char *pKeyName) const;113 bool KeyExists(const std::string& rKeyName) const; 114 const std::string &GetKeyValue(const std::string& rKeyName) const; 115 int GetKeyValueInt(const std::string& rKeyName) const; 116 bool GetKeyValueBool(const std::string& rKeyName) const; 86 117 std::vector<std::string> GetKeyNames() const; 87 118 88 bool SubConfigurationExists(const char *pSubName) const;89 const Configuration &GetSubConfiguration(const char *pSubName) const;119 bool SubConfigurationExists(const std::string& rSubName) const; 120 const Configuration &GetSubConfiguration(const std::string& rSubName) const; 90 121 std::vector<std::string> GetSubConfigurationNames() const; 91 122
Note: See TracChangeset
for help on using the changeset viewer.
