File: Util.hxx

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 (216 lines) | stat: -rw-r--r-- 5,172 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
#ifndef UTIL_HXX_INCLUDED
#define UTIL_HXX_INCLUDED

#include <optional>
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <QPoint>
#include <vector>

#ifdef _WIN64
#include <stdlib.h>
#include <io.h>
#include <process.h>
#include <direct.h>
#define F_OK 0
#define access _access
#else
#include <unistd.h>
#endif

namespace util
{
template <typename T> int sgn(T x)
{
  return (x > 0) ? 1 : ((x < 0) ? -1 : 0);
}

template <class... T> void unused(T &&...) {}

class GetExecutablePathError : public virtual std::exception
{
public:
  [[nodiscard]] const char *what() const noexcept override = 0;
};

class SetGrDirError : public virtual std::exception
{
public:
  [[nodiscard]] const char *what() const noexcept override = 0;
};

#if defined __unix__ || defined __APPLE__
class ErrnoError : public virtual std::exception
{
public:
  ErrnoError();
  [[nodiscard]] const char *what() const noexcept override;

private:
  std::string what_str_;
};
#elif defined _WIN32
class FormatMessageError : public std::exception
{
public:
  [[nodiscard]] const char *what() const noexcept override;
};

class GetLastErrorError : public virtual std::exception
{
public:
  GetLastErrorError();
  [[nodiscard]] const char *what() const noexcept override;

private:
  std::string what_str_;
};
#endif

#if defined __APPLE__
class PathTooLongError : public GetExecutablePathError
{
public:
  PathTooLongError(size_t neededSize);
  [[nodiscard]] const char *what() const noexcept override;

private:
  std::string what_str_;
};
#elif defined __unix__
class PathTooLongError : public GetExecutablePathError
{
public:
  PathTooLongError();
  [[nodiscard]] const char *what() const noexcept override;

private:
  std::string what_str_;
};

class ProcessFileLinkNotReadableError : public ErrnoError, public GetExecutablePathError
{
public:
  [[nodiscard]] const char *what() const noexcept override;
};
#endif

#if defined __unix__ || defined __APPLE__
class AbsolutePathError : public ErrnoError, public SetGrDirError
{
public:
  [[nodiscard]] const char *what() const noexcept override;
};

class CorruptedGrDirError : public SetGrDirError
{
public:
  CorruptedGrDirError(const std::string &path);
  [[nodiscard]] const char *what() const noexcept override;

private:
  std::string what_str_;
};

class SetEnvError : public ErrnoError, public SetGrDirError
{
public:
  [[nodiscard]] const char *what() const noexcept override;
};
#elif defined _WIN32
class WideCharToMultiByteError : public GetLastErrorError, public SetGrDirError
{
public:
  [[nodiscard]] const char *what() const noexcept override;
};

class ModulePathError : public GetLastErrorError, public SetGrDirError
{
public:
  [[nodiscard]] const char *what() const noexcept override;
};

class DirnameError : public SetGrDirError
{
public:
  DirnameError(const std::wstring &filepath);
  [[nodiscard]] const char *what() const noexcept override;

private:
  std::string what_str_;
};

class AbsolutePathError : public SetGrDirError
{
public:
  AbsolutePathError(const std::wstring &path);
  [[nodiscard]] const char *what() const noexcept override;

private:
  std::string what_str_;
};

class CorruptedGrDirError : public SetGrDirError
{
public:
  CorruptedGrDirError(const std::wstring &path);
  [[nodiscard]] const char *what() const noexcept override;

private:
  std::string what_str_;
};

class SetEnvError : public GetLastErrorError, public SetGrDirError
{
public:
  [[nodiscard]] const char *what() const noexcept override;
};
#endif

bool endsWith(const std::string &str, const std::string &suffix);
bool startsWith(const std::string &str, const std::string &prefix);
bool isDigits(const std::string &str);
bool isNumber(const std::string &str);
#ifdef _WIN32
bool fileExists(const std::string &file_path);
bool fileExists(const std::wstring &file_path);
std::wstring getEnvVar(const std::wstring &name, const std::wstring &default_value = L"");
#else
bool fileExists(const std::string &file_path);
std::string getEnvVar(const std::string &name, const std::string &default_value = "");
#endif

#ifdef _WIN32
std::wstring getExecutablePath();
#else
std::string getExecutablePath();
#endif

void setGrdir(bool force = false);

template <typename... Args> std::string stringFormat(const std::string &format, Args... args)
{
  // Modified version of <https://stackoverflow.com/a/26221725/5958465>
  const int needed_bytes = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
  if (needed_bytes <= 0) throw std::runtime_error("Error during formatting.");
  std::vector<char> buf(needed_bytes);
  std::snprintf(buf.data(), needed_bytes, format.c_str(), args...);
  return std::string(buf.data());
}

// `overloaded` utility taken from <https://en.cppreference.com/w/cpp/utility/variant/visit>
template <class... Ts> struct Overloaded : Ts...
{
  using Ts::operator()...;
};
template <class... Ts> Overloaded(Ts...) -> Overloaded<Ts...>;
} // namespace util

inline std::ostream &operator<<(std::ostream &os, const QPoint &point)
{
  return os << "(" << std::setw(4) << point.x() << ", " << std::setw(4) << point.y() << ")";
}

#endif /* ifndef UTIL_HXX_INCLUDED */