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