-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ring_Buffer.cpp
107 lines (79 loc) · 2.25 KB
/
Ring_Buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*--------------------------------------------------------------------
This file is part of the Arduino M590 library.
The Arduino M590 library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino M590 library is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino M590 library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#include "Ring_Buffer.h"
#include <Arduino.h>
Ring_Buffer::Ring_Buffer(unsigned int size)
{
_size = size;
// add one char to terminate the string
ringBuf = new char[size+1];
ringBufEnd = &ringBuf[size];
init();
}
Ring_Buffer::~Ring_Buffer() {}
void Ring_Buffer::reset()
{
ringBufP = ringBuf;
}
void Ring_Buffer::init()
{
ringBufP = ringBuf;
memset(ringBuf, 0, _size+1);
}
void Ring_Buffer::push(char c)
{
*ringBufP = c;
ringBufP++;
if (ringBufP>=ringBufEnd)
ringBufP = ringBuf;
}
bool Ring_Buffer::endsWith(const char* str)
{
int findStrLen = strlen(str);
// b is the start position into the ring buffer
char* b = ringBufP-findStrLen;
if(b < ringBuf)
b = b + _size;
char *p1 = (char*)&str[0];
char *p2 = p1 + findStrLen;
for(char *p=p1; p<p2; p++)
{
if(*p != *b)
return false;
b++;
if (b == ringBufEnd)
b=ringBuf;
}
return true;
}
void Ring_Buffer::getStr(char * destination, unsigned int skipChars)
{
int len = ringBufP-ringBuf-skipChars;
// copy buffer to destination string
strncpy(destination, ringBuf, len);
// terminate output string
destination[len]=0;
}
void Ring_Buffer::getStrN(char * destination, unsigned int skipChars, unsigned int num)
{
int len = ringBufP-ringBuf-skipChars;
if (len>num)
len=num;
// copy buffer to destination string
strncpy(destination, ringBuf, len);
// terminate output string
destination[len]=0;
}
/* vim: set ft=cpp ai ts=2 sts=2 et sw=2 sta nowrap nu : */