| 43 | } |
| 44 | |
| 45 | antlr4::ANTLRInputStream ANTLRFileStream_with_encoding( |
| 46 | const std::filesystem::path &file_name, const std::string &encoding) { |
| 47 | std::ifstream ifs; |
| 48 | std::string str; |
| 49 | ifs.exceptions ( std::ifstream::failbit | std::ifstream::badbit ); |
| 50 | ifs.open(file_name); |
| 51 | |
| 52 | // Sets position to the end of the file. |
| 53 | ifs.seekg(0, std::ios::end); |
| 54 | |
| 55 | // Reserves memory for the file. |
| 56 | str.reserve(ifs.tellg()); |
| 57 | |
| 58 | // Sets position to the start of the file. |
| 59 | ifs.seekg(0, std::ios::beg); |
| 60 | |
| 61 | // Sets contents of 'str' to all characters in the file. |
| 62 | str.assign(std::istreambuf_iterator<char>(ifs), |
| 63 | std::istreambuf_iterator<char>()); |
| 64 | |
| 65 | str = _to_utf8(str, encoding); |
| 66 | |
| 67 | try { |
| 68 | antlr4::ANTLRInputStream input_stream(str); |
| 69 | input_stream.name = file_name.u8string(); |
| 70 | return input_stream; |
| 71 | } catch (const std::range_error &e) { |
| 72 | throw std::range_error( |
| 73 | std::string("Invalid character/encoding in ") |
| 74 | + file_name.u8string() + " file"); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } |
no test coverage detected