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

update_generator.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 59f9e542130e14ee17a595e6b8abf00979588c22 (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
#include "update_generator.hpp"

#include "../coding/file_writer.hpp"

#include "../geometry/cellid.hpp"

#include "../platform/platform.hpp"

#include "../storage/country.hpp"
#include "../defines.hpp"

#include "../base/string_utils.hpp"
#include "../base/logging.hpp"
#include "../base/macros.hpp"

#include "../std/target_os.hpp"
#include "../std/fstream.hpp"
#include "../std/iterator.hpp"

using namespace storage;

/// files which can be updated through downloader
char const * gExtensionsToUpdate[] = {
  "*" DATA_FILE_EXTENSION, "*.txt", "*.bin", "*.skn", "*.ttf", "*.png"
};

namespace update
{
  // we don't support files without name or without extension
  bool SplitExtension(string const & file, string & name, string & ext)
  {
    // get extension
    size_t const index = file.find_last_of('.');
    if (index == string::npos || (index + 1) == file.size() || index == 0
        || file == "." || file == "..")
    {
      name = file;
      ext.clear();
      return false;
    }
    ext = file.substr(index);
    name = file.substr(0, index);
    return true;
  }

  bool GenerateFilesList(string const & dataDir)
  {
    Platform & platform = GetPlatform();

    Platform::FilesList files;
    for (size_t i = 0; i < ARRAY_SIZE(gExtensionsToUpdate); ++i)
    {
      Platform::FilesList otherFiles;
      platform.GetFilesInDir(dataDir, gExtensionsToUpdate[i], otherFiles);
      std::copy(otherFiles.begin(), otherFiles.end(), std::back_inserter(files));
    }
    { // remove minsk-pass from list
      Platform::FilesList::iterator minskPassIt = std::find(files.begin(), files.end(), "minsk-pass" DATA_FILE_EXTENSION);
      if (minskPassIt != files.end())
        files.erase(minskPassIt);
    }
    if (files.empty())
    {
      LOG(LERROR, ("Can't find any files at path", dataDir));
      return false;
    }
    else
    {
      LOG_SHORT(LINFO, ("Files count included in update file:", files.size()));
    }

    TDataFiles cellFiles;
    TCommonFiles commonFiles;
    string name, ext;
    int32_t level = -1;
    uint16_t bits;
    for (Platform::FilesList::iterator it = files.begin(); it != files.end(); ++it)
    {
      uint64_t size = 0;
      CHECK( platform.GetFileSize(dataDir + *it, size), ());
      CHECK_EQUAL( size, static_cast<uint32_t>(size), ("We don't support files > 4gb", *it));
      if (SplitExtension(*it, name, ext))
      {
        // is it data cell file?
        if (ext == DATA_FILE_EXTENSION)
        {
          if (CountryCellId::IsCellId(name))
          {
            CountryCellId cellId = CountryCellId::FromString(name);
            pair<int64_t, int> bl = cellId.ToBitsAndLevel();
            if (level < 0)
              level = bl.second;
            CHECK_EQUAL( level, bl.second, ("Data files with different level?", *it) );
            bits = static_cast<uint16_t>(bl.first);
            CHECK_EQUAL( name, CountryCellId::FromBitsAndLevel(bits, level).ToString(), (name));
            cellFiles.push_back(make_pair(bits, static_cast<uint32_t>(size)));
          }
          else
          {
            commonFiles.push_back(make_pair(*it, static_cast<uint32_t>(size)));
          }
        }
        else
        {
          commonFiles.push_back(make_pair(*it, static_cast<uint32_t>(size)));
        }
      }
    }

    SaveTiles(dataDir + DATA_UPDATE_FILE, level, cellFiles, commonFiles);

    LOG_SHORT(LINFO, ("Created update file with", cellFiles.size(), "cell data files and",
                      commonFiles.size(), "other files"));

    return true;
  }
} // namespace update