Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIN32: Support for non-ascii filename (e.g. 日本語(Japanese)) #684

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions es-app/src/CollectionSystemManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ void CollectionSystemManager::saveCustomCollection(SystemData* sys)
if (sysData.needsSave)
{
std::ofstream configFile;
#if defined(_WIN32)
configFile.open(Utils::FileSystem::convertToWideString(getCustomCollectionConfigPath(name)));
#else
configFile.open(getCustomCollectionConfigPath(name));
#endif
for(std::unordered_map<std::string, FileData*>::const_iterator iter = games.cbegin(); iter != games.cend(); ++iter)
{
std::string path = iter->first;
Expand Down Expand Up @@ -771,7 +775,11 @@ void CollectionSystemManager::populateCustomCollection(CollectionSystemData* sys
FileFilterIndex* index = newSys->getIndex();

// get Configuration for this Custom System
#if defined(_WIN32)
std::ifstream input(Utils::FileSystem::convertToWideString(path));
#else
std::ifstream input(path);
#endif

// get all files map
std::unordered_map<std::string,FileData*> allFilesMap = getAllGamesCollection()->getRootFolder()->getChildrenByFilename();
Expand Down Expand Up @@ -873,7 +881,11 @@ std::vector<std::string> CollectionSystemManager::getSystemsFromConfig()
}

pugi::xml_document doc;
#if defined(_WIN32)
pugi::xml_parse_result res = doc.load_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
pugi::xml_parse_result res = doc.load_file(path.c_str());
#endif

if(!res)
{
Expand Down
12 changes: 12 additions & 0 deletions es-app/src/Gamelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ void parseGamelist(SystemData* system)
LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";

pugi::xml_document doc;
#if defined(_WIN32)
pugi::xml_parse_result result = doc.load_file(Utils::FileSystem::convertToWideString(xmlpath).c_str());
#else
pugi::xml_parse_result result = doc.load_file(xmlpath.c_str());
#endif

if(!result)
{
Expand Down Expand Up @@ -188,7 +192,11 @@ void updateGamelist(SystemData* system)
if(Utils::FileSystem::exists(xmlReadPath))
{
//parse an existing file first
#if defined(_WIN32)
pugi::xml_parse_result result = doc.load_file(Utils::FileSystem::convertToWideString(xmlReadPath).c_str());
#else
pugi::xml_parse_result result = doc.load_file(xmlReadPath.c_str());
#endif

if(!result)
{
Expand Down Expand Up @@ -303,7 +311,11 @@ void updateGamelist(SystemData* system)

LOG(LogInfo) << "Added/Updated " << numUpdated << " entities in '" << xmlReadPath << "'";

#if defined(_WIN32)
if (!doc.save_file(Utils::FileSystem::convertToWideString(xmlWritePath).c_str())) {
#else
if (!doc.save_file(xmlWritePath.c_str())) {
#endif
LOG(LogError) << "Error saving gamelist.xml to \"" << xmlWritePath << "\" (for system " << system->getName() << ")!";
}

Expand Down
8 changes: 8 additions & 0 deletions es-app/src/SystemData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ bool SystemData::loadConfig(Window* window)
}

pugi::xml_document doc;
#if defined(_WIN32)
pugi::xml_parse_result res = doc.load_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
pugi::xml_parse_result res = doc.load_file(path.c_str());
#endif

if(!res)
{
Expand Down Expand Up @@ -387,7 +391,11 @@ bool SystemData::loadConfig(Window* window)

void SystemData::writeExampleConfig(const std::string& path)
{
#if defined(_WIN32)
std::ofstream file(Utils::FileSystem::convertToWideString(path));
#else
std::ofstream file(path.c_str());
#endif

file << "<!-- This is the EmulationStation Systems configuration file.\n"
"All systems must be contained within the <systemList> tag.-->\n"
Expand Down
8 changes: 8 additions & 0 deletions es-app/src/scrapers/GamesDBJSONScraperResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ bool TheGamesDBJSONRequestResources::saveResource(HttpReq* req, std::unordered_m

ensureScrapersResourcesDir();

#if defined(_WIN32)
std::ofstream fout(Utils::FileSystem::convertToWideString(file_name));
#else
std::ofstream fout(file_name);
#endif
fout << req->getContent();
fout.close();
loadResource(resource, resource_name, file_name);
Expand All @@ -166,7 +170,11 @@ int TheGamesDBJSONRequestResources::loadResource(
{


#if defined(_WIN32)
std::ifstream fin(Utils::FileSystem::convertToWideString(file_name));
#else
std::ifstream fin(file_name);
#endif
if (!fin.good())
{
return 1;
Expand Down
4 changes: 4 additions & 0 deletions es-app/src/scrapers/Scraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ void ImageDownloadHandle::update()
}

// download is done, save it to disk
#if defined(_WIN32)
std::ofstream stream(Utils::FileSystem::convertToWideString(mSavePath), std::ios_base::out | std::ios_base::binary);
#else
std::ofstream stream(mSavePath, std::ios_base::out | std::ios_base::binary);
#endif
if(stream.bad())
{
setError("Failed to open image path to write. Permission error? Disk full?");
Expand Down
16 changes: 16 additions & 0 deletions es-core/src/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,11 @@ bool InputManager::loadInputConfig(InputConfig* config)
return false;

pugi::xml_document doc;
#if defined(_WIN32)
pugi::xml_parse_result res = doc.load_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
pugi::xml_parse_result res = doc.load_file(path.c_str());
#endif

if(!res)
{
Expand Down Expand Up @@ -343,7 +347,11 @@ void InputManager::writeDeviceConfig(InputConfig* config)
if(Utils::FileSystem::exists(path))
{
// merge files
#if defined(_WIN32)
pugi::xml_parse_result result = doc.load_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
pugi::xml_parse_result result = doc.load_file(path.c_str());
#endif
if(!result)
{
LOG(LogError) << "Error parsing input config: " << result.description();
Expand Down Expand Up @@ -388,7 +396,11 @@ void InputManager::writeDeviceConfig(InputConfig* config)
root = doc.append_child("inputList");

config->writeToXML(root);
#if defined(_WIN32)
doc.save_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
doc.save_file(path.c_str());
#endif

Scripting::fireEvent("config-changed");
Scripting::fireEvent("controls-changed");
Expand All @@ -406,7 +418,11 @@ void InputManager::doOnFinish()

if(Utils::FileSystem::exists(path))
{
#if defined(_WIN32)
pugi::xml_parse_result result = doc.load_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
pugi::xml_parse_result result = doc.load_file(path.c_str());
#endif
if(!result)
{
LOG(LogError) << "Error parsing input config: " << result.description();
Expand Down
10 changes: 10 additions & 0 deletions es-core/src/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@ void Log::setReportingLevel(LogLevel level)

void Log::init()
{
#if defined(_WIN32)
_wunlink(Utils::FileSystem::convertToWideString(getLogPath() + ".bak").c_str());
_wrename(Utils::FileSystem::convertToWideString(getLogPath()).c_str(),
Utils::FileSystem::convertToWideString(getLogPath() + ".bak").c_str());
#else
remove((getLogPath() + ".bak").c_str());
// rename previous log file
rename(getLogPath().c_str(), (getLogPath() + ".bak").c_str());
#endif
return;
}

void Log::open()
{
#if defined(_WIN32)
file = _wfopen(Utils::FileSystem::convertToWideString(getLogPath()).c_str(), L"w");
#else
file = fopen(getLogPath().c_str(), "w");
#endif
}

std::ostringstream& Log::get(LogLevel level)
Expand Down
12 changes: 12 additions & 0 deletions es-core/src/MameNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ MameNames::MameNames()
LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";

pugi::xml_document doc;
#if defined(_WIN32)
pugi::xml_parse_result result = doc.load_file(Utils::FileSystem::convertToWideString(xmlpath).c_str());
#else
pugi::xml_parse_result result = doc.load_file(xmlpath.c_str());
#endif

if(!result)
{
Expand All @@ -66,7 +70,11 @@ MameNames::MameNames()

LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";

#if defined(_WIN32)
result = doc.load_file(Utils::FileSystem::convertToWideString(xmlpath).c_str());
#else
result = doc.load_file(xmlpath.c_str());
#endif

if(!result)
{
Expand All @@ -88,7 +96,11 @@ MameNames::MameNames()

LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";

#if defined(_WIN32)
result = doc.load_file(Utils::FileSystem::convertToWideString(xmlpath).c_str());
#else
result = doc.load_file(xmlpath.c_str());
#endif

if(!result)
{
Expand Down
8 changes: 8 additions & 0 deletions es-core/src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ void Settings::saveFile()
node.append_attribute("value").set_value(iter->second.c_str());
}

#if defined(_WIN32)
doc.save_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
doc.save_file(path.c_str());
#endif

Scripting::fireEvent("config-changed");
Scripting::fireEvent("settings-changed");
Expand All @@ -227,7 +231,11 @@ void Settings::loadFile()
return;

pugi::xml_document doc;
#if defined(_WIN32)
pugi::xml_parse_result result = doc.load_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
pugi::xml_parse_result result = doc.load_file(path.c_str());
#endif
if(!result)
{
LOG(LogError) << "Could not parse Settings file!\n " << result.description();
Expand Down
8 changes: 8 additions & 0 deletions es-core/src/ThemeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ void ThemeData::loadFile(std::map<std::string, std::string> sysDataMap, const st
mVariables.insert(sysDataMap.cbegin(), sysDataMap.cend());

pugi::xml_document doc;
#if defined(_WIN32)
pugi::xml_parse_result res = doc.load_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
pugi::xml_parse_result res = doc.load_file(path.c_str());
#endif
if(!res)
throw error << "XML parsing error: \n " << res.description();

Expand Down Expand Up @@ -297,7 +301,11 @@ void ThemeData::parseIncludes(const pugi::xml_node& root)
mPaths.push_back(path);

pugi::xml_document includeDoc;
#if defined(_WIN32)
pugi::xml_parse_result result = includeDoc.load_file(Utils::FileSystem::convertToWideString(path).c_str());
#else
pugi::xml_parse_result result = includeDoc.load_file(path.c_str());
#endif
if(!result)
throw error << "Error parsing file: \n " << result.description();

Expand Down
41 changes: 29 additions & 12 deletions es-core/src/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <fcntl.h>

#include "Log.h"
#ifdef WIN32
#include <Windows.h>
#include "utils/FileSystemUtil.h"
#endif

int runShutdownCommand()
{
Expand All @@ -31,12 +35,26 @@ int runRestartCommand()
int runSystemCommand(const std::string& cmd_utf8)
{
#ifdef WIN32
// on Windows we use _wsystem to support non-ASCII paths
// which requires converting from utf8 to a wstring
typedef std::codecvt_utf8<wchar_t> convert_type;
std::wstring_convert<convert_type, wchar_t> converter;
std::wstring wchar_str = converter.from_bytes(cmd_utf8);
return _wsystem(wchar_str.c_str());
STARTUPINFOW
si;
PROCESS_INFORMATION
pi;
DWORD
rcode = 0;

memset(&si, 0, sizeof si);
memset(&pi, 0, sizeof pi);
si.cb = sizeof si;

if(!CreateProcessW(NULL, (LPWSTR)Utils::FileSystem::convertToWideString(cmd_utf8).c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
return 9009;

CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &rcode);
CloseHandle(pi.hProcess);

return rcode;
#else
return system(cmd_utf8.c_str());
#endif
Expand All @@ -54,18 +72,17 @@ int quitES(QuitMode mode)
return 0;
}

void touch(const std::string& filename)
{
#ifdef WIN32
FILE* fp = fopen(filename.c_str(), "ab+");
if (fp != NULL)
fclose(fp);
// Windows hasn't /tmp directory usualy so nothing to touch.
inline void touch(const std::string&) {}
#else
void touch(const std::string& filename)
{
int fd = open(filename.c_str(), O_CREAT|O_WRONLY, 0644);
if (fd >= 0)
close(fd);
#endif
}
#endif

void processQuitMode()
{
Expand Down
4 changes: 4 additions & 0 deletions es-core/src/resources/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ const ResourceData ResourceManager::getFileData(const std::string& path) const

ResourceData ResourceManager::loadFile(const std::string& path) const
{
#if defined(_WIN32)
std::ifstream stream(Utils::FileSystem::convertToWideString(path), std::ios::binary);
#else
std::ifstream stream(path, std::ios::binary);
#endif

stream.seekg(0, stream.end);
std::ifstream::pos_type size = stream.tellg();
Expand Down
Loading