My Project
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
growbuf.h
Go to the documentation of this file.
1
#ifndef GROWBUF_H
2
#define GROWBUF_H
3
4
#include <stdlib.h>
5
#include <string.h>
6
7
#define GROW_AMOUNT 1024
8
10
class
GrowBuf
11
{
12
public
:
13
GrowBuf
() :
str
(0),
pos
(0),
len
(0) {}
14
~GrowBuf
() { free(
str
);
str
=0;
pos
=0;
len
=0; }
15
void
clear
() {
pos
=0; }
16
void
addChar
(
char
c) {
if
(
pos
>=
len
) {
len
+=
GROW_AMOUNT
;
str
= (
char
*)realloc(
str
,
len
); }
17
str
[
pos
++]=c;
18
}
19
void
addStr
(
const
char
*s) {
20
if
(s)
21
{
22
int
l=strlen(s);
23
if
(
pos
+l>=
len
) {
len
+=l+
GROW_AMOUNT
;
str
= (
char
*)realloc(
str
,
len
); }
24
strcpy(&
str
[
pos
],s);
25
pos+=l;
26
}
27
}
28
void
addStr
(
const
char
*s,
int
n) {
29
if
(s)
30
{
31
int
l=strlen(s);
32
if
(n<l) l=n;
33
if
(
pos
+l>=
len
) {
len
+=l+
GROW_AMOUNT
;
str
= (
char
*)realloc(
str
,
len
); }
34
strncpy(&
str
[
pos
],s,n);
35
pos+=l;
36
}
37
}
38
const
char
*
get
() {
return
str
; }
39
int
getPos
()
const
{
return
pos
; }
40
char
at
(
int
i)
const
{
return
str
[i]; }
41
private
:
42
char
*
str
;
43
int
pos
;
44
int
len
;
45
};
46
47
#endif