50 lines
1.0 KiB
C++
50 lines
1.0 KiB
C++
|
#include "WordsPeak.h"
|
||
|
#include <QDebug>
|
||
|
|
||
|
using namespace Parse;
|
||
|
|
||
|
ForwardStream::ForwardStream() :file_target(nullptr), text_input(nullptr),
|
||
|
current_line(""), current_row(-1), current_col(0) {}
|
||
|
|
||
|
ForwardStream::~ForwardStream() {
|
||
|
if (file_target)
|
||
|
delete file_target;
|
||
|
if (text_input)
|
||
|
delete text_input;
|
||
|
}
|
||
|
|
||
|
int ForwardStream::initSource(const QString & path)
|
||
|
{
|
||
|
if (file_target)
|
||
|
delete file_target;
|
||
|
if (text_input)
|
||
|
delete text_input;
|
||
|
|
||
|
if (!QFile(path).exists())
|
||
|
return -1;
|
||
|
|
||
|
file_target = new QFile(path);
|
||
|
if (!file_target->open(QIODevice::ReadOnly | QIODevice::Text))
|
||
|
return -2;
|
||
|
|
||
|
text_input = new QTextStream(file_target);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
std::tuple<ForwardStream::n_row, ForwardStream::n_col, QChar> Parse::ForwardStream::read()
|
||
|
{
|
||
|
if (current_col >= current_line.length()) {
|
||
|
if (!text_input->atEnd()) {
|
||
|
current_row++;
|
||
|
current_col = 0;
|
||
|
current_line = text_input->readLine() + '\n';
|
||
|
}
|
||
|
else {
|
||
|
return std::make_tuple(-1, -1, EOF);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return std::make_tuple(current_row, current_col, current_line[current_col++]);
|
||
|
}
|
||
|
|