1 /***********************************************************************
2     created:    Sat Mar 12 2005
3     author:     Paul D Turner
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #include "CEGUI/XMLAttributes.h"
28 #include "CEGUI/Exceptions.h"
29 #include <sstream>
30 #include <iterator>
31 
32 // Start of CEGUI namespace section
33 namespace CEGUI
34 {
XMLAttributes(void)35     XMLAttributes::XMLAttributes(void)
36     {}
37 
~XMLAttributes(void)38     XMLAttributes::~XMLAttributes(void)
39     {}
40 
add(const String & attrName,const String & attrValue)41     void XMLAttributes::add(const String& attrName, const String& attrValue)
42     {
43         d_attrs[attrName] = attrValue;
44     }
45 
remove(const String & attrName)46     void XMLAttributes::remove(const String& attrName)
47     {
48         AttributeMap::iterator pos = d_attrs.find(attrName);
49 
50         if (pos != d_attrs.end())
51             d_attrs.erase(pos);
52     }
53 
exists(const String & attrName) const54     bool XMLAttributes::exists(const String& attrName) const
55     {
56         return d_attrs.find(attrName) != d_attrs.end();
57     }
58 
getCount(void) const59     size_t XMLAttributes::getCount(void) const
60     {
61         return d_attrs.size();
62     }
63 
getName(size_t index) const64     const String& XMLAttributes::getName(size_t index) const
65     {
66         if (index >= d_attrs.size())
67         {
68             CEGUI_THROW(InvalidRequestException(
69                 "The specified index is out of range for this XMLAttributes block."));
70         }
71 
72         AttributeMap::const_iterator iter = d_attrs.begin();
73         std::advance(iter, index);
74 
75         return (*iter).first;
76     }
77 
getValue(size_t index) const78     const String& XMLAttributes::getValue(size_t index) const
79     {
80         if (index >= d_attrs.size())
81         {
82             CEGUI_THROW(InvalidRequestException(
83                 "The specified index is out of range for this XMLAttributes block."));
84         }
85 
86         AttributeMap::const_iterator iter = d_attrs.begin();
87         std::advance(iter, index);
88 
89         return (*iter).second;
90     }
91 
getValue(const String & attrName) const92     const String& XMLAttributes::getValue(const String& attrName) const
93     {
94         AttributeMap::const_iterator pos = d_attrs.find(attrName);
95 
96         if (pos != d_attrs.end())
97         {
98             return (*pos).second;
99         }
100         else
101         {
102             CEGUI_THROW(UnknownObjectException(
103                 "no value exists for an attribute named '" + attrName + "'."));
104         }
105     }
106 
getValueAsString(const String & attrName,const String & def) const107     const String& XMLAttributes::getValueAsString(const String& attrName, const String& def) const
108     {
109         return (exists(attrName)) ? getValue(attrName) : def;
110     }
111 
112 
getValueAsBool(const String & attrName,bool def) const113     bool XMLAttributes::getValueAsBool(const String& attrName, bool def) const
114     {
115         if (!exists(attrName))
116         {
117             return def;
118         }
119 
120         const String& val = getValue(attrName);
121 
122         if (val == "false" || val == "False" || val == "0")
123         {
124             return false;
125         }
126         else if (val == "true" || val == "True" || val == "1")
127         {
128             return true;
129         }
130         else
131         {
132             CEGUI_THROW(InvalidRequestException(
133                 "failed to convert attribute '" + attrName + "' with value '" + getValue(attrName) + "' to bool."));
134         }
135     }
136 
getValueAsInteger(const String & attrName,int def) const137     int XMLAttributes::getValueAsInteger(const String& attrName, int def) const
138     {
139         if (!exists(attrName))
140         {
141             return def;
142         }
143 
144         int val;
145         std::istringstream strm(getValue(attrName).c_str());
146 
147         strm >> val;
148 
149         // Check for success and end-of-file
150         if(strm.fail() || !strm.eof())
151         {
152             CEGUI_THROW(InvalidRequestException(
153                 "failed to convert attribute '" + attrName + "' with value '" + getValue(attrName) + "' to integer."));
154         }
155 
156         return val;
157     }
158 
getValueAsFloat(const String & attrName,float def) const159     float XMLAttributes::getValueAsFloat(const String& attrName, float def) const
160     {
161         if (!exists(attrName))
162         {
163             return def;
164         }
165 
166         float val;
167         std::istringstream strm(getValue(attrName).c_str());
168 
169         strm >> val;
170 
171         // Check for success and end-of-file
172         if(strm.fail() || !strm.eof())
173         {
174             CEGUI_THROW(InvalidRequestException(
175                 "failed to convert attribute '" + attrName + "' with value '" + getValue(attrName) + "' to float."));
176         }
177 
178         return val;
179     }
180 
181 
182 
183 } // End of  CEGUI namespace section
184