Debian Codegraph @

texk/dvipos/utils.c

raw
1 /* dvipos-20070107
2
3 Copyright (C) 2003 by Jin-Hwan <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 */
19
20 #include "utils.h"
21
22 #include <stdarg.h>
23
24 UNSIGNED_BYTE get_unsigned_byte (FILE *fp)
25 {
26 return (UNSIGNED_BYTE)(fgetc(fp) & 0xFF);
27 }
28
29 SIGNED_BYTE get_signed_byte (FILE *fp)
30 {
31 UNSIGNED_BYTE byte = get_unsigned_byte(fp);
32 return (SIGNED_BYTE)(byte & 0x80 ? byte - 0x100 : byte);
33 }
34
35 UNSIGNED_PAIR get_unsigned_pair (FILE *fp)
36 {
37 UNSIGNED_PAIR pair = get_unsigned_byte(fp);
38 /* Read the second byte */
39 pair = pair << 8; pair += get_unsigned_byte(fp);
40 return pair;
41 }
42
43 SIGNED_PAIR get_signed_pair (FILE *fp)
44 {
45 UNSIGNED_PAIR pair;
46 UNSIGNED_BYTE byte = get_unsigned_byte(fp);
47 pair = (byte & 0x80 ? byte - 0x100 : byte);
48 /* Read the second byte */
49 pair = pair << 8; pair += get_unsigned_byte(fp);
50 return (SIGNED_PAIR)pair;
51 }
52
53 UNSIGNED_TRIPLE get_unsigned_triple (FILE *fp)
54 {
55 UNSIGNED_TRIPLE triple = get_unsigned_byte(fp);
56 /* Read the second, the third byte */
57 triple = triple << 8; triple += get_unsigned_byte(fp);
58 triple = triple << 8; triple += get_unsigned_byte(fp);
59 return triple;
60 }
61
62 UNSIGNED_TRIPLE get_unsigned_triple_kanji (FILE *fp)
63 {
64 /* yy zz XX -> XXyyzz */
65 UNSIGNED_TRIPLE triple = get_unsigned_byte(fp);
66 triple = triple << 8; triple += get_unsigned_byte(fp);
67 triple += get_unsigned_byte(fp) << 16;
68 return triple;
69 }
70
71 SIGNED_TRIPLE get_signed_triple (FILE *fp)
72 {
73 UNSIGNED_TRIPLE triple;
74 /* Read the first byte and check the sign */
75 UNSIGNED_BYTE byte = get_unsigned_byte(fp);
76 triple = (byte & 0x80 ? byte - 0x100 : byte);
77 /* Read the second, the third byte */
78 triple = triple << 8; triple += get_unsigned_byte(fp);
79 triple = triple << 8; triple += get_unsigned_byte(fp);
80 return (SIGNED_TRIPLE)triple;
81 }
82
83 UNSIGNED_QUAD get_unsigned_quad (FILE *fp)
84 {
85 UNSIGNED_QUAD quad = get_unsigned_byte(fp);
86 /* Read the second, the third, and the fourth byte */
87 quad = quad << 8; quad += get_unsigned_byte(fp);
88 quad = quad << 8; quad += get_unsigned_byte(fp);
89 quad = quad << 8; quad += get_unsigned_byte(fp);
90 return quad;
91 }
92
93 SIGNED_QUAD get_signed_quad (FILE *fp)
94 {
95 UNSIGNED_QUAD quad;
96 /* Read the first byte and check the sign */
97 UNSIGNED_BYTE byte = get_unsigned_byte(fp);
98 quad = (byte & 0x80 ? byte - 0x100 : byte);
99 /* Read the second, the third, and the fourth byte */
100 quad = quad << 8; quad += get_unsigned_byte(fp);
101 quad = quad << 8; quad += get_unsigned_byte(fp);
102 quad = quad << 8; quad += get_unsigned_byte(fp);
103 return (SIGNED_QUAD)quad;
104 }
105
106 void put_unsigned_byte (SIGNED_QUAD quad, FILE *fp)
107 {
108 fputc(quad & 0xff, fp);
109 dbg_location++;
110 }
111
112 void put_signed_byte (SIGNED_QUAD quad, FILE *fp)
113 {
114 if (quad < 0) fputc((quad + 0x100) & 0xff, fp);
115 else fputc(quad & 0xff, fp);
116 dbg_location++;
117 }
118
119 void put_unsigned_pair (SIGNED_QUAD quad, FILE *fp)
120 {
121 put_unsigned_byte(quad >> 8, fp);
122 put_unsigned_byte(quad, fp);
123 }
124
125 void put_signed_pair (SIGNED_QUAD quad, FILE *fp)
126 {
127 put_signed_byte(quad >> 8, fp);
128 put_unsigned_byte(quad, fp);
129 }
130
131 void put_unsigned_triple (SIGNED_QUAD quad, FILE *fp)
132 {
133 put_unsigned_byte(quad >> 16, fp);
134 put_unsigned_pair(quad & 0xffff, fp);
135 }
136
137 void put_signed_triple (SIGNED_QUAD quad, FILE *fp)
138 {
139 put_signed_byte(quad >> 16, fp);
140 put_unsigned_pair(quad & 0xffff, fp);
141 }
142
143 void put_signed_quad (SIGNED_QUAD quad, FILE *fp)
144 {
145 put_signed_byte(quad >> 24, fp);
146 put_unsigned_triple(quad & 0xffffff, fp);
147 }
148
149 void sput_signed_pair (char *buf, SIGNED_QUAD quad)
150 {
151 if (quad < 0) *buf++ = (quad >> 8) + 0x100;
152 else *buf++ = quad >> 8;
153 *buf = quad & 0xff;
154 }
155
156 void sput_signed_quad (char *buf, SIGNED_QUAD quad)
157 {
158 if (quad < 0) *buf++ = (quad >> 24) + 0x100;
159 else *buf++ = quad >> 24;
160 *buf++ = quad >> 16;
161 *buf++ = quad >> 8;
162 *buf = quad & 0xff;
163 }
164
165 #ifndef KPATHSEA
166 /* Borrowed from the kpathsea library, concat3.c */
167 char *concat (const char *s1, const char *s2)
168 {
169 unsigned s1len = strlen(s1);
170 unsigned s2len = strlen(s2);
171 char *answer = (char *)xmalloc(s1len + s2len + 1);
172 strcpy(answer, s1);
173 strcat(answer + s1len, s2);
174 return answer;
175 }
176
177 /* Borrowed from the kpathsea library, concat3.c */
178 char *concat3 (const char *s1, const char *s2, const char *s3)
179 {
180 char *answer = (char *)xmalloc(strlen(s1) + strlen(s2) + strlen(s3) + 1);
181 strcpy(answer, s1);
182 strcat(answer, s2);
183 strcat(answer, s3);
184 return answer;
185 }
186
187 /* Borrowed from the kpathsea library, make-suffix.c */
188 /* Return a new string: S suffixed with SUFFIX, regardless of what it
189 * was before. This returns a newly allocated string. */
190 char *make_suffix (const char *s, const char *suffix)
191 {
192 char *new_s;
193 const char *dot_pos = strrchr(s, '.');
194 const char *slash_pos;
195
196 for (slash_pos = s + strlen(s) - 1; slash_pos > dot_pos && slash_pos > s; slash_pos--)
197 if (IS_DIR_SEP(*slash_pos)) break;
198
199 if (dot_pos == NULL || slash_pos > dot_pos)
200 new_s = concat3(s, ".", suffix);
201 else {
202 unsigned past_dot_index = dot_pos + 1 - s;
203 new_s = (char *)xmalloc (past_dot_index + strlen (suffix) + 1);
204 strncpy(new_s, s, dot_pos + 1 - s);
205 strcpy(new_s + past_dot_index, suffix);
206 }
207
208 return new_s;
209 }
210 #endif
211
212 void msg_out (int level, const char *fmt, ...)
213 {
214 va_list args;
215 if (verbose & level) {
216 va_start(args, fmt);
217 vfprintf(stdout, fmt, args);
218 va_end(args);
219 }
220 if (level == M_FAIL)
221 exit(1);
222 }