| 56 | } |
| 57 | |
| 58 | bool LoadFileRaw(const char *name, bool binary, std::string *buf) { |
| 59 | if (DirExists(name)) return false; |
| 60 | std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in); |
| 61 | if (!ifs.is_open()) return false; |
| 62 | if (binary) { |
| 63 | // The fastest way to read a file into a string. |
| 64 | ifs.seekg(0, std::ios::end); |
| 65 | auto size = ifs.tellg(); |
| 66 | (*buf).resize(static_cast<size_t>(size)); |
| 67 | ifs.seekg(0, std::ios::beg); |
| 68 | ifs.read(&(*buf)[0], (*buf).size()); |
| 69 | } else { |
| 70 | // This is slower, but works correctly on all platforms for text files. |
| 71 | std::ostringstream oss; |
| 72 | oss << ifs.rdbuf(); |
| 73 | *buf = oss.str(); |
| 74 | } |
| 75 | return !ifs.bad(); |
| 76 | } |
| 77 | |
| 78 | static LoadFileFunction g_load_file_function = LoadFileRaw; |
| 79 | static FileExistsFunction g_file_exists_function = FileExistsRaw; |