NetBurner 3.5.8
PDF Version
nb_readline.h
1#ifndef __NB_READLINE_H
2#define __NB_READLINE_H
3/*NB_REVISION*/
4
5/*NB_COPYRIGHT*/
6
7#include <predef.h>
8#include <stdint.h>
9#include <nbrtos.h>
10
11#ifdef __cplusplus
12extern "C"
13#endif
14int readline(int fd, char *buf, uint32_t buflen, uint32_t *rdidx, bool *done, uint32_t timeout);
15
16class FdLineReader {
17public:
18 enum class eLineEnd : uint16_t {
19 CR = 1,
20 LF = 2,
21 CRLF = 3,
22
23 None = 0, // internal tracking only, not allowed to be used as a setting
24 };
25protected:
26 int fd;
27 char *buf;
28 uint16_t buflen;
29 uint16_t rdidx;
30 uint16_t doEcho : 1;
31 uint16_t done : 1;
32 // Stored as raw 2-bit codes (eLineEnd values) rather than the scoped enum
33 // type itself, to avoid GCC's unconditional "bitfield too small to hold all
34 // values of 'enum class'" warning. GetLineEnd()/GetLineEndEcho() expose them
35 // as eLineEnd, so the public interface is unchanged. eLineEnd's underlying
36 // type is uint16_t, so this keeps the struct layout byte-identical.
37 uint16_t lineEnd : 2;
38 uint16_t endState: 2;
39 uint16_t doEndEcho: 2;
40 TickTimeout timeout;
41 uint32_t timeoutTicks; // configured per-ReadLine() timeout in ticks; 0 = wait forever
42
43public:
44 FdLineReader(char *rdbuf, uint16_t rdbuf_len);
45 FdLineReader(char *rdbuf, uint16_t rdbuf_len, int ioFd, uint32_t timeout = 0);
46
47 virtual int ReadLine(bool noblock = false);
48 inline char *GetLine() const { return buf; }
49 inline bool HaveLine() { return done!=0; }
50
51 void SetLineEnd(eLineEnd newEnd);
52 eLineEnd GetLineEnd() { return (eLineEnd)lineEnd; }
53
54 void SetLineEndEcho(eLineEnd newEndEcho);
55 eLineEnd GetLineEndEcho() { return (eLineEnd)doEndEcho; }
56
57 operator char *() const { return GetLine(); }
58 inline operator bool () { return done!=0; }
59
60 friend int readline(int fd, char *buf, uint32_t buflen, uint32_t *rdidx, bool *done, uint32_t timeout);
61};
62
63class FdHistoryReader : public FdLineReader
64{
65protected:
66 char *bufpool;
67 uint8_t bufcount;
68 uint8_t idx_next;
69 uint8_t idx_active;
70 uint8_t total;
71 uint8_t isEscaped;
72
73 void SwapBufs(int dir, char **pNext, char **pEnd);
74
75public:
76 FdHistoryReader(char *rdbuf, uint16_t rdbuf_len, uint8_t rdbuf_count);
77 FdHistoryReader(char *rdbuf, uint16_t rdbuf_len, uint8_t rdbuf_count, int ioFd, uint32_t timeout = 0);
78
79 virtual int ReadLine(bool noblock = false) override;
80};
81
82
83#endif /* ----- #ifndef __NB_READLINE_H ----- */