add Run::Go(...) methods with pt::TextStream arguments

This commit is contained in:
tomasz.sowa committed 2026-11-24 11:37:29 +01:00
1 parent c9fd6c1a32
commit 30e4b804e6
2 files changed
+233 -52

No files matched your search

+197 -35
View File
@@ -147,33 +147,60 @@ int Run::LastReturn()
void Run::Write(const char * in, size_t inlen)
bool Run::Write(const char * in, size_t inlen, int descriptor)
{
ssize_t len;
while( inlen > 0 )
{
len = write(desout[1], in, inlen);
len = write(descriptor, in, inlen);
if( len < 0 || len > (ssize_t)inlen )
{
// something wrong
last_status = 3;
break;
return false;
}
in += len;
inlen -= len;
}
return true;
}
bool Run::Write(const pt::TextStream & in, int descriptor)
{
pt::TextStream::const_iterator i = in.begin();
pt::TextStream::const_iterator i_end = in.end();
char buf[1024];
size_t buf_max_len = sizeof(buf) / sizeof(char);
while( i != i_end )
{
size_t buf_len=0;
for( ; i != i_end && buf_len < buf_max_len ; ++i, ++buf_len)
{
buf[buf_len] = *i;
}
if( !Write(buf, buf_len, descriptor) )
{
return false;
}
}
return true;
}
void Run::Read(std::string & out, int descriptor)
{
char buf[1024];
size_t buflen = sizeof(buf) / sizeof(char);
ssize_t len;
char buf[1024];
size_t buflen = sizeof(buf) / sizeof(char);
ssize_t len;
do
{
@@ -193,6 +220,29 @@ ssize_t len;
}
void Run::Read(pt::TextStream & out, int descriptor)
{
char buf[1024];
size_t buflen = sizeof(buf) / sizeof(char);
ssize_t len;
do
{
len = read(descriptor, buf, buflen);
if( len < 0 || len > (ssize_t)buflen )
{
// something wrong
last_status = 4;
break;
}
if( len > 0 )
out.write(buf, len);
}
while( len > 0 );
}
void Run::ChildThrow()
{
@@ -297,13 +347,28 @@ return true;
void Run::WriteRead(const char * in, size_t inlen, std::string * out, std::string * err_out)
void Run::WriteRead(const char * in, size_t inlen, pt::TextStream * out, pt::TextStream * err_out)
{
if( in && inlen>0 )
Write(in, inlen);
if( in && inlen > 0 )
Write(in, inlen, desout[1]);
close(desout[1]);
WriteReadFinish(out, err_out);
}
void Run::WriteRead(const pt::TextStream * in, pt::TextStream * out, pt::TextStream * err_out)
{
if( in && !in->empty() )
Write(*in, desout[1]);
close(desout[1]);
WriteReadFinish(out, err_out);
}
void Run::WriteReadFinish(pt::TextStream * out, pt::TextStream * err_out)
{
if( out )
Read(*out, desin[0]);
@@ -315,7 +380,6 @@ void Run::WriteRead(const char * in, size_t inlen, std::string * out, std::strin
}
void Run::CheckStatus()
{
int status;
@@ -350,8 +414,38 @@ int status;
}
int Run::GoStringStream(const char * in, size_t inlen, pt::TextStream * out, pt::TextStream * err_out)
{
if( GoPrepare(out, err_out) )
{
WriteRead(in, inlen, out, err_out);
GoFinish();
}
int Run::Go(const char * in, size_t inlen, std::string * out, std::string * err_out)
return last_return;
}
int Run::GoStringString(const char * in, size_t inlen, std::string * out, std::string * err_out)
{
pt::TextStream out_str;
pt::TextStream err_out_str;
pt::TextStream * pout_str = (out) ? &out_str : nullptr;
pt::TextStream * perr_out_str = (err_out) ? & err_out_str : nullptr;
int res = GoStringStream(in, inlen, pout_str, perr_out_str);
if( out )
out_str.to_str(*out);
if( err_out )
err_out_str.to_str(*err_out);
return res;
}
bool Run::GoPrepare(pt::TextStream * out, pt::TextStream * err_out)
{
last_status = 0;
last_return = 255;
@@ -359,89 +453,157 @@ int Run::Go(const char * in, size_t inlen, std::string * out, std::string * err_
if( out )
out->clear();
if( err_out )
err_out->clear();
if( !command )
{
last_status = 7;
return last_return;
return false;
}
if( !CreatePipes() )
return last_return;
return false;
if( !Fork() )
return last_return;
return false;
// here goes the parent
close(desin[1]);
close(desin_err[1]);
close(desout[0]);
WriteRead(in, inlen, out, err_out);
return true;
}
void Run::GoFinish()
{
CheckStatus();
if( last_status != 0 )
log << log1 << "Run: a problem with the command, last_status: " << last_status << logend;
return last_return;
}
int Run::Go(const char * in, size_t inlen, std::string & out)
int Run::Go(const pt::TextStream * in, pt::TextStream * out, pt::TextStream * err_out)
{
return Go(in, inlen, &out, nullptr);
if( GoPrepare(out, err_out) )
{
WriteRead(in, out, err_out);
GoFinish();
}
return last_return;
}
int Run::Go(const char * in, std::string & out)
int Run::Go(const pt::TextStream & in, pt::TextStream & out, pt::TextStream & err_out)
{
size_t len = strlen(in);
return Go(in, len, &out, nullptr);
return Go(&in, &out, &err_out);
}
int Run::Go(const char * in, std::string & out, std::string & err_out)
int Run::Go(const pt::TextStream & in, pt::TextStream & out)
{
size_t len = strlen(in);
return Go(in, len, &out, &err_out);
return Go(&in, &out, nullptr);
}
int Run::Go(const pt::TextStream & in)
{
return Go(&in, nullptr, nullptr);
}
int Run::Go()
{
return Go(nullptr, nullptr, nullptr);
}
int Run::Go(const char * in, size_t inlen, pt::TextStream & out, pt::TextStream & err_out)
{
return GoStringStream(in, inlen, &out, &err_out);
}
int Run::Go(const char * in, size_t inlen, pt::TextStream & out)
{
return GoStringStream(in, inlen, &out, nullptr);
}
int Run::Go(const char * in, size_t inlen)
{
return Go(in, inlen, nullptr, nullptr);
return GoStringStream(in, inlen, nullptr, nullptr);
}
int Run::Go(const char * in, pt::TextStream & out, pt::TextStream & err_out)
{
size_t len = strlen(in);
return GoStringStream(in, len, &out, &err_out);
}
int Run::Go(const char * in, pt::TextStream & out)
{
size_t len = strlen(in);
return GoStringStream(in, len, &out, nullptr);
}
int Run::Go(const char * in)
{
size_t len = strlen(in);
return Go(in, len, nullptr, nullptr);
return GoStringStream(in, len, nullptr, nullptr);
}
int Run::Go()
int Run::Go(const char * in, size_t inlen, std::string & out, std::string & err_out)
{
return Go(nullptr, 0, nullptr, nullptr);
return GoStringString(in, inlen, &out, &err_out);
}
int Run::Go(const std::string in, std::string & out)
int Run::Go(const char * in, size_t inlen, std::string & out)
{
return Go(in.c_str(), in.size(), &out, nullptr);
return GoStringString(in, inlen, &out, nullptr);
}
int Run::Go(const std::string in)
int Run::Go(const char * in, std::string & out, std::string & err_out)
{
return Go(in.c_str(), in.size(), nullptr, nullptr);
size_t len = strlen(in);
return GoStringString(in, len, &out, &err_out);
}
int Run::Go(std::string & out)
int Run::Go(const char * in, std::string & out)
{
return Go(nullptr, 0, &out, nullptr);
size_t len = strlen(in);
return GoStringString(in, len, &out, nullptr);
}
int Run::Go(const std::string & in, std::string & out, std::string & err_out)
{
return GoStringString(in.c_str(), in.size(), &out, &err_out);
}
int Run::Go(const std::string & in, std::string & out)
{
return GoStringString(in.c_str(), in.size(), &out, nullptr);
}
int Run::Go(const std::string & in)
{
return GoStringStream(in.c_str(), in.size(), nullptr, nullptr);
}
} // namespace Winix
+36 -17
View File
@@ -120,22 +120,32 @@ public:
/*
executing the command
you should call Par(), Env() and Cmd() beforehand
*/
int Go(const char * in, size_t inlen, std::string & out);
int Go(const char * in, std::string & out);
int Go(const char * in, size_t inlen);
int Go(const char * in);
int Go(const std::string in, std::string & out);
int Go(const std::string in);
int Go(std::string & out);
* executing the command
* you should call Par(), Env() and Cmd() beforehand
*
* in the first method in, out and err_out can be a null
*
*/
int Go(const pt::TextStream * in, pt::TextStream * out, pt::TextStream * err_out);
int Go(const pt::TextStream & in, pt::TextStream & out, pt::TextStream & err_out);
int Go(const pt::TextStream & in, pt::TextStream & out);
int Go(const pt::TextStream & in);
int Go();
/*
* with standard error
*/
int Go(const char * in, size_t inlen, pt::TextStream & out, pt::TextStream & err_out);
int Go(const char * in, size_t inlen, pt::TextStream & out);
int Go(const char * in, size_t inlen);
int Go(const char * in, pt::TextStream & out, pt::TextStream & err_out);
int Go(const char * in, pt::TextStream & out);
int Go(const char * in);
int Go(const char * in, size_t inlen, std::string & out, std::string & err_out);
int Go(const char * in, size_t inlen, std::string & out);
int Go(const char * in, std::string & out, std::string & err_out);
int Go(const char * in, std::string & out);
int Go(const std::string & in, std::string & out, std::string & err_out);
int Go(const std::string & in, std::string & out);
int Go(const std::string & in);
/*
@@ -163,16 +173,25 @@ public:
private:
int Go(const char * in, size_t inlen, std::string * out, std::string * err_out);
int GoStringStream(const char * in, size_t inlen, pt::TextStream * out, pt::TextStream * err_out);
int GoStringString(const char * in, size_t inlen, std::string * out, std::string * err_out);
bool GoPrepare(pt::TextStream * out, pt::TextStream * err_out);
void GoFinish();
void SetName();
void CheckStatus();
void WriteRead(const char * in, size_t inlen, std::string * out, std::string * err_out);
void WriteRead(const char * in, size_t inlen, pt::TextStream * out, pt::TextStream * err_out);
void WriteRead(const pt::TextStream * in, pt::TextStream * out, pt::TextStream * err_out);
void WriteReadFinish(pt::TextStream * out, pt::TextStream * err_out);
bool CreatePipes();
bool Fork();
void ChildThrow();
void Child();
void Write(const char * in, size_t inlen);
bool Write(const char * in, size_t inlen, int descriptor);
bool Write(const pt::TextStream & in, int descriptor);
void Read(std::string & out, int descriptor);
void Read(pt::TextStream & out, int descriptor);
int last_status;
@@ -187,7 +206,7 @@ private:
char * par[WINIX_RUN_MAX_PARAMETERS + 2];
char * env[WINIX_RUN_MAX_PARAMETERS + 1];
const char * command;
const char * command; // a pointer to the command being run
size_t parlen;
size_t envlen;