saddy-graphics-engine-2d icon indicating copy to clipboard operation
saddy-graphics-engine-2d copied to clipboard

[core] UTF-8 support in files everywhere

Open mamontov-cpp opened this issue 5 years ago • 2 comments

mamontov-cpp avatar Jan 09 '20 07:01 mamontov-cpp

Well this works, but also requires utf-8 for MSVC

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <locale>
#include <codecvt>
#ifndef LINUX

static std::wstring _widen(const char* str)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
	return convert.from_bytes(str);
}
#endif



FILE* u8open(const char* filename, const char* mode)
{
	if (!filename || !mode)
	{
		return NULL;
	}
#ifndef LINUX
	FILE* result = _wfopen(_widen(filename).c_str(), _widen(mode).c_str());
	return result;
#else
	return fopen(filename, mode);
#endif
}


int main()
{
	FILE* fd = u8open(u8"юникодовая папка\\имя.txt", u8"rb");
	if (!fd)
	{
		printf("Unable to open files\n");
		return 0;
	}
    printf("Opened a file!\n");
	while (!feof(fd))
	{
		putc(fgetc(fd), stdout);
	}
	printf("\n");
	return 0;
}

Also JSON spport is need to be tested.

mamontov-cpp avatar Feb 15 '20 20:02 mamontov-cpp

For other parts std::filesystem is ok

#include <filesystem>
#include <iostream>
#include <string>

int main()
{
#ifdef __cpp_lib_char8_t
	std::filesystem::path p(std::u8string(u8"папка"));
#else
	std::filesystem::path p = std::filesystem::u8path("папка");
#endif
	if (std::filesystem::exists(p)) 
	{
		if (std::filesystem::is_directory(p))
		{
			std::cout << "Yes";
			std::filesystem::remove(p);
		}
		else
		{
			std::cout << "Found file";
		}
	}
	else
	{
		std::cout << "No!";
		std::filesystem::create_directory(p);
	}
	return 0;
}

mamontov-cpp avatar Jun 22 '21 17:06 mamontov-cpp