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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 long fsgetn(FILE *p, size_t n)
33 v = (v << 8) | fgetbyte(p);
37 Ulong fugetn(FILE *p, size_t n)
43 v = (v << 8) | fgetbyte(p);
47 long msgetn(const Uchar *p, size_t n)
58 Ulong mugetn(const Uchar *p, size_t n)
60 Ulong v = (Ulong)*p++;
67 char *read_string(FILE *in, int s, char *buffer, size_t len)
72 n = fugetn(in, s ? s : 1);
73 if((str = buffer) == NULL || n + 1 > len)
74 str = mdvi_malloc(n + 1);
75 if(fread(str, 1, n, in) != n) {
76 if(str != buffer) mdvi_free(str);
83 size_t read_bcpl(FILE *in, char *buffer, size_t maxlen, size_t wanted)
88 if(maxlen && i > maxlen)
90 if(fread(buffer, i, 1, in) != 1)
98 char *read_alloc_bcpl(FILE *in, size_t maxlen, size_t *size)
103 i = (size_t)fuget1(in);
104 if(maxlen && i > maxlen)
106 buffer = (char *)malloc(i + 1);
109 if(fread(buffer, i, 1, in) != 1) {
120 void buff_free(Buffer *buf)
123 mdvi_free(buf->data);
127 void buff_init(Buffer *buf)
134 size_t buff_add(Buffer *buf, const char *data, size_t len)
138 if(buf->length + len + 1 > buf->size) {
139 buf->size = buf->length + len + 256;
140 buf->data = mdvi_realloc(buf->data, buf->size);
142 memcpy(buf->data + buf->length, data, len);
147 char *buff_gets(Buffer *buf, size_t *length)
153 ptr = strchr(buf->data, '\n');
156 ptr++; /* include newline */
157 len = ptr - buf->data;
158 ret = mdvi_malloc(len + 1);
160 memcpy(ret, buf->data, len);
161 memmove(buf->data, buf->data + len, buf->length - len);
165 if(length) *length = len;