-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathfile_stream.cpp
More file actions
59 lines (48 loc) · 2.03 KB
/
file_stream.cpp
File metadata and controls
59 lines (48 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "databento/file_stream.hpp"
#include <ios> // ios, streamsize
#include <sstream>
#include "databento/exceptions.hpp"
using databento::InFileStream;
using Status = databento::IReadable::Status;
InFileStream::InFileStream(const std::filesystem::path& file_path)
: stream_{file_path, std::ios::binary} {
if (stream_.fail()) {
throw InvalidArgumentError{"InFileStream", "file_path",
"Non-existent or invalid file: " + file_path.string()};
}
}
void InFileStream::ReadExact(std::byte* buffer, std::size_t length) {
const auto size = ReadSome(buffer, length);
if (size != length) {
std::ostringstream err_msg;
err_msg << "Unexpected end of file, expected " << length << " bytes, got " << size;
throw DbnResponseError{err_msg.str()};
}
}
std::size_t InFileStream::ReadSome(std::byte* buffer, std::size_t max_length) {
stream_.read(reinterpret_cast<char*>(buffer),
static_cast<std::streamsize>(max_length));
return static_cast<std::size_t>(stream_.gcount());
}
databento::IReadable::Result InFileStream::ReadSome(std::byte* buffer,
std::size_t max_length,
std::chrono::milliseconds) {
const auto bytes_read = ReadSome(buffer, max_length);
return {bytes_read, bytes_read > 0 ? Status::Ok : Status::Closed};
}
using databento::OutFileStream;
OutFileStream::OutFileStream(const std::filesystem::path& file_path)
: OutFileStream{file_path, std::ios::binary} {}
OutFileStream::OutFileStream(const std::filesystem::path& file_path,
std::ios_base::openmode mode)
: stream_{file_path, mode} {
if (stream_.fail()) {
throw InvalidArgumentError{
"OutFileStream", "file_path",
"Can't open file for writing at path: " + file_path.string()};
}
}
void OutFileStream::WriteAll(const std::byte* buffer, std::size_t length) {
stream_.write(reinterpret_cast<const char*>(buffer),
static_cast<std::streamsize>(length));
}