Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/src/line.cpp

50 lines
1.0 KiB
C++
Raw Normal View History

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