summaryrefslogtreecommitdiff
path: root/support/word2x/tblock.cc
blob: 1ede6d96089003a712f20fb7e7ef1d89f5e5b4f7 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* $Id: tblock.cc,v 1.3 1997/03/21 20:34:11 dps Exp $ */
/* Dynamically growinf text block */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <iostream.h>
#include "tblock.h"

const struct tblock::block tblock::dummy_init={0,0,NULL,NULL};

tblock::~tblock(void)
{
    struct block *blk, *nblk;
    blk=dummy_block.next;
    while (blk!=NULL)
    {
	nblk=blk->next;
	free(blk->text);
	free(blk);
	blk=nblk;
    }
}

void tblock::zero(void)
{		
    struct block *blk, *nblk;
    blk=dummy_block.next;
    while (blk!=NULL)
    {
	nblk=blk->next;
	free(blk->text);
	free(blk);
	blk=nblk;
    }
    dummy_block=dummy_init;
    head=&dummy_block;
}

int tblock::add(char c)
{
    struct block *blk, *nb;
    blk=head;
    if (blk->limit==blk->pos)
    {
	if ((nb=(struct block *) malloc(sizeof(struct block)))==NULL)
	    return 0;
	if ((nb->text=(char *) malloc(block_size))==NULL)
	{
	    free(nb);
	    return 0;
	}
	nb->limit=1024;
	nb->pos=0;
	nb->next=NULL;
	head->next=nb;
	head=nb;
    }
    head->text[(head->pos)++]=c;
    return 1;
}

int tblock::add(const char *s, int len)
{
    struct block *nb;
    const char *t;
    int size;

    if (*s=='\0')
	return 1;

    t=s;

    while (len>0)
    {
	if (head->limit==head->pos)
	{
	    if ((nb=(struct block *) malloc(sizeof(struct block)))==NULL)
		return 0;
	    if ((nb->text=(char *) malloc(block_size))==NULL)
	    {
		free(nb);
		return 0;
	    }
	    nb->limit=block_size;
	    nb->pos=0;
	    nb->next=NULL;
	    head->next=nb;
	    head=nb;
	}
	size=head->limit-head->pos;
	if (size>len)
	    size=len;
	memcpy(head->text+head->pos, t, size);
	head->pos+=size;
	len-=size;
	t+=size;
    }
    return 1;
}

const char *tblock::collect(void) const
{
    struct block *blk;
    char *s,*t;
    size_t size;

    size=0;
    blk=dummy_block.next;
    while (blk!=NULL)
    {
	size+=blk->pos;
	blk=blk->next;
    }
    
    if ((t=(char *) malloc(size+1))==NULL)
    {
        cerr<<"tblock::collect malloc failure\n";
	return NULL;
    }

    s=t; blk=dummy_block.next;
    while (blk!=NULL)
    {
	memcpy(s, blk->text, blk->pos);
	s+=blk->pos;
	blk=blk->next;
    }
    *s='\0';
    return t;
}


tblock &tblock::operator=(const tblock &arg)
{
    struct block *nb,*blk;
    const char *s;
    
    blk=dummy_block.next;
    while (blk!=NULL)
    {
	nb=blk->next;
	free(blk->text);
	free(blk);
	blk=nb;
    }

    if ((s=arg.collect())==NULL)
    {
	cerr<<"tblock:operator= fatal malloc failure\n";
	exit(2);
    }

    if ((nb=(struct block *) malloc(sizeof(struct block)))==NULL)
    {
	cerr<<"tblock:operator= fatal malloc failure\n";
	exit(2);
    }

    nb->limit=strlen(s)+1;	// Limit includes '\0'
    nb->pos=strlen(s);		// Do include '\0' in used characters
    nb->text=(char *) s;
    nb->next=NULL;
    dummy_block.next=nb;
    head=nb;

    return *this;
}

tblock::tblock(const tblock &cpy)
{
    struct block *nb;
    const char *s;
    
    if ((s=cpy.collect())==NULL)
    {
	cerr<<"tblock::tblock(const tblock &) fatal malloc failure\n";
	exit(2);
    }

    if ((nb=(struct block *) malloc(sizeof(struct block)))==NULL)
    {
	cerr<<"tblock::tblock(const tblock &) fatal malloc failure\n";
	exit(2);
    }

    nb->limit=strlen(s)+1;	// Inlcude '\0' in limit
    nb->pos=strlen(s);		// DO not include '\0' in used
    nb->text=(char *) s;
    nb->next=NULL;
    dummy_block.next=nb;
    dummy_block.pos=0;
    dummy_block.limit=0;
    dummy_block.text=NULL;
    head=nb;
}


tblock::operator const char *()
{
    struct block *nb;
    const char *s;
    

    if (dummy_block.next==head)
    {
	*(head->text+head->pos)='\0';
	return ((const char *) (head->text));
    }

    if ((s=this->collect())==NULL)
    {
	cerr<<"tblock::const char * fatal malloc failure\n";
	exit(2);
    }

    if ((nb=(struct block *) malloc(sizeof(struct block)))==NULL)
    {
	cerr<<"tblock::const char * fatal malloc failure\n";
	exit(2);
    }

    nb->limit=strlen(s)+1;	// Inlcude '\0' in limit
    nb->pos=strlen(s);		// DO not include '\0' in used
    nb->text=(char *) s;
    nb->next=NULL;
    this->zero();		// Wipe current data
    dummy_block.next=nb;
    dummy_block.pos=0;
    dummy_block.limit=0;
    dummy_block.text=NULL;
    head=nb;

    return s;
}