Fix readline

This commit is contained in:
2026-01-07 12:09:15 -06:00
parent f88cc21a85
commit 7dc4ad9b08
5 changed files with 34 additions and 10 deletions

View File

@@ -5,12 +5,14 @@ namespace Tesses::Framework::TextStreams
{ {
class TextReader class TextReader
{ {
bool eof=false;
public: public:
virtual bool Rewind(); virtual bool Rewind();
virtual bool ReadBlock(std::string& str,size_t sz)=0; virtual bool ReadBlock(std::string& str,size_t sz)=0;
int32_t ReadChar(); int32_t ReadChar();
std::string ReadLine(); std::string ReadLine();
bool ReadLine(std::string& str); bool ReadLine(std::string& str);
bool ReadLineHttp(std::string& str);
void ReadAllLines(std::vector<std::string>& lines); void ReadAllLines(std::vector<std::string>& lines);
std::string ReadToEnd(); std::string ReadToEnd();
void ReadToEnd(std::string& str); void ReadToEnd(std::string& str);

View File

@@ -104,7 +104,7 @@ namespace Tesses::Framework::Http {
auto stout = p.GetStdoutStream(); auto stout = p.GetStdoutStream();
Tesses::Framework::TextStreams::StreamReader reader(stout); Tesses::Framework::TextStreams::StreamReader reader(stout);
std::string line; std::string line;
while(reader.ReadLine(line)) while(reader.ReadLineHttp(line))
{ {
auto v = HttpUtils::SplitString(line,": ", 2); auto v = HttpUtils::SplitString(line,": ", 2);
if(v.size() == 2) if(v.size() == 2)

View File

@@ -159,7 +159,7 @@ namespace Tesses::Framework::Http
this->handleStrm=nullptr; this->handleStrm=nullptr;
StreamReader reader(strm); StreamReader reader(strm);
std::string statusLine; std::string statusLine;
if(!reader.ReadLine(statusLine)) return; if(!reader.ReadLineHttp(statusLine)) return;
auto statusLinesPart = HttpUtils::SplitString(statusLine," ",3); auto statusLinesPart = HttpUtils::SplitString(statusLine," ",3);
if(statusLinesPart.size() >= 2) if(statusLinesPart.size() >= 2)
{ {
@@ -167,7 +167,7 @@ namespace Tesses::Framework::Http
this->statusCode = (StatusCode)std::stoi(statusLinesPart[1]); this->statusCode = (StatusCode)std::stoi(statusLinesPart[1]);
} }
std::string line; std::string line;
while(reader.ReadLine(line)) while(reader.ReadLineHttp(line))
{ {
if(line.empty()) break; if(line.empty()) break;
auto v = HttpUtils::SplitString(line,": ", 2); auto v = HttpUtils::SplitString(line,": ", 2);
@@ -225,7 +225,7 @@ namespace Tesses::Framework::Http
StreamReader reader(strm); StreamReader reader(strm);
std::string statusLine; std::string statusLine;
if(!reader.ReadLine(statusLine)) break; if(!reader.ReadLineHttp(statusLine)) break;
auto statusLinesPart = HttpUtils::SplitString(statusLine," ",3); auto statusLinesPart = HttpUtils::SplitString(statusLine," ",3);
if(statusLinesPart.size() >= 2) if(statusLinesPart.size() >= 2)
{ {
@@ -233,7 +233,7 @@ namespace Tesses::Framework::Http
this->statusCode = (StatusCode)std::stoi(statusLinesPart[1]); this->statusCode = (StatusCode)std::stoi(statusLinesPart[1]);
} }
std::string line; std::string line;
while(reader.ReadLine(line)) while(reader.ReadLineHttp(line))
{ {
if(line.empty()) break; if(line.empty()) break;
auto v = HttpUtils::SplitString(line,": ", 2); auto v = HttpUtils::SplitString(line,": ", 2);

View File

@@ -468,7 +468,7 @@ namespace Tesses::Framework::Http
HttpDictionary req; HttpDictionary req;
StreamReader reader(ctx->GetStream()); StreamReader reader(ctx->GetStream());
std::string line; std::string line;
while(reader.ReadLine(line)) while(reader.ReadLineHttp(line))
{ {
auto v = HttpUtils::SplitString(line,": ", 2); auto v = HttpUtils::SplitString(line,": ", 2);
if(v.size() == 2) if(v.size() == 2)
@@ -944,7 +944,7 @@ namespace Tesses::Framework::Http
try{ try{
bool firstLine = true; bool firstLine = true;
std::string line; std::string line;
while(reader.ReadLine(line)) while(reader.ReadLineHttp(line))
{ {
if(firstLine) if(firstLine)
{ {

View File

@@ -10,7 +10,7 @@ namespace Tesses::Framework::TextStreams
{ {
std::string txt; std::string txt;
this->ReadBlock(txt,1); this->ReadBlock(txt,1);
if(txt.empty()) return -1; if(txt.empty()) { eof=true; return -1;}
return (uint8_t)txt[0]; return (uint8_t)txt[0];
} }
std::string TextReader::ReadLine() std::string TextReader::ReadLine()
@@ -19,13 +19,14 @@ namespace Tesses::Framework::TextStreams
ReadLine(str); ReadLine(str);
return str; return str;
} }
bool TextReader::ReadLine(std::string& str) bool TextReader::ReadLineHttp(std::string& str)
{ {
if(eof) return false;
bool ret = false; bool ret = false;
int32_t r = -1; int32_t r = -1;
do { do {
r = ReadChar(); r = ReadChar();
if(r == -1) break; if(r == -1) {break;}
if(r == '\r') continue; if(r == '\r') continue;
if(r == '\n') break; if(r == '\n') break;
str.push_back((char)(uint8_t)r); str.push_back((char)(uint8_t)r);
@@ -33,8 +34,25 @@ namespace Tesses::Framework::TextStreams
} while(r != -1); } while(r != -1);
return ret; return ret;
} }
bool TextReader::ReadLine(std::string& str)
{
if(eof) return false;
bool ret = false;
int32_t r = -1;
do {
r = ReadChar();
if(r == -1) break;
if(r == '\r') continue;
if(r == '\n') return true;
str.push_back((char)(uint8_t)r);
ret = true;
} while(r != -1);
return ret;
}
void TextReader::ReadAllLines(std::vector<std::string>& lines) void TextReader::ReadAllLines(std::vector<std::string>& lines)
{ {
if(eof) return;
int32_t r = -1; int32_t r = -1;
std::string builder; std::string builder;
do { do {
@@ -60,10 +78,14 @@ namespace Tesses::Framework::TextStreams
void TextReader::ReadToEnd(std::string& str) void TextReader::ReadToEnd(std::string& str)
{ {
if(eof) return;
while(ReadBlock(str,1024)); while(ReadBlock(str,1024));
} }
void TextReader::CopyTo(TextWriter& writer, size_t buffSz) void TextReader::CopyTo(TextWriter& writer, size_t buffSz)
{ {
if(eof) return;
std::string str = {}; std::string str = {};
while(ReadBlock(str,buffSz)) while(ReadBlock(str,buffSz))
{ {