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
25 static Int32 scaled_width(Int32 fix, int scale);
27 long fsgetn(FILE *p, size_t n)
35 v = (v << 8) | fgetbyte(p);
39 Ulong fugetn(FILE *p, size_t n)
45 v = (v << 8) | fgetbyte(p);
49 long msgetn(const Uchar *p, size_t n)
60 Ulong mugetn(const Uchar *p, size_t n)
62 Ulong v = (Ulong)*p++;
69 char *read_string(FILE *in, int s, char *buffer, size_t len)
74 n = fugetn(in, s ? s : 1);
75 if((str = buffer) == NULL || n + 1 > len)
76 str = mdvi_malloc(n + 1);
77 if(fread(str, 1, n, in) != n) {
78 if(str != buffer) mdvi_free(str);
85 size_t read_bcpl(FILE *in, char *buffer, size_t maxlen, size_t wanted)
90 if(maxlen && i > maxlen)
92 if(fread(buffer, i, 1, in) != 1)
100 char *read_alloc_bcpl(FILE *in, size_t maxlen, size_t *size)
105 i = (size_t)fuget1(in);
106 if(maxlen && i > maxlen)
108 buffer = (char *)malloc(i + 1);
111 if(fread(buffer, i, 1, in) != 1) {
120 /* stolen from dvips */
121 static Int32 scaled_width(Int32 fix, int scale)
126 return -scaled_width(-fix, scale);
128 return -scaled_width(fix, -scale);
134 return (((al*bl / 32768) + fix*bl + al*scale) / 32 +
140 void buff_free(Buffer *buf)
143 mdvi_free(buf->data);
147 void buff_init(Buffer *buf)
154 size_t buff_add(Buffer *buf, const char *data, size_t len)
158 if(buf->length + len + 1 > buf->size) {
159 buf->size = buf->length + len + 256;
160 buf->data = mdvi_realloc(buf->data, buf->size);
162 memcpy(buf->data + buf->length, data, len);
167 char *buff_gets(Buffer *buf, size_t *length)
173 ptr = strchr(buf->data, '\n');
176 ptr++; /* include newline */
177 len = ptr - buf->data;
178 ret = mdvi_malloc(len + 1);
180 memcpy(ret, buf->data, len);
181 memmove(buf->data, buf->data + len, buf->length - len);
185 if(length) *length = len;