File: getrect.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (253 lines) | stat: -rw-r--r-- 6,964 bytes parent folder | download
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * getrect.c -
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 *			Lawrence Livermore National Laboratory
 *
 * This file contains a procedure, GetRect(), for gobbling up
 * four integers from an input file.
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-7.5/utils/getrect.c,v 1.3 2010/03/08 13:33:08 tim Exp $";
#endif  /* not lint */

#include <stdio.h>
#include <ctype.h>

#include "utils/magic.h"
#include "utils/geometry.h"

#if defined(ultrix) || defined(linux)
typedef char STDIOCHAR;
#else
typedef unsigned char STDIOCHAR;
#endif


/*
 * ----------------------------------------------------------------------------
 *
 * GetRect --
 *
 * Parse a rectangle from a file and fill in the supplied rect struct.
 * We assume that the rectangle consists of four decimal numbers, each
 * separated from the next by a single space, and terminated by a newline.
 *
 * ESTHETIC WARNING:
 *	The algorithm used here is gross but extremely fast.
 *
 * Results:
 *	FALSE on end of file or error, TRUE otherwise.
 *	If nonmanhattan extensions are enabled, it returns a
 *	direction result for triangles.
 *
 * Side effects:
 *	Fills in the contents of *rect.
 *
 * ----------------------------------------------------------------------------
 */

#define	RECTBUFTHRESHOLD	100

#ifdef	linux
#define	FILE_CNT(fin)	((fin)->_IO_read_end - (fin)->_IO_read_ptr)
#define	FILE_PTR(fin)	((char *) fin->_IO_read_ptr)
#define FILE_DEC_CNT(fin, n)	
#define	FILE_SET_PTR(fin, cp) ((fin)->_IO_read_ptr = (cp))
#elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(CYGWIN) || defined(macosx)
#define FILE_CNT(fin)   ((fin)->_r)
#define FILE_PTR(fin)   ((char *)(fin)->_p)
#define FILE_DEC_CNT(fin, n)  ((fin)->_r -= (n))
#define FILE_SET_PTR(fin, cp) ((fin)->_p = (cp))
#else
#define	FILE_CNT(fin)	((fin)->_cnt)	
#define	FILE_PTR(fin)	((char *) fin->_ptr)
#define FILE_DEC_CNT(fin, n)	((fin)->_cnt -= (n))
#define	FILE_SET_PTR(fin, cp) ((fin)->_ptr = (cp))

#endif

#ifdef NONMANHATTAN
int
#else
bool
#endif
GetRect(fin, skip, rect, scalen, scaled)
    FILE *fin;
    int skip;			/* Number of bytes to skip before rect */
    Rect *rect;	/* Pointer to rectangle to be filled in */
    int scalen;			/* Scale up by this amount */
    int scaled;			/* Scale down by this amount */
{
    int n, c;
    char *cp;
    bool isNegative;
#ifdef NONMANHATTAN
    int dir = 0x1;
#endif

#if !defined(__DragonFly__)
    if (FILE_CNT(fin) < RECTBUFTHRESHOLD) goto slow;
    /*
     * Fast version of GetRect -- read directly from buffer.
     * This depends on the structure of a standard I/O library (FILE *).
     */
    cp = FILE_PTR(fin) + skip;		/* Skip over "ect " */

    if (isNegative = ((c = *cp++) == '-')) c = *cp++;
    for (n = 0; isdigit(c); n = n * 10 + c - '0', c = *cp++)
	/* Nothing */;
    rect->r_xbot = isNegative ? -n : n;
    if (!isspace(c)) goto fastbad;
    while (isspace(c)) c = *cp++;

    if (isNegative = (c == '-')) c = *cp++;
    for (n = 0; isdigit(c); n = n * 10 + c - '0', c = *cp++)
	/* Nothing */;
    rect->r_ybot = isNegative ? -n : n;
    if (!isspace(c)) goto fastbad;
    while (isspace(c)) c = *cp++;

    if (isNegative = (c == '-')) c = *cp++;
    for (n = 0; isdigit(c); n = n * 10 + c - '0', c = *cp++)
	/* Nothing */;
    rect->r_xtop = isNegative ? -n : n;
    if (!isspace(c)) goto fastbad;
    while (isspace(c)) c = *cp++;

    if (isNegative = (c == '-')) c = *cp++;
    for (n = 0; isdigit(c); n = n * 10 + c - '0', c = *cp++)
	/* Nothing */;
    rect->r_ytop = isNegative ? -n : n;

    if (scalen > 1)
    {
	rect->r_xbot *= scalen;
	rect->r_ybot *= scalen;
	rect->r_xtop *= scalen;
	rect->r_ytop *= scalen;
    }
    if (scaled > 1)
    {
	rect->r_xbot /= scaled;
	rect->r_ybot /= scaled;
	rect->r_xtop /= scaled;
	rect->r_ytop /= scaled;
    }

    /* Adjust the stdio pointers to reflect the characters read */
    FILE_DEC_CNT(fin, cp - FILE_PTR(fin));
    FILE_SET_PTR(fin, (STDIOCHAR *) cp);

    /* Make sure we skip to end of line or EOF */
    while (c != EOF && c != '\n')
#ifdef NONMANHATTAN
    {
	c = getc(fin);
	switch ((char)c)
	{
	    case 's':
		dir |= 0x2;
		break;
	    case 'e':
		dir |= 0x4;
		break;
	}
    }
    return dir;
#else
	c = getc(fin);
    return (TRUE);
#endif 

    /* Adjust the stdio pointers to reflect the characters read */
fastbad:
    FILE_DEC_CNT(fin, cp - FILE_PTR(fin));
    FILE_SET_PTR(fin, (STDIOCHAR *) cp);
    goto bad;

    /* Slow version of GetRect -- read via getc */
slow:

#endif /* __DragonFly__ */

    while (skip-- > 0)
	(void) getc(fin);

    if (isNegative = ((c = getc(fin)) == '-')) c = getc(fin);
    for (n = 0; isdigit(c); n = n * 10 + c - '0', c = getc(fin))
	/* Nothing */;
    rect->r_xbot = isNegative ? -n : n;
    if (!isspace(c)) goto bad;
    while ((c = getc(fin)) != EOF && isspace(c)) /* Nothing */;

    if (isNegative = (c == '-')) c = getc(fin);
    for (n = 0; isdigit(c); n = n * 10 + c - '0', c = getc(fin))
	/* Nothing */;
    rect->r_ybot = isNegative ? -n : n;
    if (!isspace(c)) goto bad;
    while ((c = getc(fin)) != EOF && isspace(c)) /* Nothing */;

    if (isNegative = (c == '-')) c = getc(fin);
    for (n = 0; isdigit(c); n = n * 10 + c - '0', c = getc(fin))
	/* Nothing */;
    rect->r_xtop = isNegative ? -n : n;
    if (!isspace(c)) goto bad;
    while ((c = getc(fin)) != EOF && isspace(c)) /* Nothing */;

    if (isNegative = (c == '-')) c = getc(fin);
    for (n = 0; isdigit(c); n = n * 10 + c - '0', c = getc(fin))
	/* Nothing */;
    rect->r_ytop = isNegative ? -n : n;

    if (scalen > 1)
    {
	rect->r_xbot *= scalen;
	rect->r_ybot *= scalen;
	rect->r_xtop *= scalen;
	rect->r_ytop *= scalen;
    }
    if (scaled > 1)
    {
	rect->r_xbot /= scaled;
	rect->r_ybot /= scaled;
	rect->r_xtop /= scaled;
	rect->r_ytop /= scaled;
    }

    while (c != EOF && c != '\n')
#ifdef NONMANHATTAN
    {
	c = getc(fin);
	switch ((char)c)
	{
	    case 's':
		dir |= 0x2;
		break;
	    case 'e':
		dir |= 0x4;
		break;
	}
    }
    return dir;
#else
	c = getc(fin);
    return (TRUE);
#endif

bad:
    while (c != EOF && c != '\n')
	c = getc(fin);
    return (FALSE);
}