Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
line.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <utils/queue.h>
8
13class LineFilter : public Source<Buffer<uint8_t>> {
14 static constexpr size_t linelen = 128;
16 Buffer<uint8_t> l = linelen; // stash
18 void recv(uint8_t b) {
19 // handle both \n and \r\n newlines
20 if (b == '\n' || b == '\r') {
21 if (l.len == 0) return;
22 q.trypush(std::move(l));
23 l = linelen;
24 return;
25 }
26 if (l.len + 1 > linelen) {
27 // XXX: failure: line was longer than our buffer
28 // Drop Line
29 l = linelen;
30 }
31 l.append(b);
32 }
33public:
35 LineFilter(Source<Buffer<uint8_t>> &p): source(p) { }
36 bool empty() override {
37 while (!q.full() && !source.empty()) {
38 for (auto b: source.pop()) {
39 recv(b);
40 }
41 }
42 return q.empty();
43 }
44 Buffer<uint8_t> pop() override { return q.pop(); }
45};
46
48class LineDelimiter : public Sink<Buffer<uint8_t>> {
50public:
53 bool full() override {
54 return p.full();
55 }
56 void push(const Buffer<uint8_t> &b) override {
57 if (b.len + 1 <= b.size) {
58 push(std::move(Buffer<uint8_t>{b}));
59 } else {
60 push(std::move(Buffer<uint8_t>{b.buf, b.size, b.size+1}));
61 }
62 }
63 void push(Buffer<uint8_t> &&b) override {
64 if (b.len + 1 <= b.size) {
65 p.push(std::move(b.append('\n')));
66 } else {
67 p.push(std::move(Buffer<uint8_t>{b.buf, b.size, b.size+1}.append('\n')));
68 }
69 }
70};
simple pipe that will append a newline to every data packet
Definition line.h:48
void push(Buffer< uint8_t > &&b) override
move semantics does not check for space. guard by using 'if (!full) { ... }'
Definition line.h:63
void push(const Buffer< uint8_t > &b) override
copy semantics does not check for space. guard by using 'if (!full) { ... }'
Definition line.h:56
LineDelimiter(Sink< Buffer< uint8_t > > &p)
set underlying Sink
Definition line.h:52
bool full() override
check if sink is full
Definition line.h:53
simple example of a pipe that receives bytes as they come in and splits them into \n delimited lines.
Definition line.h:13
LineFilter(Source< Buffer< uint8_t > > &p)
set underlying Source
Definition line.h:35
bool empty() override
check if source is empty
Definition line.h:36
Buffer< uint8_t > pop() override
pull object from source does not check for data. guard by using 'if (!empty) { ......
Definition line.h:44
simple Buffer backed queue implementation
Definition queue.h:12
T pop() override
remove front of queue and return it
Definition queue.h:65
bool empty() override
check if queue is empty
Definition queue.h:83
bool full() override
return true if queue is full
Definition queue.h:87
Copyright (c) 2023 IACE.
dynamically allocated, but fixed-size buffer template
Definition buffer.h:18
Buffer & append(T b)
simple append single item
Definition buffer.h:44
size_t size
total capacity of buffer
Definition buffer.h:23
size_t len
number of items stored in buffer
Definition buffer.h:21
generic object sink, i.e.
Definition streams.h:15
virtual bool full()=0
check if sink is full
void trypush(T &&t)
use this if you don't care if it's going to be successful. data gets discarded if sink is full.
Definition streams.h:28
virtual void push(const T &t)
copy semantics does not check for space. guard by using 'if (!full) { ... }'
Definition streams.h:20
generic object source, i.e.
Definition streams.h:40
virtual T pop()=0
pull object from source does not check for data. guard by using 'if (!empty) { ......
virtual bool empty()=0
check if source is empty