File: bwrite.c

package info (click to toggle)
epwutil 1.1-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 204 kB
  • ctags: 384
  • sloc: ansic: 3,603; makefile: 48
file content (249 lines) | stat: -rw-r--r-- 4,021 bytes parent folder | download | duplicates (5)
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
243
244
245
246
247
248
249
/*
 * bread.c - Żҥ֥å/EPWING桼ƥƥ ҽ񤭽Ф⥸塼
 *
 *	Written by Junn Ohta (ohta@src.ricoh.co.jp). Public Domain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#ifdef MSDOS
#include <io.h>
#endif
#ifdef UNIX
#include <unistd.h>
#endif
#include "epw.h"

#ifndef O_BINARY
#define	O_BINARY	0
#endif

static	int	wfd = -1;
static	int	werr = 0;
static	long	wblkno;
static	byte	wblkbuf[BLKSIZ];
static	byte	*wp, *wendp = &wblkbuf[BLKSIZ];

int
open_newbook(file)
uchr	*file;
{
    if (wfd != -1)
	close_book();
    if ((wfd = open(file, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, 0644)) < 0)
	return ERR;
    memset(wblkbuf, '\0', BLKSIZ);
    wp = wblkbuf;
    wblkno = 1L;
    werr = 0;
    return OK;
}

int
close_newbook()
{
    if (wfd == -1)
	return ERR;
    if (wp > wblkbuf)
	write_newblock();
    if (close(wfd) < 0)
	return ERR;
    return OK;
}

int
locate_newblock(blk)
long	blk;
{
    if (wfd == -1)
	return ERR;
    if (blk <= 0L)
	return ERR;
    if (wp > wblkbuf)
	write_newblock();
    if (lseek(wfd, (blk - 1L) * BLKSIZ, 0) < 0)
	return ERR;
    wp = wblkbuf;
    wblkno = blk;
    return OK;
}

int
write_newblock()
{
    if (wfd == -1)
	return ERR;
    if (write(wfd, (char *)wblkbuf, BLKSIZ) < BLKSIZ)
	return ERR;
    memset(wblkbuf, '\0', BLKSIZ);
    wp = wblkbuf;
    wblkno++;
    return OK;
}

long
cur_newblock()
{
    return wblkno;
}

int
cur_newoff()
{
    return wp - wblkbuf;
}

int
get_error()
{
    return werr;
}

void
reset_error()
{
    werr = 0;
}

int
putbyte(b)
byte	b;
{
    if (wp >= wendp) {
	if (write_newblock() == ERR)
	    werr++;
    }
    *wp++ = b;
    return OK;
}

int
putword(w)
word	w;
{
    putbyte(w >> 8);
    putbyte(w & 0xff);
    return OK;
}

int
putdword(d)
dword	d;
{
    putbyte(d >> 24);
    putbyte((d >> 16) & 0xff);
    putbyte((d >> 8) & 0xff);
    putbyte(d & 0xff);
    return OK;
}

int
putbcd(d, len)
dword	d;
int	len;
{
    int		i;
    dword	n;
    byte	*p, buf[10];

    p = buf;
    while (len--) {
	*p = (int)(d % 10L);
	d /= 10;
	*p |= (int)(d % 10L) << 4;
	d /= 10;
	p++;
    }
    while (p > buf) {
	putbyte(p[-1]);
	p--;
    }
    return OK;
}

int
putbytes(buf, len)
byte	*buf;
int	len;
{
    byte	*p;

    p = buf;
    while (len--)
	putbyte(*p++);
    return OK;
}

int
putinfo(infop, blk)
INFO_T	*infop;
long	blk;
{
    int		i, j, k;
    ITEM_T	*itemp;
    CINFO_T	*cinfop;
    CENT_T	*centp;
    CITEM_T	*citemp;

    if (locate_newblock(blk) == ERR)
	return ERR;
    putword(infop->items);
    putbytes(infop->resv1, RESV1_LEN);
    putbyte(infop->idxhndl);
    putbytes(infop->resv2, RESV2_LEN);
    itemp = infop->item;
    for (i = 0; i < infop->items; i++) {
	putbyte(itemp->itemid);
	putbytes(itemp->resv3, RESV3_LEN);
	putdword(itemp->topblk);
	putdword(itemp->blks);
	putbyte(itemp->idxvalid);
	putbyte((itemp->idxinfo >> 16) & 0xff);
	putbyte((itemp->idxinfo >> 8) & 0xff);
	putbyte(itemp->idxinfo & 0xff);
	putbytes(itemp->resv4, RESV4_LEN);
	itemp++;
    }
    putbyte(infop->dspvalid);
    putbytes(infop->resv5, RESV5_LEN);
    putbyte(infop->dsplist);
    putbyte(infop->dspstyle);
    putbytes(infop->resv6, RESV6_LEN);
    if (write_newblock() == ERR)
	return ERR;

    itemp = infop->item;
    for (i = 0; i < infop->items; i++) {
	if (itemp->itemid != ID_CINFO) {
	    itemp++;
	    continue;
	}
	if (locate_newblock(itemp->topblk) == ERR)
	    return ERR;
	cinfop = itemp->cinfo;
	putword(cinfop->cents);
	putbytes(cinfop->cresv1, CRESV1_LEN);
	centp = cinfop->cent;
	for (j = 0; j < cinfop->cents; j++) {
	    putbyte(centp->citems);
	    putbytes(centp->cresv2, CRESV2_LEN);
	    putbytes(centp->cname, CNAME_LEN);
	    citemp = centp->citem;
	    for (k = 0; k < centp->citems; k++) {
		putbyte(citemp->citemid);
		putbytes(citemp->cresv3, CRESV3_LEN);
		putdword(citemp->ctopblk);
		putdword(citemp->cblks);
		putbytes(citemp->cresv4, CRESV4_LEN);
		citemp++;
	    }
	    centp++;
	}
	if (write_newblock() == ERR)
	    return ERR;
	itemp++;
    }
    return OK;
}