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
167
168
|
#include "config.h"
#include "buffer.h"
#include "funcs.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#define CHUNK 512
void Buffer::append(int c)
{
int newsize=bufsize+CHUNK;
unsigned char *newbuf=new unsigned char[newsize];
if (!newbuf) outofmem();
if (bufsize) memcpy(newbuf, buf, bufsize);
if (buf) delete[] buf;
buf=newbuf;
bufsize = newsize;
buf[buflength++]=c;
}
void Buffer::set(const char *p)
{
int l=strlen(p);
if (bufsize < l)
{
int newsize=l + CHUNK-1;
newsize -= (newsize % CHUNK);
unsigned char *newbuf=new unsigned char[newsize];
if (!newbuf) outofmem();
if (buf) delete[] buf;
buf=newbuf;
bufsize=newsize;
}
memcpy(buf, p, l);
buflength=l;
}
int Buffer::operator-(const Buffer &o) const
{
int i;
for (i=0; i<buflength && i < o.buflength; i++)
{
if (buf[i] < o.buf[i]) return (-1);
if (buf[i] > o.buf[i]) return (1);
}
return (buflength < o.buflength ? -1:
buflength > o.buflength ? 1:0);
}
int Buffer::operator-(const char *o) const
{
int i;
for (i=0; i<buflength && o[i]; i++)
{
if (buf[i] < o[i]) return (-1);
if (buf[i] > o[i]) return (1);
}
return (i < buflength ? 1: o[i] ? -1:0);
}
Buffer &Buffer::operator=(const Buffer &o)
{
if (bufsize < o.buflength)
{
int newsize=(o.buflength + CHUNK-1);
newsize -= (newsize % CHUNK);
unsigned char *newbuf=new unsigned char[newsize];
if (!newbuf) outofmem();
if (buf) delete[] buf;
buf=newbuf;
bufsize=newsize;
}
if (o.buflength) memcpy(buf, o.buf, o.buflength);
buflength=o.buflength;
return (*this);
}
void Buffer::append(const void *p, int l)
{
if (bufsize - buflength < l)
{
int newsize=(buflength+l + CHUNK-1);
newsize -= (newsize % CHUNK);
unsigned char *newbuf=new unsigned char[newsize];
if (!newbuf) outofmem();
if (buf)
{
memcpy(newbuf, buf, buflength);
delete[] buf;
}
buf=newbuf;
bufsize=newsize;
}
if (l) memcpy(buf+buflength, p, l);
buflength += l;
}
void Buffer::append(unsigned long n)
{
char tbuf[40];
char *p=tbuf+sizeof(tbuf)-1;
*p=0;
do
{
*--p= (n % 10) + '0';
} while ( (n /= 10) != 0);
append(p);
}
void Buffer::append(double d)
{
char tbuf[MAXLONGSIZE < 40 ? 40:MAXLONGSIZE+4];
sprintf(tbuf, "%1.*g", MAXLONGSIZE, d);
operator += (tbuf);
}
int Buffer::Int(const char *def) const
{
const unsigned char *p=buf;
int l=buflength;
int minus=0;
unsigned num=0;
while (l && isspace(*p))
{
--l;
++p;
}
if ((!l || (*p != '-' && (*p < '0' || *p > '9'))) && def != 0)
{
p= (const unsigned char *)def;
l=strlen(def);
}
if (l && *p == '-')
{
minus=1;
--l;
++p;
}
while (l)
{
if (*p < '0' || *p > '9') break;
num = num * 10 + (*p-'0');
++p;
--l;
}
if (minus) num= -num;
return ( (int)num );
}
|