17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 15:39:20 +02:00
Files
maszyna/extras/piped_proc.cpp
maj00r b8f040e6fd propagate template headers transitively, import Console where used
AnimModel.cpp reaches cParser through the simulation core, not a direct
import, so the previous pass missed it; the propagation now follows the
whole import closure. Train.cpp, drivermode.cpp and Globals.cpp call
Console:: inside _WIN32 guards without importing eu07.console - invisible
on Linux, where those blocks are skipped.
2026-09-10 01:41:24 +02:00

127 lines
2.6 KiB
C++

module;
#include <array>
#include <chrono>
#include <deque>
#include <limits>
#include <mutex>
#include <ostream>
#include <sstream>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstddef>
#include <string>
module eu07.extras.piped_proc;
import eu07.utilities.logs;
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef __unix__
piped_proc::piped_proc(std::string cmd, bool write)
{
file = popen(cmd.c_str(), write ? "w" : "r");
}
piped_proc::~piped_proc()
{
if (file)
pclose(file);
}
size_t piped_proc::read(unsigned char *buf, size_t len)
{
if (!file)
return 0;
return fread(buf, 1, len, file);
}
size_t piped_proc::write(unsigned char *buf, size_t len)
{
if (!file)
return 0;
return fwrite(buf, 1, len, file);
}
#elif _WIN32
piped_proc::piped_proc(std::string cmd, bool write)
{
PROCESS_INFORMATION process;
STARTUPINFO siStartInfo;
SECURITY_ATTRIBUTES saAttr;
memset(&process, 0, sizeof(PROCESS_INFORMATION));
memset(&siStartInfo, 0, sizeof(STARTUPINFO));
memset(&saAttr, 0, sizeof(SECURITY_ATTRIBUTES));
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = nullptr;
if (!CreatePipe(&pipe_rd, &pipe_wr, &saAttr, 0)) {
ErrorLog("piped_proc: CreatePipe failed!");
return;
}
if (!SetHandleInformation(write ? pipe_wr : pipe_rd, HANDLE_FLAG_INHERIT, 0)) {
ErrorLog("piped_proc: SetHandleInformation failed!");
return;
}
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if (!write)
siStartInfo.hStdOutput = pipe_wr;
else
siStartInfo.hStdInput = pipe_rd;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
if (!CreateProcessA(nullptr, (char*)cmd.c_str(), nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &siStartInfo, &process)) {
ErrorLog("piped_proc: CreateProcess failed!");
return;
}
proc_h = process.hProcess;
if (process.hThread)
CloseHandle(process.hThread);
}
piped_proc::~piped_proc()
{
if (pipe_wr)
CloseHandle(pipe_wr);
if (pipe_rd)
CloseHandle(pipe_rd);
if (proc_h)
CloseHandle(proc_h);
}
size_t piped_proc::read(unsigned char *buf, size_t len)
{
if (!pipe_rd)
return 0;
DWORD read = 0;
BOOL ret = ReadFile(pipe_rd, buf, len, &read, nullptr);
return read;
}
size_t piped_proc::write(unsigned char *buf, size_t len)
{
if (!pipe_wr)
return 0;
DWORD wrote = 0;
BOOL ret = WriteFile(pipe_wr, buf, len, &wrote, nullptr);
return wrote;
}
#endif