OptionEntry.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <common/OptionEntry.h>
00022 #include <common/Defines.h>
00023 #include <common/Logger.h>
00024 #include <XML/XMLFile.h>
00025 #include <stdio.h>
00026 #include <map>
00027 
00028 OptionEntry::OptionEntry(std::list<OptionEntry *> &group,
00029                                                  const char *name,
00030                                                  const char *description,
00031                                                  unsigned int data) :
00032         name_(name),
00033         description_(description),
00034         data_(data),
00035         changedValue_(false)
00036 {
00037         group.push_back(this);
00038 }
00039 
00040 OptionEntry::~OptionEntry()
00041 {
00042 
00043 }
00044 
00045 bool OptionEntryHelper::addToArgParser(std::list<OptionEntry *> &options,
00046                                                                            ARGParser &parser)
00047 {
00048         std::list<OptionEntry *>::iterator itor;
00049         for (itor = options.begin();
00050                 itor != options.end();
00051                 itor++)
00052         {
00053                 if (!(*itor)->addToArgParser(parser)) return false;
00054         }
00055 
00056         return true;
00057 }
00058 
00059 bool OptionEntryHelper::writeToBuffer(std::list<OptionEntry *> &options,
00060                                                                           NetBuffer &buffer,
00061                                                                           bool useprotected)
00062 {
00063         // Write out all of the options
00064         // We do this as strings this make the message larger than it needs to be but
00065         // we always know how to read the options at the other end.
00066         // Even if the other end does not have all the options this end does.
00067         std::list<OptionEntry *>::iterator itor;
00068         for (itor = options.begin();
00069                 itor != options.end();
00070                 itor++)
00071         {
00072                 OptionEntry *entry = *itor;
00073                 // Only send data that is allowed to be sent
00074                 if (!(entry->getData() & OptionEntry::DataProtected) || useprotected)
00075                 {
00076                         // Optimization, only send options that have changed from thier default
00077                         if (!entry->isDefaultValue())
00078                         {
00079                                 buffer.addToBuffer(entry->getName());
00080                                 buffer.addToBuffer(entry->getComsBufferValue());
00081                         }
00082                 }
00083         }
00084         buffer.addToBuffer("-"); // Marks the end of the entries
00085 
00086         return true;
00087 }
00088 
00089 bool OptionEntryHelper::readFromBuffer(std::list<OptionEntry *> &options,
00090                                                                            NetBufferReader &reader,
00091                                                                            bool useprotected)
00092 {
00093         // Create a map from string name to existing options
00094         // So we can find the named option when reading the buffer
00095         std::map<std::string, OptionEntry *> entryMap;
00096         std::list<OptionEntry *>::iterator itor;
00097         for (itor = options.begin();
00098                 itor != options.end();
00099                 itor++)
00100         {
00101                 OptionEntry *entry = (*itor);
00102                 if (!(entry->getData() & OptionEntry::DataProtected) || useprotected)
00103                 {
00104                         // Create map
00105                         std::string name = entry->getName();
00106                         entryMap[name] = entry;
00107 
00108                         // Reset to the default value as only the non-default values are sent
00109                         if (!entry->isDefaultValue())
00110                         {
00111                                 entry->setValueFromString(entry->getDefaultValueAsString());
00112                         }
00113                 }
00114         }
00115 
00116         // Read the strings from the other end
00117         for (;;)
00118         {
00119                 std::string name, value;
00120                 if (!reader.getFromBuffer(name)) return false;
00121                 if (strcmp(name.c_str(), "-") == 0) break; // The end symbol
00122                 if (!reader.getFromBuffer(value)) return false;
00123                 
00124                 std::map<std::string, OptionEntry *>::iterator finditor =
00125                         entryMap.find(name);
00126                 if (finditor == entryMap.end())
00127                 {
00128                         Logger::log(S3D::formatStringBuffer("Warning:Does not support server option \"%s\"",
00129                                 name.c_str()));
00130                 }
00131                 else
00132                 {
00133                         OptionEntry *entry = (*finditor).second;
00134                         if (!entry->setComsBufferValue(value.c_str())) return false;
00135                 }
00136         }
00137 
00138         return true;
00139 }
00140 
00141 bool OptionEntryHelper::writeToXML(std::list<OptionEntry *> &options,
00142         XMLNode *node)
00143 {
00144         std::list<OptionEntry *>::iterator itor;
00145         for (itor = options.begin();
00146                 itor != options.end();
00147                 itor++)
00148         {
00149                 OptionEntry *entry = (*itor);
00150 
00151                 if (!(entry->getData() & OptionEntry::DataDepricated))
00152                 {
00153                         // Add the comments for this node
00154                         node->addChild(new XMLNode("", 
00155                                 entry->getDescription(), XMLNode::XMLCommentType));
00156                         std::string defaultValue = "(default value : \"";
00157                         defaultValue += entry->getDefaultValueAsString();
00158                         defaultValue += "\")";
00159                         node->addChild(new XMLNode("", 
00160                                 defaultValue.c_str(), XMLNode::XMLCommentType));
00161                         
00162                         // Add the name and value
00163                         XMLNode *optionNode = new XMLNode("option");
00164                         node->addChild(optionNode);
00165                         optionNode->addChild(
00166                                 new XMLNode("name", entry->getName()));
00167                         optionNode->addChild(
00168                                 new XMLNode("value",entry->getValueAsString()));
00169                 }
00170         }
00171         return true;
00172 }
00173 
00174 bool OptionEntryHelper::writeToFile(std::list<OptionEntry *> &options,
00175                                                                         const std::string &filePath)
00176 {
00177         XMLNode optionsNode("options");
00178         optionsNode.addParameter(
00179                 new XMLNode("source", "Scorched3D", XMLNode::XMLParameterType));
00180         writeToXML(options, &optionsNode);
00181 
00182         if (!optionsNode.writeToFile(filePath.c_str())) return false;
00183 
00184         return true;
00185 }
00186 
00187 bool OptionEntryHelper::readFromXML(std::list<OptionEntry *> &options,
00188                                                                          XMLNode *rootnode)
00189 {
00190         // Create a map from string name to existing options
00191         // So we can find the named option when parsing the file
00192         std::map<std::string, OptionEntry *> entryMap;
00193         std::list<OptionEntry *>::iterator itor;
00194         for (itor = options.begin();
00195                 itor != options.end();
00196                 itor++)
00197         {
00198                 OptionEntry *entry = (*itor);
00199                 std::string name = entry->getName();
00200                 _strlwr((char *) name.c_str());
00201 
00202                 entryMap[name] = entry;
00203         }
00204 
00205         // Itterate all of the options in the file
00206         XMLNode *currentNode = 0;
00207         while (rootnode->getNamedChild("option", currentNode, false))
00208         {
00209                 XMLNode *nameNode, *valueNode; 
00210                 if (!currentNode->getNamedChild("name", nameNode)) return false;
00211                 if (!currentNode->getNamedChild("value", valueNode)) return false;
00212 
00213                 std::string name = nameNode->getContent();
00214                 std::string value = valueNode->getContent();
00215                 _strlwr((char *) name.c_str());
00216 
00217                 std::map<std::string, OptionEntry *>::iterator finditor =
00218                         entryMap.find(name);
00219                 if (finditor == entryMap.end())
00220                 {
00221                         Logger::log(S3D::formatStringBuffer(
00222                                 "ERROR: Failed to parse file \"%s\". Cannot find option \"%s\"",
00223                                 currentNode->getSource(), name.c_str()));
00224                 }
00225                 else
00226                 {
00227                         if (!(*finditor).second->setValueFromString(value))
00228                         {
00229                                 S3D::dialogMessage("Scorched3D Options", S3D::formatStringBuffer(
00230                                         "ERROR: Failed to parse file \"%s\"\n"
00231                                         "Failed to set option \"%s\" with \"%s\"",
00232                                         currentNode->getSource(), name.c_str(), value.c_str()));
00233                                 return false;           
00234                         }       
00235                 }
00236         }
00237         return true;
00238 }                                                                
00239 
00240 bool OptionEntryHelper::readFromFile(std::list<OptionEntry *> &options,
00241                                                                          const std::string &filePath)
00242 {
00243         // Parse the XML file
00244         XMLFile file;
00245         if (!file.readFile(filePath.c_str()))
00246         {
00247                 S3D::dialogMessage("Scorched3D Options", S3D::formatStringBuffer(
00248                         "ERROR: Failed to parse file \"%s\"\n"
00249                         "%s",
00250                         filePath.c_str(),
00251                         file.getParserError()));
00252                 return false;
00253         }
00254 
00255         // return true for an empty file
00256         if (!file.getRootNode()) return true;
00257 
00258         // Read the options from the XML node
00259         if (!readFromXML(options, file.getRootNode())) return false;
00260 
00261         return true;
00262 }
00263 
00264 OptionEntry *OptionEntryHelper::getEntry(
00265         std::list<OptionEntry *> &options, const char *name)
00266 {
00267         std::list<OptionEntry *>::iterator itor;
00268         for (itor = options.begin();
00269                 itor != options.end();
00270                 itor++)
00271         {
00272                 OptionEntry *entry = (*itor);
00273                 if (0 == strcmp(entry->getName(), name)) return entry;
00274         }
00275         return 0;
00276 }
00277 
00278 OptionEntryInt::OptionEntryInt(std::list<OptionEntry *> &group,
00279                                                            const char *name,
00280                                                            const char *description,
00281                                                            unsigned int data,
00282                                                            int value) :
00283         OptionEntry(group, name, description, data), 
00284         value_(value), defaultValue_(value)
00285 {
00286         
00287 }
00288 
00289 OptionEntryInt::~OptionEntryInt()
00290 {
00291 
00292 }
00293 
00294 const char *OptionEntryInt::getValueAsString()
00295 {
00296         static char value[256];
00297         snprintf(value, 256, "%i", value_);
00298         return value;
00299 }
00300 
00301 const char *OptionEntryInt::getDefaultValueAsString()
00302 {
00303         static char value[256];
00304         snprintf(value, 256, "%i", defaultValue_);
00305         return value;
00306 }
00307 
00308 bool OptionEntryInt::setValueFromString(const std::string &string)
00309 {
00310         int val;
00311         if (sscanf(string.c_str(), "%i", &val) != 1) return false;
00312         return setValue(val);
00313 }
00314 
00315 int OptionEntryInt::getValue() 
00316 {
00317         return value_;
00318 }
00319 
00320 bool OptionEntryInt::setValue(int value)
00321 {
00322         changedValue_ = true;
00323         value_ = value;
00324         return true;
00325 }
00326 
00327 bool OptionEntryInt::setIntArgument(int value)
00328 {
00329         return setValue(value);
00330 }
00331 
00332 bool OptionEntryInt::addToArgParser(ARGParser &parser)
00333 {
00334         char name[256], description[1024];
00335         snprintf(name, 256, "-%s", getName());
00336         snprintf(description, 1024, "%s (Default : %s)",
00337                         (char *) getDescription(), 
00338                         (char *) getDefaultValueAsString());
00339 
00340         parser.addEntry(name , this, description);
00341         return true;
00342 }
00343 
00344 OptionEntryBoundedInt::OptionEntryBoundedInt(std::list<OptionEntry *> &group,
00345                                                                                          const char *name,
00346                                                                                          const char *description,
00347                                                                                          unsigned int data,
00348                                                                                          int value,
00349                                                                                          int minValue, int maxValue, int stepValue) :
00350         OptionEntryInt(group, name, description, data, value), 
00351         minValue_(minValue), maxValue_(maxValue), stepValue_(stepValue)
00352 {
00353 }
00354 
00355 OptionEntryBoundedInt::~OptionEntryBoundedInt()
00356 {
00357 }
00358 
00359 const char *OptionEntryBoundedInt::getDescription()
00360 {
00361         static std::string result;
00362 
00363         result = description_;
00364         result += S3D::formatStringBuffer(" (Max = %i, Min = %i)", getMaxValue(), getMinValue());
00365 
00366         return result.c_str();
00367 }
00368 
00369 bool OptionEntryBoundedInt::setValue(int value)
00370 {
00371         if (value < minValue_ || value > maxValue_) return false;
00372         return OptionEntryInt::setValue(value);
00373 }
00374 
00375 OptionEntryEnum::OptionEntryEnum(std::list<OptionEntry *> &group,
00376                                                                                          const char *name,
00377                                                                                          const char *description,
00378                                                                                          unsigned int data,
00379                                                                                          int value,
00380                                                                                          OptionEntryEnum::EnumEntry enums[]) :
00381         OptionEntryInt(group, name, description, data, value), 
00382         enums_(enums)
00383 {       
00384 }
00385 
00386 OptionEntryEnum::~OptionEntryEnum()
00387 {
00388 }
00389 
00390 const char *OptionEntryEnum::getDescription()
00391 {
00392         static std::string result;
00393 
00394         result = description_;
00395         result += " ( possible values = [";
00396         for (EnumEntry *current = enums_; current->description[0]; current++)
00397         {
00398                 result += S3D::formatStringBuffer(" \"%s\" ",
00399                         current->description, current->value);
00400         }
00401         result += "] )";
00402 
00403         return result.c_str();
00404 }
00405 
00406 bool OptionEntryEnum::setValue(int value)
00407 {
00408         for (EnumEntry *current = enums_; current->description[0]; current++)
00409         {
00410                 if (current->value == value)
00411                 {
00412                         return OptionEntryInt::setValue(value);
00413                 }
00414         }
00415         return false;
00416 }
00417 
00418 const char *OptionEntryEnum::getDefaultValueAsString()
00419 {
00420         for (EnumEntry *current = enums_; current->description[0]; current++)
00421         {
00422                 if (current->value == defaultValue_)
00423                 {
00424                         return current->description;
00425                 }
00426         }
00427         return "-";
00428 }
00429 
00430 const char *OptionEntryEnum::getValueAsString()
00431 {
00432         for (EnumEntry *current = enums_; current->description[0]; current++)
00433         {
00434                 if (current->value == value_)
00435                 {
00436                         return current->description;
00437                 }
00438         }
00439         return "-";
00440 }
00441 
00442 bool OptionEntryEnum::setValueFromString(const std::string &string)
00443 {
00444         for (EnumEntry *current = enums_; current->description[0]; current++)
00445         {
00446                 if (0 == strcmp(current->description, string.c_str()))
00447                 {
00448                         setValue(current->value);
00449                         return true;
00450                 }
00451         }
00452 
00453         int val;
00454         if (sscanf(string.c_str(), "%i", &val) != 1) return false;
00455         return setValue(val);
00456 }
00457 
00458 OptionEntryBool::OptionEntryBool(std::list<OptionEntry *> &group,
00459                                                                  const char *name, 
00460                                                                  const char *description, 
00461                                                                  unsigned int data,
00462                                                                  bool value) :
00463         OptionEntry(group, name, description, data), 
00464         value_(value), defaultValue_(value)
00465 {
00466         
00467 }
00468 
00469 OptionEntryBool::~OptionEntryBool()
00470 {
00471         
00472 }
00473 
00474 const char *OptionEntryBool::getDefaultValueAsString()
00475 {
00476         return (defaultValue_?"on":"off");
00477 }
00478 
00479 const char *OptionEntryBool::getValueAsString()
00480 {
00481         return (value_?"on":"off");
00482 }
00483 
00484 bool OptionEntryBool::setValueFromString(const std::string &string)
00485 {
00486         if (stricmp("on", string.c_str()) == 0)
00487         {
00488                 setValue(true);
00489                 return true;
00490         }
00491         if (stricmp("off", string.c_str()) == 0)
00492         {
00493                 setValue(false);
00494                 return true;
00495         }
00496         return false;
00497 }
00498 
00499 bool OptionEntryBool::setValue(bool value)
00500 {
00501         changedValue_ = true;
00502         value_ = (value?1:0);
00503         return true;
00504 }
00505 
00506 bool OptionEntryBool::getValue()
00507 {
00508         return ((value_==0)?false:true);
00509 }
00510 
00511 bool OptionEntryBool::setBoolArgument(bool value)
00512 {
00513         return setValue(value);
00514 }
00515 
00516 bool OptionEntryBool::addToArgParser(ARGParser &parser)
00517 {
00518         char name[256], description[1024];
00519         snprintf(name, 256, "-%s", getName());
00520         snprintf(description, 1024, "%s (Default : %s)",
00521                         (char *) getDescription(), 
00522                         (char *) getDefaultValueAsString());
00523 
00524         parser.addEntry(name , this, description);
00525         return true;
00526 }
00527 
00528 OptionEntryString::OptionEntryString(std::list<OptionEntry *> &group,
00529                                                                          const char *name, 
00530                                                                          const char *description, 
00531                                                                          unsigned int data,
00532                                                                          const char *value,
00533                                                                          bool multiline) :
00534         OptionEntry(group, name, description, data),
00535         value_(value), defaultValue_(value), multiline_(multiline)
00536 {
00537         
00538 }
00539 
00540 OptionEntryString::~OptionEntryString()
00541 {
00542 
00543 }
00544 
00545 const char *OptionEntryString::getValueAsString()
00546 {
00547         return value_.c_str();
00548 }
00549 
00550 const char *OptionEntryString::getDefaultValueAsString()
00551 {
00552         return defaultValue_.c_str();
00553 }
00554 
00555 bool OptionEntryString::setValueFromString(const std::string &string)
00556 {
00557         return setValue(string);
00558 }
00559 
00560 const char *OptionEntryString::getValue() 
00561 {
00562         return value_.c_str();
00563 }
00564 
00565 bool OptionEntryString::setValue(const std::string &value)
00566 {
00567         changedValue_ = true;
00568         value_ = value;
00569         return true;
00570 }
00571 
00572 bool OptionEntryString::setStringArgument(const char* value)
00573 {
00574         return setValue(value);
00575 }
00576 
00577 bool OptionEntryString::addToArgParser(ARGParser &parser)
00578 {
00579         char name[256], description[1024];
00580         snprintf(name, 256, "-%s", getName());
00581         snprintf(description, 1024, "%s (Default : %s)",
00582                         (char *) getDescription(), 
00583                         (char *) getDefaultValueAsString());
00584 
00585         parser.addEntry(name , this, description);
00586         return true;
00587 }
00588 
00589 OptionEntryStringEnum::OptionEntryStringEnum(std::list<OptionEntry *> &group,
00590                                                                                          const char *name,
00591                                                                                          const char *description,
00592                                                                                          unsigned int data,
00593                                                                                          const char *value,
00594                                                                                          OptionEntryStringEnum::EnumEntry enums[]) :
00595         OptionEntryString(group, name, description, data, value), 
00596         enums_(enums)
00597 {       
00598 }
00599 
00600 OptionEntryStringEnum::~OptionEntryStringEnum()
00601 {
00602 }
00603 
00604 const char *OptionEntryStringEnum::getDescription()
00605 {
00606         static std::string result;
00607 
00608         result = description_;
00609         for (EnumEntry *current = enums_; current->value[0]; current++)
00610         {
00611                 result += S3D::formatStringBuffer(" (\"%s\")",
00612                         current->value);
00613         }
00614 
00615         return result.c_str();
00616 }
00617 
00618 bool OptionEntryStringEnum::setValue(const std::string &value)
00619 {
00620         for (EnumEntry *current = enums_; current->value[0]; current++)
00621         {
00622                 if (0 == strcmp(current->value, value.c_str()))
00623                 {
00624                         return OptionEntryString::setValue(value);
00625                 }
00626         }
00627         return false;
00628 }
00629 
00630 bool OptionEntryStringEnum::setValueFromString(const std::string &string)
00631 {
00632         return setValue(string);
00633 }
00634 
00635 OptionEntryFloat::OptionEntryFloat(std::list<OptionEntry *> &group,
00636                                                            const char *name,
00637                                                            const char *description,
00638                                                            unsigned int data,
00639                                                            float value,
00640                                                            bool truncate) :
00641         OptionEntry(group, name, description, data), 
00642         value_(value), defaultValue_(value), truncate_(truncate)
00643 {
00644         
00645 }
00646 
00647 OptionEntryFloat::~OptionEntryFloat()
00648 {
00649 
00650 }
00651 
00652 const char *OptionEntryFloat::getValueAsString()
00653 {
00654         static char value[256];
00655         if (truncate_) snprintf(value, 256, "%.2f", value_);
00656         else snprintf(value, 256, "%.8f", value_);
00657         return value;
00658 }
00659 
00660 const char *OptionEntryFloat::getDefaultValueAsString()
00661 {
00662         static char value[256];
00663         if (truncate_) snprintf(value, 256, "%.2f", defaultValue_);
00664         else snprintf(value, 256, "%.8f", defaultValue_);
00665         return value;
00666 }
00667 
00668 bool OptionEntryFloat::setValueFromString(const std::string &string)
00669 {
00670         float val;
00671         if (sscanf(string.c_str(), "%f", &val) != 1) return false;
00672         return setValue(val);
00673 }
00674 
00675 float OptionEntryFloat::getValue() 
00676 {
00677         return value_;
00678 }
00679 
00680 bool OptionEntryFloat::setValue(float value)
00681 {
00682         changedValue_ = true;
00683         value_ = value;
00684         return true;
00685 }
00686 
00687 bool OptionEntryFloat::addToArgParser(ARGParser &parser)
00688 {
00689         DIALOG_ASSERT(0);
00690         return false;
00691 }
00692 
00693 OptionEntryVector::OptionEntryVector(std::list<OptionEntry *> &group,
00694                                                            const char *name,
00695                                                            const char *description,
00696                                                            unsigned int data,
00697                                                            Vector value,
00698                                                            bool truncate) :
00699         OptionEntry(group, name, description, data), 
00700         value_(value), defaultValue_(value), truncate_(truncate)
00701 {
00702         
00703 }
00704 
00705 OptionEntryVector::~OptionEntryVector()
00706 {
00707 
00708 }
00709 
00710 const char *OptionEntryVector::getValueAsString()
00711 {
00712         static char value[256];
00713         if (truncate_)
00714         {
00715                 snprintf(value, 256, "%.2f %.2f %.2f", 
00716                         value_[0], value_[1], value_[2]);
00717         }
00718         else
00719         {
00720                 snprintf(value, 256, "%.8f %.8f %.8f", 
00721                         value_[0], value_[1], value_[2]);
00722         }
00723         return value;
00724 }
00725 
00726 const char *OptionEntryVector::getDefaultValueAsString()
00727 {
00728         static char value[256];
00729         if (truncate_)
00730         {
00731                 snprintf(value, 256, "%.2f %.2f %.2f", 
00732                         defaultValue_[0], defaultValue_[1], defaultValue_[2]);
00733         }
00734         else
00735         {
00736                 snprintf(value, 256, "%.8f %.8f %.8f", 
00737                         defaultValue_[0], defaultValue_[1], defaultValue_[2]);
00738         }
00739         return value;
00740 }
00741 
00742 bool OptionEntryVector::setValueFromString(const std::string &string)
00743 {
00744         Vector vec;
00745         if (sscanf(string.c_str(), "%f %f %f", 
00746                 &vec[0], &vec[1], &vec[2]) != 3) return false;
00747         return setValue(vec);
00748 }
00749 
00750 Vector &OptionEntryVector::getValue() 
00751 {
00752         return value_;
00753 }
00754 
00755 bool OptionEntryVector::setValue(Vector value)
00756 {
00757         changedValue_ = true;
00758         value_ = value;
00759         return true;
00760 }
00761 
00762 bool OptionEntryVector::addToArgParser(ARGParser &parser)
00763 {
00764         DIALOG_ASSERT(0);
00765         return false;
00766 }
00767 
00768 OptionEntryFixed::OptionEntryFixed(std::list<OptionEntry *> &group,
00769                                                            const char *name,
00770                                                            const char *description,
00771                                                            unsigned int data,
00772                                                            fixed value) :
00773         OptionEntry(group, name, description, data), 
00774         value_(value), defaultValue_(value)
00775 {
00776         
00777 }
00778 
00779 OptionEntryFixed::~OptionEntryFixed()
00780 {
00781 
00782 }
00783 
00784 const char *OptionEntryFixed::getValueAsString()
00785 {
00786         return value_.asString();
00787 }
00788 
00789 const char *OptionEntryFixed::getDefaultValueAsString()
00790 {
00791         return defaultValue_.asString();
00792 }
00793 
00794 bool OptionEntryFixed::setValueFromString(const std::string &string)
00795 {
00796         return setValue(fixed(string.c_str()));
00797 }
00798 
00799 fixed OptionEntryFixed::getValue() 
00800 {
00801         return value_;
00802 }
00803 
00804 bool OptionEntryFixed::setValue(fixed value)
00805 {
00806         changedValue_ = true;
00807         value_ = value;
00808         return true;
00809 }
00810 
00811 bool OptionEntryFixed::addToArgParser(ARGParser &parser)
00812 {
00813         DIALOG_ASSERT(0);
00814         return false;
00815 }
00816 
00817 OptionEntryFixedVector::OptionEntryFixedVector(std::list<OptionEntry *> &group,
00818                                                            const char *name,
00819                                                            const char *description,
00820                                                            unsigned int data,
00821                                                            FixedVector value) :
00822         OptionEntry(group, name, description, data), 
00823         value_(value), defaultValue_(value)
00824 {
00825         
00826 }
00827 
00828 OptionEntryFixedVector::~OptionEntryFixedVector()
00829 {
00830 
00831 }
00832 
00833 const char *OptionEntryFixedVector::getValueAsString()
00834 {
00835         std::string a = value_[0].asString();
00836         std::string b = value_[1].asString();
00837         std::string c = value_[2].asString();
00838 
00839         static char value[256];
00840         snprintf(value, 256, "%s %s %s", 
00841                 a.c_str(), b.c_str(), c.c_str());
00842         return value;
00843 }
00844 
00845 const char *OptionEntryFixedVector::getDefaultValueAsString()
00846 {
00847         std::string a = defaultValue_[0].asString();
00848         std::string b = defaultValue_[1].asString();
00849         std::string c = defaultValue_[2].asString();
00850 
00851         static char value[256];
00852         snprintf(value, 256, "%s %s %s", 
00853                 a.c_str(), b.c_str(), c.c_str());
00854         return value;
00855 }
00856 
00857 bool OptionEntryFixedVector::setValueFromString(const std::string &string)
00858 {
00859         FixedVector value;
00860 
00861         int i=0;
00862         char *token = strtok((char *) string.c_str(), " ");
00863         while(token != 0)
00864         {
00865                 value[i++] = fixed(token);              
00866                 token = strtok(0, " ");
00867         }
00868 
00869         return setValue(value);
00870 }
00871 
00872 FixedVector &OptionEntryFixedVector::getValue() 
00873 {
00874         return value_;
00875 }
00876 
00877 bool OptionEntryFixedVector::setValue(FixedVector value)
00878 {
00879         changedValue_ = true;
00880         value_ = value;
00881         return true;
00882 }
00883 
00884 bool OptionEntryFixedVector::addToArgParser(ARGParser &parser)
00885 {
00886         DIALOG_ASSERT(0);
00887         return false;
00888 }

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3