#include "platform/file_impl.h" #include #include namespace location { namespace nearby { // InputFile InputFileImpl::InputFileImpl(const std::string& path, std::int64_t size) : file_(path), path_(path), total_size_(size) {} ExceptionOr> InputFileImpl::read(int64_t size) { if (!file_.is_open()) { return ExceptionOr>(Exception::IO); } if (file_.peek() == EOF) { return ExceptionOr>(ConstPtr()); } if (!file_.good()) { return ExceptionOr>(Exception::IO); } std::unique_ptr read_bytes {new char [size]}; file_.read(read_bytes.get(), static_cast(size)); auto num_bytes_read = file_.gcount(); if (num_bytes_read == 0) { return ExceptionOr>(Exception::IO); } return ExceptionOr>( MakeConstPtr(new ByteArray(read_bytes.get(), num_bytes_read))); } std::string InputFileImpl::getFilePath() const { return path_; } std::int64_t InputFileImpl::getTotalSize() const { return total_size_; } void InputFileImpl::close() { if (file_.is_open()) { file_.close(); } } // OutputFile OutputFileImpl::OutputFileImpl(const std::string& path) : file_(path) {} Exception::Value OutputFileImpl::write(ConstPtr data) { ScopedPtr> scoped_data(data); if (!file_.is_open()) { return Exception::IO; } if (!file_.good()) { return Exception::IO; } file_.write(data->getData(), data->size()); file_.flush(); return file_.good() ? Exception::NONE : Exception::IO; } void OutputFileImpl::close() { if (file_.is_open()) { file_.close(); } } } // namespace nearby } // namespace location