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/libnntpchan/line.cpp

40 lines
762 B
C++
Raw Normal View History

2016-10-15 21:37:59 +05:00
#include "line.hpp"
namespace nntpchan {
2017-05-03 17:09:23 +05:00
LineReader::LineReader(size_t limit) : m_close(false), lineLimit(limit) {}
2017-05-04 03:31:19 +05:00
void LineReader::Data(const char * data, 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
2016-10-15 21:37:59 +05:00
std::size_t idx = 0;
2017-05-03 22:33:04 +05:00
std::size_t pos = 0;
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 22:33:04 +05:00
OnLine(data, pos);
pos = 0;
2017-05-03 17:09:23 +05:00
data += idx;
} else if (c == '\r' && data[idx] == '\n') {
2017-05-03 22:33:04 +05:00
OnLine(data, pos);
2017-05-03 17:09:23 +05:00
data += idx + 1;
2017-05-03 22:33:04 +05:00
pos = 0;
} else {
pos ++;
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
}
}