NEURON
xred.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 /* /local/src/master/nrn/src/oc/xred.cpp,v 1.3 1996/02/16 16:19:33 hines Exp */
3 /*
4 xred.cpp,v
5  * Revision 1.3 1996/02/16 16:19:33 hines
6  * OCSMALL used to throw out things not needed by teaching programs
7  *
8  * Revision 1.2 1995/04/03 13:58:43 hines
9  * Port to MSWindows
10  *
11  * Revision 1.1.1.1 1994/10/12 17:22:16 hines
12  * NEURON 3.0 distribution
13  *
14  * Revision 2.22 93/02/12 08:51:42 hines
15  * beginning of port to PC-Dos
16  *
17  * Revision 2.20 93/02/03 11:27:59 hines
18  * a bit more generic for portability. will possibly work on NeXT
19  *
20  * Revision 2.7 93/01/12 08:58:48 hines
21  * to assign to a hoc string use
22  * hoc_assign_str(char** cpp, char* buf)
23  * Some minor modifications to allow use of some functions by File class
24  *
25  * Revision 1.2 92/08/12 10:45:40 hines
26  * Changes of sejnowski lab. also gets sred from hoc. Major addition is
27  * a new x.cpp which is modified for screen updating following an expose
28  * event. This is in x_sejnowski.cpp and will compile to x.o when
29  * Sejnowski is defined as 1 in Imakefile (don't forget imknrn -a when
30  * changed and delete old x.o)
31  * Does not contain get_default on this checkin
32  *
33  * Revision 1.2 1992/06/30 23:14:11 fisher
34  * added strstr() function for the MIPS - the MIPS' string.h doesn't
35  * contain strstr() (see hoc.h comment).
36  *
37  * Revision 1.1 1992/05/22 19:35:47 fisher
38  * Initial revision
39  *
40  * Revision 4.59 92/04/13 11:09:08 hines
41  * Stewart Jasloves contribution of sred(). usage is
42  * i = sred("prompt", "default", "charlist")
43  * type one of the characters in the charlist. the default becomes that
44  * character. return value is position in charlist (0 if first char).
45  *
46  * Revision 1.1 90/02/14 09:47:19 mlh
47  * Initial revision
48  *
49 */
50 
51 #include "hoc.h"
52 
53 
54 /* input a n integer in range > min and < max */
55 int ired(const char* prompt, int defalt, int min, int max)
56 {
57  return( (int) xred(prompt, (double) defalt,
58  (double) min, (double) max));
59 }
60 /* input a double number in range > min and < max
61  program loops til proper number is typed in by user
62  prompt and default are typed by computer
63  default is used if user types RETURN key
64  input is freeform as scanf can make it.
65 */
66 #include <stdio.h>
67 double xred(const char* prompt, double defalt, double min, double max) {
68 #if !OCSMALL
69  char istr[80], c[2] ; double input;
70  for (;;) {
71  IGNORE(fprintf(stderr,"%s (%-.5g)",prompt,defalt));
72 #ifdef WIN32
73  if (gets(istr) != NULL) {
74  strcat(istr, "\n");
75 #else
76  if (fgets(istr,79,stdin) != NULL) {
77 #endif
78  if (istr[0] == '\n')
79  {
80  input = defalt;
81  goto label;
82  }
83  if (sscanf(istr,"%lf%1s",&input,c) == 1)
84  if (sscanf(istr,"%lf",&input) == 1)
85  label: {
86  if (input >= min && input <= max)
87  return(input);
88 IGNORE(fprintf(stderr,"must be > %-.5g and < %-.5g\n", min,max));
89  continue;
90  }
91  } else {
92  rewind(stdin);
93  }
94  IGNORE(fprintf(stderr,"input error\n"));
95  }
96 #else
97 return 0.;
98 #endif
99 }
100 
101 
102 /* hoc_Sred.cpp SW Jaslove March 23, 1992
103  This is the hoc interface for the sred() function, which follows.
104 */
105 
106 void hoc_Sred(void) {
107 #if !OCSMALL
108  char defalt[80], **pdefalt;
109  double x;
110  strcpy(defalt,gargstr(2));
111  pdefalt = hoc_pgargstr(2);
112  x = (double) hoc_sred(gargstr(1), defalt, gargstr(3));
113  hoc_assign_str(pdefalt, defalt);
114 #else
115  double x = 0.;
116 #endif
117  ret();
118  hoc_pushx(x);
119 }
120 #if !OCSMALL
121 
122 /* sred.cpp SW Jaslove March 23, 1992
123  n = sred(prompt,default,charlist)
124  Outputs a prompt and inputs a string which MUST be a member of charlist.
125  If default exists: default is returned if user types RETURN key only.
126  If default is null: a string MUST be entered, there is no default.
127  If charlist is null and default exists: any typed string is accepted.
128  Default MUST be a member of charlist, so a return value can be specified.
129  Input is terminated by RETURN or first space after a nonspace char.
130  Program loops until proper input is typed in by user.
131  RETURNS: Starting position of input in charlist, beginning with 0.
132  *** NOTE: default is replaced by entered string ***
133 */
134 
135 int hoc_sred(const char* prompt, char* defalt, char* charlist) {
136  char istr[80], c[2], instring[40], *result;
137 #if !defined(HAVE_STRSTR)
138  extern char *strstr();
139 #endif
140 
141  for (;;) { /* cycle until done */
142  IGNORE(fprintf(stderr,"%s (%s)",prompt,defalt)); /* print prompt */
143 #ifdef WIN32
144  if (gets(istr) != NULL) {
145  strcat(istr, "\n");
146 #else
147  if (fgets(istr,79,stdin) != NULL) { /* read input */
148 #endif
149  if (defalt[0]!='\0' && istr[0]=='\n') {
150  strcpy(istr,defalt); /* if CR only, use default */
151  }else{
152  istr[strlen(istr)-1]='\0'; /* if real input, strip return */
153  }
154  if ( sscanf(istr,"%s%s",instring,c) == 1 ) { /* only single input */
155  if ( charlist==NULL ) { /* if charlist is null: */
156  strcpy(defalt,instring); /* accept any input, so */
157  return(0); /* update default and return 0 */
158  }
159  if ((result = strstr(charlist,instring)) != NULL ) {
160  strcpy(defalt,instring); /* if input is in charlist: */
161  return(result-charlist); /* update default and return pos */
162  }
163  }
164  IGNORE(fprintf(stderr,
165  "input must be a substring of <<%s>>\n", charlist));
166  continue; /* go back for another cycle */
167  } else {
168  rewind(stdin);
169  }
170  IGNORE(fprintf(stderr,"input error\n")); /* recycle */
171  }
172  return 0;
173 }
174 
175 #if !defined(HAVE_STRSTR)
176 char *
177 strstr(cs, ct)
178  char *cs, *ct;
179 {
180  char *strchr_ptr, *cs_ptr;
181  int ct_len;
182 
183  ct_len = strlen(ct);
184 
185  for (cs_ptr = cs;
186  ((strchr_ptr = (char *)strchr(cs_ptr, ct[0]))
187  && (ct_len <= strlen(strchr_ptr)));
188  cs_ptr = strchr_ptr + 1) {
189  if (memcmp(ct, strchr_ptr, ct_len) == 0) {
190  return strchr_ptr; }
191  }
192 
193  return (char *)0;
194 }
195 #endif
196 #endif
197 
#define gets
Definition: mwprefix.h:28
char * strstr(cs, ct) char *cs
double max(double a, double b)
Definition: geometry3d.cpp:22
int hoc_sred(const char *prompt, char *defalt, char *charlist)
Definition: xred.cpp:135
#define min(a, b)
Definition: matrix.h:157
int ct_len
Definition: xred.cpp:179
char ** hoc_pgargstr(int narg)
Definition: code.cpp:1580
int ired(const char *prompt, int defalt, int min, int max)
Definition: xred.cpp:55
#define gargstr
Definition: hocdec.h:14
#define IGNORE(arg)
Definition: model.h:262
void hoc_assign_str(char **cpp, const char *buf)
Definition: code.cpp:2337
#define ret
Definition: redef.h:123
fprintf(stderr, "Don't know the location of params at %p\, pp)
double xred(const char *prompt, double defalt, double min, double max)
Definition: xred.cpp:67
void hoc_pushx(double)
char * ct
Definition: xred.cpp:178
#define c
void hoc_Sred(void)
Definition: xred.cpp:106
#define input(prompt, fmt, var)
Definition: matrix.h:368
return NULL
Definition: cabcode.cpp:461