1 | // Scintilla source code edit control |
---|---|
2 | /** @file PropSetSimple.cxx |
3 | ** A basic string to string map. |
4 | **/ |
5 | // Copyright 1998-2010 by Neil Hodgson <neilh@scintilla.org> |
6 | // The License.txt file describes the conditions under which this software may be distributed. |
7 | |
8 | // Maintain a dictionary of properties |
9 | |
10 | #include <cstdlib> |
11 | #include <cstring> |
12 | |
13 | #include <string> |
14 | #include <string_view> |
15 | #include <map> |
16 | #include <functional> |
17 | |
18 | #include "PropSetSimple.h" |
19 | |
20 | using namespace Lexilla; |
21 | |
22 | namespace { |
23 | |
24 | typedef std::map<std::string, std::string, std::less<>> mapss; |
25 | |
26 | mapss *PropsFromPointer(void *impl) noexcept { |
27 | return static_cast<mapss *>(impl); |
28 | } |
29 | |
30 | } |
31 | |
32 | PropSetSimple::PropSetSimple() { |
33 | mapss *props = new mapss; |
34 | impl = static_cast<void *>(props); |
35 | } |
36 | |
37 | PropSetSimple::~PropSetSimple() { |
38 | mapss *props = PropsFromPointer(impl); |
39 | delete props; |
40 | impl = nullptr; |
41 | } |
42 | |
43 | bool PropSetSimple::Set(std::string_view key, std::string_view val) { |
44 | mapss *props = PropsFromPointer(impl); |
45 | if (!props) |
46 | return false; |
47 | mapss::iterator it = props->find(key); |
48 | if (it != props->end()) { |
49 | if (val == it->second) |
50 | return false; |
51 | it->second = val; |
52 | } else { |
53 | props->emplace(key, val); |
54 | } |
55 | return true; |
56 | } |
57 | |
58 | const char *PropSetSimple::Get(std::string_view key) const { |
59 | mapss *props = PropsFromPointer(impl); |
60 | if (props) { |
61 | mapss::const_iterator keyPos = props->find(key); |
62 | if (keyPos != props->end()) { |
63 | return keyPos->second.c_str(); |
64 | } |
65 | } |
66 | return ""; |
67 | } |
68 | |
69 | int PropSetSimple::GetInt(std::string_view key, int defaultValue) const { |
70 | const char *val = Get(key); |
71 | if (*val) { |
72 | return atoi(val); |
73 | } |
74 | return defaultValue; |
75 | } |
76 |