forked from therion/therion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thbuffer.cxx
140 lines (110 loc) · 2.7 KB
/
thbuffer.cxx
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
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @file thbuffer.cxx
* String buffer class.
*/
/* Copyright (C) 2000 Stacho Mudrak
*
* $Date: $
* $RCSfile: $
* $Revision: $
*
* --------------------------------------------------------------------
* This program 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 2 of the License, or
* any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* --------------------------------------------------------------------
*/
#include "thbuffer.h"
thbuffer::thbuffer()
{
this->size = 16;
this->buff = new char [this->size];
this->buff[0] = 0;
}
thbuffer::~thbuffer()
{
delete [] this->buff;
}
void thbuffer::enlarge(size_t min_size)
{
char * new_buff;
size_t new_size = 4 * this->size;
while(new_size <= min_size)
new_size *= 4;
new_buff = new char [new_size];
memcpy(new_buff, this->buff, this->size);
delete [] this->buff;
this->buff = new_buff;
this->size = new_size;
}
char * thbuffer::strncpy(const char * src, size_t n)
{
if (n >= this->size)
this->enlarge(n);
::strncpy(this->buff, src, n);
this->buff[n] = 0;
return this->buff;
}
char * thbuffer::strcpy(const char * src)
{
size_t srclen = strlen(src);
if (srclen >= this->size)
this->enlarge(srclen);
return ::strcpy(this->buff, src);
}
char * thbuffer::strcat(const char * src)
{
size_t newlen = strlen(src) + strlen(this->buff);
if (newlen >= this->size)
this->enlarge(newlen);
return ::strcat(this->buff, src);
}
char * thbuffer::strncat(const char * src, size_t n)
{
size_t newlen = n + 1 + strlen(this->buff);
if (newlen >= this->size)
this->enlarge(newlen);
return ::strncat(this->buff, src, n);
}
thbuffer::operator char* ()
{
return this->buff;
}
thbuffer::operator char** ()
{
return &(this->buff);
}
char * thbuffer::get_buffer()
{
return this->buff;
}
thbuffer & thbuffer::operator=(const char * src)
{
this->strcpy(src);
return *this;
}
thbuffer & thbuffer::operator=(thbuffer const & srcbf)
{
this->strcpy(srcbf.buff);
return *this;
}
thbuffer & thbuffer::operator+=(const char * src)
{
this->strcat(src);
return *this;
}
void thbuffer::guarantee(size_t bs)
{
if (this->size <= bs)
this->enlarge(bs);
}