From: Nickolay S. <sk...@us...> - 2002-11-30 15:08:37
|
Build Version : T1.5.0.1627 Firebird 1.5 Alpha 4 (writeBuildNum.sh,v 1.1609 2002/11/30 15:08:06 skidder Exp ) Update of /cvsroot/firebird/firebird2/src/common/config In directory sc8-pr-cvs1:/tmp/cvs-serv15070/src/common/config Modified Files: config.cpp config.h config_impl.h Log Message: New changes by Dmitry Emanov. Configuration manager and Windows build environment improvements Index: config.cpp =================================================================== RCS file: /cvsroot/firebird/firebird2/src/common/config/config.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -b -U3 -r1.3 -r1.4 --- config.cpp 10 Nov 2002 13:41:20 -0000 1.3 +++ config.cpp 30 Nov 2002 15:08:04 -0000 1.4 @@ -27,6 +27,7 @@ #ifdef _MSC_VER #pragma warning (disable: 4786) // debug identifier truncated +#pragma warning (disable: 4800) // forcing value to bool 'true' or 'false' #endif #include "../../common/config/config.h" @@ -40,20 +41,20 @@ #include "../jrd/gdsassert.h" typedef Firebird::string string; -typedef Firebird::vector<string> string_vector; /****************************************************************************** * - * Configuration keys (physical names) + * Configuration entries */ -const string keys[] = { - "RootDirectory", // 0 - "SortMemBlockSize", // 1 - "SortMemUpperLimit", // 2 - "RemoteFileOpenAbility", // 3 - "TempDirectories", // 4 - "GuardianOption" // 5 +const ConfigImpl::ConfigEntry ConfigImpl::entries[] = +{ + {TYPE_STRING, "RootDirectory", (ConfigValue) 0}, + {TYPE_INTEGER, "SortMemBlockSize", (ConfigValue) 1048576}, + {TYPE_INTEGER, "SortMemUpperLimit", (ConfigValue) 268435456}, + {TYPE_BOOLEAN, "RemoteFileOpenAbility", (ConfigValue) false}, + {TYPE_INTEGER, "GuardianOption", (ConfigValue) 1}, + {TYPE_INTEGER, "CpuAffinityMask", (ConfigValue) 1} }; /****************************************************************************** @@ -61,11 +62,7 @@ * Static instance of the system configuration file */ -ConfigImpl& ConfigImpl::instance() -{ - static ConfigImpl sysConfig; - return sysConfig; -} +const static ConfigImpl sysConfig; /****************************************************************************** * @@ -74,76 +71,96 @@ ConfigImpl::ConfigImpl() { + /* Prepare some stuff */ + + ConfigFile file; + root_dir = getRootDirectory().c_str(); + MemoryPool *pool = getDefaultMemoryPool(); + int size = sizeof(entries) / sizeof(entries[0]); + values = FB_NEW(*pool) ConfigValue[size]; + + string val_sep = ","; file.setConfigFile(getConfigFile()); - val_sep = ","; -} -void ConfigImpl::validateKey(ConfigKey key) -{ - assert(key >= 0 && key < (sizeof(keys) / sizeof(string))); -} + /* Iterate through the known configuration entries */ -void ConfigImpl::getValue(ConfigKey key, string &result) -{ - validateKey(key); + for (int i = 0; i < size; i++) + { + ConfigEntry entry = entries[i]; + string value = getValue(file, entries[i].key); - if (file.doesKeyExist(keys[key])) + if (!value.length()) { - result = file.getString(keys[key]); + /* Assign the default value */ + + values[i] = entries[i].default_value; + continue; } -} -void ConfigImpl::getValue(ConfigKey key, int &result) -{ - validateKey(key); + /* Assign the actual value */ - if (file.doesKeyExist(keys[key])) + switch (entry.data_type) { - result = atoi(file.getString(keys[key]).data()); + case TYPE_BOOLEAN: + values[i] = (ConfigValue) asBoolean(value); + break; + case TYPE_INTEGER: + values[i] = (ConfigValue) asInteger(value); + break; + case TYPE_STRING: + { + const char *src = asString(value); + char *dst = FB_NEW(*pool) char[strlen(src) + 1]; + strcpy(dst, src); + values[i] = (ConfigValue) dst; + } + break; + case TYPE_STRING_VECTOR: + ; + } } } -void ConfigImpl::getValue(ConfigKey key, bool &result) +ConfigImpl::~ConfigImpl() { - validateKey(key); + /* Free allocated memory */ + + for (int i = 0; i < sizeof(entries) / sizeof(entries[0]); i++) + { + if (values[i] == entries[i].default_value) + continue; - if (file.doesKeyExist(keys[key])) + switch (entries[i].data_type) { - result = (atoi(file.getString(keys[key]).data()) != 0); + case TYPE_STRING: + delete[] values[i]; + break; + case TYPE_STRING_VECTOR: + ; + } } + + delete[] values; } -void ConfigImpl::getValueList(ConfigKey key, Firebird::vector<string> &result) +string ConfigImpl::getValue(ConfigFile file, ConfigKey key) { - validateKey(key); + return file.doesKeyExist(key) ? file.getString(key) : ""; +} - string value = result[0]; - if (file.doesKeyExist(keys[key])) - { - value = file.getString(keys[key]); - } +int ConfigImpl::asInteger(const string &value) +{ + return atoi(value.data()); +} - result.empty(); +bool ConfigImpl::asBoolean(const string &value) +{ + return (atoi(value.data()) != 0); +} - int index = value.find(val_sep); - while (index != string::npos) - { - string temp = value.substr(0, index); - ConfigFile::stripLeadingWhiteSpace(temp); - ConfigFile::stripTrailingWhiteSpace(temp); - if (temp.length()) - { - result.push_back(temp); - } - value = value.substr(index + 1, value.length()); - index = value.find(val_sep); - } - ConfigFile::stripLeadingWhiteSpace(value); - ConfigFile::stripTrailingWhiteSpace(value); - if (value.length()) - { - result.push_back(value); - } +const char* ConfigImpl::asString(const string &value) +{ + return value.c_str(); } /****************************************************************************** @@ -151,45 +168,33 @@ * Public interface */ -string Config::getRootDirectory() +const char* Config::getRootDirectory() { - string result = ConfigImpl::instance().getRootDirectory(); - ConfigImpl::instance().getValue(ROOT_DIRECTORY, result); - return result; + const char* result = (char*) sysConfig.values[KEY_ROOT_DIRECTORY]; + return result ? result : sysConfig.root_dir; } -int Config::getSortMemBlockSize(int default_value) +int Config::getSortMemBlockSize() { - int result = default_value; - ConfigImpl::instance().getValue(SORT_MEM_BLOCK_SIZE, result); - return result; + return (int) sysConfig.values[KEY_SORT_MEM_BLOCK_SIZE]; } -int Config::getSortMemUpperLimit(int default_value) +int Config::getSortMemUpperLimit() { - int result = default_value; - ConfigImpl::instance().getValue(SORT_MEM_UPPER_LIMIT, result); - return result; + return (int) sysConfig.values[KEY_SORT_MEM_UPPER_LIMIT]; } -bool Config::getRemoteFileOpenAbility(bool default_value) +bool Config::getRemoteFileOpenAbility() { - bool result = default_value; - ConfigImpl::instance().getValue(REMOTE_FILE_OPEN_ABILITY, result); - return result; + return (bool) sysConfig.values[KEY_REMOTE_FILE_OPEN_ABILITY]; } -string_vector Config::getTempDirectories(string default_value) +int Config::getGuardianOption() { - string_vector result(0); - result.push_back(default_value); - ConfigImpl::instance().getValueList(TEMP_DIRECTORIES, result); - return result; + return (int) sysConfig.values[KEY_GUARDIAN_OPTION]; } -int Config::getGuardianOption(int default_value) +int Config::getCpuAffinityMask() { - int result = default_value; - ConfigImpl::instance().getValue(GUARDIAN_OPTION, result); - return result; + return (int) sysConfig.values[KEY_CPU_AFFINITY_MASK]; } Index: config.h =================================================================== RCS file: /cvsroot/firebird/firebird2/src/common/config/config.h,v retrieving revision 1.3 retrieving revision 1.4 diff -b -U3 -r1.3 -r1.4 --- config.h 10 Nov 2002 13:41:20 -0000 1.3 +++ config.h 30 Nov 2002 15:08:04 -0000 1.4 @@ -39,7 +39,7 @@ functions. Each of these functions corresponds to one and only one key and has one input argument - default value, which is used when the requested key is missing or the configuration file is not found. Supported - value datatypes are "string", "int" and "bool". Usual default values for + value datatypes are "const char*", "int" and "bool". Usual default values for these datatypes are empty string, zero and false respectively. There are two types of member functions - scalar and vector. The former ones return single value of the given type. The latter ones return vector which @@ -47,12 +47,13 @@ There's one exception - getRootDirectory() member function, which returns root pathname of the current installation. This value isn't stored in the - configuration file, but is managed by the code itself. + configuration file, but is managed by the code itself. But there's a way + to override this value via the configuration file as well. To add new configuration item, you have to take the following steps: - 1. Add physical key to keys[] array (config.cpp) - 2. Add logical key to ConfigKey enumeration (config_impl.h) + 1. Add key description to ConfigImpl::entries[] array (config.cpp) + 2. Add logical key to Config::ConfigKey enumeration (config.h) (note: both physical and logical keys MUST have the same ordinal position within appropriate structures) 3. Add member function to Config class (config.h) and implement it @@ -61,40 +62,47 @@ class Config { - typedef Firebird::string string; - typedef Firebird::vector<string> string_vector; + enum ConfigKey + { + KEY_ROOT_DIRECTORY, // 0 + KEY_SORT_MEM_BLOCK_SIZE, // 1 + KEY_SORT_MEM_UPPER_LIMIT, // 2 + KEY_REMOTE_FILE_OPEN_ABILITY, // 3 + KEY_GUARDIAN_OPTION, // 4 + KEY_CPU_AFFINITY_MASK // 5 + }; public: /* Root directory of current installation */ - static string getRootDirectory(); + static const char* getRootDirectory(); /* Block size for the sorting manager */ - static int getSortMemBlockSize(int default_value = 0); + static int getSortMemBlockSize(); /* Memory usage limit for the sorting manager */ - static int getSortMemUpperLimit(int default_value = 0); + static int getSortMemUpperLimit(); /* Whether remote (NFS) files can be opened */ - static bool getRemoteFileOpenAbility(bool default_value = false); + static bool getRemoteFileOpenAbility(); /* - List of directories to store temporary files in + Startup option for the guardian */ - static string_vector getTempDirectories(string default_value = ""); + static int getGuardianOption(); /* - Startup option for the guardian + CPU affinity mask */ - static int getGuardianOption(int default_value = 0); + static int getCpuAffinityMask(); }; #endif // CONFIG_H Index: config_impl.h =================================================================== RCS file: /cvsroot/firebird/firebird2/src/common/config/config_impl.h,v retrieving revision 1.3 retrieving revision 1.4 diff -b -U3 -r1.3 -r1.4 --- config_impl.h 10 Nov 2002 13:41:20 -0000 1.3 +++ config_impl.h 30 Nov 2002 15:08:04 -0000 1.4 @@ -35,45 +35,47 @@ /****************************************************************************** * - * Configuration keys (logical names) - */ - -enum ConfigKey -{ - ROOT_DIRECTORY, // 0 - SORT_MEM_BLOCK_SIZE, // 1 - SORT_MEM_UPPER_LIMIT, // 2 - REMOTE_FILE_OPEN_ABILITY, // 3 - TEMP_DIRECTORIES, // 4 - GUARDIAN_OPTION // 5 -}; - -/****************************************************************************** - * * Main implementation class */ class ConfigImpl : public ConfigRoot { + friend class Config; + typedef Firebird::string string; - typedef Firebird::vector<string> string_vector; + + enum ConfigType + { + TYPE_BOOLEAN, + TYPE_INTEGER, + TYPE_STRING, + TYPE_STRING_VECTOR + }; + + typedef char* ConfigKey; + typedef void* ConfigValue; + + struct ConfigEntry + { + ConfigType data_type; + ConfigKey key; + ConfigValue default_value; + }; public: ConfigImpl(); + ~ConfigImpl(); - static void validateKey(ConfigKey); - - void getValue(ConfigKey, string&); - void getValue(ConfigKey, int&); - void getValue(ConfigKey, bool&); - - void getValueList(ConfigKey key, string_vector&); + static string getValue(ConfigFile, ConfigKey); - static ConfigImpl& instance(); + static int asInteger(const string&); + static bool asBoolean(const string&); + static const char* asString(const string&); private: - ConfigFile file; - string val_sep; + const static ConfigEntry entries[]; + const char *root_dir; + ConfigValue *values; ConfigImpl(const ConfigImpl&); void operator=(const ConfigImpl&); |