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

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

#include "coding/file_name_utils.hpp"

#include "base/logging.hpp"
#include "base/scope_guard.hpp"

#include "std/algorithm.hpp"
#include "std/cstring.hpp"
#include "std/regex.hpp"
#include "std/unique_ptr.hpp"

#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#if defined(OMIM_OS_MAC) || defined(OMIM_OS_IPHONE)
  #include <sys/mount.h>
#else
  #include <sys/vfs.h>
#endif

namespace
{
struct CloseDir
{
  void operator()(DIR * dir) const
  {
    if (dir)
      closedir(dir);
  }
};
}  // namespace

void Platform::GetSystemFontNames(FilesList & res) const
{
#if defined(OMIM_OS_MAC) || defined(OMIM_OS_IPHONE)
#else
  char const * fontsWhitelist[] = {
    "Roboto-Medium.ttf",
    "Roboto-Regular.ttf",
    "DroidSansFallback.ttf",
    "DroidSansFallbackFull.ttf",
    "DroidSans.ttf",
    "DroidSansArabic.ttf",
    "DroidSansSemc.ttf",
    "DroidSansSemcCJK.ttf",
    "DroidNaskh-Regular.ttf",
    "Lohit-Bengali.ttf",
    "Lohit-Devanagari.ttf",
    "Lohit-Tamil.ttf",
    "PakType Naqsh.ttf",
    "wqy-microhei.ttc",
    "Jomolhari.ttf",
    "Jomolhari-alpha3c-0605331.ttf",
    "Padauk.ttf",
    "KhmerOS.ttf",
    "Umpush.ttf",
    "DroidSansThai.ttf",
    "DroidSansArmenian.ttf",
    "DroidSansEthiopic-Regular.ttf",
    "DroidSansGeorgian.ttf",
    "DroidSansHebrew-Regular.ttf",
    "DroidSansHebrew.ttf",
    "DroidSansJapanese.ttf",
    "LTe50872.ttf",
    "LTe50259.ttf",
    "DevanagariOTS.ttf",
    "FreeSans.ttf",
    "DejaVuSans.ttf",
    "arial.ttf",
    "AbyssinicaSIL-R.ttf",
  };

  char const * systemFontsPath[] = {
    "/system/fonts/",
#ifdef OMIM_OS_LINUX
    "/usr/share/fonts/truetype/roboto/",
    "/usr/share/fonts/truetype/droid/",
    "/usr/share/fonts/truetype/ttf-dejavu/",
    "/usr/share/fonts/truetype/wqy/",
    "/usr/share/fonts/truetype/freefont/",
    "/usr/share/fonts/truetype/padauk/",
    "/usr/share/fonts/truetype/dzongkha/",
    "/usr/share/fonts/truetype/ttf-khmeros-core/",
    "/usr/share/fonts/truetype/tlwg/",
    "/usr/share/fonts/truetype/abyssinica/",
    "/usr/share/fonts/truetype/paktype/",
    "/usr/share/fonts/truetype/mapsme/",
#endif
  };

  const uint64_t fontSizeBlacklist[] = {
    183560,   // Samsung Duos DroidSans
    7140172,  // Serif font without Emoji
    14416824  // Serif font with Emoji
  };

  uint64_t fileSize = 0;

  for (size_t i = 0; i < ARRAY_SIZE(fontsWhitelist); ++i)
  {
    for (size_t j = 0; j < ARRAY_SIZE(systemFontsPath); ++j)
    {
      string const path = string(systemFontsPath[j]) + fontsWhitelist[i];
      if (IsFileExistsByFullPath(path))
      {
        if (GetFileSizeByName(path, fileSize))
        {
          uint64_t const * end = fontSizeBlacklist + ARRAY_SIZE(fontSizeBlacklist);
          if (find(fontSizeBlacklist, end, fileSize) == end)
          {
            res.push_back(path);
            LOG(LINFO, ("Found usable system font", path, "with file size", fileSize));
          }
        }
      }
    }
  }
#endif
}

// static
Platform::EError Platform::RmDir(string const & dirName)
{
  if (rmdir(dirName.c_str()) != 0)
    return ErrnoToError();
  return ERR_OK;
}

// static
Platform::EError Platform::GetFileType(string const & path, EFileType & type)
{
  struct stat stats;
  if (stat(path.c_str(), &stats) != 0)
    return ErrnoToError();
  if (S_ISREG(stats.st_mode))
    type = FILE_TYPE_REGULAR;
  else if (S_ISDIR(stats.st_mode))
    type = FILE_TYPE_DIRECTORY;
  else
    type = FILE_TYPE_UNKNOWN;
  return ERR_OK;
}

bool Platform::IsFileExistsByFullPath(string const & filePath)
{
  struct stat s;
  return stat(filePath.c_str(), &s) == 0;
}

bool Platform::IsDirectoryEmpty(string const & directory)
{
  unique_ptr<DIR, CloseDir> dir(opendir(directory.c_str()));
  if (!dir)
    return true;

  struct dirent * entry;

  // Invariant: all files met so far are "." or "..".
  while ((entry = readdir(dir.get())) != nullptr)
  {
    // A file is not a special UNIX file. Early exit here.
    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
      return false;
  }
  return true;
}

bool Platform::GetFileSizeByFullPath(string const & filePath, uint64_t & size)
{
  struct stat s;
  if (stat(filePath.c_str(), &s) == 0)
  {
    size = s.st_size;
    return true;
  }
  else return false;
}

Platform::TStorageStatus Platform::GetWritableStorageStatus(uint64_t neededSize) const
{
  struct statfs st;
  int const ret = statfs(m_writableDir.c_str(), &st);

  LOG(LDEBUG, ("statfs return =", ret,
               "; block size =", st.f_bsize,
               "; blocks available =", st.f_bavail));

  if (ret != 0)
  {
    LOG(LERROR, ("Path:", m_writableDir, "statfs error:", ErrnoToError()));
    return STORAGE_DISCONNECTED;
  }

  /// @todo May be add additional storage space.
  if (st.f_bsize * st.f_bavail < neededSize)
    return NOT_ENOUGH_SPACE;

  return STORAGE_OK;
}

uint64_t Platform::GetWritableStorageSpace() const
{
  struct statfs st;
  int const ret = statfs(m_writableDir.c_str(), &st);

  LOG(LDEBUG, ("statfs return =", ret,
               "; block size =", st.f_bsize,
               "; blocks available =", st.f_bavail));

  if (ret != 0)
    LOG(LERROR, ("Path:", m_writableDir, "statfs error:", ErrnoToError()));

  return (ret != 0) ? 0 : st.f_bsize * st.f_bavail;
}

namespace pl
{
void EnumerateFilesByRegExp(string const & directory, string const & regexp,
                            vector<string> & res)
{
  unique_ptr<DIR, CloseDir> dir(opendir(directory.c_str()));
  if (!dir)
    return;

  regex exp(regexp);

  struct dirent * entry;
  while ((entry = readdir(dir.get())) != 0)
  {
    string const name(entry->d_name);
    if (regex_search(name.begin(), name.end(), exp))
      res.push_back(name);
  }
}
}  // namespace pl