This repository was archived by the owner on Aug 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsjcpp_core.h
More file actions
283 lines (245 loc) · 11.4 KB
/
Copy pathwsjcpp_core.h
File metadata and controls
283 lines (245 loc) · 11.4 KB
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
/*
* MIT License
*
* Copyright (c) 2019-2026 Evgenii Sopov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// original source-code: https://github.com/wsjcpp/wsjcpp-core
#ifndef WSJCPP_CORE_H
#define WSJCPP_CORE_H
#define WSJCPP_CORE_VER_0
#define WSJCPP_CORE_VER_0_3
#define WSJCPP_CORE_VER_0_3_0
#include <deque>
#include <iostream>
#include <map>
#include <mutex>
#include <string>
#include <vector>
class WsjcppFilePermissions {
public:
WsjcppFilePermissions();
WsjcppFilePermissions(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag, bool bGroupReadFlag,
bool bGroupWriteFlag, bool bGroupExecuteFlag, bool bOtherReadFlag, bool bOtherWriteFlag,
bool bOtherExecuteFlag);
WsjcppFilePermissions(uint16_t nFilePermission);
// owner flags
void setOwnerReadFlag(bool bOwnerReadFlag);
bool getOwnerReadFlag() const;
void setOwnerWriteFlag(bool bOwnerWriteFlag);
bool getOwnerWriteFlag() const;
void setOwnerExecuteFlag(bool bOwnerExecuteFlag);
bool getOwnerExecuteFlag() const;
void setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag);
// group flags
void setGroupReadFlag(bool bGroupReadFlag);
bool getGroupReadFlag() const;
void setGroupWriteFlag(bool bGroupWriteFlag);
bool getGroupWriteFlag() const;
void setGroupExecuteFlag(bool bGroupExecuteFlag);
bool getGroupExecuteFlag() const;
void setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag);
// for other flags
void setOtherReadFlag(bool bOtherReadFlag);
bool getOtherReadFlag() const;
void setOtherWriteFlag(bool bOtherWriteFlag);
bool getOtherWriteFlag() const;
void setOtherExecuteFlag(bool bOtherExecuteFlag);
bool getOtherExecuteFlag() const;
void setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag);
std::string toString() const;
uint16_t toUInt16() const;
private:
bool m_bOwnerReadFlag;
bool m_bOwnerWriteFlag;
bool m_bOwnerExecuteFlag;
bool m_bGroupReadFlag;
bool m_bGroupWriteFlag;
bool m_bGroupExecuteFlag;
bool m_bOtherReadFlag;
bool m_bOtherWriteFlag;
bool m_bOtherExecuteFlag;
};
class WsjcppCore {
public:
static bool init(int argc, char **argv, const std::string &sApplicationName, const std::string &sApplicationVersion,
const std::string &sApplicationAuthor, const std::string &sLibraryNameForExports);
static std::string extractDirpath(const std::string &sFullPath);
static std::string getCurrentDirectory();
static long getCurrentTimeInMilliseconds();
static long getCurrentTimeInSeconds();
static std::string getCurrentTimeForFilename();
static std::string getCurrentTimeForLogFormat();
static std::string getThreadId();
static std::string formatTimeForWeb(long nTimeInSec);
static std::string formatTimeForFilename(long nTimeInSec);
static std::string formatTimeUTC(int nTimeInSec);
static std::vector<std::string> getListOfFiles(const std::string &sDirname);
static bool makeDir(const std::string &sDirname);
static bool makeDirsPath(const std::string &sDirname);
static bool writeFile(const std::string &sFilename, const std::string &sContent);
static bool readTextFile(const std::string &sFilename, std::string &sOutputContent);
static bool readFileToBuffer(const std::string &sFilename, char *pBuffer[], int &nBufferSize);
static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize);
static bool removeFile(const std::string &sFilename);
static bool copyFile(const std::string &sSourceFilename, const std::string &sTargetFilename);
static std::string <rim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
static std::string &rtrim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
static std::string &trim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
static std::string toLower(const std::string &str);
static std::string toUpper(const std::string &str);
static void initRandom();
static std::string uint2hexString(unsigned int n);
static unsigned long convertVoidToULong(void *p);
static std::string getPointerAsHex(void *p);
static bool getEnv(const std::string &sName, std::string &sValue);
static std::string encodeUriComponent(const std::string &sValue);
static std::string decodeUriComponent(const std::string &sValue);
static std::string getHumanSizeBytes(long nBytes);
static bool setFilePermissions(const std::string &sFilePath, const WsjcppFilePermissions &filePermissions,
std::string &sError);
static bool getFilePermissions(const std::string &sFilePath, WsjcppFilePermissions &filePermissions,
std::string &sError);
};
enum WsjcppColorCode { // deprecated, see https://github.com/sea5kg/sea5kg-logger
FG_RED = 31,
FG_GREEN = 32,
FG_YELLOW = 93,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_BLUE = 44,
BG_DEFAULT = 49
};
class WsjcppColorModifier { // deprecated, see https://github.com/sea5kg/sea5kg-logger
WsjcppColorCode code;
public:
WsjcppColorModifier(WsjcppColorCode pCode) : code(pCode) {
}
friend std::ostream &operator<<(std::ostream &os, const WsjcppColorModifier &mod) {
return os << "\033[" << mod.code << "m";
}
};
// ---------------------------------------------------------------------
class WsjcppLogGlobalConf { // deprecated, see https://github.com/sea5kg/sea5kg-logger
public:
WsjcppLogGlobalConf();
void doLogRotateUpdateFilename(bool bForce = false);
std::mutex logMutex;
std::string logDir;
std::string logPrefixFile;
std::string logFile;
bool enableLogFile;
long logStartTime;
long logRotationPeriodInSeconds;
std::deque<std::string> logLastMessages;
};
class WsjcppLog { // deprecated, see https://github.com/sea5kg/sea5kg-logger
public:
static WsjcppLogGlobalConf g_WSJCPP_LOG_GLOBAL_CONF;
static void info(const std::string &sTag, const std::string &sMessage);
static void err(const std::string &sTag, const std::string &sMessage);
static void throw_err(const std::string &sTag, const std::string &sMessage);
static void warn(const std::string &sTag, const std::string &sMessage);
static void ok(const std::string &sTag, const std::string &sMessage);
static std::vector<std::string> getLastLogMessages();
static void setLogDirectory(const std::string &sDirectoryPath);
static void setPrefixLogFile(const std::string &sPrefixLogFile);
static void setEnableLogFile(bool bEnable);
static void setRotationPeriodInSec(long nRotationPeriodInSec);
private:
static void add(WsjcppColorModifier &clr, const std::string &sType, const std::string &sTag,
const std::string &sMessage);
};
// ---------------------------------------------------------------------
// WsjcppResourceFile
class WsjcppResourceFile {
public:
WsjcppResourceFile();
virtual const std::string &getFilename() const = 0;
virtual const std::string &getPackAs() const = 0;
virtual int getBufferSize() const = 0;
virtual const char *getBuffer() const = 0;
};
// ---------------------------------------------------------------------
// WsjcppResourcesManager
extern std::vector<WsjcppResourceFile *> *g_pWsjcppResourceFiles;
class WsjcppResourcesManager {
public:
static void initGlobalVariables();
static void add(WsjcppResourceFile *);
static const std::vector<WsjcppResourceFile *> &list();
static bool has(const std::string &sFilename);
static WsjcppResourceFile *get(const std::string &sFilename);
static bool make(const std::string &sWorkspace);
// static bool createFolders(const std::string &sWorkspace);
// static bool extractFiles(const std::string &sWorkspace);
};
// ---------------------------------------------------------------------
// Registry WsjcppResourceFile
#define REGISTRY_WSJCPP_RESOURCE_FILE(classname) \
static classname *pRegistryWsjcppResourceFile##classname = new classname();
namespace wsjcpp {
class Core {
public:
static const std::string &englishAlphabetLowerCase();
static const std::string &englishAlphabetUpperCase();
static const std::string &englishAlphabetBothCase();
static const std::string &englishAlphabetBothCaseAndNumbers();
static std::string randomString(const std::string &alphabet, int length);
};
std::string normalize_filepath(const std::string &path);
std::string extract_url_protocol(const std::string &sValue);
bool recursive_copy_files(const std::string &source_dir, const std::string &target_dir, std::string &error);
bool recursive_remove_dir(const std::string &dirpath, std::string &error);
bool starts_with(const std::string &str, const std::string &start_str);
bool ends_with(const std::string &str, const std::string &end_str);
std::vector<std::string> directory_list(const std::string &dirpath);
std::string generate_uuid();
std::string parent_dirpath(const std::string &filepath);
std::string to_snake_case(const std::string &name);
bool dir_exists(const std::string &dir_path);
bool file_exists(const std::string &file_path);
std::vector<std::string> split(const std::string &source, const std::string &delimiter);
std::string join(const std::vector<std::string> &source, const std::string &delimiter);
void replace_all_in(std::string &target, const std::string &from, const std::string &to);
bool create_empty_file(const std::string &filepath);
std::string extract_filename(const std::string &filepath);
int levenshtein_distance(const std::string &str1, const std::string &str2);
bool user_is_root();
bool change_current_process_privileges(int user_id, std::string &error);
enum class operation_system { UNKNOWN, LINUX, WINDOWS };
operation_system operation_system_platform();
bool is_linux();
bool is_windows();
std::string padding_right(const std::string &source, char pad_char, size_t length);
std::string padding_left(const std::string &source, char pad_char, size_t length);
bool is_valid_ip4(const std::string &value, std::string &error);
bool is_valid_ip6(const std::string &value, std::string &error);
bool is_valid_date(const std::string &value, std::string &error);
bool is_valid_time24(const std::string &value, std::string &error);
bool is_valid_url_protocol(const std::string &value, std::string &error);
bool is_valid_domain_name(const std::string &value, std::string &error);
bool is_valid_port(const std::string &value, std::string &error);
bool is_valid_port(int nValue, std::string &error);
bool is_valid_base64(const std::string &value, std::string &error);
} // namespace wsjcpp
#endif // WSJCPP_CORE_H