[go: up one dir, main page]

File: readint.c

package info (click to toggle)
libforms1 1.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,648 kB
  • ctags: 14,351
  • sloc: ansic: 90,441; makefile: 9,902; sh: 8
file content (168 lines) | stat: -rw-r--r-- 3,491 bytes parent folder | download | duplicates (3)
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
/*
 *
 *  This file is part of the XForms library package.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with XForms; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */


/***********************************************************************
 * $Id: readint.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
 *
 *.  Copyright(c) 1993,1994 by T.C. Zhao
 *   All rights reserved.
 *.
 *
 *   Read an integer (decimal or hex) from a file.
 *   Valid sepertors are  SPACE, \t \n  and ,
 *   Comment is introduced by #
 *
 * For hex integers, a-f, A-F is assumed continous
 ***********************************************************************/
#if !defined(lint) && defined(F_ID)
char *id_rdint = "$Id: readint.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
#endif

#include <stdio.h>
#include <ctype.h>		/* for isdigit */
#include "forms.h"
#include "ulib.h"

#define IS_FS(c)      ((c)==' ' || (c)=='\t' || (c)=='\n' || (c)==',' )
#define IS_COMMENT(c) ((c) == '#')

static int yell = 0;

static void
bad_character(int c)
{
    if (yell && c != EOF)
	(void) fprintf(stderr, "Bad character %c Code=%d\n", c, c);
}

static int
skip_comment(FILE * fp)
{
    register int c;
    while ((c = getc(fp)) != EOF && c != '\n')
	;
    return (c != EOF) ? getc(fp) : EOF;
}

/* read an integer [+-]nnn. No way to return error status */
int
fl_readint(FILE * fp)
{
    register int c, num = 0, sign = 1;

    do
    {
	c = getc(fp);
	while (IS_COMMENT(c))
	    c = skip_comment(fp);
    }
    while (IS_FS(c));

    if (c == '-' || c == '+')
    {
	sign = (c == '-') ? -1 : 1;
	c = getc(fp);
    }

    while (isdigit(c))
    {
	num = 10 * num + c - '0';
	c = getc(fp);
    }

    if (!IS_FS(c))
    {
	bad_character(c);
	num = 123456;
    }

    return sign * num;
}

/* Read a positive integer. return EOF if error */
int
fl_readpint(FILE * fp)
{
    register int c, num = 0;

    do
    {
	c = getc(fp);
	while (IS_COMMENT(c))
	    c = skip_comment(fp);
    }
    while (IS_FS(c));

    if (!(c == '+' || isdigit(c)))
    {
	bad_character(c);
	return EOF;
    }

    do
    {
	num = 10 * num + c - '0';
	c = getc(fp);
    }
    while (isdigit(c));

    return num;
}

/*  Read a hex integer */
int
fl_readhexint(FILE * fp)
{
    register int num = 0, i, c;
    static short hextab[256];

    /* initialize the hex table */
    if (!hextab['1'])
    {
	for (i = '1'; i <= '9'; i++)
	    hextab[i] = i - '0';
	for (i = 'A'; i <= 'F'; i++)
	    hextab[i] = 10 + i - 'A';
	for (i = 'a'; i <= 'f'; i++)
	    hextab[i] = 10 + i - 'a';
    }

    do
    {
	c = getc(fp);
	while (IS_COMMENT(c))
	    c = skip_comment(fp);
    }
    while (IS_FS(c));

    /* demand  0[xX] */
    if (c != '0' || ((c = getc(fp)) != 'x' && c != 'X'))
    {
	bad_character(c);
	return EOF;
    }

    /* now do the coversion */
    while ((c = getc(fp)), isxdigit(c))
	num = (num << 4) + hextab[c];
    return num;
}