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

country.cpp « storage - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9cf2fee815eee52781331fed121dc7b2b61a4b17 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "storage/country.hpp"

#include "platform/platform.hpp"

#include "base/logging.hpp"

#include "3party/jansson/myjansson.hpp"

using platform::CountryFile;

namespace storage
{
uint64_t Country::Size(MapOptions opt) const
{
  uint64_t size = 0;
  for (CountryFile const & file : m_files)
    size += file.GetRemoteSize(opt);
  return size;
}

void Country::AddFile(CountryFile const & file) { m_files.push_back(file); }

////////////////////////////////////////////////////////////////////////

template <class ToDo>
void LoadGroupImpl(int depth, json_t * group, ToDo & toDo)
{
  for (size_t i = 0; i < json_array_size(group); ++i)
  {
    json_t * j = json_array_get(group, i);

    // name is mandatory
    char const * name = json_string_value(json_object_get(j, "n"));
    if (!name)
      MYTHROW(my::Json::Exception, ("Country name is missing"));

    char const * file = json_string_value(json_object_get(j, "f"));
    // if file is empty, it's the same as the name
    if (!file)
      file = name;

    char const * flag = json_string_value(json_object_get(j, "c"));
    toDo(name, file, flag ? flag : "",
         // We expect what mwm and routing files should be less 2Gb
         static_cast<uint32_t>(json_integer_value(json_object_get(j, "s"))),
         static_cast<uint32_t>(json_integer_value(json_object_get(j, "rs"))), depth);

    json_t * children = json_object_get(j, "g");
    if (children)
      LoadGroupImpl(depth + 1, children, toDo);
  }
}

template <class ToDo>
bool LoadCountriesImpl(string const & jsonBuffer, ToDo & toDo)
{
  try
  {
    my::Json root(jsonBuffer.c_str());
    json_t * children = json_object_get(root.get(), "g");
    if (!children)
      MYTHROW(my::Json::Exception, ("Root country doesn't have any groups"));
    LoadGroupImpl(0, children, toDo);
    return true;
  }
  catch (my::Json::Exception const & e)
  {
    LOG(LERROR, (e.Msg()));
    return false;
  }
}

namespace
{
class DoStoreCountries
{
  CountriesContainerT & m_cont;

public:
  DoStoreCountries(CountriesContainerT & cont) : m_cont(cont) {}

  void operator()(string const & name, string const & file, string const & flag, uint32_t mapSize,
                  uint32_t routingSize, int depth)
  {
    Country country(name, flag);
    if (mapSize)
    {
      CountryFile countryFile(file);
      countryFile.SetRemoteSizes(mapSize, routingSize);
      country.AddFile(countryFile);
    }
    m_cont.AddAtDepth(depth, country);
  }
};

class DoStoreFile2Info
{
  map<string, CountryInfo> & m_file2info;
  string m_lastFlag;

public:
  DoStoreFile2Info(map<string, CountryInfo> & file2info) : m_file2info(file2info) {}

  void operator()(string name, string file, string const & flag, uint32_t mapSize, uint32_t, int)
  {
    if (!flag.empty())
      m_lastFlag = flag;

    if (mapSize)
    {
      CountryInfo info;

      // if 'file' is empty - it's equal to 'name'
      if (!file.empty())
      {
        // make compound name: country_region
        size_t const i = file.find_first_of('_');
        if (i != string::npos)
          name = file.substr(0, i) + '_' + name;

        // fill 'name' only when it differs with 'file'
        if (name != file)
          info.m_name.swap(name);
      }
      else
        file.swap(name);

      // Do not use 'name' here! It was swapped!

      ASSERT(!m_lastFlag.empty(), ());
      info.m_flag = m_lastFlag;

      m_file2info[file] = info;
    }
  }
};

class DoStoreCode2File
{
  multimap<string, string> & m_code2file;

public:
  DoStoreCode2File(multimap<string, string> & code2file) : m_code2file(code2file) {}

  void operator()(string const &, string const & file, string const & flag, uint32_t, uint32_t, int)
  {
    m_code2file.insert(make_pair(flag, file));
  }
};
}

int64_t LoadCountries(string const & jsonBuffer, CountriesContainerT & countries)
{
  countries.Clear();

  int64_t version = -1;
  try
  {
    my::Json root(jsonBuffer.c_str());
    version = json_integer_value(json_object_get(root.get(), "v"));
    DoStoreCountries doStore(countries);
    if (!LoadCountriesImpl(jsonBuffer, doStore))
      return -1;
  }
  catch (my::Json::Exception const & e)
  {
    LOG(LERROR, (e.Msg()));
  }
  return version;
}

void LoadCountryFile2CountryInfo(string const & jsonBuffer, map<string, CountryInfo> & id2info)
{
  ASSERT(id2info.empty(), ());
  DoStoreFile2Info doStore(id2info);
  LoadCountriesImpl(jsonBuffer, doStore);
}

void LoadCountryCode2File(string const & jsonBuffer, multimap<string, string> & code2file)
{
  ASSERT(code2file.empty(), ());
  DoStoreCode2File doStore(code2file);
  LoadCountriesImpl(jsonBuffer, doStore);
}

template <class T>
void SaveImpl(T const & v, json_t * jParent)
{
  size_t const siblingsCount = v.SiblingsCount();
  CHECK_GREATER(siblingsCount, 0, ());

  my::JsonHandle jArray;
  jArray.AttachNew(json_array());
  for (size_t i = 0; i < siblingsCount; ++i)
  {
    my::JsonHandle jCountry;
    jCountry.AttachNew(json_object());

    string const strName = v[i].Value().Name();
    CHECK(!strName.empty(), ("Empty country name?"));
    json_object_set_new(jCountry.get(), "n", json_string(strName.c_str()));
    string const strFlag = v[i].Value().Flag();
    if (!strFlag.empty())
      json_object_set_new(jCountry.get(), "c", json_string(strFlag.c_str()));

    size_t countriesCount = v[i].Value().GetFilesCount();
    ASSERT_LESS_OR_EQUAL(countriesCount, 1, ());
    if (countriesCount > 0)
    {
      CountryFile const & file = v[i].Value().GetFile();
      string const & strFile = file.GetNameWithoutExt();
      if (strFile != strName)
        json_object_set_new(jCountry.get(), "f", json_string(strFile.c_str()));
      json_object_set_new(jCountry.get(), "s", json_integer(file.GetRemoteSize(MapOptions::Map)));
      json_object_set_new(jCountry.get(), "rs",
                          json_integer(file.GetRemoteSize(MapOptions::CarRouting)));
    }

    if (v[i].SiblingsCount())
      SaveImpl(v[i], jCountry.get());

    json_array_append(jArray.get(), jCountry.get());
  }

  json_object_set(jParent, "g", jArray.get());
}

bool SaveCountries(int64_t version, CountriesContainerT const & countries, string & jsonBuffer)
{
  my::JsonHandle root;
  root.AttachNew(json_object());

  json_object_set_new(root.get(), "v", json_integer(version));
  json_object_set_new(root.get(), "n", json_string("World"));
  SaveImpl(countries, root.get());

  char * res = json_dumps(root.get(), JSON_PRESERVE_ORDER | JSON_COMPACT | JSON_INDENT(1));
  jsonBuffer = res;
  free(res);
  return true;
}

}  // namespace storage