Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIXMLAttributes.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002     filename:   CEGUIXMLAttributes.cpp
00003     created:    Sat Mar 12 2005
00004     author:     Paul D Turner
00005 *************************************************************************/
00006 /*************************************************************************
00007     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00008     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00009  
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2.1 of the License, or (at your option) any later version.
00014  
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019  
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 *************************************************************************/
00024 #include "CEGUIXMLAttributes.h"
00025 #include "CEGUIExceptions.h"
00026 #include <sstream>
00027 #include <iterator>
00028 
00029 // Start of CEGUI namespace section
00030 namespace CEGUI
00031 {
00032     XMLAttributes::XMLAttributes(void)
00033     {}
00034 
00035     XMLAttributes::~XMLAttributes(void)
00036     {}
00037 
00038     void XMLAttributes::add(const String& attrName, const String& attrValue)
00039     {
00040         d_attrs[attrName] = attrValue;
00041     }
00042 
00043     void XMLAttributes::remove(const String& attrName)
00044     {
00045         AttributeMap::iterator pos = d_attrs.find(attrName);
00046 
00047         if (pos != d_attrs.end())
00048             d_attrs.erase(pos);
00049     }
00050 
00051     bool XMLAttributes::exists(const String& attrName) const
00052     {
00053         return d_attrs.find(attrName) != d_attrs.end();
00054     }
00055 
00056     size_t XMLAttributes::getCount(void) const
00057     {
00058         return d_attrs.size();
00059     }
00060 
00061     const String& XMLAttributes::getName(size_t index) const
00062     {
00063         if (index >= d_attrs.size())
00064         {
00065             throw InvalidRequestException("XMLAttributes::getName - The specified index is out of range for this XMLAttributes block.");
00066         }
00067 
00068         AttributeMap::const_iterator iter = d_attrs.begin();
00069         std::advance(iter, index);
00070 
00071         return (*iter).first;
00072     }
00073 
00074     const String& XMLAttributes::getValue(size_t index) const
00075     {
00076         if (index >= d_attrs.size())
00077         {
00078             throw InvalidRequestException("XMLAttributes::getValue - The specified index is out of range for this XMLAttributes block.");
00079         }
00080 
00081         AttributeMap::const_iterator iter = d_attrs.begin();
00082         std::advance(iter, index);
00083 
00084         return (*iter).second;
00085     }
00086 
00087     const String& XMLAttributes::getValue(const String& attrName) const
00088     {
00089         AttributeMap::const_iterator pos = d_attrs.find(attrName);
00090 
00091         if (pos != d_attrs.end())
00092         {
00093             return (*pos).second;
00094         }
00095         else
00096         {
00097             throw UnknownObjectException("XMLAttributes::getValue - no value exists for an attribute named '" + attrName + "'.");
00098         }
00099     }
00100 
00101     const String& XMLAttributes::getValueAsString(const String& attrName, const String& def) const
00102     {
00103         return (exists(attrName)) ? getValue(attrName) : def;
00104     }
00105 
00106 
00107     bool XMLAttributes::getValueAsBool(const String& attrName, bool def) const
00108     {
00109         if (!exists(attrName))
00110         {
00111             return def;
00112         }
00113 
00114         const String& val = getValue(attrName);
00115 
00116         if (val == "false" || val == "0")
00117         {
00118             return false;
00119         }
00120         else if (val == "true" || val == "1")
00121         {
00122             return true;
00123         }
00124         else
00125         {
00126             throw InvalidRequestException("XMLAttributes::getValueAsInteger - failed to convert attribute '" + attrName + "' with value '" + getValue(attrName) + "' to bool.");
00127         }
00128     }
00129 
00130     int XMLAttributes::getValueAsInteger(const String& attrName, int def) const
00131     {
00132         if (!exists(attrName))
00133         {
00134             return def;
00135         }
00136 
00137         int val;
00138         std::istringstream strm(getValue(attrName).c_str());
00139 
00140         strm >> val;
00141 
00142         // success?
00143         if (strm.fail())
00144         {
00145             throw InvalidRequestException("XMLAttributes::getValueAsInteger - failed to convert attribute '" + attrName + "' with value '" + getValue(attrName) + "' to integer.");
00146         }
00147 
00148         return val;
00149     }
00150 
00151     float XMLAttributes::getValueAsFloat(const String& attrName, float def) const
00152     {
00153         if (!exists(attrName))
00154         {
00155             return def;
00156         }
00157 
00158         float val;
00159         std::istringstream strm(getValue(attrName).c_str());
00160 
00161         strm >> val;
00162 
00163         // success?
00164         if (strm.fail())
00165         {
00166             throw InvalidRequestException("XMLAttributes::getValueAsInteger - failed to convert attribute '" + attrName + "' with value '" + getValue(attrName) + "' to float.");
00167         }
00168 
00169         return val;
00170     }
00171 
00172 
00173 
00174 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:35 2005 for Crazy Eddies GUI System by  doxygen 1.4.3