first commit
This commit is contained in:
39
src/TextStreams/StreamReader.cpp
Normal file
39
src/TextStreams/StreamReader.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "TessesFramework/TextStreams/StreamReader.hpp"
|
||||
#include "TessesFramework/Streams/FileStream.hpp"
|
||||
using Stream = Tesses::Framework::Streams::Stream;
|
||||
using FileStream = Tesses::Framework::Streams::FileStream;
|
||||
|
||||
namespace Tesses::Framework::TextStreams {
|
||||
StreamReader::StreamReader(Stream& strm) : StreamReader(&strm, false)
|
||||
{
|
||||
|
||||
}
|
||||
StreamReader::StreamReader(std::filesystem::path path) : StreamReader(new FileStream(path,"rb"),true)
|
||||
{
|
||||
|
||||
}
|
||||
StreamReader::StreamReader(Stream* strm, bool owns) : TextReader()
|
||||
{
|
||||
this->strm = strm;
|
||||
this->owns = owns;
|
||||
}
|
||||
|
||||
Stream& StreamReader::GetStream()
|
||||
{
|
||||
return *(this->strm);
|
||||
}
|
||||
|
||||
bool StreamReader::ReadBlock(std::string& str, size_t len)
|
||||
{
|
||||
uint8_t buff[len];
|
||||
len = strm->ReadBlock(buff,len);
|
||||
if(len == 0) return false;
|
||||
str.append((const char*)buff, len);
|
||||
return true;
|
||||
}
|
||||
StreamReader::~StreamReader()
|
||||
{
|
||||
if(this->owns)
|
||||
delete this->strm;
|
||||
}
|
||||
};
|
||||
34
src/TextStreams/StreamWriter.cpp
Normal file
34
src/TextStreams/StreamWriter.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "TessesFramework/Streams/FileStream.hpp"
|
||||
#include "TessesFramework/TextStreams/StreamWriter.hpp"
|
||||
using Stream = Tesses::Framework::Streams::Stream;
|
||||
using FileStream = Tesses::Framework::Streams::FileStream;
|
||||
|
||||
namespace Tesses::Framework::TextStreams
|
||||
{
|
||||
Stream& StreamWriter::GetStream()
|
||||
{
|
||||
return *(this->strm);
|
||||
}
|
||||
StreamWriter::StreamWriter(Stream& strm) : StreamWriter(&strm, false)
|
||||
{
|
||||
|
||||
}
|
||||
StreamWriter::StreamWriter(Stream* strm, bool owns) : TextWriter()
|
||||
{
|
||||
this->strm = strm;
|
||||
this->owns = owns;
|
||||
}
|
||||
StreamWriter::StreamWriter(std::filesystem::path filename, bool append) : StreamWriter(new FileStream(filename, append ? "ab" : "wb"),true)
|
||||
{
|
||||
|
||||
}
|
||||
void StreamWriter::WriteData(const char* text, size_t len)
|
||||
{
|
||||
this->strm->WriteBlock((const uint8_t*)text, len);
|
||||
}
|
||||
StreamWriter::~StreamWriter()
|
||||
{
|
||||
if(this->owns)
|
||||
delete this->strm;
|
||||
}
|
||||
}
|
||||
58
src/TextStreams/TextReader.cpp
Normal file
58
src/TextStreams/TextReader.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include "TessesFramework/TextStreams/TextReader.hpp"
|
||||
|
||||
namespace Tesses::Framework::TextStreams
|
||||
{
|
||||
int32_t TextReader::ReadChar()
|
||||
{
|
||||
std::string txt;
|
||||
this->ReadBlock(txt,1);
|
||||
if(txt.empty()) return -1;
|
||||
return (uint8_t)txt[0];
|
||||
}
|
||||
std::string TextReader::ReadLine()
|
||||
{
|
||||
std::string str = {};
|
||||
ReadLine(str);
|
||||
return str;
|
||||
}
|
||||
bool TextReader::ReadLine(std::string& str)
|
||||
{
|
||||
bool ret = false;
|
||||
int32_t r = -1;
|
||||
do {
|
||||
r = ReadChar();
|
||||
if(r == -1) break;
|
||||
if(r == '\r') continue;
|
||||
if(r == '\n') break;
|
||||
str.push_back((char)(uint8_t)r);
|
||||
ret = true;
|
||||
} while(r != -1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string TextReader::ReadToEnd()
|
||||
{
|
||||
std::string str = {};
|
||||
ReadToEnd(str);
|
||||
return str;
|
||||
}
|
||||
|
||||
void TextReader::ReadToEnd(std::string& str)
|
||||
{
|
||||
while(ReadBlock(str,1024));
|
||||
}
|
||||
void TextReader::CopyTo(TextWriter& writer, size_t buffSz)
|
||||
{
|
||||
std::string str = {};
|
||||
while(ReadBlock(str,buffSz))
|
||||
{
|
||||
writer.Write(str);
|
||||
str.clear();
|
||||
}
|
||||
}
|
||||
|
||||
TextReader::~TextReader()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
31
src/TextStreams/TextWriter.cpp
Normal file
31
src/TextStreams/TextWriter.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "TessesFramework/TextStreams/TextWriter.hpp"
|
||||
|
||||
namespace Tesses::Framework::TextStreams
|
||||
{
|
||||
TextWriter::TextWriter()
|
||||
{
|
||||
#if defined(WIN32) || defined(_WIN32)
|
||||
newline = "\r\n";
|
||||
#else
|
||||
newline = "\n";
|
||||
#endif
|
||||
}
|
||||
void TextWriter::Write(std::string txt)
|
||||
{
|
||||
WriteData(txt.c_str(),txt.size());
|
||||
}
|
||||
void TextWriter::WriteLine(std::string txt)
|
||||
{
|
||||
std::string str = txt;
|
||||
str.append(newline);
|
||||
Write(str);
|
||||
}
|
||||
void TextWriter::WriteLine()
|
||||
{
|
||||
Write(newline);
|
||||
}
|
||||
TextWriter::~TextWriter()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user