-
Notifications
You must be signed in to change notification settings - Fork 117
/
ADM_folder_vs.cpp
executable file
·298 lines (232 loc) · 8.06 KB
/
ADM_folder_vs.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/***************************************************************************
copyright : (C) 2016 by mean
email : fixounet@free.fr
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <errno.h>
#include <string>
#include <algorithm>
#include <io.h>
#include <direct.h>
#include <shlobj.h>
#include <fcntl.h>
#include "ADM_win32.h"
#include "ADM_default.h"
extern char *ADM_getRelativePath(const char *base0, const char *base1, const char *base2, const char *base3);
#undef fread
#undef fwrite
#undef fopen
#undef fclose
char *ADM_getInstallRelativePath(const char *base1, const char *base2, const char *base3)
{
wchar_t wcModuleName[MAX_PATH];
GetModuleFileNameW(0, wcModuleName, sizeof(wcModuleName) / sizeof(wchar_t));
int len = wideCharStringToUtf8(wcModuleName, -1, NULL);
char *moduleName = new char[len];
wideCharStringToUtf8(wcModuleName, -1, moduleName);
char *slash = strrchr(moduleName, '\\');
if (slash)
*slash = '\0';
char *relativePath = ADM_getRelativePath(moduleName, base1, base2, base3);
delete [] moduleName;
return relativePath;
}
/**
\fn ADM_fopen
\brief utf8 aware fopen, so that we can use utf8 string even on win32
*/
FILE *ADM_fopen(const char *file, const char *mode)
{
// Override fopen to handle Unicode filenames and to ensure exclusive access when initially writing to a file.
int fileNameLength = utf8StringToWideChar(file, -1, NULL);
wchar_t *wcFile = new wchar_t[fileNameLength];
int creation = 0, access = 0;
HANDLE hFile;
utf8StringToWideChar(file, -1, wcFile);
while (true)
{
if (strchr(mode, 'w'))
{
creation = CREATE_ALWAYS;
access = GENERIC_WRITE;
if (strchr(mode, '+'))
access |= GENERIC_READ;
}
else if (strchr(mode, 'r'))
{
creation = OPEN_EXISTING;
access = GENERIC_READ;
if (strchr(mode, '+'))
access = GENERIC_WRITE;
}
else if (strchr(mode, 'a'))
{
creation = OPEN_ALWAYS;
access = GENERIC_WRITE;
if (strchr(mode, '+'))
access |= GENERIC_READ;
}
if (creation & GENERIC_WRITE)
{
hFile = CreateFileW(wcFile, access, 0, NULL, creation, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
break;
else
CloseHandle(hFile);
}
hFile = CreateFileW(wcFile, access, FILE_SHARE_READ, NULL, creation, 0, NULL);
break;
}
delete [] wcFile;
if (hFile == INVALID_HANDLE_VALUE)
return NULL;
else
return _fdopen(_open_osfhandle((intptr_t)hFile, 0), mode);
}
extern "C"
{
// libavformat uses open (in the file_open function) so we need to override that too.
// Following the same rules as ADM_fopen.
int ADM_open(const char *path, int oflag, ...)
{
int fileNameLength = utf8StringToWideChar(path, -1, NULL);
wchar_t *wcFile = new wchar_t[fileNameLength];
int creation = 0, access = 0;
HANDLE hFile;
utf8StringToWideChar(path, -1, wcFile);
delete [] wcFile;
if (oflag & O_WRONLY || oflag & O_RDWR)
{
access = GENERIC_WRITE;
if (oflag & O_RDWR)
access |= GENERIC_READ;
if (oflag & O_CREAT)
{
if (oflag & O_EXCL)
creation = CREATE_NEW;
else if (oflag & O_TRUNC)
creation = CREATE_ALWAYS;
else
creation = OPEN_ALWAYS;
}
else if (oflag & O_TRUNC)
creation = TRUNCATE_EXISTING;
}
else if (oflag & O_RDONLY)
creation = OPEN_EXISTING;
if (creation & GENERIC_WRITE)
{
hFile = CreateFileW(wcFile, access, 0, NULL, creation, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return -1;
else
CloseHandle(hFile);
}
hFile = CreateFileW(wcFile, access, FILE_SHARE_READ, NULL, creation, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return -1;
else
return _open_osfhandle((intptr_t)hFile, oflag);
}
}
/*----------------------------------------
Create a directory
If it already exists, do nothing
------------------------------------------*/
uint8_t ADM_mkdir(const char *dirname)
{
int dirNameLength = utf8StringToWideChar(dirname, -1, NULL);
wchar_t *dirname2 = new wchar_t[dirNameLength];
utf8StringToWideChar(dirname, -1, dirname2);
int er = _wmkdir(dirname2);
delete[] dirname2;
uint8_t retVal = 0;
if (er != -1)
retVal = true;
else
{
if (errno == EEXIST)
retVal = true;
else
{
ADM_warning("Failed to create folder %s with error %d\n", dirname, errno);
retVal = false;
}
}
return retVal;
}
// Convert string from Wide Char to ANSI code page
static int wideCharStringToAnsi(const wchar_t *wideCharString, int wideCharStringLength, char *ansiString, const char *filler)
{
int flags = WC_COMPOSITECHECK;
if (filler)
flags |= WC_NO_BEST_FIT_CHARS | WC_DEFAULTCHAR;
int ansiStringLen = WideCharToMultiByte(CP_ACP, flags, wideCharString, wideCharStringLength, NULL, 0, filler, NULL);
if (ansiString)
WideCharToMultiByte(CP_ACP, flags, wideCharString, wideCharStringLength, ansiString, ansiStringLen, filler, NULL);
return ansiStringLen;
}
/**
* \fn buildDirectoryContent
* \brief Returns the content of a dir with the extension ext. The receiving vector must be allocated by caller
*/
uint8_t buildDirectoryContent(const char *base, std::vector<std::string> *list, const char *ext)
{
std::string joker = base;
joker += "/*.";
joker += ext;
list->clear();
int dirNameLength = utf8StringToWideChar(joker.c_str(), -1, NULL);
wchar_t *base2 = new wchar_t[dirNameLength];
utf8StringToWideChar(joker.c_str(), -1, base2);
//--
HANDLE hFind;
WIN32_FIND_DATAW FindFileData;
hFind = FindFirstFileW(base2,&FindFileData);
if(hFind == INVALID_HANDLE_VALUE)
{
ADM_warning("Cannot list content of %s\n", base);
delete[] base2;
return true;
}
do
{
WCHAR *wname = FindFileData.cFileName;
//int wideCharStringToAnsi(const wchar_t *wideCharString, int wideCharStringLength, char *ansiString, const char *filler)
int nameLength = wideCharStringToUtf8(wname, -1, NULL);
char *shortName = new char[nameLength];
nameLength = wideCharStringToUtf8(wname, -1, shortName);
std::string item = base;
item += ADM_SEPARATOR;
item += shortName;
list->push_back(item);
delete[] shortName;
} while (FindNextFileW(hFind, &FindFileData));
FindClose(hFind);
delete[] base2;
std::sort(list->begin(),list->end());
return true;
}
/**
\fn ADM_copyFile
*/
uint8_t ADM_renameFile(const char *source, const char *target)
{
int sourceFileNameLength = utf8StringToWideChar(source, -1, NULL);
int targetFileNameLength = utf8StringToWideChar(target, -1, NULL);
wchar_t *wcFileSource=(wchar_t*)_alloca(sourceFileNameLength*sizeof(wchar_t));
wchar_t *wcFileTarget= (wchar_t*)_alloca(targetFileNameLength * sizeof(wchar_t));
utf8StringToWideChar(source, -1, wcFileSource);
utf8StringToWideChar(target, -1, wcFileTarget);
if(!_wrename(wcFileSource,wcFileTarget)) return true;
ADM_error("Failed to rename %s to %s\n",source,target);
return false;
}
// EOF