#pragma once #if !defined(MIJIN_IO_PROCESS_HPP_INCLUDED) #define MIJIN_IO_PROCESS_HPP_INCLUDED 1 #include #include "./stream.hpp" #include "../internal/common.hpp" namespace mijin { class ProcessStream : public Stream { private: std::FILE* handle = nullptr; FileOpenMode mode; int bufferedChar; // for isAtEnd public: ~ProcessStream() override; StreamError open(const char* command, FileOpenMode mode_); inline StreamError open(const std::string& command, FileOpenMode mode_) { return open(command.c_str(), mode_); } inline StreamError open(const std::vector& args, FileOpenMode mode_); int close(); [[nodiscard]] inline bool isOpen() const { return handle != nullptr; } // Stream overrides StreamError readRaw(std::span buffer, const ReadOptions& options, std::size_t* outBytesRead) override; StreamError writeRaw(std::span buffer) override; std::size_t tell() override; StreamError seek(std::intptr_t pos, SeekMode seekMode = SeekMode::ABSOLUTE) override; void flush() override; bool isAtEnd() override; StreamFeatures getFeatures() override; }; [[nodiscard]] std::string shellEscape(const std::string& arg) MIJIN_NOEXCEPT; [[nodiscard]] std::string makeShellCommand(const std::vector& args) MIJIN_NOEXCEPT; StreamError ProcessStream::open(const std::vector& args, FileOpenMode mode_) { return open(makeShellCommand(args), mode_); } } #endif // MIJIN_IO_PROCESS_HPP_INCLUDED