Welcome to mirror list, hosted at ThFree Co, Russian Federation.

brands_holder.cpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18c8e7d7dd7181c108e36a1202a5f25d10c1d7ac (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "indexer/brands_holder.hpp"

#include "indexer/search_string_utils.hpp"

#include "platform/platform.hpp"

#include "coding/reader.hpp"
#include "coding/reader_streambuf.hpp"
#include "coding/string_utf8_multilang.hpp"

#include "base/logging.hpp"

#include <sstream>
#include <utility>

#include "defines.hpp"

using namespace std;

namespace
{
enum class State
{
  ParseBrand,
  ParseLanguages
};
}  // namespace

namespace indexer
{
bool BrandsHolder::Brand::Name::operator==(BrandsHolder::Brand::Name const & rhs) const
{
  return m_locale == rhs.m_locale && m_name == rhs.m_name;
}

bool BrandsHolder::Brand::Name::operator<(BrandsHolder::Brand::Name const & rhs) const
{
  if (m_locale != rhs.m_locale)
    return m_locale < rhs.m_locale;
  return m_name < rhs.m_name;
}

BrandsHolder::BrandsHolder(unique_ptr<Reader> && reader)
{
  ReaderStreamBuf buffer(move(reader));
  istream s(&buffer);
  LoadFromStream(s);
}

void BrandsHolder::ForEachNameByKey(std::string const & key,
                                    std::function<void(Brand::Name const &)> const & toDo) const
{
  auto const it = m_keyToName.find(key);
  if (it == m_keyToName.end())
    return;

  for (auto const & name : it->second->m_synonyms)
    toDo(name);
}

void BrandsHolder::ForEachNameByKeyAndLang(std::string const & key, std::string const & lang,
                                           std::function<void(std::string const &)> const & toDo) const
{
  int8_t const locale = StringUtf8Multilang::GetLangIndex(lang);
  ForEachNameByKey(key, [&](Brand::Name const & name) {
    if (name.m_locale == locale)
      toDo(name.m_name);
  });
}

void BrandsHolder::LoadFromStream(istream & s)
{
  m_keys.clear();
  m_keyToName.clear();

  State state = State::ParseBrand;
  string line;
  Brand brand;
  string key;

  char const kKeyPrefix[] = "brand.";
  size_t const kKeyPrefixLength = strlen(kKeyPrefix);

  int lineNumber = 0;
  while (s.good())
  {
    ++lineNumber;
    getline(s, line);
    strings::Trim(line);
    // Allow comments starting with '#' character.
    if (!line.empty() && line[0] == '#')
      continue;

    strings::SimpleTokenizer iter(line, state == State::ParseBrand ? "|" : ":|");

    if (state == State::ParseBrand)
    {
      if (!iter)
        continue;

      AddBrand(brand, key);

      CHECK_GREATER((*iter).size(), kKeyPrefixLength, (lineNumber, *iter));
      ASSERT_EQUAL((*iter).find(kKeyPrefix), 0, (lineNumber, *iter));
      key = (*iter).substr(kKeyPrefixLength);
      CHECK(!++iter, (lineNumber));
      state = State::ParseLanguages;
    }
    else if (state == State::ParseLanguages)
    {
      if (!iter)
      {
        state = State::ParseBrand;
        continue;
      }

      int8_t const langCode = StringUtf8Multilang::GetLangIndex(*iter);
      CHECK_NOT_EQUAL(langCode, StringUtf8Multilang::kUnsupportedLanguageCode, ());

      while (++iter)
      {
        Brand::Name name(*iter, langCode);

        if (!name.m_name.empty())
          brand.m_synonyms.push_back(name);
      }
    }
  }

  // Add last brand.
  AddBrand(brand, key);
}

void BrandsHolder::AddBrand(Brand & brand, string const & key)
{
  if (key.empty())
    return;

  CHECK(!brand.m_synonyms.empty(), ());

  shared_ptr<Brand> p(new Brand());
  p->Swap(brand);

  m_keyToName.emplace(key, p);
  CHECK(m_keys.insert(key).second, ("Key duplicate", key));
}

string DebugPrint(BrandsHolder::Brand::Name const & name)
{
  std::ostringstream out;
  out << "BrandName[" << StringUtf8Multilang::GetLangByCode(name.m_locale) << ", " << name.m_name
      << "]";
  return out.str();
}

BrandsHolder const & GetDefaultBrands()
{
  static BrandsHolder const instance(GetPlatform().GetReader(SEARCH_BRAND_CATEGORIES_FILE_NAME));
  return instance;
}
}  // namespace indexer