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  return ((int) xred(prompt, (double) defalt, (double) min, (double) max));
57 }
58 /* input a double number in range > min and < max
59  program loops til proper number is typed in by user
60  prompt and default are typed by computer
61  default is used if user types RETURN key
62  input is freeform as scanf can make it.
63 */
64 #include <stdio.h>
65 double 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  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 #if !defined(HAVE_STRSTR)
136  extern char* strstr();
137 #endif
138 
139  for (;;) { /* cycle until done */
140  IGNORE(fprintf(stderr, "%s (%s)", prompt, defalt)); /* print prompt */
141 #ifdef WIN32
142  if (gets(istr) != NULL) {
143  strcat(istr, "\n");
144 #else
145  if (fgets(istr, 79, stdin) != NULL) { /* read input */
146 #endif
147  if (defalt[0] != '\0' && istr[0] == '\n') {
148  strcpy(istr, defalt); /* if CR only, use default */
149  } else {
150  istr[strlen(istr) - 1] = '\0'; /* if real input, strip return */
151  }
152  if (sscanf(istr, "%s%s", instring, c) == 1) { /* only single input */
153  if (charlist == NULL) { /* if charlist is null: */
154  strcpy(defalt, instring); /* accept any input, so */
155  return (0); /* update default and return 0 */
156  }
157  if ((result = strstr(charlist, instring)) != NULL) {
158  strcpy(defalt, instring); /* if input is in charlist: */
159  return (result - charlist); /* update default and return pos */
160  }
161  }
162  IGNORE(fprintf(stderr, "input must be a substring of <<%s>>\n", charlist));
163  continue; /* go back for another cycle */
164  } else {
165  rewind(stdin);
166  }
167  IGNORE(fprintf(stderr, "input error\n")); /* recycle */
168  }
169  return 0;
170 }
171 
172 #if !defined(HAVE_STRSTR)
173 char* strstr(char* cs, char* ct) {
174  char *strchr_ptr, *cs_ptr;
175  int ct_len;
176 
177  ct_len = strlen(ct);
178 
179  for (cs_ptr = cs;
180  ((strchr_ptr = (char*) strchr(cs_ptr, ct[0])) && (ct_len <= strlen(strchr_ptr)));
181  cs_ptr = strchr_ptr + 1) {
182  if (memcmp(ct, strchr_ptr, ct_len) == 0) {
183  return strchr_ptr;
184  }
185  }
186 
187  return (char*) 0;
188 }
189 #endif
190 #endif
#define c
void hoc_assign_str(char **cpp, const char *buf)
Definition: code.cpp:2350
int hoc_sred(const char *prompt, char *defalt, char *charlist)
Definition: xred.cpp:133
char ** hoc_pgargstr(int narg)
Definition: code.cpp:1599
#define gargstr
Definition: hocdec.h:14
void hoc_pushx(double)
#define input(prompt, fmt, var)
Definition: matrix.h:368
#define min(a, b)
Definition: matrix.h:157
#define max(a, b)
Definition: matrix.h:154
#define IGNORE(arg)
Definition: model.h:247
#define fprintf
Definition: mwprefix.h:30
#define gets
Definition: mwprefix.h:28
#define ret
Definition: redef.h:123
#define NULL
Definition: sptree.h:16
char * strstr(char *cs, char *ct)
Definition: xred.cpp:173
int ired(const char *prompt, int defalt, int min, int max)
Definition: xred.cpp:55
void hoc_Sred(void)
Definition: xred.cpp:104
double xred(const char *prompt, double defalt, double min, double max)
Definition: xred.cpp:65