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
|
/*
* 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. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file readint.c
*
* 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <ctype.h> /* for isdigit */
#include "include/forms.h"
#include "flinternal.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 )
fprintf(stderr, "Bad character %c Code=%d\n", c, c);
}
/***************************************
***************************************/
static int
skip_comment( FILE * fp )
{
int c;
while ( ( c = getc( fp ) ) != EOF && c != '\n' )
/* empty */ ;
return c != EOF ? getc( fp ) : EOF;
}
/***************************************
* read an integer [+-]nnn. No way to return error status
***************************************/
int
fli_readint( FILE * fp )
{
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( ( unsigned char ) 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
fli_readpint( FILE * fp )
{
int c,
num = 0;
do
{
c = getc( fp );
while ( IS_COMMENT( c ) )
c = skip_comment( fp );
} while ( IS_FS( c ) );
if ( ! ( c == '+' || isdigit( ( unsigned char ) c ) ) )
{
bad_character( c );
return EOF;
}
do
{
num = 10 * num + c - '0';
c = getc( fp );
} while ( isdigit( ( unsigned char ) c ) );
return num;
}
/***************************************
* Read a hex integer
***************************************/
int
fli_readhexint( FILE * fp )
{
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( ( unsigned char ) c ) )
num = ( num << 4 ) + hextab[ ( unsigned char ) c ];
return num;
}
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|