source: trunk/indexers/mg/src/images/utils.c@ 3745

Last change on this file since 3745 was 3745, checked in by mdewsnip, 21 years ago

Addition of MG package for search and retrieval

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1/**************************************************************************
2 *
3 * utils.c -- Functions which are common utilities for the image programs
4 * Copyright (C) 1994 Stuart Inglis
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id: utils.c 3745 2003-02-20 21:20:24Z mdewsnip $
21 *
22 **************************************************************************/
23
24#include "sysfuncs.h"
25
26#include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
27
28#include "utils.h"
29
30
31int V = 0;
32
33
34int
35isEOF (FILE * fp)
36{
37 int ch;
38
39 ch = getc (fp);
40 ungetc (ch, fp);
41 if (ch == EOF)
42 return 1;
43 return 0;
44}
45
46
47int
48getmagicno_short (FILE * fp)
49{
50 int ch1, ch2;
51 ch1 = getc (fp);
52 if (ch1 == EOF)
53 {
54 ungetc (ch1, fp);
55 return 0;
56 }
57 ch2 = getc (fp);
58 if (ch2 == EOF)
59 {
60 ungetc (ch2, fp);
61 return 0;
62 }
63 ungetc (ch2, fp);
64 ungetc (ch1, fp);
65
66 return ((ch1 << 8) | ch2);
67}
68
69int
70getmagicno_short_pop (FILE * fp)
71{
72 int ch1, ch2;
73 ch1 = getc (fp);
74 if (ch1 == EOF)
75 return 0;
76 ch2 = getc (fp);
77 if (ch2 == EOF)
78 return 0;
79
80 return ((ch1 << 8) | ch2);
81}
82
83int
84getmagicno_byte (FILE * fp)
85{
86 int ch1;
87 ch1 = getc (fp);
88 if (ch1 == EOF)
89 {
90 ungetc (ch1, fp);
91 return 0;
92 }
93 ungetc (ch1, fp);
94 return (ch1);
95}
96
97
98int
99getmagicno_long (FILE * fp)
100{
101 int ch1, ch2, ch3, ch4;
102 ch1 = getc (fp);
103 if (ch1 == EOF)
104 {
105 ungetc (ch1, fp);
106 return 0;
107 }
108 ch2 = getc (fp);
109 if (ch2 == EOF)
110 {
111 ungetc (ch2, fp);
112 return 0;
113 }
114 ch3 = getc (fp);
115 if (ch3 == EOF)
116 {
117 ungetc (ch3, fp);
118 return 0;
119 }
120 ch4 = getc (fp);
121 if (ch4 == EOF)
122 {
123 ungetc (ch4, fp);
124 return 0;
125 }
126 ungetc (ch4, fp);
127 ungetc (ch3, fp);
128 ungetc (ch2, fp);
129 ungetc (ch1, fp);
130
131 return ((ch1 << 24) | (ch2 << 16) | (ch3 << 8) | (ch4));
132}
133
134
135
136
137void
138error_msg (char *prog, char *message, char *extra)
139{
140 fprintf (stderr, "%s: %s %s\n", prog, message, extra);
141 exit (1);
142}
143
144void
145warn (char *prog, char *message, char *extra)
146{
147 fprintf (stderr, "%s: %s %s\n", prog, message, extra);
148}
149
150
151
152void
153readline (char str[], FILE * fp)
154{
155 int i = 0, ch;
156
157 while (((ch = fgetc (fp)) != '\n') && (!feof (fp)))
158 str[i++] = ch;
159 str[i] = '\0';
160}
161
162
163
164
165
166
167
168int
169getint (FILE * fp)
170{
171 register char ch;
172 register unsigned int i = 0;
173
174 do
175 {
176 ch = getc (fp);
177 if (feof (fp))
178 return EOF;
179 }
180 while (ch < '0' || ch > '9');
181
182 do
183 {
184 i = i * 10 + ch - '0';
185 ch = getc (fp);
186 if (feof (fp))
187 return EOF;
188 }
189 while (ch >= '0' && ch <= '9');
190
191 return i;
192}
193
194/* get_header_int i.e. get an integer from a PNM header ; basically
195 the same as
196 above except for comment checking ; above kept because of lower
197 overhead */
198
199unsigned int
200gethint (FILE * fp)
201{
202 register char ch, prev = '\0';
203 register unsigned int i = 0;
204
205 do
206 {
207 ch = getc (fp);
208 if ((ch == '#') && (prev == '\n'))
209 while (((ch = getc (fp)) != '\n') && !(feof (fp)));
210 prev = ch;
211 if (feof (fp))
212 return EOF;
213 }
214 while (ch < '0' || ch > '9');
215
216 do
217 {
218 i = i * 10 + ch - '0';
219 ch = getc (fp);
220 if (feof (fp))
221 return EOF;
222 }
223 while (ch >= '0' && ch <= '9');
224
225 return i;
226}
227
228
229
230
231void
232magic_write (FILE * fp, u_long magic_num)
233{
234 HTONUL(magic_num); /* [RPAP - Jan 97: Endian Ordering] */
235 if (fwrite (&magic_num, sizeof (magic_num), 1, fp) != 1)
236 error_msg ("magic num", "Couldn't write magic number.", "");
237}
238
239
240void
241magic_check (FILE * fp, u_long magic_num)
242{
243 u_long magic;
244 if (fread (&magic, sizeof (magic), 1, fp) != 1 || NTOHUL(magic) != magic_num) /* [RPAP - Jan 97: Endian Ordering] */
245 error_msg ("magic num", "Incorrect magic number.", "");
246}
247
248
249u_long
250magic_read (FILE * fp)
251{
252 u_long magic;
253 if (fread (&magic, sizeof (magic), 1, fp) != 1)
254 error_msg ("magic num", "Couldn't read magic number.", "");
255 return NTOHUL(magic); /* [RPAP - Jan 97: Endian Ordering] */
256}
257
258
259
260
261int
262isinteger (char s[])
263{
264 int i = 0;
265
266 for (i = 0; s[i] != '\0'; i++)
267 if (!isdigit (s[i]))
268 return 0;
269 return 1;
270}
Note: See TracBrowser for help on using the repository browser.