Add raw_fd_istream. (#121)

This commit is contained in:
Peter Johnson
2016-10-08 16:03:33 -07:00
committed by GitHub
parent 15cb505163
commit 7b3f6eeae2
2 changed files with 68 additions and 0 deletions

View File

@@ -7,8 +7,15 @@
#include "support/raw_istream.h"
#include <cstdlib>
#include <cstring>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
using namespace wpi;
void raw_mem_istream::close() {}
@@ -22,3 +29,47 @@ void raw_mem_istream::read_impl(void* data, std::size_t len) {
m_cur += len;
m_left -= len;
}
raw_fd_istream::raw_fd_istream(int fd, bool shouldClose, std::size_t bufSize)
: m_bufSize(bufSize), m_fd(fd), m_shouldClose(shouldClose) {
m_cur = m_end = m_buf = static_cast<char*>(std::malloc(bufSize));
}
raw_fd_istream::~raw_fd_istream() {
if (m_shouldClose) close();
std::free(m_buf);
}
void raw_fd_istream::close() {
if (m_fd >= 0) {
::close(m_fd);
m_fd = -1;
}
}
void raw_fd_istream::read_impl(void* data, std::size_t len) {
std::size_t left = m_end - m_cur;
if (left < len) {
// not enough data
if (m_cur == m_end) {
#ifdef _WIN32
int count = ::_read(m_fd, m_buf, m_bufSize);
#else
ssize_t count = ::read(m_fd, m_buf, m_bufSize);
#endif
if (count < 0) {
error_detected();
return;
}
m_cur = m_buf;
m_end = m_buf + count;
return read_impl(data, len);
}
std::memcpy(data, m_cur, left);
return read_impl(static_cast<char*>(data) + left, len - left);
}
std::memcpy(data, m_cur, len);
m_cur += len;
}