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.

41 lines
796 B
C++
Raw Normal View History

2017-10-11 09:48:27 -04:00
#include <nntpchan/line.hpp>
2016-10-15 12:37:59 -04:00
namespace nntpchan {
2017-05-03 08:09:23 -04:00
LineReader::LineReader(size_t limit) : m_close(false), lineLimit(limit) {}
2017-05-03 18:31:19 -04:00
void LineReader::Data(const char * data, 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
2016-10-15 12:37:59 -04:00
std::size_t idx = 0;
2017-05-03 13:33:04 -04:00
std::size_t pos = 0;
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 13:33:04 -04:00
OnLine(data, pos);
pos = 0;
2017-05-03 08:09:23 -04:00
data += idx;
} else if (c == '\r' && data[idx] == '\n') {
2017-05-03 13:33:04 -04:00
OnLine(data, pos);
2017-05-03 08:09:23 -04:00
data += idx + 1;
2017-05-03 13:33:04 -04:00
pos = 0;
} else {
pos ++;
2016-10-15 12:37:59 -04:00
}
}
}
void LineReader::OnLine(const char *d, const size_t l)
{
2017-10-09 12:51:49 -04:00
std::string line;
line += std::string(d, l);
2016-10-15 12:37:59 -04:00
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
}
}