forked from rizonesoft/Notepad3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathLib.h
154 lines (126 loc) · 7.7 KB
/
PathLib.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
// encoding: UTF-8
/******************************************************************************
* *
* *
* Notepad3 *
* *
* PathLib.h *
* Definition for dynamic wide char Long Path handling *
* Based on DynStrg module *
* *
* (c) Rizonesoft 2008-2024 *
* https://rizonesoft.com *
* *
* *
*******************************************************************************/
#pragma once
// comment out PATHCCH_NO_DEPRECATE to show deprecated API
#define PATHCCH_NO_DEPRECATE 1
///#include <pathcch.h>
#include "TypeDefs.h"
#include "DynStrg.h"
#define PTHAPI __stdcall
// PATHCCH_MAX_CCH: (0x7FFF + 1 for NULL terminator)
#define PATHLONG_MAX_CCH 0x8000
// ----------------------------------------------------------------------------
__forceinline size_t max_sz(const size_t x, const size_t y) { return (x < y) ? y : x; }
// ----------------------------------------------------------------------------
__forceinline LPCWSTR PTHAPI Path_Get(HPATHL hpth)
{
return StrgGet((HSTRINGW)hpth);
}
__forceinline size_t PTHAPI Path_GetBufCount(HPATHL hpth)
{
return StrgGetAllocLength((HSTRINGW)hpth);
}
__forceinline bool IsReadOnly(const DWORD dwFileAttr)
{
return ((dwFileAttr != INVALID_FILE_ATTRIBUTES) && (dwFileAttr & FILE_ATTRIBUTE_READONLY));
}
__forceinline bool IsExistingFile(const DWORD dwFileAttr)
{
return ((dwFileAttr != INVALID_FILE_ATTRIBUTES) && !(dwFileAttr & FILE_ATTRIBUTE_DIRECTORY));
}
__forceinline bool IsExistingDirectory(const DWORD dwFileAttr)
{
return ((dwFileAttr != INVALID_FILE_ATTRIBUTES) && (dwFileAttr & FILE_ATTRIBUTE_DIRECTORY));
}
// ----------------------------------------------------------------------------
// get wchar buffer with at least MAX_PATH_EXPLICIT size to minimize reallocations
// execept for len = 0, where no reallocation (except buffer is NULL) is done
__forceinline LPWSTR PTHAPI Path_WriteAccessBuf(HPATHL hpth, size_t len)
{
return StrgWriteAccessBuf((HSTRINGW)hpth, len ? max_sz(len, MAX_PATH_EXPLICIT) : 0);
}
__forceinline void PTHAPI Path_Sanitize(HPATHL hpth)
{
StrgSanitize((HSTRINGW)hpth);
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
/**************************************************/
/* */
/* DYNAMIC WIDCHAR LONG PATH */
/* */
/**************************************************/
HPATHL PTHAPI Path_Allocate(LPCWSTR path);
void PTHAPI Path_Release(HPATHL hpth);
void PTHAPI Path_Empty(HPATHL hpth, bool truncate);
int PTHAPI Path_Reset(HPATHL hpth, LPCWSTR path);
size_t PTHAPI Path_GetLength(HPATHL hpth);
HPATHL PTHAPI Path_Copy(const HPATHL hpth);
bool PTHAPI Path_Append(HPATHL hpth, LPCWSTR more);
void PTHAPI Path_Swap(HPATHL hpth1, HPATHL hpth2);
void PTHAPI Path_FreeExtra(HPATHL hpth_in_out, size_t keep_length);
bool PTHAPI Path_FixBackslashes(HPATHL hpth_in_out);
bool PTHAPI Path_Canonicalize(HPATHL hpth_in_out);
bool PTHAPI Path_IsEmpty(const HPATHL hpth);
inline bool PTHAPI Path_IsNotEmpty(const HPATHL hpth) { return !Path_IsEmpty(hpth); };
bool PTHAPI Path_IsRoot(const HPATHL hpth);
bool PTHAPI Path_IsValidUNC(const HPATHL hpth);
bool PTHAPI Path_IsExistingDirectory(const HPATHL hpth);
int PTHAPI Path_StrgComparePath(const HPATHL hpth1, const HPATHL hpth2, const HPATHL hpth_wrkdir);
bool PTHAPI Path_RemoveFileSpec(HPATHL hpth_in_out);
bool PTHAPI Path_RenameExtension(HPATHL hpth, LPCWSTR ext);
void PTHAPI Path_ExpandEnvStrings(HPATHL hpth_in_out);
void PTHAPI Path_UnExpandEnvStrings(HPATHL hpth);
void PTHAPI Path_GetModuleFilePath(HPATHL hpth_out);
void PTHAPI Path_GetAppDirectory(HPATHL hpth_out);
bool PTHAPI Path_IsRelative(const HPATHL hpath);
bool PTHAPI Path_IsPrefix(const HPATHL hprefix, const HPATHL hpth);
size_t PTHAPI Path_CommonPrefix(const HPATHL hpth1, const HPATHL hpth2, HPATHL hpfx_out);
LPCWSTR PTHAPI Path_FindFileName(const HPATHL hpth);
LPCWSTR PTHAPI Path_FindExtension(const HPATHL hpth);
bool PTHAPI Path_QuoteSpaces(HPATHL hpth_in_out, bool bForceQuotes);
void PTHAPI Path_UnQuoteSpaces(HPATHL hpth_in_out);
int PTHAPI Path_GetDriveNumber(const HPATHL hpth);
bool PTHAPI Path_IsUNC(const HPATHL hpth);
wchar_t PTHAPI Path_GetDriveLetterByNumber(const int number);
DWORD PTHAPI Path_GetFileAttributes(const HPATHL hpth);
bool PTHAPI Path_SetFileAttributes(HPATHL hpth, DWORD dwAttributes);
bool PTHAPI Path_StripToRoot(HPATHL hpth_in_out);
bool PTHAPI Path_GetCurrentDirectory(HPATHL hpth_out);
size_t PTHAPI Path_ToShortPathName(HPATHL hpth_in_out); // use only, if neccessary
size_t PTHAPI Path_GetLongPathNameEx(HPATHL hpth_in_out);
void PTHAPI Path_GetDisplayName(LPWSTR lpszDisplayName, const DWORD cchDisplayName, const HPATHL hpth, LPCWSTR repl, bool bStripPath);
bool PTHAPI Path_GetLnkPath(const HPATHL hLnkFilePth, HPATHL hResPath_out);
bool PTHAPI Path_IsLnkFile(const HPATHL hpth);
bool PTHAPI Path_IsLnkToDirectory(const HPATHL hlnk_pth, HPATHL hpth_out);
bool PTHAPI Path_CreateFavLnk(LPCWSTR lpszLinkName, const HPATHL hTargetPth, const HPATHL hDirPth);
bool PTHAPI Path_CreateDeskLnk(const HPATHL hDocumentPath, LPCWSTR pszDescription);
bool PTHAPI Path_BrowseDirectory(HWND hwndParent, LPCWSTR lpszTitle, HPATHL hpth_in_out, const HPATHL hbase, bool bNewDialogStyle);
bool PTHAPI Path_CanonicalizeEx(HPATHL hpth_in_out, const HPATHL hdir_rel_base);
size_t PTHAPI Path_NormalizeEx(HPATHL hpth_in_out, const HPATHL hpth_wrkdir, bool bRealPath, bool bSearchPathIfRelative);
bool PTHAPI Path_RelativePathTo(HPATHL hrecv, const HPATHL hfrom, DWORD attr_from, const HPATHL hto, DWORD attr_to);
void PTHAPI Path_RelativeToApp(HPATHL hpth_in_out, bool bSrcIsFile, bool bUnexpandEnv, bool bUnexpandMyDocs);
bool PTHAPI Path_GetKnownFolder(REFKNOWNFOLDERID rfid, HPATHL hpth_out);
void PTHAPI ExpandEnvironmentStrgs(HSTRINGW hstr, bool bStripQ);
// ============================================================================
// Duplicates for INTERMEDIATE DEV
// ============================================================================
bool PTHAPI Path_IsExistingFile(const HPATHL hpth);
bool PTHAPI PathIsExistingFile(LPCWSTR pszPath);
void PTHAPI Path_AbsoluteFromApp(HPATHL hpth_in_out, bool bExpandEnv);
void PTHAPI PathAbsoluteFromApp(LPWSTR lpszPath, const size_t cchPath, bool bExpandEnv);
// ============================================================================
// ============================================================================