File: otherio.c

package info (click to toggle)
meschach 1.2b-17
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 2,176 kB
  • sloc: ansic: 21,959; makefile: 480; sh: 17
file content (164 lines) | stat: -rw-r--r-- 4,226 bytes parent folder | download | duplicates (7)
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

/**************************************************************************
**
** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
**
**			     Meschach Library
** 
** This Meschach Library is provided "as is" without any express 
** or implied warranty of any kind with respect to this software. 
** In particular the authors shall not be liable for any direct, 
** indirect, special, incidental or consequential damages arising 
** in any way from use of the software.
** 
** Everyone is granted permission to copy, modify and redistribute this
** Meschach Library, provided:
**  1.  All copies contain this copyright notice.
**  2.  All modified copies shall carry a notice stating who
**      made the last modification and the date of such modification.
**  3.  No charge is made for this software or works derived from it.  
**      This clause shall not be construed as constraining other software
**      distributed on the same medium as this software, nor is a
**      distribution fee considered a charge.
**
***************************************************************************/


/*
	File for doing assorted I/O operations not invlolving
	MAT/VEC/PERM objects
*/
static	char	rcsid[] = "$Id: otherio.c,v 1.2 1994/01/13 05:34:52 des Exp $";

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



/* scratch area -- enough for a single line */
static	char	scratch[MAXLINE+1];

/* default value for fy_or_n */
static	int	y_n_dflt = TRUE;

/* fy_or_n -- yes-or-no to question is string s
	-- question written to stderr, input from fp 
	-- if fp is NOT a tty then return y_n_dflt */
int	fy_or_n(fp,s)
FILE	*fp;
char	*s;
{
	char	*cp;

	if ( ! isatty(fileno(fp)) )
		return y_n_dflt;

	for ( ; ; )
	{
		fprintf(stderr,"%s (y/n) ? ",s);
		if ( fgets(scratch,MAXLINE,fp)==NULL )
			error(E_INPUT,"fy_or_n");
		cp = scratch;
		while ( isspace(*cp) )
			cp++;
		if ( *cp == 'y' || *cp == 'Y' )
			return TRUE;
		if ( *cp == 'n' || *cp == 'N' )
			return FALSE;
		fprintf(stderr,"Please reply with 'y' or 'Y' for yes ");
		fprintf(stderr,"and 'n' or 'N' for no.\n");
	}
}

/* yn_dflt -- sets the value of y_n_dflt to val */
int	yn_dflt(val)
int	val;
{	return y_n_dflt = val;		}

/* fin_int -- return integer read from file/stream fp
	-- prompt s on stderr if fp is a tty
	-- check that x lies between low and high: re-prompt if
		fp is a tty, error exit otherwise
	-- ignore check if low > high		*/
int	fin_int(fp,s,low,high)
FILE	*fp;
char	*s;
int	low, high;
{
	int	retcode, x;

	if ( ! isatty(fileno(fp)) )
	{
		skipjunk(fp);
		if ( (retcode=fscanf(fp,"%d",&x)) == EOF )
			error(E_INPUT,"fin_int");
		if ( retcode <= 0 )
			error(E_FORMAT,"fin_int");
		if ( low <= high && ( x < low || x > high ) )
			error(E_BOUNDS,"fin_int");
		return x;
	}

	for ( ; ; )
	{
		fprintf(stderr,"%s: ",s);
		if ( fgets(scratch,MAXLINE,stdin)==NULL )
			error(E_INPUT,"fin_int");
		retcode = sscanf(scratch,"%d",&x);
		if ( ( retcode==1 && low > high ) ||
					( x >= low && x <= high ) )
			return x;
		fprintf(stderr,"Please type an integer in range [%d,%d].\n",
							low,high);
	}
}


/* fin_double -- return double read from file/stream fp
	-- prompt s on stderr if fp is a tty
	-- check that x lies between low and high: re-prompt if
		fp is a tty, error exit otherwise
	-- ignore check if low > high		*/
double	fin_double(fp,s,low,high)
FILE	*fp;
char	*s;
double	low, high;
{
	Real	retcode, x;

	if ( ! isatty(fileno(fp)) )
	{
		skipjunk(fp);
#if REAL == DOUBLE
		if ( (retcode=fscanf(fp,"%lf",&x)) == EOF )
#elif REAL == FLOAT
		if ( (retcode=fscanf(fp,"%f",&x)) == EOF )
#endif
			error(E_INPUT,"fin_double");
		if ( retcode <= 0 )
			error(E_FORMAT,"fin_double");
		if ( low <= high && ( x < low || x > high ) )
			error(E_BOUNDS,"fin_double");
		return (double)x;
	}

	for ( ; ; )
	{
		fprintf(stderr,"%s: ",s);
		if ( fgets(scratch,MAXLINE,stdin)==NULL )
			error(E_INPUT,"fin_double");
#if REAL == DOUBLE
		retcode = sscanf(scratch,"%lf",&x);
#elif REAL == FLOAT 
		retcode = sscanf(scratch,"%f",&x);
#endif
		if ( ( retcode==1 && low > high ) ||
					( x >= low && x <= high ) )
			return (double)x;
		fprintf(stderr,"Please type an double in range [%g,%g].\n",
							low,high);
	}
}