amath  1.8.5
Simple command line calculator
charbuf.h
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
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  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #ifndef AMATH_CHAR_BUFFER_H
31 #define AMATH_CHAR_BUFFER_H
32 
33 /**
34  * @file charbuf.h
35  * @brief String and character array logic
36  */
37 
38 #include "aengine.h"
39 
40 /**
41  * @brief Encapsulate an character array which can be used as a string
42  * @details The CharBuffer class eases the task of allocating a releasing memory.
43  */
45 {
46 public:
47  CharBuffer();
48  explicit CharBuffer(unsigned int size);
49  ~CharBuffer();
50  void ClearBuffer();
51  void ClearAndCopy(const char* source);
52  void ClearAndAlloc(unsigned int size);
53  void EnsureSize(unsigned int size);
54  void EnsureSize(unsigned int blocksize, unsigned int blocks);
55  void EnsureMinimumSize();
56  void EnsureGrowth(unsigned int size);
57 
58  void Empty();
59 
60  bool IsEmpty() const;
61 
62  /**
63  * @brief Compare content of CharBuffer with string)
64  */
65  bool Is(const char* string) const;
66  bool Contains(const char c) const;
67 
68  void Copy(CharBuffer* buf);
69  void Append(const char* source);
70  void Append(const char c);
71  void Append(const char c, unsigned int count);
72 
73  void DeleteLastChar();
74  bool RemoveTrailing(const char c);
75  bool RemoveTrailing(const char* string);
76  char* GetString() const;
77 
78 private:
79  friend class AnsiConoleEngine;
80 
81  char* buf;
82  char* ptr;
83  unsigned int cursize;
84  static const unsigned int minimumSize = 64;
85 };
86 
87 #endif
void Append(const char c)
Definition: charbuf.cpp:245
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
bool RemoveTrailing(const char c)
Definition: charbuf.cpp:270
CharBuffer(unsigned int size)
Initialize while allocating specified amount of memory.
Definition: charbuf.cpp:49
bool Is(const char *string) const
Compare content of CharBuffer with string)
Definition: charbuf.cpp:194
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
bool RemoveTrailing(const char *string)
Definition: charbuf.cpp:286
void Append(const char *source)
Definition: charbuf.cpp:262
CharBuffer()
Initialize without allocating memory.
Definition: charbuf.cpp:38
ANSI console controller.
Definition: aengine.h:47
void EnsureMinimumSize()
Definition: charbuf.cpp:100
unsigned int cursize
Definition: charbuf.h:83
void EnsureGrowth(unsigned int size)
Definition: charbuf.cpp:169
void DeleteLastChar()
Definition: charbuf.cpp:228
void Copy(CharBuffer *buf)
Definition: charbuf.cpp:233
bool Contains(const char c) const
Definition: charbuf.cpp:199
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
bool IsEmpty() const
Definition: charbuf.cpp:174
void ClearAndCopy(const char *source)
Release memory, allocate and copy source.
Definition: charbuf.cpp:81
void EnsureSize(unsigned int blocksize, unsigned int blocks)
Definition: charbuf.cpp:146
void Append(const char c, unsigned int count)
Definition: charbuf.cpp:250
static const unsigned int minimumSize
Definition: charbuf.h:84
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
void ClearAndAlloc(unsigned int size)
Release memory and allocate new size.
Definition: charbuf.cpp:92
~CharBuffer()
Definition: charbuf.cpp:56