amath  1.6.2
Simple command line calculator
charbuf.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2017 Carsten Sonne Larsen <cs@innolan.dk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #include "clib.h"
28 #include "lib/charbuf.h"
29 
30 // -----------------------------------------------------
31 // -------------------- CharBuffer ---------------------
32 // -----------------------------------------------------
33 
34 /**
35  * @brief Initialize without allocating memory.
36  *
37  */
39 {
40  buf = NOMEM;
41  ptr = buf;
42  cursize = 0;
43 }
44 
45 /**
46  * @brief Initialize while allocating specified amount of memory.
47  *
48  */
49 CharBuffer::CharBuffer(unsigned int size)
50 {
51  cursize = (size < minimumSize ? minimumSize : size);
52  buf = new char[cursize];
53  ptr = buf;
54 }
55 
57 {
59 }
60 
61 /**
62  * @brief Release memory in buffer.
63  *
64  */
66 {
67  if (buf != NOMEM) {
68  delete [] buf;
69  }
70 
71  buf = NOMEM;
72  ptr = buf;
73  cursize = 0;
74 }
75 
76 /**
77  * @brief Release memory, allocate and copy source.
78  *
79  */
80 void CharBuffer::ClearAndCopy(const char* source)
81 {
83  cursize = AllocAndCopy(&buf, source);
84  ptr = buf + cursize - sizeof(char);
85 }
86 
87 /**
88  * @brief Release memory and allocate new size.
89  *
90  */
91 void CharBuffer::ClearAndAlloc(unsigned int size)
92 {
94  cursize = (size < minimumSize ? minimumSize : size);
95  buf = new char[cursize];
96  ptr = buf;
97 }
98 
100 {
101  if (buf == NOMEM) {
102  unsigned int size = minimumSize;
103  buf = new char[size];
104  ptr = buf;
105  }
106 }
107 
108 /**
109  * @brief Ensure a memory block of speficied size is allocated.
110  *
111  */
112 void CharBuffer::EnsureSize(unsigned int size)
113 {
114  if (cursize < size) {
115 
116  unsigned int tempsize = cursize;
117  cursize = (size < minimumSize ? minimumSize : size);
118 
119  if (buf == NOMEM) { // Nothing allocated yet. Just allocate requested size.
120  buf = new char[cursize];
121  ptr = buf;
122  } else if (buf == ptr) { // Already allocated but buffer is empty.
123  delete [] buf;
124  buf = new char[cursize];
125  ptr = buf;
126  } else { // Buffer already in use.
127  // Make at least double size
128  cursize = cursize < tempsize * 2 ? tempsize * 2 : cursize;
129  unsigned int offset = ptr - buf;
130  char *temp = new char[cursize];
131  MemCopy(temp, buf, tempsize);
132  delete [] buf;
133  buf = temp;
134  ptr = buf + offset;
135  }
136  }
137 }
138 
139 void CharBuffer::EnsureSize(unsigned int blocksize, unsigned int blocks)
140 {
141  if (cursize < blocksize * blocks) {
142  if (buf == NOMEM) {
143  cursize = blocksize * blocks;
144  buf = new char[cursize];
145  ptr = buf;
146  } else {
147  unsigned int tptr = ptr - buf;
148  char *temp = new char[blocksize * blocks];
149  MemCopy(temp, buf, cursize);
150  delete [] buf;
151  cursize = blocksize * blocks;
152  buf = temp;
153  ptr = buf + tptr;
154  }
155  }
156 }
157 
158 void CharBuffer::EnsureGrowth(unsigned int size)
159 {
160  EnsureSize((ptr - buf) + size);
161 }
162 
163 bool CharBuffer::Is(const char* string)
164 {
165  return StrIsEqual(GetString(), string);
166 }
167 
169 {
170  if (buf == NOMEM) {
172  }
173 
174  ptr = buf;
175 }
176 
178 {
179  ptr--;
180 
181 }
182 
183 void CharBuffer::Copy(CharBuffer* source)
184 {
185  EnsureSize(source->cursize);
186  const char *s = source->GetString();
187 
188  ptr = buf;
189  while ((*ptr++ = *s++))
190  ;
191 
192  ptr--;
193 }
194 
195 void CharBuffer::Append(const char c)
196 {
197  *ptr++ = c;
198 }
199 
200 void CharBuffer::Append(const char c, unsigned int count)
201 {
202  if (count == 0) {
203  return;
204  }
205 
206  unsigned int n = count;
207  while (n--)
208  *ptr++ = c;
209 }
210 
211 void CharBuffer::Append(const char* source)
212 {
213  while ((*ptr++ = *source++));
214 
215  ptr--;
216 }
217 
218 bool CharBuffer::RemoveTrailing(const char c)
219 {
220  if (ptr == buf) {
221  return false;
222  }
223 
224  if (*(--ptr) == c) {
225  return true;
226  }
227 
228  ptr++;
229  return false;
230 }
231 
232 bool CharBuffer::RemoveTrailing(const char *string)
233 {
234  int len = StrLen(string) * sizeof(char);
235  char* s = ptr - len;
236  if (s < buf) {
237  return false;
238  }
239 
240  *ptr = '\0';
241 
242  if (StrIsEqual(s, string)) {
243  ptr = s;
244  return true;
245  }
246 
247  return false;
248 }
249 
251 {
252  *ptr = '\0';
253  return buf;
254 }
void Append(const char c)
Definition: charbuf.cpp:195
bool Is(const char *string)
Compare content of CharBuffer with string)
Definition: charbuf.cpp:163
void Empty()
Definition: charbuf.cpp:168
bool RemoveTrailing(const char c)
Definition: charbuf.cpp:218
CharBuffer(unsigned int size)
Initialize while allocating specified amount of memory.
Definition: charbuf.cpp:49
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
bool RemoveTrailing(const char *string)
Definition: charbuf.cpp:232
void Append(const char *source)
Definition: charbuf.cpp:211
CharBuffer()
Initialize without allocating memory.
Definition: charbuf.cpp:38
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:49
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:34
#define NOMEM
Definition: platform.h:43
char * GetString()
Definition: charbuf.cpp:250
void EnsureMinimumSize()
Definition: charbuf.cpp:99
unsigned int cursize
Definition: charbuf.h:78
void EnsureGrowth(unsigned int size)
Definition: charbuf.cpp:158
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:31
void DeleteLastChar()
Definition: charbuf.cpp:177
void Copy(CharBuffer *buf)
Definition: charbuf.cpp:183
char * ptr
Definition: charbuf.h:77
char * buf
Definition: charbuf.h:76
void ClearAndCopy(const char *source)
Release memory, allocate and copy source.
Definition: charbuf.cpp:80
void EnsureSize(unsigned int blocksize, unsigned int blocks)
Definition: charbuf.cpp:139
void Append(const char c, unsigned int count)
Definition: charbuf.cpp:200
static const unsigned int minimumSize
Definition: charbuf.h:79
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:43
void EnsureSize(unsigned int size)
Ensure a memory block of speficied size is allocated.
Definition: charbuf.cpp:112
void MemCopy(void *destination, const void *source, unsigned int length)
Copy a block of memory, handling overlap.
Definition: memcpy.c:71
void ClearAndAlloc(unsigned int size)
Release memory and allocate new size.
Definition: charbuf.cpp:91
~CharBuffer()
Definition: charbuf.cpp:56