Skip to content

Commit c6756a9

Browse files
committed
[ADD] : add unicode support (WIP)
1 parent 7239e99 commit c6756a9

1 file changed

Lines changed: 86 additions & 12 deletions

File tree

ImGuiFileDialog.cpp

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,43 @@ namespace IGFD
245245
return strcoll((*a)->d_name, (*b)->d_name);
246246
}
247247

248+
#ifdef WIN32
249+
inline bool wreplaceString(std::wstring& str, const std::wstring& oldStr, const std::wstring& newStr)
250+
{
251+
bool found = false;
252+
size_t pos = 0;
253+
while ((pos = str.find(oldStr, pos)) != std::wstring::npos)
254+
{
255+
found = true;
256+
str.replace(pos, oldStr.length(), newStr);
257+
pos += newStr.length();
258+
}
259+
return found;
260+
}
261+
262+
inline std::vector<std::wstring> wsplitStringToVector(const std::wstring& text, char delimiter, bool pushEmpty)
263+
{
264+
std::vector<std::wstring> arr;
265+
if (!text.empty())
266+
{
267+
std::wstring::size_type start = 0;
268+
std::wstring::size_type end = text.find(delimiter, start);
269+
while (end != std::wstring::npos)
270+
{
271+
std::wstring token = text.substr(start, end - start);
272+
if (!token.empty() || (token.empty() && pushEmpty)) //-V728
273+
arr.push_back(token);
274+
start = end + 1;
275+
end = text.find(delimiter, start);
276+
}
277+
std::wstring token = text.substr(start);
278+
if (!token.empty() || (token.empty() && pushEmpty)) //-V728
279+
arr.push_back(token);
280+
}
281+
return arr;
282+
}
283+
#endif
284+
248285
inline bool replaceString(std::string& str, const std::string& oldStr, const std::string& newStr)
249286
{
250287
bool found = false;
@@ -285,19 +322,32 @@ namespace IGFD
285322
std::vector<std::string> res;
286323

287324
#ifdef WIN32
288-
DWORD mydrives = 2048;
289-
char lpBuffer[2048];
325+
DWORD mydrives = 128;
326+
wchar_t wlpBuffer[128];
290327
#define mini(a,b) (((a) < (b)) ? (a) : (b))
291-
DWORD countChars = mini(GetLogicalDriveStringsA(mydrives, lpBuffer), 2047);
328+
DWORD countChars = mini(GetLogicalDriveStringsW(mydrives, wlpBuffer), 128 - 1);
292329
#undef mini
293330
if (countChars > 0)
294331
{
295-
std::string var = std::string(lpBuffer, (size_t)countChars);
296-
replaceString(var, "\\", "");
297-
res = splitStringToVector(var, '\0', false);
332+
std::wstring var = std::wstring(wlpBuffer, (size_t)countChars);
333+
wreplaceString(var, L"\\", L"");
334+
auto arr = wsplitStringToVector(var, '\0', false);
335+
size_t n = 0;
336+
char lpBuffer[128];
337+
for (auto a : arr)
338+
{
339+
int error = dirent_wcstombs_s(&n, lpBuffer, 128 - 1, a.data(), a.size());
340+
if (!error && n)
341+
{
342+
res.push_back(std::string(lpBuffer, n));
343+
}
344+
}
298345
}
299346
#endif // WIN32
300347

348+
if (res.empty())
349+
std::cout << "fail to obtain Logical Drives" << std::endl;
350+
301351
return res;
302352
}
303353

@@ -330,7 +380,15 @@ namespace IGFD
330380
res = true;
331381

332382
#ifdef WIN32
333-
CreateDirectoryA(name.c_str(), nullptr);
383+
//CreateDirectoryA(name.c_str(), nullptr);
384+
size_t n;
385+
std::vector<wchar_t> wn(name.size() + 1);
386+
int error = dirent_mbstowcs_s(&n, wn.data(), wn.size(), name.c_str(), wn.size());
387+
if (error || !CreateDirectoryW(wn.data(), nullptr))
388+
{
389+
std::cout << "Error creating directory " << name << std::endl;
390+
res = false;
391+
}
334392
#elif defined(__EMSCRIPTEN__)
335393
std::string str = std::string("FS.mkdir('") + name.c_str() + "');";
336394
emscripten_run_script(str.c_str());
@@ -2095,19 +2153,35 @@ namespace IGFD
20952153
if (s_fs_root == path)
20962154
path += PATH_SEP;
20972155
#endif // WIN32
2156+
char real_path[PATH_MAX];
20982157
DIR* dir = opendir(path.c_str());
2099-
char real_path[PATH_MAX];
2100-
2101-
if (nullptr == dir)
2158+
if (dir == nullptr)
21022159
{
21032160
path = ".";
21042161
dir = opendir(path.c_str());
21052162
}
21062163

2107-
if (nullptr != dir)
2164+
if (dir != nullptr)
21082165
{
21092166
#ifdef WIN32
2110-
size_t numchar = GetFullPathNameA(path.c_str(), PATH_MAX - 1, real_path, nullptr);
2167+
// numchar = GetFullPathNameA(path.c_str(), PATH_MAX - 1, real_path, nullptr);
2168+
size_t numchar = 0;
2169+
size_t n = 0;
2170+
wchar_t wreal_path[PATH_MAX];
2171+
std::vector<wchar_t> wn(path.size() + 1);
2172+
int error = dirent_mbstowcs_s(&n, wn.data(), wn.size(), path.c_str(), wn.size());
2173+
if (!error)
2174+
numchar = GetFullPathNameW(wn.data(), PATH_MAX - 1, wreal_path, nullptr);
2175+
if (error || !numchar)
2176+
{
2177+
std::cout << "fail to obtain FullPathName " << path << std::endl;
2178+
}
2179+
else
2180+
{
2181+
error = dirent_wcstombs_s(&n, real_path, PATH_MAX - 1, wreal_path, numchar);
2182+
if (error)
2183+
std::cout << "fail to obtain FullPathName " << path << std::endl;
2184+
}
21112185
#elif defined(UNIX) // UNIX is LINUX or APPLE
21122186
char* numchar = realpath(path.c_str(), real_path);
21132187
#endif // defined(UNIX)

0 commit comments

Comments
 (0)