Skip to content

Commit e0bb604

Browse files
committed
[UPD] update for unicode support (#45)
1 parent c6756a9 commit e0bb604

1 file changed

Lines changed: 40 additions & 48 deletions

File tree

ImGuiFileDialog.cpp

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -322,32 +322,19 @@ namespace IGFD
322322
std::vector<std::string> res;
323323

324324
#ifdef WIN32
325-
DWORD mydrives = 128;
326-
wchar_t wlpBuffer[128];
325+
const DWORD mydrives = 2048;
326+
char lpBuffer[2048];
327327
#define mini(a,b) (((a) < (b)) ? (a) : (b))
328-
DWORD countChars = mini(GetLogicalDriveStringsW(mydrives, wlpBuffer), 128 - 1);
328+
const DWORD countChars = mini(GetLogicalDriveStringsA(mydrives, lpBuffer), 2047);
329329
#undef mini
330330
if (countChars > 0)
331331
{
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-
}
332+
std::string var = std::string(lpBuffer, (size_t)countChars);
333+
replaceString(var, "\\", "");
334+
res = splitStringToVector(var, '\0', false);
345335
}
346336
#endif // WIN32
347337

348-
if (res.empty())
349-
std::cout << "fail to obtain Logical Drives" << std::endl;
350-
351338
return res;
352339
}
353340

@@ -369,6 +356,20 @@ namespace IGFD
369356
return bExists; // this is not a directory!
370357
}
371358

359+
#ifdef WIN32
360+
inline std::wstring wGetString(const char* str)
361+
{
362+
std::wstring ret;
363+
size_t sz;
364+
if (!dirent_mbstowcs_s(&sz, nullptr, 0, str, 0))
365+
{
366+
ret.resize(sz);
367+
dirent_mbstowcs_s(nullptr, (wchar_t*)ret.data(), sz, str, sz - 1);
368+
}
369+
return ret;
370+
}
371+
#endif
372+
372373
inline bool CreateDirectoryIfNotExist(const std::string& name)
373374
{
374375
bool res = false;
@@ -377,33 +378,30 @@ namespace IGFD
377378
{
378379
if (!IsDirectoryExist(name))
379380
{
380-
res = true;
381-
382381
#ifdef WIN32
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))
382+
std::wstring wname = wGetString(name.c_str());
383+
if (CreateDirectoryW(wname.c_str(), nullptr))
388384
{
389-
std::cout << "Error creating directory " << name << std::endl;
390-
res = false;
385+
res = true;
391386
}
392387
#elif defined(__EMSCRIPTEN__)
393-
std::string str = std::string("FS.mkdir('") + name.c_str() + "');";
388+
std::string str = std::string("FS.mkdir('") + name + "');";
394389
emscripten_run_script(str.c_str());
390+
res = true;
395391
#elif defined(UNIX)
396392
char buffer[PATH_MAX] = {};
397393
snprintf(buffer, PATH_MAX, "mkdir -p %s", name.c_str());
398394
const int dir_err = std::system(buffer);
399-
if (dir_err == -1)
395+
if (dir_err != -1)
400396
{
401-
std::cout << "Error creating directory " << name << std::endl;
402-
res = false;
397+
res = true;
403398
}
404399
#endif // defined(UNIX)
405-
}
400+
if (!res) {
401+
std::cout << "Error creating directory " << name << std::endl;
402+
}
406403
}
404+
}
407405

408406
return res;
409407
}
@@ -2164,24 +2162,18 @@ namespace IGFD
21642162
if (dir != nullptr)
21652163
{
21662164
#ifdef WIN32
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)
2165+
DWORD numchar = 0;
2166+
// numchar = GetFullPathNameA(path.c_str(), PATH_MAX, real_path, nullptr);
2167+
std::wstring wpath = wGetString(path.c_str());
2168+
numchar = GetFullPathNameW(wpath.c_str(), 0, nullptr, nullptr);
2169+
std::wstring fpath(numchar, 0);
2170+
GetFullPathNameW(wpath.c_str(), numchar, (wchar_t*)fpath.data(), nullptr);
2171+
int error = dirent_wcstombs_s(nullptr, real_path, PATH_MAX, fpath.c_str(), PATH_MAX - 1);
2172+
if (error)numchar = 0;
2173+
if (!numchar)
21762174
{
21772175
std::cout << "fail to obtain FullPathName " << path << std::endl;
21782176
}
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-
}
21852177
#elif defined(UNIX) // UNIX is LINUX or APPLE
21862178
char* numchar = realpath(path.c_str(), real_path);
21872179
#endif // defined(UNIX)

0 commit comments

Comments
 (0)