2
* Copyright (C) 2000, Matias Atria
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
static Int32 scaled_width(Int32 fix, int scale);
26
long fsgetn(FILE *p, size_t n)
34
v = (v << 8) | fgetbyte(p);
38
Ulong fugetn(FILE *p, size_t n)
44
v = (v << 8) | fgetbyte(p);
48
long msgetn(const Uchar *p, size_t n)
59
Ulong mugetn(const Uchar *p, size_t n)
61
Ulong v = (Ulong)*p++;
68
char *read_string(FILE *in, int s, char *buffer, size_t len)
73
n = fugetn(in, s ? s : 1);
74
if((str = buffer) == NULL || n + 1 > len)
75
str = mdvi_malloc(n + 1);
76
if(fread(str, 1, n, in) != n) {
77
if(str != buffer) mdvi_free(str);
84
size_t read_bcpl(FILE *in, char *buffer, size_t maxlen, size_t wanted)
89
if(maxlen && i > maxlen)
91
if(fread(buffer, i, 1, in) != 1)
99
char *read_alloc_bcpl(FILE *in, size_t maxlen, size_t *size)
104
i = (size_t)fuget1(in);
105
if(maxlen && i > maxlen)
107
buffer = (char *)malloc(i + 1);
110
if(fread(buffer, i, 1, in) != 1) {
119
/* stolen from dvips */
120
static Int32 scaled_width(Int32 fix, int scale)
125
return -scaled_width(-fix, scale);
127
return -scaled_width(fix, -scale);
133
return (((al*bl / 32768) + fix*bl + al*scale) / 32 +
139
void buff_free(Buffer *buf)
142
mdvi_free(buf->data);
146
void buff_init(Buffer *buf)
153
size_t buff_add(Buffer *buf, const char *data, size_t len)
157
if(buf->length + len + 1 > buf->size) {
158
buf->size = buf->length + len + 256;
159
buf->data = mdvi_realloc(buf->data, buf->size);
161
memcpy(buf->data + buf->length, data, len);
166
char *buff_gets(Buffer *buf, size_t *length)
172
ptr = strchr(buf->data, '\n');
175
ptr++; /* include newline */
176
len = ptr - buf->data;
177
ret = mdvi_malloc(len + 1);
179
memcpy(ret, buf->data, len);
180
memmove(buf->data, buf->data + len, buf->length - len);
184
if(length) *length = len;