Doxygen
Loading...
Searching...
No Matches
growbuf.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2022 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#ifndef GROWBUF_H
17#define GROWBUF_H
18
19#include <utility>
20#include <stdlib.h>
21#include <string.h>
22#include <string>
23
24#define GROW_AMOUNT 1024*4
25
26/** Class representing a string buffer optimized for growing. */
28{
29 public:
30 GrowBuf() : m_str(nullptr), m_pos(0), m_len(0) {}
31 GrowBuf(size_t initialSize) : m_pos(0), m_len(initialSize) { m_str=static_cast<char*>(malloc(m_len)); }
32 ~GrowBuf() { free(m_str); }
33 GrowBuf(const GrowBuf &other)
34 {
35 m_len = other.m_len;
36 m_pos = other.m_pos;
37 m_str = static_cast<char*>(malloc(m_len));
38 memcpy(m_str,other.m_str,m_len);
39 }
40 GrowBuf &operator=(const GrowBuf &other)
41 {
42 if (this!=&other)
43 {
44 free(m_str);
45 m_len = other.m_len;
46 m_pos = other.m_pos;
47 m_str = static_cast<char*>(malloc(m_len));
48 memcpy(m_str,other.m_str,m_len);
49 }
50 return *this;
51 }
53 : m_str(std::exchange(other.m_str,static_cast<char*>(nullptr)))
54 , m_pos(std::exchange(other.m_pos,0))
55 , m_len(std::exchange(other.m_len,0))
56 {
57 }
59 {
60 if (this==&other)
61 return *this;
62 m_len = std::exchange(other.m_len,0);
63 m_pos = std::exchange(other.m_pos,0);
64 m_str = std::exchange(other.m_str,static_cast<char*>(nullptr));
65 return *this;
66 }
67 void reserve(size_t size) { if (m_len<size) { m_len = size; m_str = static_cast<char*>(realloc(m_str,m_len)); } }
68 void clear() { m_pos=0; }
69 void addChar(char c) { if (m_pos>=m_len) { m_len+=GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
70 m_str[m_pos++]=c;
71 }
72 void addStr(const QCString &s) {
73 if (!s.isEmpty())
74 {
75 size_t l=s.length();
76 if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
77 strcpy(&m_str[m_pos],s.data());
78 m_pos+=l;
79 }
80 }
81 void addStr(const std::string &s) {
82 if (!s.empty())
83 {
84 size_t l=s.length();
85 if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
86 strcpy(&m_str[m_pos],s.c_str());
87 m_pos+=l;
88 }
89 }
90 void addStr(const char *s) {
91 if (s)
92 {
93 size_t l=strlen(s);
94 if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
95 strcpy(&m_str[m_pos],s);
96 m_pos+=l;
97 }
98 }
99 void addStr(const char *s,size_t n) {
100 if (s)
101 {
102 size_t l=strlen(s);
103 if (n<l) l=n;
104 if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
105 strncpy(&m_str[m_pos],s,n);
106 m_pos+=l;
107 }
108 }
109 void addInt(const char *fmt,int value) {
110 char tmp[50];
111 snprintf(tmp,50,fmt,value);
112 addStr(tmp);
113 }
114 char *get() { return m_str; }
115 const char *get() const { return m_str; }
116 size_t getPos() const { return m_pos; }
117 void setPos(size_t newPos) { m_pos = newPos; }
118 const char &at(size_t i) const { return m_str[i]; }
119 bool empty() const { return m_pos==0; }
120 private:
121 char *m_str;
122 size_t m_pos;
123 size_t m_len;
124};
125
126#endif
size_t getPos() const
Definition growbuf.h:116
size_t m_pos
Definition growbuf.h:122
GrowBuf & operator=(const GrowBuf &other)
Definition growbuf.h:40
void addInt(const char *fmt, int value)
Definition growbuf.h:109
void addChar(char c)
Definition growbuf.h:69
GrowBuf(GrowBuf &&other)
Definition growbuf.h:52
void addStr(const QCString &s)
Definition growbuf.h:72
const char & at(size_t i) const
Definition growbuf.h:118
GrowBuf()
Definition growbuf.h:30
void clear()
Definition growbuf.h:68
const char * get() const
Definition growbuf.h:115
char * get()
Definition growbuf.h:114
char * m_str
Definition growbuf.h:121
void setPos(size_t newPos)
Definition growbuf.h:117
void reserve(size_t size)
Definition growbuf.h:67
bool empty() const
Definition growbuf.h:119
GrowBuf(const GrowBuf &other)
Definition growbuf.h:33
void addStr(const char *s)
Definition growbuf.h:90
GrowBuf & operator=(GrowBuf &&other)
Definition growbuf.h:58
size_t m_len
Definition growbuf.h:123
void addStr(const char *s, size_t n)
Definition growbuf.h:99
GrowBuf(size_t initialSize)
Definition growbuf.h:31
~GrowBuf()
Definition growbuf.h:32
void addStr(const std::string &s)
Definition growbuf.h:81
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:153
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:150
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:159
#define GROW_AMOUNT
Definition growbuf.h:24