PIDUINO
Loading...
Searching...
No Matches
linearbuffer.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 * This file is part of the Piduino Library.
3 *
4 * The Piduino Library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * The Piduino Library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <cstring>
20#include <ctsdio>
21#include <stdio.h>
22
23#ifndef BUFSIZ
24#define BUFSIZ 8192
25#endif
26
33namespace Piduino {
35
36 public:
37 //--------------------------------------------------------------------
38 LinearBuffer (size_t s = BUFSIZ) : _buffersize (s), _capacity (0), _pwrite (0), _pread (0), _buf (0) {}
39 //--------------------------------------------------------------------
41 delete [] _buf;
42 }
43 //--------------------------------------------------------------------
44 void clear() {
45 _pread = _pwrite = 0;
46 }
47 //--------------------------------------------------------------------
48 int size() const {
49 return (_pwrite - _pread);
50 }
51 //--------------------------------------------------------------------
52 int bufferSize() const {
53 return _buffersize;
54 }
55 //--------------------------------------------------------------------
56 bool empty() const {
57 return size() == 0;
58 }
59 //--------------------------------------------------------------------
60 int getChar() {
61 if (size() == 0) {
62 return -1;
63 }
64 int i = static_cast<unsigned char> (*_pread);
65 _pread++;
66 return i;
67 }
68 //--------------------------------------------------------------------
69 int peek (char * target, int maxlen) {
70 int r = std::min (maxlen, size());
71 ::memcpy (target, _pread, r);
72 return r;
73 }
74 //--------------------------------------------------------------------
75 int read (char * target, int maxlen) {
76 int r = peek (target, maxlen);
77 _pread += r;
78 return r;
79 }
80 //--------------------------------------------------------------------
81 std::vector<char> readAll() {
82 std::vector<char> result;
83
84 result.resize (size());
85 result.assign (_pread, _pread + size());
86 clear();
87 return result;
88 }
89 //--------------------------------------------------------------------
90 bool canReadLine() const {
91 return memchr (_pread, '\n', size()) != nullptr;
92 }
93 //--------------------------------------------------------------------
94 int readLine (char * target, int maxlen) {
95 int r = std::min (maxlen, size());
96 char * eol = static_cast<char *> (memchr (_pread, '\n', r));
97 if (eol) {
98 r = (eol - _pread) + 1;
99 }
100 return read (target, r);
101 }
102 //--------------------------------------------------------------------
103 void skip (int n) {
104 if (n >= size()) {
105 clear();
106 }
107 else {
108 _pread += n;
109 }
110 }
111 //--------------------------------------------------------------------
112 void chop (int n) {
113 if (n >= size()) {
114 clear();
115 }
116 else {
117 _pwrite -= n;
118 }
119 }
120 //--------------------------------------------------------------------
121 void ungetChar (char c) {
122 if (_pread == _buf) {
123 resize (size() + 1);
124 }
125 _pread--;
126 *_pread = c;
127 }
128 //--------------------------------------------------------------------
129 void ungetBlock (const char * block, int len) {
130 if ( (_pread - _buf) < len) {
131 resize (size() + len);
132 }
133 _pread -= len;
134 memcpy (_pread, block, len);
135 }
136 //--------------------------------------------------------------------
137 /* Ajoute un bloc de len bytes à la fin du buffer
138 * déplace le pointeur d'écriture après ce bloc
139 * retourne le pointeur d'écriture avant ajout
140 */
141 char * reserve (int len) {
142 char * p;
143
144 resize (len + size(), true);
145 p = _pwrite;
146 _pwrite += len;
147 return p;
148 }
149
150 protected:
151 //--------------------------------------------------------------------
152 void resize (size_t required, bool atEnd = false) {
153 size_t newsize = std::max (_capacity, _buffersize);
154
155 while (newsize < required) {
156 newsize *= 2;
157 }
158
159 int offset = atEnd ? 0 : newsize - size();
160 if (newsize > _capacity) {
161 char * newbuf = new char[newsize];
162 memmove (newbuf + offset, _pread, size());
163 delete [] _buf;
164 _buf = newbuf;
165 _capacity = newsize;
166 }
167 else {
168 memmove (_buf + offset, _pread, size());
169 }
170 _pread = _buf + offset;
171 }
172 private:
174 size_t _capacity;
175 char * _pread;
176 char * _pwrite;
177 char * _buf;
178 };
179}
184/* ========================================================================== */
std::vector< char > readAll()
int readLine(char *target, int maxlen)
char * reserve(int len)
void ungetBlock(const char *block, int len)
bool canReadLine() const
void resize(size_t required, bool atEnd=false)
LinearBuffer(size_t s=BUFSIZ)
int peek(char *target, int maxlen)
int read(char *target, int maxlen)
Global namespace for Piduino.
Definition board.h:28