2016-09-25 17:23:39 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "support/raw_istream.h"
|
|
|
|
|
|
2016-10-08 16:03:33 -07:00
|
|
|
#include <cstdlib>
|
2016-09-25 17:23:39 -07:00
|
|
|
#include <cstring>
|
|
|
|
|
|
2016-10-08 16:03:33 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <io.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-08-13 00:37:50 -07:00
|
|
|
#include "llvm/FileSystem.h"
|
2017-08-13 00:56:35 -07:00
|
|
|
#include "llvm/SmallVector.h"
|
|
|
|
|
#include "llvm/StringRef.h"
|
|
|
|
|
|
2017-08-13 00:37:14 -07:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
|
#ifndef STDIN_FILENO
|
|
|
|
|
# define STDIN_FILENO 0
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef STDOUT_FILENO
|
|
|
|
|
# define STDOUT_FILENO 1
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef STDERR_FILENO
|
|
|
|
|
# define STDERR_FILENO 2
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-09-25 17:23:39 -07:00
|
|
|
using namespace wpi;
|
|
|
|
|
|
2017-08-13 00:56:35 -07:00
|
|
|
llvm::StringRef raw_istream::getline(llvm::SmallVectorImpl<char>& buf,
|
|
|
|
|
int maxLen) {
|
|
|
|
|
buf.clear();
|
|
|
|
|
for (int i = 0; i < maxLen; ++i) {
|
|
|
|
|
char c;
|
|
|
|
|
read(c);
|
|
|
|
|
if (has_error()) return llvm::StringRef{buf.data(), buf.size()};
|
|
|
|
|
if (c == '\r') continue;
|
|
|
|
|
buf.push_back(c);
|
|
|
|
|
if (c == '\n') break;
|
|
|
|
|
}
|
|
|
|
|
return llvm::StringRef{buf.data(), buf.size()};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 20:53:45 -07:00
|
|
|
void raw_mem_istream::close() {}
|
|
|
|
|
|
2016-10-21 22:50:38 -07:00
|
|
|
std::size_t raw_mem_istream::in_avail() const { return m_left; }
|
|
|
|
|
|
2016-09-02 20:53:45 -07:00
|
|
|
void raw_mem_istream::read_impl(void* data, std::size_t len) {
|
|
|
|
|
if (len > m_left) {
|
|
|
|
|
error_detected();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
std::memcpy(data, m_cur, len);
|
|
|
|
|
m_cur += len;
|
|
|
|
|
m_left -= len;
|
|
|
|
|
}
|
2016-10-08 16:03:33 -07:00
|
|
|
|
2017-08-13 00:37:50 -07:00
|
|
|
static int getFD(llvm::StringRef Filename, std::error_code &EC) {
|
|
|
|
|
// Handle "-" as stdin. Note that when we do this, we consider ourself
|
|
|
|
|
// the owner of stdin. This means that we can do things like close the
|
|
|
|
|
// file descriptor when we're done and set the "binary" flag globally.
|
|
|
|
|
if (Filename == "-") {
|
|
|
|
|
EC = std::error_code();
|
|
|
|
|
return STDIN_FILENO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FD;
|
|
|
|
|
|
|
|
|
|
EC = llvm::sys::fs::openFileForRead(Filename, FD);
|
|
|
|
|
if (EC)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
EC = std::error_code();
|
|
|
|
|
return FD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
raw_fd_istream::raw_fd_istream(llvm::StringRef filename, std::error_code& ec,
|
|
|
|
|
std::size_t bufSize)
|
|
|
|
|
: raw_fd_istream(getFD(filename, ec), true, bufSize) {}
|
|
|
|
|
|
2016-10-08 16:03:33 -07:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-21 22:50:38 -07:00
|
|
|
std::size_t raw_fd_istream::in_avail() const { return m_end - m_cur; }
|
|
|
|
|
|
2016-10-08 16:03:33 -07:00
|
|
|
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;
|
|
|
|
|
}
|