- Timestamp:
- Apr 12, 2008, 3:34:55 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core2/src/orxonox/core/ConfigValueContainer.cc
r1023 r1030 35 35 #include "ConfigValueContainer.h" 36 36 #include "Language.h" 37 #include "util/SubString.h" 38 #include "util/Convert.h" 39 40 #define MAX_VECTOR_INDEX 255 // to avoid up to 4*10^9 vector entries in the config file after accidentally using a wrong argument 37 41 38 42 … … 41 45 /** 42 46 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable. 43 @param value This is only needed to determine the right type.44 @param classname The nameof the class the variable belongs to47 @param type The type of the corresponding config-file 48 @param identifier The identifier of the class the variable belongs to 45 49 @param varname The name of the variable 46 50 @param defvalue The default-value 47 51 */ 48 ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, MultiTypeMathdefvalue)52 ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const MultiTypeMath& defvalue) 49 53 { 50 54 this->type_ = type; … … 55 59 this->value_ = defvalue; 56 60 this->bAddedDescription_ = false; 61 this->bIsVector_ = false; 57 62 58 63 this->defvalueString_ = defvalue.toString(); 59 64 this->update(); 65 } 66 67 /** 68 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable. 69 @param type The type of the corresponding config-file 70 @param identifier The identifier of the class the variable belongs to 71 @param varname The name of the variable 72 @param defvalue The default-value 73 */ 74 ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<MultiTypeMath>& defvalue) 75 { 76 this->type_ = type; 77 this->identifier_ = identifier; 78 this->sectionname_ = identifier->getName(); 79 this->varname_ = varname; 80 81 this->valueVector_ = defvalue; 82 this->bAddedDescription_ = false; 83 this->bIsVector_ = true; 84 85 if (defvalue.size() > 0) 86 this->value_ = defvalue[0]; 87 88 for (unsigned int i = 0; i < defvalue.size(); i++) 89 ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, defvalue[i].toString()); 90 91 for (unsigned int i = 0; i < defvalue.size(); i++) 92 this->defvalueStringVector_.push_back(defvalue[i].toString()); 93 94 this->update(); 95 } 96 97 /** 98 @brief Adds a new entry to the end of the vector. 99 @param input The new entry 100 @return True if the new entry was successfully added 101 */ 102 bool ConfigValueContainer::add(const std::string& input) 103 { 104 if (this->bIsVector_) 105 return this->set(this->valueVector_.size(), input); 106 107 COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; 108 return false; 109 } 110 111 /** 112 @brief Removes an existing entry from the vector. 113 @param index The index of the entry 114 @return True if the entry was removed 115 */ 116 bool ConfigValueContainer::remove(unsigned int index) 117 { 118 if (this->bIsVector_) 119 { 120 if (index < this->valueVector_.size()) 121 { 122 this->valueVector_.erase(this->valueVector_.begin() + index); 123 for (unsigned int i = index; i < this->valueVector_.size(); i++) 124 ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i]); 125 ConfigFileManager::getSingleton()->deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->valueVector_.size()); 126 127 return true; 128 } 129 COUT(1) << "Error: Invalid vector-index." << std::endl; 130 } 131 132 COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; 133 return false; 60 134 } 61 135 … … 67 141 bool ConfigValueContainer::set(const std::string& input) 68 142 { 143 if (this->bIsVector_) 144 { 145 SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0'); 146 int index = -1; 147 bool success = false; 148 149 if (token.size() > 0) 150 success = ConvertValue(&index, token[0]); 151 152 if (!success || index < 0 || index > MAX_VECTOR_INDEX) 153 { 154 if (!success) 155 COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl; 156 else 157 COUT(1) << "Error: Invalid vector-index." << std::endl; 158 return false; 159 } 160 161 if (token.size() >= 2) 162 return this->set(index, token.subSet(1).join()); 163 else 164 return this->set(index, ""); 165 } 166 69 167 bool success = this->tset(input); 70 this->setLineInConfigFile(input);168 ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input); 71 169 return success; 170 } 171 172 /** 173 @brief Assigns a new value to the config-value of all objects and writes the change into the config-file. 174 @param index The index in the vector 175 @param input The new value 176 @return True if the new value was successfully assigned 177 */ 178 bool ConfigValueContainer::set(unsigned int index, const std::string& input) 179 { 180 if (this->bIsVector_) 181 { 182 bool success = this->tset(index, input); 183 ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, index, input); 184 return success; 185 } 186 187 COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; 188 return false; 72 189 } 73 190 … … 86 203 87 204 /** 205 @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary). 206 @param index The index in the vector 207 @param input The new value 208 @return True if the new value was successfully assigned 209 */ 210 bool ConfigValueContainer::tset(unsigned int index, const std::string& input) 211 { 212 if (this->bIsVector_) 213 { 214 bool success = this->parse(index, input); 215 if (this->identifier_) 216 this->identifier_->updateConfigValues(); 217 return success; 218 } 219 220 COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; 221 return false; 222 } 223 224 /** 88 225 @brief Sets the value of the variable back to the default value and resets the config-file entry. 89 226 */ 90 227 bool ConfigValueContainer::reset() 91 228 { 92 return this->set(this->defvalueString_); 229 if (!this->bIsVector_) 230 return this->set(this->defvalueString_); 231 else 232 { 233 bool success = true; 234 for (unsigned int i = 0; i < this->defvalueStringVector_.size(); i++) 235 if (!this->set(i, this->defvalueStringVector_[i])) 236 success = false; 237 ConfigFileManager::getSingleton()->deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->defvalueStringVector_.size()); 238 return success; 239 } 93 240 } 94 241 … … 98 245 void ConfigValueContainer::update() 99 246 { 100 this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_)); 247 if (!this->bIsVector_) 248 this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_)); 249 else 250 { 251 this->valueVector_.clear(); 252 for (unsigned int i = 0; i < ConfigFileManager::getSingleton()->getVectorSize(this->type_, this->sectionname_, this->varname_); i++) 253 { 254 this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, this->defvalueStringVector_[i])); 255 this->valueVector_.push_back(this->value_); 256 } 257 } 101 258 } 102 259 … … 108 265 bool ConfigValueContainer::parse(const std::string& input) 109 266 { 267 if (this->bIsVector_) 268 { 269 SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0'); 270 int index = -1; 271 bool success = false; 272 273 if (token.size() > 0) 274 success = ConvertValue(&index, token[0]); 275 276 if (!success || index < 0 || index > MAX_VECTOR_INDEX) 277 return false; 278 279 if (token.size() >= 2) 280 return this->parse(index, token.subSet(1).join()); 281 else 282 return this->parse(index, ""); 283 } 284 110 285 MultiTypeMath temp = this->value_; 111 286 if (temp.fromString(input)) … … 114 289 return true; 115 290 } 291 return false; 292 } 293 294 /** 295 @brief Parses a given std::string into a value of the type of the associated variable and assigns it. 296 @param index The index in the vector 297 @param input The string to convert 298 @return True if the string was successfully parsed 299 */ 300 bool ConfigValueContainer::parse(unsigned int index, const std::string& input) 301 { 302 if (this->bIsVector_) 303 { 304 if (index >= this->valueVector_.size()) 305 { 306 for (unsigned int i = this->valueVector_.size(); i <= index; i++) 307 { 308 this->valueVector_.push_back(MultiTypeMath()); 309 ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i]); 310 } 311 } 312 313 MultiTypeMath temp = this->value_; 314 if (temp.fromString(input)) 315 { 316 this->valueVector_[index] = temp; 317 return true; 318 } 319 return false; 320 } 321 322 COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; 116 323 return false; 117 324 } … … 125 332 bool ConfigValueContainer::parse(const std::string& input, const MultiTypeMath& defvalue) 126 333 { 127 MultiTypeMath temp = defvalue; 128 if (temp.fromString(input)) 129 { 130 this->value_ = temp; 334 if (this->parse(input)) 131 335 return true; 132 } 133 else 134 { 135 this->value_ = defvalue; 336 337 this->value_ = defvalue; 338 return false; 339 } 340 341 /** 342 @brief Parses a given std::string into a value of the type of the associated variable and assigns it. 343 @param index The index in the vector 344 @param input The string to convert 345 @param defvalue The default value to assign if the parsing fails 346 @return True if the string was successfully parsed 347 */ 348 bool ConfigValueContainer::parse(unsigned int index, const std::string& input, const MultiTypeMath& defvalue) 349 { 350 if (this->bIsVector_) 351 { 352 if (this->parse(index, input)) 353 return true; 354 355 this->valueVector_[index] = defvalue; 136 356 return false; 137 357 } 138 } 139 140 /** 141 @brief Sets the corresponding entry in the config-file to a given value. 142 */ 143 void ConfigValueContainer::setLineInConfigFile(const std::string& input) 144 { 145 ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input); 146 } 147 148 /** 149 @brief Sets the corresponding entry in the config-file back to the default value. 150 */ 151 void ConfigValueContainer::resetLineInConfigFile() 152 { 153 this->setLineInConfigFile(this->value_.toString()); 358 359 COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl; 360 return false; 154 361 } 155 362
Note: See TracChangeset
for help on using the changeset viewer.