Loading...
Searching...
No Matches
line_buffer.cpp
Go to the documentation of this file.
/**********************************************************************
** line_buffer.cpp ****************************************************
***********************************************************************
*
* line_buffer - line buffer for use with read(2)
*
* Copyright (c) 2015 Alec Leamas
*
* */
#include <errno.h>
#include <string.h>
#include "line_buffer.h"
bool LineBuffer::has_lines()
{
return buff.find('\n') != std::string::npos;
}
void LineBuffer::append(const char* input, size_t size)
{
buff.append(input, size);
}
const char* LineBuffer::c_str()
{
return buff.c_str();
}
std::string LineBuffer::get_next_line()
{
size_t nl = buff.find('\n');
if (nl == std::string::npos)
return "";
std::string line(buff.substr(0, nl + 1));
buff.erase(0, nl + 1);
/* remove DOS line endings */
size_t pos = line.rfind("\r");
if (pos == line.size() - 1)
line.erase(pos, 1);
return line;
}
LineBuffer::LineBuffer()
{
buff = "";
}
LineBuffer::c_str
const char * c_str()
Peek the complete buffer contents.
Definition line_buffer.cpp:34
LineBuffer::append
void append(const char *line, size_t size)
Insert data in buffer.
Definition line_buffer.cpp:28
LineBuffer::get_next_line
std::string get_next_line()
Return and remove first line in buffer, possibly "".
Definition line_buffer.cpp:40
LineBuffer::has_lines
bool has_lines()
Check if get_next_line() returns a non-empty string.
Definition line_buffer.cpp:22
line_buffer.h
Implements the line buffer class.