NEURON
otherio.c
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 /**************************************************************************
4 **
5 ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
6 **
7 ** Meschach Library
8 **
9 ** This Meschach Library is provided "as is" without any express
10 ** or implied warranty of any kind with respect to this software.
11 ** In particular the authors shall not be liable for any direct,
12 ** indirect, special, incidental or consequential damages arising
13 ** in any way from use of the software.
14 **
15 ** Everyone is granted permission to copy, modify and redistribute this
16 ** Meschach Library, provided:
17 ** 1. All copies contain this copyright notice.
18 ** 2. All modified copies shall carry a notice stating who
19 ** made the last modification and the date of such modification.
20 ** 3. No charge is made for this software or works derived from it.
21 ** This clause shall not be construed as constraining other software
22 ** distributed on the same medium as this software, nor is a
23 ** distribution fee considered a charge.
24 **
25 ***************************************************************************/
26 
27 
28 /*
29  File for doing assorted I/O operations not invlolving
30  MAT/VEC/PERM objects
31 */
32 static char rcsid[] = "otherio.c,v 1.1 1997/12/04 17:55:44 hines Exp";
33 
34 #include <stdio.h>
35 #include <ctype.h>
36 #include "matrix.h"
37 
38 
39 
40 /* scratch area -- enough for a single line */
41 static char scratch[MAXLINE+1];
42 
43 /* default value for fy_or_n */
44 static int y_n_dflt = TRUE;
45 
46 /* fy_or_n -- yes-or-no to question is string s
47  -- question written to stderr, input from fp
48  -- if fp is NOT a tty then return y_n_dflt */
49 int fy_or_n(fp,s)
50 FILE *fp;
51 char *s;
52 {
53  char *cp;
54 
55  if ( ! isatty(fileno(fp)) )
56  return y_n_dflt;
57 
58  for ( ; ; )
59  {
60  fprintf(stderr,"%s (y/n) ? ",s);
61  if ( fgets(scratch,MAXLINE,fp)==NULL )
62  error(E_INPUT,"fy_or_n");
63  cp = scratch;
64  while ( isspace(*cp) )
65  cp++;
66  if ( *cp == 'y' || *cp == 'Y' )
67  return TRUE;
68  if ( *cp == 'n' || *cp == 'N' )
69  return FALSE;
70  fprintf(stderr,"Please reply with 'y' or 'Y' for yes ");
71  fprintf(stderr,"and 'n' or 'N' for no.\n");
72  }
73 }
74 
75 /* yn_dflt -- sets the value of y_n_dflt to val */
76 int yn_dflt(val)
77 int val;
78 { return y_n_dflt = val; }
79 
80 /* fin_int -- return integer read from file/stream fp
81  -- prompt s on stderr if fp is a tty
82  -- check that x lies between low and high: re-prompt if
83  fp is a tty, error exit otherwise
84  -- ignore check if low > high */
85 int fin_int(fp,s,low,high)
86 FILE *fp;
87 char *s;
88 int low, high;
89 {
90  int retcode, x;
91 
92  if ( ! isatty(fileno(fp)) )
93  {
94  skipjunk(fp);
95  if ( (retcode=fscanf(fp,"%d",&x)) == EOF )
96  error(E_INPUT,"fin_int");
97  if ( retcode <= 0 )
98  error(E_FORMAT,"fin_int");
99  if ( low <= high && ( x < low || x > high ) )
100  error(E_BOUNDS,"fin_int");
101  return x;
102  }
103 
104  for ( ; ; )
105  {
106  fprintf(stderr,"%s: ",s);
107  if ( fgets(scratch,MAXLINE,stdin)==NULL )
108  error(E_INPUT,"fin_int");
109  retcode = sscanf(scratch,"%d",&x);
110  if ( ( retcode==1 && low > high ) ||
111  ( x >= low && x <= high ) )
112  return x;
113  fprintf(stderr,"Please type an integer in range [%d,%d].\n",
114  low,high);
115  }
116 }
117 
118 
119 /* fin_double -- return double read from file/stream fp
120  -- prompt s on stderr if fp is a tty
121  -- check that x lies between low and high: re-prompt if
122  fp is a tty, error exit otherwise
123  -- ignore check if low > high */
124 double fin_double(fp,s,low,high)
125 FILE *fp;
126 char *s;
127 double low, high;
128 {
129  Real retcode, x;
130 
131  if ( ! isatty(fileno(fp)) )
132  {
133  skipjunk(fp);
134 #if REAL == DOUBLE
135  if ( (retcode=fscanf(fp,"%lf",&x)) == EOF )
136 #elif REAL == FLOAT
137  if ( (retcode=fscanf(fp,"%f",&x)) == EOF )
138 #endif
139  error(E_INPUT,"fin_double");
140  if ( retcode <= 0 )
141  error(E_FORMAT,"fin_double");
142  if ( low <= high && ( x < low || x > high ) )
143  error(E_BOUNDS,"fin_double");
144  return (double)x;
145  }
146 
147  for ( ; ; )
148  {
149  fprintf(stderr,"%s: ",s);
150  if ( fgets(scratch,MAXLINE,stdin)==NULL )
151  error(E_INPUT,"fin_double");
152 #if REAL == DOUBLE
153  retcode = sscanf(scratch,"%lf",&x);
154 #elif REAL == FLOAT
155  retcode = sscanf(scratch,"%f",&x);
156 #endif
157  if ( ( retcode==1 && low > high ) ||
158  ( x >= low && x <= high ) )
159  return (double)x;
160  fprintf(stderr,"Please type an double in range [%g,%g].\n",
161  low,high);
162  }
163 }
164 
165 
static Frame * fp
Definition: code.cpp:161
#define TRUE
Definition: err.c:57
#define FALSE
Definition: err.c:56
#define E_INPUT
Definition: err.h:101
#define error(err_num, fn_name)
Definition: err.h:73
#define E_FORMAT
Definition: err.h:100
#define E_BOUNDS
Definition: err.h:96
#define Real
Definition: machine.h:189
#define MAXLINE
Definition: matrix.h:168
int skipjunk(FILE *fp)
Definition: matrixio.c:47
#define fprintf
Definition: mwprefix.h:30
int fin_int(FILE *fp, char *s, int low, int high)
Definition: otherio.c:85
static int y_n_dflt
Definition: otherio.c:44
static char scratch[MAXLINE+1]
Definition: otherio.c:41
double fin_double(FILE *fp, char *s, double low, double high)
Definition: otherio.c:124
int fy_or_n(FILE *fp, char *s)
Definition: otherio.c:49
int yn_dflt(int val)
Definition: otherio.c:76
static char rcsid[]
Definition: otherio.c:32
#define NULL
Definition: sptree.h:16