/* * Copyright (C) 2000, Matias Atria * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include "common.h" long fsgetn(FILE *p, size_t n) { long v; v = fgetbyte(p); if(v & 0x80) v -= 0x100; while(--n > 0) v = (v << 8) | fgetbyte(p); return v; } Ulong fugetn(FILE *p, size_t n) { Ulong v; v = fgetbyte(p); while(--n > 0) v = (v << 8) | fgetbyte(p); return v; } long msgetn(const Uchar *p, size_t n) { long v = (long)*p++; if(v & 0x80) v -= 0x100; while(--n > 0) v = (v << 8) | *p++; return v; } Ulong mugetn(const Uchar *p, size_t n) { Ulong v = (Ulong)*p++; while(--n > 0) v = (v << 8) | *p++; return v; } char *read_string(FILE *in, int s, char *buffer, size_t len) { int n; char *str; n = fugetn(in, s ? s : 1); if((str = buffer) == NULL || n + 1 > len) str = xmalloc(n + 1); if(fread(str, 1, n, in) != n) { if(str != buffer) xfree(str); return NULL; } str[n] = 0; return str; } size_t read_bcpl(FILE *in, char *buffer, size_t maxlen, size_t wanted) { size_t i; i = (int)fuget1(in); if(maxlen && i > maxlen) i = maxlen; if(fread(buffer, i, 1, in) != 1) return -1; buffer[i] = '\0'; while(wanted-- > i) (void)fgetc(in); return i; } char *read_alloc_bcpl(FILE *in, size_t maxlen, size_t *size) { size_t i; char *buffer; i = (size_t)fuget1(in); if(maxlen && i > maxlen) i = maxlen; buffer = (char *)malloc(i + 1); if(buffer == NULL) return NULL; if(fread(buffer, i, 1, in) != 1) { free(buffer); return NULL; } buffer[i] = '\0'; if(size) *size = i; return buffer; } /* stolen from dvips */ Int32 scaled_width(Int32 fix, int scale) { Int32 al, bl; if(fix < 0) return -scaled_width(-fix, scale); if(scale < 0) return -scaled_width(fix, -scale); al = fix & 32767; bl = scale & 32767; al >>= 15; bl >>= 15; return (((al*bl / 32768) + fix*bl + al*scale) / 32 + fix * scale / 1024); } /* buffers */ void buff_free(Buffer *buf) { if(buf->data) xfree(buf->data); buff_init(buf); } void buff_init(Buffer *buf) { buf->data = NULL; buf->size = 0; buf->length = 0; } size_t buff_add(Buffer *buf, const char *data, size_t len) { if(!len && data) len = strlen(data); if(buf->length + len + 1 > buf->size) { buf->size = buf->length + len + 256; buf->data = xrealloc(buf->data, buf->size); } memcpy(buf->data + buf->length, data, len); buf->length += len; return buf->length; } char *buff_gets(Buffer *buf, size_t *length) { char *ptr; char *ret; size_t len; ptr = strchr(buf->data, '\n'); if(ptr == NULL) return NULL; ptr++; /* include newline */ len = ptr - buf->data; ret = xmalloc(len + 1); if(len > 0) { memcpy(ret, buf->data, len); memmove(buf->data, buf->data + len, buf->length - len); buf->length -= len; } ret[len] = 0; if(length) *length = len; return ret; }