30 std::lock_guard<std::mutex> lock(m_mutex);
31 m_buffer.push_back(line);
35 std::lock_guard<std::mutex> lock(m_mutex);
36 if (m_buffer.size() == 0) {
45 if (m_last_ch ==
'\n' && ch ==
'\r') {
48 }
else if (m_last_ch ==
'\r' && ch ==
'\n') {
50 if (m_buff.size() > 0) m_buff[m_buff.size() - 1] =
'\n';
56 if (ch ==
'\n') m_line_count += 1;
63 auto nl_pos = find(m_buff.begin(), m_buff.end(),
'\n');
64 if (nl_pos == m_buff.end()) {
65 return vector<uint8_t>();
67 auto line = vector<uint8_t>(m_buff.begin(), nl_pos);
68 m_buff = vector<uint8_t>(nl_pos + 1, m_buff.end());
75 if (m_lines.empty())
return "";
76 auto sentence = m_lines.front();
83 case State::PrefixWait:
85 if (ch ==
'$' || ch ==
'!') {
88 m_state = State::Data;
93 if (std::isprint(ch)) {
95 if (ch ==
'*') m_state = State::CsDigit1;
98 m_state = State::PrefixWait;
101 case State::CsDigit1:
103 if (std::isxdigit(ch)) {
104 m_line.push_back(ch);
105 m_state = State::CsDigit2;
107 m_state = State::PrefixWait;
110 case State::CsDigit2:
112 if (std::isxdigit(ch)) {
113 m_line.push_back(ch);
114 m_lines.emplace_back(m_line.begin(), m_line.end());
116 m_state = State::PrefixWait;
void Put(uint8_t ch)
Add a single character.
std::vector< uint8_t > GetLine()
Retrieve a line from buffer, return empty line if none available.
bool HasLine() const
Return true if a line is available to be returned by GetLine().
std::string GetSentence()
Retrieve a sentence from buffer.
void Put(uint8_t ch)
Add a single character, possibly making a sentence available.
void Put(const std::string line)
Insert line in buffer.
bool Get(std::string &line)
Retrieve a line in buffer.
Line-oriented input/output buffers.