Identifier.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <OpenKarto/Identifier.h>
19 #include <OpenKarto/Exception.h>
20 
21 namespace karto
22 {
23 
25  {
26  }
27 
28  Identifier::Identifier(const char* pString)
29  {
30  Parse(pString);
31  }
32 
34  {
35  Parse(rString);
36  }
37 
39  {
40  Parse(rOther.ToString());
41  }
42 
44  {
45  }
46 
47  const String& Identifier::GetName() const
48  {
49  return m_Name;
50  }
51 
52  void Identifier::SetName(const String& rName)
53  {
54  if (rName.Size() != 0)
55  {
56  std::string name(rName.ToCString());
57 
58  std::string::size_type pos = name.find_last_of('/');
59  if (pos != 0 && pos != std::string::npos)
60  {
61  throw Exception("Name can't contain a scope!");
62  }
63 
64  m_Name = rName;
65  }
66  else
67  {
68  m_Name.Clear();
69  }
70 
71  Update();
72  }
73 
75  {
76  return m_Scope;
77  }
78 
79  void Identifier::SetScope(const String& rScope)
80  {
81  if (rScope.Size() != 0)
82  {
83  m_Scope = rScope;
84  }
85  else
86  {
87  m_Scope.Clear();
88  }
89 
90  Update();
91  }
92 
94  {
95  return m_FullName;
96  }
97 
99  {
100  m_Name.Clear();
101  m_Scope.Clear();
102  m_FullName.Clear();
103  }
104 
105  void Identifier::Parse(const String& rString)
106  {
107  if (rString.Size() == 0)
108  {
109  Clear();
110  return;
111  }
112 
113  std::string id(rString.ToCString());
114 
115  std::string::size_type pos = id.find_last_of('/');
116 
117  if (pos == std::string::npos)
118  {
119  m_Name = rString;
120  }
121  else
122  {
123  m_Scope = rString.SubString(0, pos);
124  m_Name = rString.SubString(pos+1, rString.Size());
125 
126  // remove '/' from m_Scope if first!!
127  if (m_Scope.Size() > 0 && m_Scope[0] == '/')
128  {
130  }
131  }
132 
133  Update();
134  }
135 
136  void Identifier::Validate(const String& rString)
137  {
138  if (rString.Size() == 0)
139  {
140  return;
141  }
142 
143  std::string id(rString.ToCString());
144 
145  char c = id[0];
146  if (IsValidFirst(c))
147  {
148  for (size_t i = 1; i < id.size(); ++i)
149  {
150  c = id[i];
151  if (!IsValid(c))
152  {
153  throw Exception("Invalid character in name. Valid characters must be within the ranges A-Z, a-z, 0-9, '/', '_' and '-'.");
154  }
155  }
156  }
157  else
158  {
159  throw Exception("Invalid first character in name. Valid characters must be within the ranges A-Z, a-z, and '/'.");
160  }
161  }
162 
164  {
165  m_FullName.Clear();
166 
167  if (m_Scope.Size() > 0)
168  {
169  m_FullName.Append("/");
171  m_FullName.Append("/");
172  }
174  }
175 
177  {
178  if (&rOther != this)
179  {
180  m_Name = rOther.m_Name;
181  m_Scope = rOther.m_Scope;
182  m_FullName = rOther.m_FullName;
183  }
184 
185  return *this;
186  }
187 
189  {
190  return (m_FullName == rOther.m_FullName);
191  }
192 
194  {
195  return m_FullName < rOther.m_FullName;
196  }
197 
199  {
200  return m_FullName.Size();
201  }
202 
203 }
void Append(const String &rString)
Definition: String.cpp:82
virtual ~Identifier()
Definition: Identifier.cpp:43
bool kt_bool
Definition: Types.h:145
std::size_t kt_size_t
Definition: Types.h:138
String SubString(kt_size_t index) const
Definition: String.cpp:87
const String & GetName() const
Definition: Identifier.cpp:47
void Validate(const String &rString)
Definition: Identifier.cpp:136
void SetName(const String &pName)
Definition: Identifier.cpp:52
kt_bool operator==(const Identifier &rOther) const
Definition: Identifier.cpp:188
Identifier & operator=(const Identifier &rOther)
Definition: Identifier.cpp:176
kt_size_t Size() const
Definition: Identifier.cpp:198
kt_bool operator<(const Identifier &rOther) const
Definition: Identifier.cpp:193
kt_bool IsValidFirst(char c)
Definition: Identifier.h:169
const String & ToString() const
Definition: Identifier.cpp:93
void SetScope(const String &rScope)
Definition: Identifier.cpp:79
void Parse(const String &rString)
Definition: Identifier.cpp:105
void Clear()
Definition: String.cpp:122
const String & GetScope() const
Definition: Identifier.cpp:74
kt_bool IsValid(char c)
Definition: Identifier.h:179
Definition: Any.cpp:20
const char * ToCString() const
Definition: String.cpp:72
kt_size_t Size() const
Definition: String.cpp:77


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36