-
Notifications
You must be signed in to change notification settings - Fork 1
/
PathName.cpp
283 lines (220 loc) · 7.08 KB
/
PathName.cpp
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
#include "stdafx.h"
#include "PathName.h"
BOOL ParseFileFilter(CStringArray & arrDescription, CStringArray & arrExtension, LPCTSTR lpszFilter)
{
TCHAR szFilter[2048]; strcpy( szFilter, lpszFilter );
if( szFilter[strlen(szFilter)-1] != '|' ) strcat( szFilter, "|" );
arrDescription.RemoveAll(); arrExtension.RemoveAll();
TCHAR * pFilter = szFilter; INT nLen = strlen(szFilter);
BOOL bDescription = TRUE; CString szDescription;
for(INT i = 0; i < nLen; i++) {
if( szFilter[i] == '|' ) {
szFilter[i] = '\0';
if( bDescription ) {
szDescription = pFilter;
bDescription = FALSE;
} else {
if( strlen(szDescription) && strlen(pFilter) ) {
arrDescription.Add( szDescription );
arrExtension.Add( pFilter );
}
bDescription = TRUE;
}
pFilter += strlen(pFilter) + 1;
}
}
return TRUE;
}
BOOL MatchFileFilter(LPCTSTR lpszPath, LPCTSTR lpszFilter)
{
TCHAR szFilter[2048]; strcpy( szFilter, lpszFilter );
if( szFilter[strlen(szFilter)-1] != ';' ) strcat( szFilter, ";" );
TCHAR * pFilter = szFilter; INT nLen = strlen(szFilter);
for(INT i = 0; i < nLen; i++) {
if( szFilter[i] == ';' ) {
szFilter[i] = '\0';
INT nFilter = strlen( pFilter );
INT nPath = strlen( lpszPath );
if( ! stricmp(pFilter, "*.*") ) return TRUE;
if( ! strnicmp(pFilter, "*.", 2) && (nPath >= nFilter-1)
&& ! stricmp(pFilter+1, lpszPath+nPath-(nFilter-1)) ) return TRUE;
pFilter += strlen(pFilter) + 1;
}
}
return FALSE;
}
BOOL VerifyPathName(LPCTSTR lpszPath)
{
CFileFind find;
return find.FindFile(lpszPath);
}
BOOL VerifyFilePath(LPCTSTR lpszPath)
{
CFileFind find;
BOOL bFound = find.FindFile(lpszPath);
while( bFound ) {
bFound = find.FindNextFile();
if( ! find.IsDirectory() ) return TRUE;
}
return FALSE;
}
BOOL FindAllFilePath(CStringArray & arrPath, LPCTSTR lpszPath)
{
CFileFind find; BOOL bResult = FALSE;
BOOL bFound = find.FindFile(lpszPath);
while( bFound ) {
bFound = find.FindNextFile();
if( ! find.IsDirectory() && ! find.IsHidden() ) {
arrPath.Add(find.GetFilePath());
bResult = TRUE;
}
}
return bResult;
}
CString QuotePathName(LPCTSTR lpszPathName)
{
INT nLen = strlen(lpszPathName);
CString szPathName;
if( nLen >= 2 && lpszPathName[0] == '\"' && lpszPathName[nLen-1] == '\"' ) szPathName = lpszPathName;
else szPathName.Format("\"%s\"", lpszPathName);
return szPathName;
}
CString ChopDirectory(LPCTSTR lpszDirectory)
{
INT nLen = strlen(lpszDirectory);
CString szDirectory = lpszDirectory;
if( nLen >= 1 && lpszDirectory[nLen-1] == '\\' ) return szDirectory.Mid(0, nLen-1);
else return szDirectory;
}
CString RemotePathToLocalPath(LPCTSTR lpszPathName)
{
CString szPathName = lpszPathName;
szPathName.Replace( "%", "%25" ); szPathName.Replace( '/', '\\' );
szPathName.Replace( "*", "%2A" ); szPathName.Replace( "?", "%3F" );
return szPathName;
}
CString LocalPathToRemotePath(LPCTSTR lpszPathName)
{
CString szPathName = lpszPathName;
szPathName.Replace( "%2A", "*" ); szPathName.Replace( "%3F", "?" );
szPathName.Replace( "%25", "%" ); szPathName.Replace( '\\' , '/' );
return szPathName;
}
CString GetFileDirectory(LPCTSTR lpszPath)
{
CString szTemp = lpszPath;
INT nLen = szTemp.GetLength(); if( ! nLen ) return "";
if( szTemp[nLen-1] == '\\' ) szTemp.SetAt(nLen-1, '\0');
if( szTemp[nLen-1] == '/' ) szTemp.SetAt(nLen-1, '\0');
INT nPos = szTemp.ReverseFind( '\\' );
if( nPos < 0 ) nPos = szTemp.ReverseFind( '/' );
if( nPos >= 0 ) return szTemp.Mid( 0, nPos );
return "";
}
CString GetFileName(LPCTSTR lpszPath)
{
CString szTemp = lpszPath;
INT nLen = szTemp.GetLength(); if( ! nLen ) return "";
if( szTemp[nLen-1] == '\\' ) szTemp.SetAt(nLen-1, '\0');
if( szTemp[nLen-1] == '/' ) szTemp.SetAt(nLen-1, '\0');
INT nPos = szTemp.ReverseFind( '\\' );
if( nPos < 0 ) nPos = szTemp.ReverseFind( '/' );
if( nPos >= 0 ) return szTemp.Mid( nPos + 1 );
return lpszPath;
}
CString GetFileTitle(LPCTSTR lpszPath)
{
CString szTemp = GetFileName(lpszPath);
INT nPos = szTemp.ReverseFind( '.' );
if( nPos >= 0 ) return szTemp.Mid( 0, nPos );
else return szTemp;
}
CString GetFileExtension(LPCTSTR lpszPath)
{
CString szTemp = GetFileName(lpszPath);
INT nPos = szTemp.ReverseFind( '.' );
if( nPos >= 0 ) return szTemp.Mid( nPos );
else return "";
}
CString GetShortPathName(LPCTSTR lpszPath)
{
TCHAR szShortPath[2048]; szShortPath[0] = '\0';
GetShortPathName(lpszPath, szShortPath, 2048);
return szShortPath;
}
/* not compatable in Win95 & WinNT
CString GetLongPathName(LPCTSTR lpszPath)
{
TCHAR szLongPath[2048]; szLongPath[0] = '\0';
GetLongPathName(lpszPath, szLongPath, 2048);
return szLongPath;
} */
CString GetLongPathName(LPCTSTR lpszPath)
{
CString szTemp, szFile, szPath = lpszPath, szLongPath;
WIN32_FIND_DATA findData; HANDLE hFind; BOOL bDir;
INT nFwd, nIdx, nLen = szPath.GetLength();
if( nLen < 3 ) return szPath;
if( szPath[0] == '\\' ) { // it's UNC path name
nIdx = 2; // skip first two backslashes
nIdx = szPath.Find('\\', nIdx) + 1; // skip UNC server name
nIdx = szPath.Find('\\', nIdx) + 1; // skip UNC share name
} else { // it's normal path name
nIdx = 3; // skip drive letter
}
// get UNC share name or drive name
szLongPath = szPath.Left(nIdx);
while( nIdx < nLen ) {
nFwd = nIdx; while( nFwd < nLen && szPath[nFwd] != '\\' ) nFwd++;
szTemp = szPath.Left(nFwd);
szFile = szPath.Mid(nIdx, nFwd-nIdx);
if( nFwd < nLen && szPath[nFwd] == '\\' ) { bDir = TRUE; nIdx = nFwd+1; }
else { bDir = FALSE; nIdx = nFwd; }
if( ! szFile.Compare("." ) ) {
// do nothing
} else if( ! szFile.Compare("..") ) {
INT nPre = szLongPath.GetLength() - 2; // skip last backslash
while( nPre >= 0 && szLongPath[nPre] != '\\' ) nPre--;
szLongPath = szLongPath.Left(nPre) + "\\";
} else {
hFind = FindFirstFile( szTemp, & findData );
if( hFind == INVALID_HANDLE_VALUE ) return szPath;
FindClose( hFind );
szLongPath += findData.cFileName;
if( bDir ) szLongPath += "\\";
}
}
return szLongPath;
}
BOOL TouchFile(LPCTSTR lpszPath)
{
CString szTemp, szFile, szPath = lpszPath;
WIN32_FIND_DATA findData; HANDLE hFind; BOOL bDir;
INT nFwd, nIdx, nLen = szPath.GetLength();
if( nLen < 3 ) return FALSE;
if( szPath[0] == '\\' ) { // it's UNC path name
nIdx = 2; // skip first two backslashes
nIdx = szPath.Find('\\', nIdx) + 1; // skip UNC server name
nIdx = szPath.Find('\\', nIdx) + 1; // skip UNC share name
} else { // it's normal path name
nIdx = 3; // skip drive letter
}
while( nIdx < nLen ) {
nFwd = nIdx; while( nFwd < nLen && szPath[nFwd] != '\\' ) nFwd++;
szTemp = szPath.Left(nFwd);
szFile = szPath.Mid(nIdx, nFwd-nIdx);
if( nFwd < nLen && szPath[nFwd] == '\\' ) { bDir = TRUE; nIdx = nFwd+1; }
else { bDir = FALSE; nIdx = nFwd; }
hFind = FindFirstFile( szTemp, & findData );
if( hFind == INVALID_HANDLE_VALUE ) {
if( ! bDir ) {
HANDLE hFile = CreateFile( szTemp, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
if( hFile == INVALID_HANDLE_VALUE ) return FALSE;
CloseHandle( hFile );
} else if( ! CreateDirectory( szTemp, NULL ) ) {
return FALSE;
}
} else FindClose( hFind );
}
return TRUE;
}