LIRC libraries
LinuxInfraredRemoteControl
 All Classes Files Functions Variables Typedefs Enumerations Macros Modules Pages
line_buffer.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** line_buffer.cpp ****************************************************
3 ***********************************************************************
4 *
5 * line_buffer - line buffer for use with read(2)
6 *
7 * Copyright (c) 2015 Alec Leamas
8 *
9 * */
10 
16 #include <errno.h>
17 #include <string.h>
18 
19 #include "line_buffer.h"
20 
21 
23 {
24  return buff.find('\n') != std::string::npos;
25 }
26 
27 
28 void LineBuffer::append(const char* input, size_t size)
29 {
30  buff.append(input, size);
31 }
32 
33 
34 const char* LineBuffer::c_str()
35 {
36  return buff.c_str();
37 }
38 
39 
41 {
42  size_t nl = buff.find('\n');
43  if (nl == std::string::npos)
44  return "";
45  std::string line(buff.substr(0, nl + 1));
46  buff.erase(0, nl + 1);
47 
48  /* remove DOS line endings */
49  size_t pos = line.rfind("\r");
50  if (pos == line.size() - 1)
51  line.erase(pos, 1);
52  return line;
53 }
54 
55 
56 LineBuffer::LineBuffer()
57 {
58  buff = "";
59 }
std::string get_next_line()
Return and remove first line in buffer, possibly "".
Definition: line_buffer.cpp:40
const char * c_str()
Peek the complete buffer contents.
Definition: line_buffer.cpp:34
void append(const char *line, size_t size)
Insert data in buffer.
Definition: line_buffer.cpp:28
Implements the line buffer class.
bool has_lines()
Check if get_next_line() returns a non-empty string.
Definition: line_buffer.cpp:22