Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dvi/mdvi-lib/common.c
blob: 70066828af1167e35506bf1f765c97d0f56eca36 (plain)
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
/*
 * 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 <stdlib.h>
#include <string.h>

#include "common.h"

static Int32	scaled_width(Int32 fix, int scale);

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 = mdvi_malloc(n + 1);
	if(fread(str, 1, n, in) != n) {
		if(str != buffer) mdvi_free(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 */
static 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)
		mdvi_free(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 = mdvi_realloc(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 = mdvi_malloc(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;	
}