2016-10-15 12:37:59 -04:00
|
|
|
#include "line.hpp"
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
namespace nntpchan {
|
|
|
|
|
|
2017-05-03 08:09:23 -04:00
|
|
|
LineReader::LineReader(size_t limit) : m_close(false), lineLimit(limit) {}
|
|
|
|
|
|
2017-05-03 09:44:42 -04:00
|
|
|
void LineReader::Data(const char * d, ssize_t l)
|
2016-10-15 12:37:59 -04:00
|
|
|
{
|
|
|
|
|
if(l <= 0) return;
|
2017-05-03 08:09:23 -04:00
|
|
|
// process leftovers
|
|
|
|
|
std::string current = m_leftovers + std::string(d, l);
|
|
|
|
|
if(current.size() > lineLimit) {
|
|
|
|
|
m_close = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-10-15 12:37:59 -04:00
|
|
|
std::size_t idx = 0;
|
2017-05-03 08:09:23 -04:00
|
|
|
ssize_t begin = l;
|
|
|
|
|
const char * data = current.c_str();
|
2016-10-15 12:37:59 -04:00
|
|
|
while(l-- > 0) {
|
2017-05-03 08:09:23 -04:00
|
|
|
char c = data[idx++];
|
2016-10-15 12:37:59 -04:00
|
|
|
if(c == '\n') {
|
2017-05-03 08:09:23 -04:00
|
|
|
OnLine(data, idx-1);
|
|
|
|
|
data += idx;
|
|
|
|
|
} else if (c == '\r' && data[idx] == '\n') {
|
|
|
|
|
OnLine(data, idx-1);
|
|
|
|
|
data += idx + 1;
|
2016-10-15 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-05-03 08:09:23 -04:00
|
|
|
if (idx < begin)
|
|
|
|
|
{
|
|
|
|
|
// leftovers
|
|
|
|
|
m_leftovers = std::string(data, begin-idx);
|
|
|
|
|
}
|
2017-05-03 09:44:42 -04:00
|
|
|
else
|
|
|
|
|
m_leftovers = "";
|
2016-10-15 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LineReader::OnLine(const char *d, const size_t l)
|
|
|
|
|
{
|
|
|
|
|
std::string line(d, l);
|
|
|
|
|
HandleLine(line);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-03 08:09:23 -04:00
|
|
|
bool LineReader::ShouldClose()
|
2016-10-15 12:37:59 -04:00
|
|
|
{
|
2017-05-03 08:09:23 -04:00
|
|
|
return m_close;
|
2016-10-15 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
}
|