-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcunicode.cpp
277 lines (256 loc) · 9.59 KB
/
cunicode.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
#include <windows.h>
#include <stdlib.h>
#include <shellapi.h>
#include "cunicode.h"
char usysychecked=0;
BOOL usys()
{
if (!usysychecked) {
OSVERSIONINFO vx;
vx.dwOSVersionInfoSize=sizeof(vx);
GetVersionEx(&vx);
if (vx.dwPlatformId==VER_PLATFORM_WIN32_NT)
usysychecked=1;
else
usysychecked=2;
}
return (usysychecked==1);
}
char* walcopy(char* outname,WCHAR* inname,int maxlen)
{
if (inname) {
WideCharToMultiByte(CP_ACP,0,inname,-1,outname,maxlen,NULL,NULL);
outname[maxlen]=0;
return outname;
} else
return NULL;
}
WCHAR* awlcopy(WCHAR* outname,char* inname,int maxlen)
{
if (inname) {
MultiByteToWideChar(CP_ACP,0,inname,-1,outname,maxlen);
outname[maxlen]=0;
return outname;
} else
return NULL;
}
WCHAR* wcslcpy(WCHAR *str1,const WCHAR *str2,int imaxlen)
{
if ((int)wcslen(str2)>=imaxlen-1) {
wcsncpy(str1,str2,imaxlen-1);
str1[imaxlen-1]=0;
} else
wcscpy(str1,str2);
return str1;
}
WCHAR* wcslcat(wchar_t *str1,const WCHAR *str2,int imaxlen)
{
int l1=(int)wcslen(str1);
if ((int)wcslen(str2)+l1>=imaxlen-1) {
wcsncpy(str1+l1,str2,imaxlen-1-l1);
str1[imaxlen-1]=0;
} else
wcscat(str1,str2);
return str1;
}
// return true if name wasn't cut
BOOL MakeExtraLongNameW(WCHAR* outbuf,const WCHAR* inbuf,int maxlen)
{
if (wcslen(inbuf)>259) {
if (inbuf[0]=='\\' && inbuf[1]=='\\') { // UNC-Path! Use \\?\UNC\server\share\subdir\name.ext
wcslcpy(outbuf,L"\\\\?\\UNC",maxlen);
wcslcat(outbuf,inbuf+1,maxlen);
} else {
wcslcpy(outbuf,L"\\\\?\\",maxlen);
wcslcat(outbuf,inbuf,maxlen);
}
} else
wcslcpy(outbuf,inbuf,maxlen);
return (int)wcslen(inbuf)+4<=maxlen;
}
/***********************************************************************************************/
void copyfinddatawa(WIN32_FIND_DATA *lpFindFileDataA,WIN32_FIND_DATAW *lpFindFileDataW)
{
walcopy(lpFindFileDataA->cAlternateFileName,lpFindFileDataW->cAlternateFileName,sizeof(lpFindFileDataW->cAlternateFileName)-1);
walcopy(lpFindFileDataA->cFileName,lpFindFileDataW->cFileName,sizeof(lpFindFileDataW->cFileName)-1);
lpFindFileDataA->dwFileAttributes=lpFindFileDataW->dwFileAttributes;
lpFindFileDataA->dwReserved0=lpFindFileDataW->dwReserved0;
lpFindFileDataA->dwReserved1=lpFindFileDataW->dwReserved1;
lpFindFileDataA->ftCreationTime=lpFindFileDataW->ftCreationTime;
lpFindFileDataA->ftLastAccessTime=lpFindFileDataW->ftLastAccessTime;
lpFindFileDataA->ftLastWriteTime=lpFindFileDataW->ftLastWriteTime;
lpFindFileDataA->nFileSizeHigh=lpFindFileDataW->nFileSizeHigh;
lpFindFileDataA->nFileSizeLow=lpFindFileDataW->nFileSizeLow;
}
void copyfinddataaw(WIN32_FIND_DATAW *lpFindFileDataW,WIN32_FIND_DATA *lpFindFileDataA)
{
awlcopy(lpFindFileDataW->cAlternateFileName,lpFindFileDataA->cAlternateFileName,countof(lpFindFileDataW->cAlternateFileName)-1);
awlcopy(lpFindFileDataW->cFileName,lpFindFileDataA->cFileName,countof(lpFindFileDataW->cFileName)-1);
lpFindFileDataW->dwFileAttributes=lpFindFileDataA->dwFileAttributes;
lpFindFileDataW->dwReserved0=lpFindFileDataA->dwReserved0;
lpFindFileDataW->dwReserved1=lpFindFileDataA->dwReserved1;
lpFindFileDataW->ftCreationTime=lpFindFileDataA->ftCreationTime;
lpFindFileDataW->ftLastAccessTime=lpFindFileDataA->ftLastAccessTime;
lpFindFileDataW->ftLastWriteTime=lpFindFileDataA->ftLastWriteTime;
lpFindFileDataW->nFileSizeHigh=lpFindFileDataA->nFileSizeHigh;
lpFindFileDataW->nFileSizeLow=lpFindFileDataA->nFileSizeLow;
}
/***********************************************************************************************/
BOOL CopyFileT(WCHAR* lpExistingFileName,WCHAR* lpNewFileName,BOOL bFailIfExists)
{
if (usys()) {
WCHAR wbuf1[wdirtypemax+longnameprefixmax],wbuf2[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf1,lpExistingFileName,wdirtypemax-1+longnameprefixmax) &&
MakeExtraLongNameW(wbuf2,lpNewFileName,wdirtypemax-1+longnameprefixmax))
return CopyFileW(wbuf1,wbuf2,bFailIfExists);
else
return false;
} else {
char buf1[MAX_PATH],buf2[MAX_PATH];
return CopyFile(wafilenamecopy(buf1,lpExistingFileName),wafilenamecopy(buf2,lpNewFileName),bFailIfExists);
}
}
BOOL CreateDirectoryT(WCHAR* lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpPathName,wdirtypemax-1+longnameprefixmax))
return CreateDirectoryW(wbuf,lpSecurityAttributes);
else
return false;
} else {
char buf[MAX_PATH];
return CreateDirectory(wafilenamecopy(buf,lpPathName),lpSecurityAttributes);
}
}
BOOL RemoveDirectoryT(WCHAR* lpPathName)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpPathName,wdirtypemax-1+longnameprefixmax))
return RemoveDirectoryW(wbuf);
else
return false;
} else {
char buf[MAX_PATH];
return RemoveDirectory(wafilenamecopy(buf,lpPathName));
}
}
BOOL DeleteFileT(WCHAR* lpFileName)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpFileName,wdirtypemax-1+longnameprefixmax))
return DeleteFileW(wbuf);
else
return false;
} else {
char buf[MAX_PATH];
return DeleteFile(wafilenamecopy(buf,lpFileName));
}
}
BOOL MoveFileT(WCHAR* lpExistingFileName,WCHAR* lpNewFileName)
{
if (usys()) {
WCHAR wbuf1[wdirtypemax+longnameprefixmax],wbuf2[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf1,lpExistingFileName,wdirtypemax-1+longnameprefixmax) &&
MakeExtraLongNameW(wbuf2,lpNewFileName,wdirtypemax-1+longnameprefixmax))
return MoveFileW(wbuf1,wbuf2);
else
return false;
} else {
char buf1[MAX_PATH],buf2[MAX_PATH];
return MoveFile(wafilenamecopy(buf1,lpExistingFileName),wafilenamecopy(buf2,lpNewFileName));
}
}
BOOL SetFileAttributesT(WCHAR* lpFileName,DWORD dwFileAttributes)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpFileName,wdirtypemax-1+longnameprefixmax))
return SetFileAttributesW(wbuf,dwFileAttributes);
else
return false;
} else {
char buf[MAX_PATH];
return SetFileAttributes(wafilenamecopy(buf,lpFileName),dwFileAttributes);
}
}
HANDLE CreateFileT(WCHAR* lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpFileName,wdirtypemax-1+longnameprefixmax))
return CreateFileW(wbuf,dwDesiredAccess,dwShareMode,
lpSecurityAttributes,dwCreationDisposition,
dwFlagsAndAttributes,hTemplateFile);
else
return INVALID_HANDLE_VALUE;
} else {
char buf[MAX_PATH];
return CreateFile(wafilenamecopy(buf,lpFileName),dwDesiredAccess,dwShareMode,
lpSecurityAttributes,dwCreationDisposition,
dwFlagsAndAttributes,hTemplateFile);
}
}
UINT ExtractIconExT(WCHAR* lpszFile,int nIconIndex,HICON *phiconLarge,HICON *phiconSmall,UINT nIcons)
{
if (usys()) { // Unfortunately this function cannot handle names longer than 259 characters
return ExtractIconExW(lpszFile,nIconIndex,phiconLarge,phiconSmall,nIcons);
} else {
char buf[MAX_PATH];
return ExtractIconEx(wafilenamecopy(buf,lpszFile),nIconIndex,phiconLarge,phiconSmall,nIcons);
}
}
HANDLE FindFirstFileT(WCHAR* lpFileName,LPWIN32_FIND_DATAW lpFindFileData)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpFileName,wdirtypemax-1+longnameprefixmax))
return FindFirstFileW(wbuf,lpFindFileData);
else
return INVALID_HANDLE_VALUE;
} else {
char buf[MAX_PATH];
WIN32_FIND_DATA FindFileDataA;
HANDLE retval=FindFirstFile(wafilenamecopy(buf,lpFileName),&FindFileDataA);
if (retval!=INVALID_HANDLE_VALUE) {
awlcopy(lpFindFileData->cAlternateFileName,FindFileDataA.cAlternateFileName,countof(lpFindFileData->cAlternateFileName)-1);
awlcopy(lpFindFileData->cFileName,FindFileDataA.cFileName,countof(lpFindFileData->cFileName)-1);
lpFindFileData->dwFileAttributes=FindFileDataA.dwFileAttributes;
lpFindFileData->dwReserved0=FindFileDataA.dwReserved0;
lpFindFileData->dwReserved1=FindFileDataA.dwReserved1;
lpFindFileData->ftCreationTime=FindFileDataA.ftCreationTime;
lpFindFileData->ftLastAccessTime=FindFileDataA.ftLastAccessTime;
lpFindFileData->ftLastWriteTime=FindFileDataA.ftLastWriteTime;
lpFindFileData->nFileSizeHigh=FindFileDataA.nFileSizeHigh;
lpFindFileData->nFileSizeLow=FindFileDataA.nFileSizeLow;
}
return retval;
}
}
BOOL FindNextFileT(HANDLE hFindFile,LPWIN32_FIND_DATAW lpFindFileData)
{
if (usys()) {
return FindNextFileW(hFindFile,lpFindFileData);
} else {
WIN32_FIND_DATA FindFileDataA;
memset(&FindFileDataA,0,sizeof(FindFileDataA));
BOOL retval=FindNextFile(hFindFile,&FindFileDataA);
if (retval) {
awlcopy(lpFindFileData->cAlternateFileName,FindFileDataA.cAlternateFileName,countof(lpFindFileData->cAlternateFileName)-1);
awlcopy(lpFindFileData->cFileName,FindFileDataA.cFileName,countof(lpFindFileData->cFileName)-1);
lpFindFileData->dwFileAttributes=FindFileDataA.dwFileAttributes;
lpFindFileData->dwReserved0=FindFileDataA.dwReserved0;
lpFindFileData->dwReserved1=FindFileDataA.dwReserved1;
lpFindFileData->ftCreationTime=FindFileDataA.ftCreationTime;
lpFindFileData->ftLastAccessTime=FindFileDataA.ftLastAccessTime;
lpFindFileData->ftLastWriteTime=FindFileDataA.ftLastWriteTime;
lpFindFileData->nFileSizeHigh=FindFileDataA.nFileSizeHigh;
lpFindFileData->nFileSizeLow=FindFileDataA.nFileSizeLow;
}
return retval;
}
}