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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/******************************************************************************
*
* Copyright (C) 1997-2022 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/
#ifndef GROWBUF_H
#define GROWBUF_H
#include <utility>
#include <stdlib.h>
#include <string.h>
#include <string>
#define GROW_AMOUNT 1024*4
/** Class representing a string buffer optimised for growing. */
class GrowBuf
{
public:
GrowBuf() : m_str(0), m_pos(0), m_len(0) {}
GrowBuf(size_t initialSize) : m_pos(0), m_len(initialSize) { m_str=static_cast<char*>(malloc(m_len)); }
~GrowBuf() { free(m_str); }
GrowBuf(const GrowBuf &other)
{
m_len = other.m_len;
m_pos = other.m_pos;
m_str = static_cast<char*>(malloc(m_len));
memcpy(m_str,other.m_str,m_len);
}
GrowBuf &operator=(const GrowBuf &other)
{
if (this!=&other)
{
free(m_str);
m_len = other.m_len;
m_pos = other.m_pos;
m_str = static_cast<char*>(malloc(m_len));
memcpy(m_str,other.m_str,m_len);
}
return *this;
}
GrowBuf(GrowBuf &&other)
: m_str(std::exchange(other.m_str,static_cast<char*>(0)))
, m_pos(std::exchange(other.m_pos,0))
, m_len(std::exchange(other.m_len,0))
{
}
GrowBuf &operator=(GrowBuf &&other)
{
if (this==&other)
return *this;
m_len = std::exchange(other.m_len,0);
m_pos = std::exchange(other.m_pos,0);
m_str = std::exchange(other.m_str,static_cast<char*>(0));
return *this;
}
void reserve(size_t size) { if (m_len<size) { m_len = size; m_str = static_cast<char*>(realloc(m_str,m_len)); } }
void clear() { m_pos=0; }
void addChar(char c) { if (m_pos>=m_len) { m_len+=GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
m_str[m_pos++]=c;
}
void addStr(const QCString &s) {
if (!s.isEmpty())
{
size_t l=s.length();
if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
strcpy(&m_str[m_pos],s.data());
m_pos+=l;
}
}
void addStr(const std::string &s) {
if (!s.empty())
{
size_t l=s.length();
if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
strcpy(&m_str[m_pos],s.c_str());
m_pos+=l;
}
}
void addStr(const char *s) {
if (s)
{
size_t l=strlen(s);
if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
strcpy(&m_str[m_pos],s);
m_pos+=l;
}
}
void addStr(const char *s,size_t n) {
if (s)
{
size_t l=strlen(s);
if (n<l) l=n;
if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = static_cast<char*>(realloc(m_str,m_len)); }
strncpy(&m_str[m_pos],s,n);
m_pos+=l;
}
}
void addInt(const char *fmt,int value) {
char tmp[50];
snprintf(tmp,50,fmt,value);
addStr(tmp);
}
char *get() { return m_str; }
const char *get() const { return m_str; }
size_t getPos() const { return m_pos; }
void setPos(size_t newPos) { m_pos = newPos; }
char at(size_t i) const { return m_str[i]; }
bool empty() const { return m_pos==0; }
private:
char *m_str;
size_t m_pos;
size_t m_len;
};
#endif
|