File: Util.cxx

package info (click to toggle)
gr-framework 0.73.22%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,476 kB
  • sloc: ansic: 87,950; cpp: 58,388; objc: 3,057; javascript: 2,647; python: 1,000; yacc: 855; pascal: 554; sh: 281; fortran: 228; makefile: 174
file content (390 lines) | stat: -rw-r--r-- 11,576 bytes parent folder | download
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#if defined __unix__ || defined __APPLE__
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#endif
#include <array>
#include <exception>
#include <iostream>
#include <memory>
#include <sstream>
#include <vector>

#if defined __APPLE__
#include <libgen.h>
#include <mach-o/dyld.h>
#elif defined __unix__
#define _XOPEN_SOURCE 600
#include <libgen.h>
#include <unistd.h>
#elif defined _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <stringapiset.h>
#include <strsafe.h>
#else
#error "Unsupported system"
#endif

#include "Util.hxx"

namespace util
{
const unsigned int MAXPATHLEN = 1024;


#if defined __unix__ || defined __APPLE__
ErrnoError::ErrnoError()
{
  std::stringstream what_stream;

  what_stream << "Error " << errno << ": " << strerror(errno);
  what_str_ = what_stream.str();
}

const char *ErrnoError::what() const noexcept
{
  return what_str_.c_str();
}
#elif defined _WIN32
const char *FormatMessageError::what() const noexcept
{
  return "No message could be created, FormatMessage failed.";
}

GetLastErrorError::GetLastErrorError()
{
  unsigned int error_code = GetLastError();
  std::stringstream what_stream;
  LPTSTR error_message = nullptr;

  if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                     nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&error_message, 0,
                     nullptr))
    {
      throw(FormatMessageError());
    }
  what_stream << "Error " << error_code << ": " << error_message;
  LocalFree(error_message);
  what_str_ = what_stream.str();
}

const char *GetLastErrorError::what() const noexcept
{
  return what_str_.c_str();
}
#endif

#if defined __APPLE__
PathTooLongError::PathTooLongError(size_t neededSize)
{
  std::stringstream what_stream;

  what_stream << MAXPATHLEN << " Bytes are not sufficient for storing the path. " << neededSize << " Bytes are needed.";
  what_str_ = what_stream.str();
}

const char *PathTooLongError::what() const noexcept
{
  return what_str_.c_str();
}

#elif defined __unix__
PathTooLongError::PathTooLongError()
{
  std::stringstream what_stream;

  what_stream << MAXPATHLEN << " Bytes are not sufficient for storing the path.";
  what_str_ = what_stream.str();
}

const char *PathTooLongError::what() const noexcept
{
  return what_str_.c_str();
}

const char *ProcessFileLinkNotReadableError::what() const noexcept
{
  return ErrnoError::what();
}
#endif

#if defined __unix__ || defined __APPLE__
const char *AbsolutePathError::what() const noexcept
{
  return ErrnoError::what();
}

CorruptedGrDirError::CorruptedGrDirError(const std::string &path)
{
  std::stringstream what_stream;

  what_stream << "The directory \"" << path << "\" is not a valid GR directory (missing \"include/gr.h\")";
  what_str_ = what_stream.str();
}

const char *CorruptedGrDirError::what() const noexcept
{
  return what_str_.c_str();
}

const char *SetEnvError::what() const noexcept
{
  return ErrnoError::what();
}
#elif defined _WIN32
const char *WideCharToMultiByteError::what() const noexcept
{
  return GetLastErrorError::what();
}

const char *ModulePathError::what() const noexcept
{
  return GetLastErrorError::what();
}

DirnameError::DirnameError(const std::wstring &file_path)
{
  std::wstringstream what_stream;

  what_stream << "Could not extract the directory name of \"" << file_path << "\".";

  int needed_bytes_utf_8_string =
      WideCharToMultiByte(CP_UTF8, 0, what_stream.str().c_str(), -1, nullptr, 0, nullptr, nullptr);
  if (needed_bytes_utf_8_string == 0) throw(WideCharToMultiByteError());
  std::vector<char> utf_8_bytes(needed_bytes_utf_8_string);
  int written_bytes_utf_8_string = WideCharToMultiByte(CP_UTF8, 0, what_stream.str().c_str(), -1, utf_8_bytes.data(),
                                                       needed_bytes_utf_8_string, nullptr, nullptr);
  if (written_bytes_utf_8_string == 0) throw(WideCharToMultiByteError());
  what_str_ = utf_8_bytes.data();
}

const char *DirnameError::what() const noexcept
{
  return what_str_.c_str();
}

AbsolutePathError::AbsolutePathError(const std::wstring &path)
{
  std::wstringstream what_stream;

  what_stream << "Could not determine the absolute path of \"" << path << "\".";

  int needed_bytes_utf_8_string =
      WideCharToMultiByte(CP_UTF8, 0, what_stream.str().c_str(), -1, nullptr, 0, nullptr, nullptr);
  if (needed_bytes_utf_8_string == 0) throw(WideCharToMultiByteError());
  std::vector<char> utf_8_bytes(needed_bytes_utf_8_string);
  int written_bytes_utf_8_string = WideCharToMultiByte(CP_UTF8, 0, what_stream.str().c_str(), -1, utf_8_bytes.data(),
                                                       needed_bytes_utf_8_string, nullptr, nullptr);
  if (written_bytes_utf_8_string == 0) throw(WideCharToMultiByteError());
  what_str_ = utf_8_bytes.data();
}

const char *AbsolutePathError::what() const noexcept
{
  return what_str_.c_str();
}

CorruptedGrDirError::CorruptedGrDirError(const std::wstring &path)
{
  std::wstringstream what_stream;

  what_stream << "The directory \"" << path << "\" is not a valid GR directory (missing \"include\\gr.h\")";

  int needed_bytes_utf_8_string =
      WideCharToMultiByte(CP_UTF8, 0, what_stream.str().c_str(), -1, nullptr, 0, nullptr, nullptr);
  if (needed_bytes_utf_8_string == 0) throw(WideCharToMultiByteError());
  std::vector<char> utf_8_bytes(needed_bytes_utf_8_string);
  int written_bytes_utf_8_string = WideCharToMultiByte(CP_UTF8, 0, what_stream.str().c_str(), -1, utf_8_bytes.data(),
                                                       needed_bytes_utf_8_string, nullptr, nullptr);
  if (written_bytes_utf_8_string == 0) throw(WideCharToMultiByteError());
  what_str_ = utf_8_bytes.data();
}

const char *CorruptedGrDirError::what() const noexcept
{
  return what_str_.c_str();
}

const char *SetEnvError::what() const noexcept
{
  return GetLastErrorError::what();
}
#else
#error "Unsupported system"
#endif

bool endsWith(const std::string &str, const std::string &suffix)
{
  return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
}

bool startsWith(const std::string &str, const std::string &prefix)
{
  return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
}

bool isDigits(const std::string &str)
{
  return str.find_first_not_of("0123456789") == std::string::npos;
}

bool isNumber(const std::string &str)
{
  const char minus[] = {(char)0xe2, (char)0x88, (char)0x92, '\0'}; // gr minus sign
  auto em_dash = std::string(minus);
  size_t start_pos = 0;
  if (startsWith(str, em_dash)) start_pos = em_dash.size();
  auto pos = str.find_first_not_of(".-0123456789", start_pos);
  return pos == std::string::npos;
}

#ifdef _WIN32
std::wstring getEnvVar(const std::wstring &name, const std::wstring &default_value)
#else
std::string getEnvVar(const std::string &name, const std::string &default_value)
#endif
{
#ifdef _WIN32
  DWORD needed_wide_chars = GetEnvironmentVariableW(name.c_str(), nullptr, 0);
  if (GetLastError() != ERROR_ENVVAR_NOT_FOUND)
    {
      std::vector<wchar_t> value_wide(needed_wide_chars);
      GetEnvironmentVariableW(name.c_str(), value_wide.data(), needed_wide_chars);
      return std::wstring(value_wide.data());
    }
  else
    {
      return default_value;
    }
#else
  const char *value_c_ptr = getenv(name.c_str());
  if (value_c_ptr != nullptr) return std::string(value_c_ptr);
  return default_value;
#endif
}

#ifdef _WIN32
std::wstring getExecutablePath()
#else
std::string getExecutablePath()
#endif
{
#ifdef _WIN32
  std::array<wchar_t, MAXPATHLEN> exe_path{L""};
#else
  std::array<char, MAXPATHLEN> exe_path{""};
#endif

#if defined __APPLE__
  unsigned int path_len = MAXPATHLEN - 1;

  if (_NSGetExecutablePath(exe_path.data(), &path_len)) throw(PathTooLongError(path_len));
#elif defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
  ssize_t path_len = -1;

  path_len = readlink("/proc/curproc/file", exe_path.data(), MAXPATHLEN);
  if (path_len < 0)
    {
      throw(ProcessFileLinkNotReadableError());
    }
  else if (path_len == MAXPATHLEN)
    {
      throw(PathTooLongError());
    }
#elif defined __linux__
  ssize_t path_len = -1;

  path_len = readlink("/proc/self/exe", exe_path.data(), MAXPATHLEN);
  if (path_len < 0) throw(ProcessFileLinkNotReadableError());
  if (path_len == MAXPATHLEN) throw(PathTooLongError());
#elif defined _WIN32
  unsigned int path_len = 0;

  path_len = GetModuleFileNameW(nullptr, exe_path.data(), MAXPATHLEN);
  if (GetLastError() != ERROR_SUCCESS) throw(ModulePathError());
#else
#error "Unsupported system"
#endif
  exe_path.at(path_len) = '\0';

  return exe_path.data();
}

#ifdef _WIN32
bool fileExists(const std::string &file_path)
{
  int needed_wide_chars = MultiByteToWideChar(CP_UTF8, 0, file_path.c_str(), -1, nullptr, 0);
  std::vector<wchar_t> file_path_wide(needed_wide_chars);
  MultiByteToWideChar(CP_UTF8, 0, file_path.c_str(), -1, file_path_wide.data(), needed_wide_chars);
  return fileExists(file_path_wide.data());
}

bool fileExists(const std::wstring &file_path)
{
  DWORD file_attributes = GetFileAttributesW(file_path.c_str());
  return (file_attributes != INVALID_FILE_ATTRIBUTES && !(file_attributes & FILE_ATTRIBUTE_DIRECTORY));
}
#else
bool fileExists(const std::string &file_path)
{
  struct stat file_stat;
  return stat(file_path.c_str(), &file_stat) == 0 && S_ISREG(file_stat.st_mode);
}
#endif

void setGrdir(bool force)
{
  if (!force)
    {
#ifdef _WIN32
      GetEnvironmentVariableW(L"GRDIR", nullptr, 0);
      if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) return;
#else
      if (getenv("GRDIR") != nullptr) return;
#endif
    }

  auto exe_path = getExecutablePath();
#ifdef _WIN32
  std::array<wchar_t, MAXPATHLEN> exe_dirname;
  std::wstringstream gr_dir_relative_stream;
  std::array<wchar_t, MAXPATHLEN> gr_dir_absolute;
  std::wstringstream gr_header_path_stream;

  if (_wsplitpath_s(exe_path.c_str(), nullptr, 0, exe_dirname.data(), MAXPATHLEN, nullptr, 0, nullptr, 0))
    {
      throw(DirnameError(exe_path));
    }
  gr_dir_relative_stream << exe_dirname.data() << L"/..";
  if (_wfullpath(gr_dir_absolute.data(), gr_dir_relative_stream.str().c_str(), MAXPATHLEN) == nullptr)
    {
      throw(AbsolutePathError(gr_dir_relative_stream.str()));
    }
  gr_header_path_stream << gr_dir_absolute.data() << L"/include/gr.h";
  if (!fileExists(gr_header_path_stream.str())) throw(CorruptedGrDirError(gr_dir_absolute.data()));
  if (!SetEnvironmentVariableW(L"GRDIR", gr_dir_absolute.data())) throw(SetEnvError());
#else
  std::stringstream gr_dir_relative_stream, gr_header_path_stream;

  gr_dir_relative_stream
      << dirname(&std::string(exe_path)[0])
#ifdef __APPLE__
      // On macOS, the `grplot` executable is located in `${GRDIR}/Applications/grplot.app/Contents/MacOS`
      << "/../../../..";
#else
      // On other Unixes, the `grplot` executable is located in `${GRDIR}/bin`
      << "/..";
#endif
  std::unique_ptr<char, decltype(std::free) *> gr_dir_absolute_c_ptr{
      realpath(gr_dir_relative_stream.str().c_str(), nullptr), std::free};
  if (!gr_dir_absolute_c_ptr) throw(AbsolutePathError());
  gr_header_path_stream << gr_dir_absolute_c_ptr.get() << "/include/gr.h";
  if (!fileExists(gr_header_path_stream.str())) throw(CorruptedGrDirError(gr_dir_absolute_c_ptr.get()));
  if (setenv("GRDIR", gr_dir_absolute_c_ptr.get(), 1) != 0) throw(SetEnvError());
#endif
}
} // namespace util