| 1 | #include "duckdb/common/fstream_util.hpp" |
|---|---|
| 2 | |
| 3 | using namespace std; |
| 4 | using namespace duckdb; |
| 5 | |
| 6 | void FstreamUtil::OpenFile(const string &file_path, fstream &new_file, ios_base::openmode mode) { |
| 7 | new_file.open(file_path, mode); |
| 8 | if (!new_file.good()) { |
| 9 | throw IOException("Could not open File!"+ file_path); |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | void FstreamUtil::CloseFile(fstream &file) { |
| 14 | file.close(); |
| 15 | // check the success of the write |
| 16 | if (file.fail()) { |
| 17 | throw IOException("Failed to close the file!"); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | idx_t FstreamUtil::GetFileSize(fstream &file) { |
| 22 | file.seekg(0, ios::end); |
| 23 | return file.tellg(); |
| 24 | } |
| 25 | |
| 26 | data_ptr FstreamUtil::ReadBinary(fstream &file) { |
| 27 | auto file_size = GetFileSize(file); |
| 28 | file.seekg(0, ios::beg); |
| 29 | auto result = data_ptr(new char[file_size]); |
| 30 | file.read(result.get(), file_size); |
| 31 | |
| 32 | return result; |
| 33 | } |
| 34 |