forked from wangjun7121/npp-task-list
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCommon.h
291 lines (228 loc) · 10.1 KB
/
Common.h
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
// This file is part of Notepad++ project
// Copyright (C)2021 Don HO <[email protected]>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// at your option any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <vector>
#include <string>
#include <sstream>
#include <windows.h>
#include <iso646.h>
#include <cstdint>
#include <unordered_set>
#include <algorithm>
#include <tchar.h>
#pragma deprecated(PathFileExists) // Use doesFileExist, doesDirectoryExist or doesPathExist (for file or directory) instead.
#pragma deprecated(PathIsDirectory) // Use doesDirectoryExist instead.
const bool dirUp = true;
const bool dirDown = false;
#define NPP_CP_WIN_1252 1252
#define NPP_CP_DOS_437 437
#define NPP_CP_BIG5 950
#define LINKTRIGGERED WM_USER+555
#define BCKGRD_COLOR (RGB(255,102,102))
#define TXT_COLOR (RGB(255,255,255))
#ifndef __MINGW32__
#define WCSTOK wcstok
#else
#define WCSTOK wcstok_s
#endif
#define NPP_INTERNAL_FUCTION_STR L"Notepad++::InternalFunction"
std::wstring folderBrowser(HWND parent, const std::wstring & title = L"", int outputCtrlID = 0, const wchar_t *defaultStr = NULL);
std::wstring getFolderName(HWND parent, const wchar_t *defaultDir = NULL);
void printInt(int int2print);
void printStr(const wchar_t *str2print);
std::wstring commafyInt(size_t n);
void writeLog(const wchar_t* logFileName, const char* log2write);
void writeLog(const wchar_t* logFileName, const wchar_t* log2write);
int filter(unsigned int code, struct _EXCEPTION_POINTERS *ep);
std::wstring purgeMenuItemString(const wchar_t* menuItemStr, bool keepAmpersand = false);
std::vector<std::wstring> tokenizeString(const std::wstring & tokenString, const char delim);
void ClientRectToScreenRect(HWND hWnd, RECT* rect);
void ScreenRectToClientRect(HWND hWnd, RECT* rect);
std::wstring string2wstring(const std::string & rString, UINT codepage);
std::string wstring2string(const std::wstring & rwString, UINT codepage);
bool isInList(const wchar_t *token, const wchar_t *list);
std::wstring BuildMenuFileName(int filenameLen, unsigned int pos, const std::wstring &filename, bool ordinalNumber = true);
std::string getFileContent(const wchar_t *file2read);
std::wstring relativeFilePathToFullFilePath(const wchar_t *relativeFilePath);
void writeFileContent(const wchar_t *file2write, const char *content2write);
bool matchInList(const wchar_t *fileName, const std::vector<std::wstring> & patterns);
bool matchInExcludeDirList(const wchar_t* dirName, const std::vector<std::wstring>& patterns, size_t level);
bool allPatternsAreExclusion(const std::vector<std::wstring>& patterns);
class WcharMbcsConvertor final
{
public:
static WcharMbcsConvertor& getInstance() {
static WcharMbcsConvertor instance;
return instance;
}
const wchar_t * char2wchar(const char *mbStr, size_t codepage, int lenMbcs =-1, int* pLenOut=NULL, int* pBytesNotProcessed=NULL);
const wchar_t * char2wchar(const char *mbcs2Convert, size_t codepage, intptr_t* mstart, intptr_t* mend);
const char * wchar2char(const wchar_t *wcStr, size_t codepage, int lenIn = -1, int* pLenOut = NULL);
const char * wchar2char(const wchar_t *wcStr, size_t codepage, intptr_t* mstart, intptr_t* mend);
const char * encode(UINT fromCodepage, UINT toCodepage, const char *txt2Encode, int lenIn = -1, int* pLenOut=NULL, int* pBytesNotProcessed=NULL)
{
int lenWc = 0;
const wchar_t * strW = char2wchar(txt2Encode, fromCodepage, lenIn, &lenWc, pBytesNotProcessed);
return wchar2char(strW, toCodepage, lenWc, pLenOut);
}
protected:
WcharMbcsConvertor() = default;
~WcharMbcsConvertor() = default;
// Since there's no public ctor, we need to void the default assignment operator and copy ctor.
// Since these are marked as deleted does not matter under which access specifier are kept
WcharMbcsConvertor(const WcharMbcsConvertor&) = delete;
WcharMbcsConvertor& operator= (const WcharMbcsConvertor&) = delete;
// No move ctor and assignment
WcharMbcsConvertor(WcharMbcsConvertor&&) = delete;
WcharMbcsConvertor& operator= (WcharMbcsConvertor&&) = delete;
template <class T>
class StringBuffer final
{
public:
~StringBuffer() { if (_allocLen) delete[] _str; }
void sizeTo(size_t size)
{
if (_allocLen < size)
{
if (_allocLen)
delete[] _str;
_allocLen = std::max<size_t>(size, initSize);
_str = new T[_allocLen];
}
}
void empty()
{
static T nullStr = 0; // routines may return an empty string, with null terminator, without allocating memory; a pointer to this null character will be returned in that case
if (_allocLen == 0)
_str = &nullStr;
else
_str[0] = 0;
}
operator T* () { return _str; }
operator const T* () const { return _str; }
protected:
static const int initSize = 1024;
size_t _allocLen = 0;
T* _str = nullptr;
};
StringBuffer<char> _multiByteStr;
StringBuffer<wchar_t> _wideCharStr;
};
#define REBARBAND_SIZE sizeof(REBARBANDINFO)
std::wstring pathRemoveFileSpec(std::wstring & path);
std::wstring pathAppend(std::wstring &strDest, const std::wstring & str2append);
COLORREF getCtrlBgColor(HWND hWnd);
std::wstring stringToUpper(std::wstring strToConvert);
std::wstring stringToLower(std::wstring strToConvert);
std::wstring stringReplace(std::wstring subject, const std::wstring& search, const std::wstring& replace);
void stringSplit(const std::wstring& input, const std::wstring& delimiter, std::vector<std::wstring>& output);
bool str2numberVector(std::wstring str2convert, std::vector<size_t>& numVect);
void stringJoin(const std::vector<std::wstring>& strings, const std::wstring& separator, std::wstring& joinedString);
std::wstring stringTakeWhileAdmissable(const std::wstring& input, const std::wstring& admissable);
double stodLocale(const std::wstring& str, _locale_t loc, size_t* idx = NULL);
bool str2Clipboard(const std::wstring &str2cpy, HWND hwnd);
class Buffer;
bool buf2Clipboard(const std::vector<Buffer*>& buffers, bool isFullPath, HWND hwnd);
std::wstring GetLastErrorAsString(DWORD errorCode = 0);
std::wstring intToString(int val);
std::wstring uintToString(unsigned int val);
HWND CreateToolTip(int toolID, HWND hDlg, HINSTANCE hInst, const PTSTR pszText, bool isRTL);
HWND CreateToolTipRect(int toolID, HWND hWnd, HINSTANCE hInst, const PTSTR pszText, const RECT rc);
bool isCertificateValidated(const std::wstring & fullFilePath, const std::wstring & subjectName2check);
bool isAssoCommandExisting(LPCTSTR FullPathName);
std::wstring s2ws(const std::string& str);
std::string ws2s(const std::wstring& wstr);
bool deleteFileOrFolder(const std::wstring& f2delete);
void getFilesInFolder(std::vector<std::wstring>& files, const std::wstring& extTypeFilter, const std::wstring& inFolder);
template<typename T> size_t vecRemoveDuplicates(std::vector<T>& vec, bool isSorted = false, bool canSort = false)
{
if (!isSorted && canSort)
{
std::sort(vec.begin(), vec.end());
isSorted = true;
}
if (isSorted)
{
typename std::vector<T>::iterator it;
it = std::unique(vec.begin(), vec.end());
vec.resize(distance(vec.begin(), it)); // unique() does not shrink the vector
}
else
{
std::unordered_set<T> seen;
auto newEnd = std::remove_if(vec.begin(), vec.end(), [&seen](const T& value)
{
return !seen.insert(value).second;
});
vec.erase(newEnd, vec.end());
}
return vec.size();
}
void trim(std::wstring& str);
int nbDigitsFromNbLines(size_t nbLines);
std::wstring getDateTimeStrFrom(const std::wstring& dateTimeFormat, const SYSTEMTIME& st);
HFONT createFont(const wchar_t* fontName, int fontSize, bool isBold, HWND hDestParent);
bool removeReadOnlyFlagFromFileAttributes(const wchar_t* fileFullPath);
bool isWin32NamespacePrefixedFileName(const std::wstring& fileName);
bool isWin32NamespacePrefixedFileName(const wchar_t* szFileName);
bool isUnsupportedFileName(const std::wstring& fileName);
bool isUnsupportedFileName(const wchar_t* szFileName);
class Version final
{
public:
Version() = default;
Version(const std::wstring& versionStr);
void setVersionFrom(const std::wstring& filePath);
std::wstring toString();
bool isNumber(const std::wstring& s) const {
return !s.empty() &&
find_if(s.begin(), s.end(), [](wchar_t c) { return !_istdigit(c); }) == s.end();
};
int compareTo(const Version& v2c) const;
bool operator < (const Version& v2c) const {
return compareTo(v2c) == -1;
};
bool operator <= (const Version& v2c) const {
int r = compareTo(v2c);
return r == -1 || r == 0;
};
bool operator > (const Version& v2c) const {
return compareTo(v2c) == 1;
};
bool operator >= (const Version& v2c) const {
int r = compareTo(v2c);
return r == 1 || r == 0;
};
bool operator == (const Version& v2c) const {
return compareTo(v2c) == 0;
};
bool operator != (const Version& v2c) const {
return compareTo(v2c) != 0;
};
bool empty() const {
return _major == 0 && _minor == 0 && _patch == 0 && _build == 0;
}
bool isCompatibleTo(const Version& from, const Version& to) const;
private:
unsigned long _major = 0;
unsigned long _minor = 0;
unsigned long _patch = 0;
unsigned long _build = 0;
};
DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
bool doesPathExist(const wchar_t* path, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);