| 63 | }; |
| 64 | |
| 65 | static bool read_file(const std::filesystem::path &path, std::string &file_data) |
| 66 | { |
| 67 | // Read file contents into memory |
| 68 | #ifndef _WIN32 |
| 69 | FILE *const file = fopen(path.c_str(), "rb"); |
| 70 | #else |
| 71 | FILE *const file = _wfsopen(path.c_str(), L"rb", SH_DENYWR); |
| 72 | #endif |
| 73 | if (file == nullptr) |
| 74 | return false; |
| 75 | |
| 76 | fseek(file, 0, SEEK_END); |
| 77 | const size_t file_size = ftell(file); |
| 78 | fseek(file, 0, SEEK_SET); |
| 79 | |
| 80 | file_data.resize(file_size + 1, '\0'); // One additional character at the end for new line feed set below |
| 81 | const size_t file_size_read = fread(file_data.data(), 1, file_size, file); |
| 82 | |
| 83 | // No longer need to have a handle open to the file, since all data was read, so can safely close it |
| 84 | fclose(file); |
| 85 | |
| 86 | if (file_size_read != file_size) |
| 87 | return false; |
| 88 | |
| 89 | // Append a new line feed to the end of the input string to avoid issues with parsing |
| 90 | file_data.back() = '\n'; |
| 91 | |
| 92 | // Remove UTF-8 BOM (0xEFBBBF is the UTF-8 byte sequence for the character 0xFEFF) |
| 93 | if (file_data.size() >= 3 && |
| 94 | static_cast<unsigned char>(file_data[0]) == 0xEF && |
| 95 | static_cast<unsigned char>(file_data[1]) == 0xBB && |
| 96 | static_cast<unsigned char>(file_data[2]) == 0xBF) |
| 97 | file_data.erase(0, 3); |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | template <char ESCAPE_CHAR = '\\'> |
| 103 | static std::string escape_string(std::string s) |
no test coverage detected