forked from mapsme/omim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icons_info.cpp
73 lines (63 loc) · 1.63 KB
/
icons_info.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "local_ads/icons_info.hpp"
#include "coding/reader.hpp"
#include "platform/platform.hpp"
#include "base/assert.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include <functional>
namespace
{
char const kDelimiter = '_';
void ParseIconsFile(std::string const & iconsFile,
std::function<void(std::string const &)> const & handler)
{
ASSERT(handler != nullptr, ());
try
{
std::string fileData;
ReaderPtr<Reader>(GetPlatform().GetReader(iconsFile)).ReadAsString(fileData);
strings::Tokenize(fileData, "\n", [&handler](std::string const & str) {
if (!str.empty())
handler(str);
});
}
catch (Reader::Exception const & e)
{
LOG(LWARNING, ("Error reading file ", iconsFile, " : ", e.what()));
}
}
} // namespace
namespace local_ads
{
IconsInfo & IconsInfo::Instance()
{
static IconsInfo iconsInfo;
return iconsInfo;
}
void IconsInfo::SetSourceFile(std::string const & fileName)
{
std::map<uint16_t, std::string> icons;
ParseIconsFile(fileName, [&icons](std::string const & icon) {
auto const pos = icon.find(kDelimiter);
if (pos == std::string::npos)
return;
uint32_t index;
if (!strings::to_uint(icon.substr(0, pos), index))
index = 0;
icons[static_cast<uint16_t>(index)] = icon;
});
{
std::lock_guard<std::mutex> lock(m_mutex);
m_fileName = fileName;
std::swap(m_icons, icons);
}
}
std::string IconsInfo::GetIcon(uint16_t index) const
{
std::lock_guard<std::mutex> lock(m_mutex);
auto const it = m_icons.find(index);
if (it == m_icons.end())
return {};
return it->second;
}
} // namespace local_ads