blob: c1eb2a9d5eb36f491765ed9866990f29fe9b6885 (
plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/*
This class implements a dynamic string buffer
Copyright (C) 1998 Martin Vogt
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation.
For more information look at the file COPYRIGHT in this package
*/
#include "dynBuffer.h"
#include <iostream>
using namespace std;
DynBuffer::DynBuffer(int size) {
nSize=size;
msg=(char*) malloc(sizeof(char)*(nSize+1));
msg[nSize]='\0';
clear();
}
DynBuffer::~DynBuffer() {
free (msg);
}
void DynBuffer::clear() {
msg[0]='\0';
}
void DynBuffer::append(int value) {
DynBuffer buf(30);
sprintf(buf.getData(),"%d",value);
append(buf.getData());
}
void DynBuffer::append(char* appendMsg) {
if (appendMsg == msg) {
cout << "cannot append to self"<<endl;
exit(0);
}
char* appendPos=getAppendPos();
int nlen=strlen(appendMsg);
if (appendPos == NULL) return;
append(appendMsg,nlen);
}
void DynBuffer::append(const char* appendMsg) {
append((char*)appendMsg);
}
void DynBuffer::append(char* buffer,int buflen) {
int nlen=len();
int nBedarf;
if (buffer == msg) {
cout << "cannot append to self"<<endl;
exit(0);
}
if (buflen+nlen <= nSize) {
char* appendPos=getAppendPos();
strncpy(appendPos,buffer,buflen);
appendPos[buflen]='\0';
return;
}
nBedarf=(nlen+buflen)-nSize;
grow(nBedarf);
append(buffer,buflen);
}
char* DynBuffer::getAppendPos() {
int i;
// this Array has nSize+1 entries!
// and it *is* granted that msg[nSize]=0; (think so)
for (i=0;i<=nSize;i++) {
if (msg[i] == '\0') return &(msg[i]);
}
// should never reach this point
return NULL;
}
void DynBuffer::setData(char* msg) {
if (strlen(msg) == 0) {
clear();
return;
}
clear();
append(msg);
}
char* DynBuffer::getData() {
return msg;
}
int DynBuffer::len() {
return strlen(msg);
}
int DynBuffer::getSize() {
return nSize;
}
void DynBuffer::grow(int size) {
int i;
int newSize=nSize+size;
char* tmp=(char*) malloc(sizeof(char)*(newSize+1));
tmp[newSize]='\0';
for(i=0;i<=nSize;i++) {
tmp[i]=msg[i];
}
nSize=newSize;
free(msg);
msg=tmp;
}
int DynBuffer::tqfind(char zeichen) {
int i;
int nlen=len();
for(i=0;i<nlen;i++) {
if (msg[i] == zeichen) return i;
}
return -1;
}
void DynBuffer::forward(int bytes) {
int i;
int aktPos;
int nlen=len();
if (bytes > nlen) {
bytes=nlen;
}
i=0;
aktPos=bytes;
while(aktPos <= nlen) {
msg[i]=msg[aktPos];
i++;
aktPos++;
}
}
void DynBuffer::print() {
printf("DynBuffer:%s\n",msg);
}
|