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