Added ProcessStream to for running processes (on Linux) and streaming their output.
Added map() function for creating a mapping iterator. Added mijin::pipe types for map() and join().
This commit is contained in:
47
source/mijin/io/process.hpp
Normal file
47
source/mijin/io/process.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#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, bool partial = false, std::size_t* outBytesRead = nullptr) 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
|
||||
Reference in New Issue
Block a user