Skip to content

Commit

Permalink
emu/common: add function to read utf16 file
Browse files Browse the repository at this point in the history
  • Loading branch information
ssine committed Feb 28, 2020
1 parent 934466b commit b9a8204
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/emu/common/include/common/fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ namespace eka2l1::common {
*/
bool remove(const std::string &path);

/* !\brief Get the content of a UTF-16 LE encoded file as UTF8 string.
*/
std::string get_file_content_u16(const std::string &path);

bool move_file(const std::string &path, const std::string &new_path);

/**
Expand Down
31 changes: 24 additions & 7 deletions src/emu/common/src/fileutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ namespace eka2l1::common {

do {
cycles_to_next_entry();
d = reinterpret_cast<struct dirent*>(find_data);
d = reinterpret_cast<struct dirent *>(find_data);
} while (d && (strncmp(d->d_name, ".", 1) == 0 || strncmp(d->d_name, "..", 2) == 0));
#endif

Expand Down Expand Up @@ -306,14 +306,32 @@ namespace eka2l1::common {
#endif
}

std::string get_file_content_u16(const std::string &path) {
std::ifstream fin(path, std::ios::binary);
fin.seekg(0, std::ios::end);
size_t size = static_cast<size_t>(fin.tellg());
if (size <= 2) return "";

//skip BOM
fin.seekg(2, std::ios::beg);
size -= 2;

std::u16string u16((size / 2) + 1, '\0');
fin.read(reinterpret_cast<char*>(&u16[0]), size);

std::string utf8 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16);

return utf8;
}

bool move_file(const std::string &path, const std::string &new_path) {
#if EKA2L1_PLATFORM(WIN32)
return MoveFileA(path.c_str(), new_path.c_str());
#else
return (rename(path.c_str(), new_path.c_str()) == 0);
#endif
}

bool copy_file(const std::string &target_file, const std::string &dest, const bool overwrite_if_dest_exists) {
#if EKA2L1_PLATFORM(WIN32)
return CopyFile(target_file.c_str(), dest.c_str(), !overwrite_if_dest_exists);
Expand All @@ -325,7 +343,7 @@ namespace eka2l1::common {
if (eka2l1::exists(dest) && !overwrite_if_dest_exists) {
return false;
}

std::ifstream src(target_file, std::ios::binary);
std::ofstream dst(dest, std::ios::binary | std::ios::trunc);

Expand All @@ -344,13 +362,12 @@ namespace eka2l1::common {
if (GetFileAttributesExW(reinterpret_cast<const LPCWSTR>(path.c_str()), GetFileExInfoStandard, &attrib_data) == false) {
return 0xFFFFFFFFFFFFFFFF;
}

FILETIME last_modify_time = attrib_data.ftLastWriteTime;

// 100 nanoseconds = 0.1 microseconds
return convert_microsecs_win32_1601_epoch_to_1ad(
static_cast<std::uint64_t>(last_modify_time.dwLowDateTime) |
(static_cast<std::uint64_t>(last_modify_time.dwHighDateTime) << 32));
static_cast<std::uint64_t>(last_modify_time.dwLowDateTime) | (static_cast<std::uint64_t>(last_modify_time.dwHighDateTime) << 32));
#else
const std::string name_utf8 = common::ucs2_to_utf8(path);
struct stat st;
Expand All @@ -363,7 +380,7 @@ namespace eka2l1::common {
return convert_microsecs_epoch_to_1ad(static_cast<std::uint64_t>(st.st_mtime));
#endif
}

bool is_system_case_insensitive() {
#if EKA2L1_PLATFORM(WIN32)
return true;
Expand Down

0 comments on commit b9a8204

Please sign in to comment.