| 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 <[email protected]> |
| 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 <cassert> |
| 12 |
#include <cstring> |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <string_view> |
| 16 |
#include <map> |
| 17 |
#include <functional> |
| 18 |
|
| 19 |
#include "PropSetSimple.h" |
| 20 |
|
| 21 |
using namespace Lexilla; |
| 22 |
|
| 23 |
namespace { |
| 24 |
|
| 25 |
using mapss = std::map<std::string, std::string, std::less<>>; |
| 26 |
|
| 27 |
mapss *PropsFromPointer(void *impl) noexcept { |
| 28 |
return static_cast<mapss *>(impl); |
| 29 |
} |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
PropSetSimple::PropSetSimple() { |
| 34 |
mapss *props = new mapss; |
| 35 |
impl = static_cast<void *>(props); |
| 36 |
} |
| 37 |
|
| 38 |
PropSetSimple::~PropSetSimple() { |
| 39 |
mapss *props = PropsFromPointer(impl); |
| 40 |
delete props; |
| 41 |
impl = nullptr; |
| 42 |
} |
| 43 |
|
| 44 |
bool PropSetSimple::Set(std::string_view key, std::string_view val) { |
| 45 |
mapss *props = PropsFromPointer(impl); |
| 46 |
if (!props) |
| 47 |
return false; |
| 48 |
mapss::iterator const it = props->find(key); |
| 49 |
if (it != props->end()) { |
| 50 |
if (val == it->second) |
| 51 |
return false; |
| 52 |
it->second = val; |
| 53 |
} else { |
| 54 |
props->emplace(key, val); |
| 55 |
} |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
const char *PropSetSimple::Get(std::string_view key) const { |
| 60 |
mapss *props = PropsFromPointer(impl); |
| 61 |
if (props) { |
| 62 |
mapss::const_iterator const keyPos = props->find(key); |
| 63 |
if (keyPos != props->end()) { |
| 64 |
return keyPos->second.c_str(); |
| 65 |
} |
| 66 |
} |
| 67 |
return ""; |
| 68 |
} |
| 69 |
|
| 70 |
int PropSetSimple::GetInt(std::string_view key, int defaultValue) const { |
| 71 |
const char *val = Get(key); |
| 72 |
assert(val); |
| 73 |
if (*val) { |
| 74 |
return atoi(val); |
| 75 |
} |
| 76 |
return defaultValue; |
| 77 |
} |