1414// You should have received a copy of the GNU General Public License
1515// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
17+
1718#pragma once
18- #include < vector>
19- #include < string>
20- #include < sstream>
19+
2120#include < windows.h>
21+
2222#include < commctrl.h>
23- #include < iso646.h>
24- #include < cstdint>
25- #include < unordered_set>
26- #include < algorithm>
2723#include < tchar.h>
2824
25+ #include < algorithm>
26+ #include < locale>
27+ #include < sstream>
28+ #include < string>
29+ #include < unordered_set>
30+ #include < vector>
31+
32+ #if defined(_MSC_VER)
2933#pragma deprecated(PathFileExists) // Use doesFileExist, doesDirectoryExist or doesPathExist (for file or directory) instead.
3034#pragma deprecated(PathIsDirectory) // Use doesDirectoryExist instead.
31-
32-
33- const bool dirUp = true ;
34- const bool dirDown = false ;
35-
36- #define NPP_CP_WIN_1252 1252
37- #define NPP_CP_DOS_437 437
38- #define NPP_CP_BIG5 950
39-
40- #define LINKTRIGGERED WM_USER+555
41-
42- #define BCKGRD_COLOR (RGB(255 ,102 ,102 ))
43- #define TXT_COLOR (RGB(255 ,255 ,255 ))
44-
45- #ifndef __MINGW32__
46- #define WCSTOK wcstok
47- #else
48- #define WCSTOK wcstok_s
4935#endif
5036
5137
52- #define NPP_INTERNAL_FUNCTION_STR L" Notepad++::InternalFunction"
53-
5438
5539std::wstring folderBrowser (HWND parent, const std::wstring & title = L" " , int outputCtrlID = 0 , const wchar_t *defaultStr = NULL );
5640std::wstring getFolderName (HWND parent, const wchar_t *defaultDir = NULL );
@@ -68,12 +52,12 @@ std::vector<std::wstring> tokenizeString(const std::wstring & tokenString, const
6852void ClientRectToScreenRect (HWND hWnd, RECT* rect);
6953void ScreenRectToClientRect (HWND hWnd, RECT* rect);
7054
71- std::wstring string2wstring (const std::string & rString, UINT codepage);
72- std::string wstring2string (const std::wstring & rwString, UINT codepage);
73- bool isInList (const wchar_t * token, const wchar_t * list);
55+ std::wstring string2wstring (const std::string& rString, UINT codepage = CP_UTF8 );
56+ std::string wstring2string (const std::wstring& rwString, UINT codepage = CP_UTF8 );
57+ bool isInList (const wchar_t * token, const wchar_t * list);
7458std::wstring BuildMenuFileName (int filenameLen, unsigned int pos, const std::wstring &filename, bool ordinalNumber = true );
7559
76- std::string getFileContent (const wchar_t * file2read);
60+ std::string getFileContent (const wchar_t * file2read, bool * pbFailed = nullptr );
7761std::wstring relativeFilePathToFullFilePath (const wchar_t *relativeFilePath);
7862void writeFileContent (const wchar_t *file2write, const char *content2write);
7963bool matchInList (const wchar_t *fileName, const std::vector<std::wstring> & patterns);
@@ -88,12 +72,12 @@ class WcharMbcsConvertor final
8872 return instance;
8973 }
9074
91- const wchar_t * char2wchar (const char * mbStr , size_t codepage, int lenMbcs = -1 , int * pLenOut = NULL , int * pBytesNotProcessed = NULL );
92- const wchar_t * char2wchar (const char * mbcs2Convert, size_t codepage, intptr_t * mstart, intptr_t * mend, int len = 0 );
93- size_t getSizeW () { return _wideCharStr.size (); };
94- const char * wchar2char (const wchar_t * wcStr , size_t codepage, int lenIn = -1 , int * pLenOut = NULL );
95- const char * wchar2char (const wchar_t * wcStr , size_t codepage, intptr_t * mstart, intptr_t * mend, int lenIn = 0 , int * lenOut = nullptr );
96- size_t getSizeA () { return _multiByteStr.size (); };
75+ const wchar_t * char2wchar (const char * mbcs2Convert , size_t codepage, int lenMbcs = -1 , int * pLenWc = nullptr , int * pBytesNotProcessed = NULL );
76+ const wchar_t * char2wchar (const char * mbcs2Convert, size_t codepage, intptr_t * mstart, intptr_t * mend, int mbcsLen = 0 );
77+ size_t getSizeW () const { return _wideCharStr.size (); }
78+ const char * wchar2char (const wchar_t * wcharStr2Convert , size_t codepage, int lenWc = -1 , int * pLenMbcs = nullptr );
79+ const char * wchar2char (const wchar_t * wcharStr2Convert , size_t codepage, intptr_t * mstart, intptr_t * mend, int wcharLenIn = 0 , int * lenOut = nullptr );
80+ size_t getSizeA () const { return _multiByteStr.size (); }
9781
9882 const char * encode (UINT fromCodepage, UINT toCodepage, const char * txt2Encode, int lenIn = -1 , int * pLenOut = NULL , int * pBytesNotProcessed = NULL ) {
9983 int lenWc = 0 ;
@@ -144,7 +128,7 @@ class WcharMbcsConvertor final
144128 operator const T* () const { return _str; }
145129
146130 protected:
147- static const int initSize = 1024 ;
131+ static constexpr int initSize = 1024 ;
148132 size_t _allocLen = 0 ;
149133 size_t _dataLen = 0 ;
150134 T* _str = nullptr ;
@@ -154,8 +138,6 @@ class WcharMbcsConvertor final
154138 StringBuffer<wchar_t > _wideCharStr;
155139};
156140
157- #define REBARBAND_SIZE sizeof (REBARBANDINFO)
158-
159141std::wstring pathRemoveFileSpec (std::wstring & path);
160142std::wstring pathAppend (std::wstring &strDest, const std::wstring & str2append);
161143COLORREF getCtrlBgColor (HWND hWnd);
@@ -168,6 +150,8 @@ void stringJoin(const std::vector<std::wstring>& strings, const std::wstring& se
168150std::wstring stringTakeWhileAdmissable (const std::wstring& input, const std::wstring& admissable);
169151double stodLocale (const std::wstring& str, _locale_t loc, size_t * idx = NULL );
170152
153+ const std::locale& getSysLocale ();
154+
171155bool str2Clipboard (const std::wstring &str2cpy, HWND hwnd);
172156std::wstring strFromClipboard ();
173157class Buffer ;
@@ -178,11 +162,11 @@ std::wstring GetLastErrorAsString(DWORD errorCode = 0);
178162std::wstring intToString (int val);
179163std::wstring uintToString (unsigned int val);
180164
181- HWND CreateToolTip (int toolID, HWND hDlg, HINSTANCE hInst, const PTSTR pszText, bool isRTL);
182- HWND CreateToolTipRect (int toolID, HWND hWnd, HINSTANCE hInst, const PTSTR pszText, const RECT rc);
165+ HWND CreateToolTip (int toolID, HWND hDlg, HINSTANCE hInst, const PWSTR pszText, bool isRTL);
166+ HWND CreateToolTipRect (int toolID, HWND hWnd, HINSTANCE hInst, const PWSTR pszText, const RECT rc);
183167
184168bool isCertificateValidated (const std::wstring & fullFilePath, const std::wstring & subjectName2check);
185- bool isAssoCommandExisting (LPCTSTR FullPathName);
169+ bool isAssoCommandExisting (LPCWSTR FullPathName);
186170
187171bool deleteFileOrFolder (const std::wstring& f2delete);
188172
@@ -233,42 +217,43 @@ class Version final
233217{
234218public:
235219 Version () = default ;
236- Version (const std::wstring& versionStr);
220+ explicit Version (const std::wstring& versionStr);
237221
238222 void setVersionFrom (const std::wstring& filePath);
239- std::wstring toString ();
240- bool isNumber (const std::wstring& s) const {
223+ std::wstring toString () const ;
224+ static bool isNumber (const std::wstring& s) {
225+ static const auto & loc = std::locale::classic ();
241226 return !s.empty () &&
242- find_if (s.begin (), s.end (), [](wchar_t c) { return !_istdigit (c ); }) == s.end ();
243- };
227+ find_if (s.begin (), s.end (), [](auto c) { return !std::isdigit (c, loc ); }) == s.end ();
228+ }
244229
245230 int compareTo (const Version& v2c) const ;
246231
247232 bool operator < (const Version& v2c) const {
248233 return compareTo (v2c) == -1 ;
249- };
234+ }
250235
251236 bool operator <= (const Version& v2c) const {
252237 int r = compareTo (v2c);
253238 return r == -1 || r == 0 ;
254- };
239+ }
255240
256241 bool operator > (const Version& v2c) const {
257242 return compareTo (v2c) == 1 ;
258- };
243+ }
259244
260245 bool operator >= (const Version& v2c) const {
261246 int r = compareTo (v2c);
262247 return r == 1 || r == 0 ;
263- };
248+ }
264249
265250 bool operator == (const Version& v2c) const {
266251 return compareTo (v2c) == 0 ;
267- };
252+ }
268253
269254 bool operator != (const Version& v2c) const {
270255 return compareTo (v2c) != 0 ;
271- };
256+ }
272257
273258 bool empty () const {
274259 return _major == 0 && _minor == 0 && _patch == 0 && _build == 0 ;
@@ -300,26 +285,25 @@ bool isWindowVisibleOnAnyMonitor(const RECT& rectWndIn);
300285bool isCoreWindows ();
301286
302287
303- #define IDT_HIDE_TOOLTIP 1001
304-
305288class ControlInfoTip final
306289{
307290public:
308- ControlInfoTip () {} ;
291+ ControlInfoTip () = default ;
309292 ~ControlInfoTip () {
310293 if (_hWndInfoTip) {
311294 hide ();
312295 }
313- };
296+ }
297+
314298 bool init (HINSTANCE hInst, HWND ctrl2attached, HWND ctrl2attachedParent, const std::wstring& tipStr, bool isRTL, unsigned int remainTimeMillisecond = 0 , int maxWidth = 200 ); // remainTimeMillisecond = 0: no timeout
315299
316300 bool isValid () const {
317301 return _hWndInfoTip != nullptr ;
318- };
302+ }
319303
320304 HWND getTipHandle () const {
321305 return _hWndInfoTip;
322- };
306+ }
323307
324308 enum showPosition {beginning, middle, end};
325309 void show (showPosition pos = middle) const ;
@@ -334,9 +318,38 @@ class ControlInfoTip final
334318 ControlInfoTip& operator =(const ControlInfoTip&) = delete ;
335319};
336320
321+ DWORD invokeNppUacOp (const std::wstring& strCmdLineParams);
322+ bool fileTimeToYMD (const FILETIME& ft, int & yyyymmdd);
323+ void expandEnv (std::wstring& path2Expand);
337324
338- #define NPP_UAC_SAVE_SIGN L" #UAC-SAVE#"
339- #define NPP_UAC_SETFILEATTRIBUTES_SIGN L" #UAC-SETFILEATTRIBUTES#"
340- #define NPP_UAC_MOVEFILE_SIGN L" #UAC-MOVEFILE#"
341- #define NPP_UAC_CREATEEMPTYFILE_SIGN L" #UAC-CREATEEMPTYFILE#"
342- DWORD invokeNppUacOp (std::wstring& strCmdLineParams);
325+ class ScopedCOMInit final // never use this in DllMain
326+ {
327+ public:
328+ ScopedCOMInit () {
329+ HRESULT hr = ::CoInitializeEx (nullptr , COINIT_APARTMENTTHREADED); // attempt STA init 1st (older CoInitialize(NULL))
330+ if (hr == RPC_E_CHANGED_MODE) {
331+ hr = ::CoInitializeEx (nullptr , COINIT_MULTITHREADED); // STA init failed, switch to MTA
332+ }
333+ if (SUCCEEDED (hr)) {
334+ // S_OK or S_FALSE, both needs subsequent CoUninitialize()
335+ _bInitialized = true ;
336+ }
337+ }
338+
339+ ~ScopedCOMInit () {
340+ if (_bInitialized) {
341+ _bInitialized = false ;
342+ ::CoUninitialize ();
343+ }
344+ }
345+
346+ bool isInitialized () const {
347+ return _bInitialized;
348+ }
349+
350+ private:
351+ bool _bInitialized = false ;
352+
353+ ScopedCOMInit (const ScopedCOMInit&) = delete ;
354+ ScopedCOMInit& operator =(const ScopedCOMInit&) = delete ;
355+ };
0 commit comments