diff --git a/src/simplnx/Utilities/FileUtilities.cpp b/src/simplnx/Utilities/FileUtilities.cpp index 01558debe2..e40c53cc44 100644 --- a/src/simplnx/Utilities/FileUtilities.cpp +++ b/src/simplnx/Utilities/FileUtilities.cpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -95,7 +96,7 @@ Result<> ValidateCSVFile(const std::string& filePath) } // Obtain the file size - const size_t fileSize = fs::file_size(absPath); + usize fileSize = fs::file_size(absPath); // Open the file std::ifstream in(absPath.c_str(), std::ios_base::binary); @@ -104,7 +105,18 @@ Result<> ValidateCSVFile(const std::string& filePath) return MakeErrorResult(-301, fmt::format("Could not open file for reading: {}", absPath.string())); } - size_t actualSize = bufferSize; + auto isUtf8 = IsUtf8(absPath); + if(isUtf8.first) + { + // The file is UTF8 with a BOM marker, so read the first 3 bytes and dump them. + char a = '\0'; + char b = '\0'; + char c = '\0'; + in >> a >> b >> c; + fileSize -= 3; + } + + usize actualSize = bufferSize; if(fileSize <= bufferSize) { actualSize = fileSize; @@ -230,4 +242,25 @@ Result<> ValidateDirectoryWritePermission(const fs::path& path, bool isFile) } return MakeErrorResult(-8, fmt::format("ValidateDirectoryWritePermission() Error: User does not have write permissions to path '{}'", path.string())); } + +std::pair IsUtf8(const fs::path& filePath) +{ + FILE* f = fopen(filePath.string().c_str(), "rb"); + if(nullptr == f) + { + return {false, -1}; + } + std::array buf = {0, 0, 0}; + if(fread(buf.data(), 1, 3, f) != 3) + { + std::ignore = fclose(f); + return {false, -1}; + } + std::ignore = fclose(f); + if(buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) + { + return {true, 0}; + } + return {false, 0}; +} } // namespace nx::core::FileUtilities diff --git a/src/simplnx/Utilities/FileUtilities.hpp b/src/simplnx/Utilities/FileUtilities.hpp index 33e9f97c0b..f461ba1dff 100644 --- a/src/simplnx/Utilities/FileUtilities.hpp +++ b/src/simplnx/Utilities/FileUtilities.hpp @@ -68,4 +68,11 @@ SIMPLNX_EXPORT bool HasWriteAccess(const std::string& path); * @return */ SIMPLNX_EXPORT Result<> ValidateDirectoryWritePermission(const fs::path& path, bool isFile); + +/** + * @brief + * @param filePath + * @return + */ +SIMPLNX_EXPORT std::pair IsUtf8(const fs::path& filePath); } // namespace nx::core::FileUtilities