Skip to content

A better way to implement IMGUI::filesystem. #13

@daysofbeingwild

Description

@daysofbeingwild

My operating system is Windows 10 64-bits and the system's language is chinese.
Here I have a folder, its cotents show like these:
1
When I use the IMGUIFs::Dialog and enter this folder, I got:

2
It seems that:

  1. All files or directries with non-ASCII name are not listed;
  2. Files with long name are shrinked.

Both 1 and 2 are caused by using of API scandir. I hadn't known this API before. But when I debug into the Directory::GetFiles and Directory::GetDirectories I make sure it is the causing reason.

(By the way, I got compiler error when I compiler filesystem.cpp under Visual Studio 2015 X64 model. I fix it by adding #ifdef _WIN32 #include <Windows.h> #endif piror to the first include directive in this file.)

For myself, I modified the filesystem.cpp to resolve this as following:
Add supporting header and API:

#ifdef IMGUI_FILESYSTEM_USE_STD_FILESYSTEM
#include <experimental\filesystem> // VS-builtin experimental library for c++17
#include <boost\locale.hpp> // encoding related
/* the so-called `exec_char_enc` is a kind of encoding that C++'s specified compiler use in C++ standard API such as printf std::cout and so on. When you write "IMGUI", the string has this encoding. ` */
inline std::string to_exec_char_enc(
	const std::string &text,
	const std::string &fromencoding)
{
	return boost::locale::conv::between(text, "GB2312", fromencoding);
}

inline std::string u8_to_exec_char_enc(
	const std::string &text)
{
	return to_exec_char_enc(text, "UTF-8");
}
#endif

Subsitute the code fragments in Directory::GetFiles or Directory::GetDirectorys by:

#ifdef IMGUI_FILESYSTEM_USE_STD_FILESYSTEM
namespace fs = std::experimental::filesystem;
		{
			auto srcdir = u8_to_exec_char_enc(directoryName);
			result.clear();
			if (pOptionalNamesOut)
				pOptionalNamesOut->clear();
			fs::path rootpath(srcdir);
			std::error_code errc;
			auto r = fs::directory_iterator(rootpath, errc);
			if (errc)
				return;
			for (auto &item : r)
			{
				if (item.status().type() == fs::file_type::directory) // or, fs::file_type::regular_file
				{
					auto stdstr = item.path().u8string();
					String::PushBack(result, stdstr.c_str());
					auto fnamepath = item.path().filename().u8string();
					if (pOptionalNamesOut)
						String::PushBack(*pOptionalNamesOut, fnamepath.c_str());
				}
			}
			return;
		}
#endif

The result:

3
Hope this helps. ------ Leslie.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions