48 lines
1.5 KiB
C++
48 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#if !defined(MIJIN_IO_PROCESS_HPP_INCLUDED)
|
|
#define MIJIN_IO_PROCESS_HPP_INCLUDED 1
|
|
|
|
#include <vector>
|
|
#include "./stream.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<std::string>& args, FileOpenMode mode_);
|
|
int close();
|
|
[[nodiscard]] inline bool isOpen() const { return handle != nullptr; }
|
|
|
|
// Stream overrides
|
|
StreamError readRaw(std::span<std::uint8_t> buffer, const ReadOptions& options, std::size_t* outBytesRead) override;
|
|
StreamError writeRaw(std::span<const std::uint8_t> 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) noexcept;
|
|
[[nodiscard]] std::string makeShellCommand(const std::vector<std::string>& args) noexcept;
|
|
|
|
StreamError ProcessStream::open(const std::vector<std::string>& args, FileOpenMode mode_)
|
|
{
|
|
return open(makeShellCommand(args), mode_);
|
|
}
|
|
}
|
|
|
|
#endif // MIJIN_IO_PROCESS_HPP_INCLUDED
|