]> www.fi.muni.cz Git - evince.git/blob - backend/dvi/mdvi-lib/common.c
Include config.h. Bug #504721.
[evince.git] / backend / dvi / mdvi-lib / common.c
1 /*
2  * Copyright (C) 2000, Matias Atria
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "common.h"
24
25 static Int32    scaled_width(Int32 fix, int scale);
26
27 long    fsgetn(FILE *p, size_t n)
28 {
29         long    v;
30         
31         v = fgetbyte(p);        
32         if(v & 0x80)
33                 v -= 0x100;
34         while(--n > 0)
35                 v = (v << 8) | fgetbyte(p);
36         return v;
37 }
38
39 Ulong   fugetn(FILE *p, size_t n)
40 {
41         Ulong   v;
42         
43         v = fgetbyte(p);
44         while(--n > 0)
45                 v = (v << 8) | fgetbyte(p);
46         return v;
47 }
48
49 long    msgetn(const Uchar *p, size_t n)
50 {
51         long    v = (long)*p++;
52         
53         if(v & 0x80)
54                 v -= 0x100;
55         while(--n > 0)
56                 v = (v << 8) | *p++;
57         return v;
58 }
59
60 Ulong   mugetn(const Uchar *p, size_t n)
61 {
62         Ulong   v = (Ulong)*p++;
63
64         while(--n > 0)
65                 v = (v << 8) | *p++;
66         return v;
67 }
68
69 char    *read_string(FILE *in, int s, char *buffer, size_t len)
70 {
71         int     n;
72         char    *str;
73         
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);
79                 return NULL;
80         }
81         str[n] = 0;
82         return str;
83 }
84
85 size_t  read_bcpl(FILE *in, char *buffer, size_t maxlen, size_t wanted)
86 {
87         size_t  i;
88         
89         i = (int)fuget1(in);
90         if(maxlen && i > maxlen)
91                 i = maxlen;
92         if(fread(buffer, i, 1, in) != 1)
93                 return -1;
94         buffer[i] = '\0';
95         while(wanted-- > i)
96                 (void)fgetc(in);
97         return i;
98 }
99
100 char    *read_alloc_bcpl(FILE *in, size_t maxlen, size_t *size)
101 {
102         size_t  i;
103         char    *buffer;
104         
105         i = (size_t)fuget1(in);
106         if(maxlen && i > maxlen)
107                 i = maxlen;
108         buffer = (char *)malloc(i + 1);
109         if(buffer == NULL)
110                 return NULL;
111         if(fread(buffer, i, 1, in) != 1) {
112                 free(buffer);
113                 return NULL;
114         }
115         buffer[i] = '\0';
116         if(size) *size = i;
117         return buffer;
118 }
119
120 /* stolen from dvips */
121 static Int32    scaled_width(Int32 fix, int scale)
122 {
123         Int32   al, bl;
124         
125         if(fix < 0)
126                 return -scaled_width(-fix, scale);
127         if(scale < 0)
128                 return -scaled_width(fix, -scale);
129         al = fix & 32767;
130         bl = scale & 32767;
131         al >>= 15;
132         bl >>= 15;
133         
134         return (((al*bl / 32768) + fix*bl + al*scale) / 32 + 
135                 fix * scale / 1024);
136 }
137
138 /* buffers */
139
140 void    buff_free(Buffer *buf)
141 {
142         if(buf->data)
143                 mdvi_free(buf->data);
144         buff_init(buf); 
145 }
146
147 void    buff_init(Buffer *buf)
148 {
149         buf->data = NULL;
150         buf->size = 0;
151         buf->length = 0;
152 }
153
154 size_t  buff_add(Buffer *buf, const char *data, size_t len)
155 {
156         if(!len && data)
157                 len = strlen(data);
158         if(buf->length + len + 1 > buf->size) {
159                 buf->size = buf->length + len + 256;
160                 buf->data = mdvi_realloc(buf->data, buf->size);
161         }
162         memcpy(buf->data + buf->length, data, len);
163         buf->length += len;
164         return buf->length;
165 }
166
167 char    *buff_gets(Buffer *buf, size_t *length)
168 {
169         char    *ptr;
170         char    *ret;
171         size_t  len;
172         
173         ptr = strchr(buf->data, '\n');
174         if(ptr == NULL)
175                 return NULL;
176         ptr++; /* include newline */
177         len = ptr - buf->data;
178         ret = mdvi_malloc(len + 1);
179         if(len > 0) {
180                 memcpy(ret, buf->data, len);
181                 memmove(buf->data, buf->data + len, buf->length - len);
182                 buf->length -= len;
183         }
184         ret[len] = 0;
185         if(length) *length = len;
186         return ret;     
187 }
188