xoreos  0.0.5
stringmap.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cstring>
26 
27 #include "src/common/stringmap.h"
28 #include "src/common/util.h"
29 
30 namespace Common {
31 
32 StringListMap::StringListMap(const char * const *strings, size_t count, bool onlyFirstWord) : _onlyFirstWord(onlyFirstWord) {
33  for (size_t i = 0; i < count; i++)
34  _map.insert(std::make_pair(UString(strings[i]), i));
35 }
36 
37 size_t StringListMap::find(const char *str, const char **match) const {
38  UString sStr = str;
39 
40  if (_onlyFirstWord) {
41  UString::iterator space = sStr.findFirst(' ');
42  if (space != sStr.end())
43  sStr.truncate(space);
44  }
45 
46  StrMap::const_iterator s = _map.find(sStr);
47  if (s == _map.end())
48  return kInvalidIndex;
49 
50  if (match)
51  *match = str + s->first.size() + 1;
52  return s->second;
53 }
54 
55 size_t StringListMap::find(const UString &str, const char **match) const {
56  UString sStr = str;
57 
58  if (_onlyFirstWord) {
59  UString::iterator space = sStr.findFirst(' ');
60  if (space != sStr.end())
61  sStr.truncate(space);
62  }
63 
64  StrMap::const_iterator s = _map.find(sStr);
65  if (s == _map.end())
66  return kInvalidIndex;
67 
68  if (match)
69  *match = str.c_str() + s->first.size() + 1;
70  return s->second;
71 }
72 
73 } // End of namespace Common
Definition: 2dafile.h:39
A class holding an UTF-8 string.
Definition: ustring.h:48
StringListMap(const char *const *strings, size_t count, bool onlyFirstWord=false)
Build a string map to match a list of strings against.
Definition: stringmap.cpp:32
void truncate(const iterator &it)
Definition: ustring.cpp:343
iterator findFirst(uint32 c) const
Definition: ustring.cpp:261
utf8::iterator< std::string::const_iterator > iterator
Definition: ustring.h:50
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Utility templates and functions.
static const size_t kInvalidIndex
Definition: stringmap.h:45
size_t find(const char *str, const char **match) const
Match a string against the map.
Definition: stringmap.cpp:37
A map to quickly match strings from a list.
iterator end() const
Definition: ustring.cpp:257