StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StPrompt.hh
1 /***************************************************************************
2  *
3  * $Id: StPrompt.hh,v 1.6 2003/09/02 17:59:35 perev Exp $
4  *
5  * Author: Thomas Ullrich, Oct 15 1997
6  ***************************************************************************
7  *
8  * Description:
9  * Template function to prompt the user for input of type T.
10  * Reads the new input value, unless <CR> is
11  * pressed directly after the prompt. In this case the
12  * previous value (indicated in brackets) stays untouched.
13  *
14  * For non-build-in types/classes make sure the input and output
15  * operators '<<' and '>>' are defined.
16  *
17  * Syntax:
18  *
19  * StPrompt("text", T); // T can be: int double, float, string, etc.
20  *
21  *
22  * Specialization:
23  *
24  * bool answer;
25  * StPrompt("text", answer); // accepts: true, t, yes, y, on, 1
26  * // as 'true', everything else is assumed
27  * // to have value 'false'
28  *
29  * char name[10];
30  * StPrompt("text", name, 10); // note the length as last parameter
31  *
32  ***************************************************************************
33  *
34  * $Log: StPrompt.hh,v $
35  * Revision 1.6 2003/09/02 17:59:35 perev
36  * gcc 3.2 updates + WarnOff
37  *
38  * Revision 1.5 2002/05/01 01:09:50 ullrich
39  * Changed int to unsigned int to get rid of warning message.
40  *
41  * Revision 1.4 2000/06/22 01:57:29 ullrich
42  * Removed unused variable.
43  *
44  * Revision 1.3 1999/12/21 20:20:35 ullrich
45  * Fixed bug in macro for Sun.
46  *
47  * Revision 1.2 1999/12/21 15:14:28 ullrich
48  * Modified to cope with new compiler version on Sun (CC5.0).
49  *
50  * Revision 1.1 1999/01/30 03:59:05 fisyak
51  * Root Version of StarClassLibrary
52  *
53  * Revision 1.1 1999/01/23 00:28:01 ullrich
54  * Initial Revision
55  *
56  **************************************************************************/
57 #ifndef ST_PROMPT_HH
58 #define ST_PROMPT_HH
59 
60 #include <Stiostream.h>
61 #include <Stsstream.h>
62 #include <string>
63 #include <ctype.h>
64 #if !defined(ST_NO_NAMESPACES)
65 using std::string;
66 #endif
67 
68 inline void StPrompt()
69 {
70  cout << "-- Press return to continue -- ";
71  cin.get();
72 }
73 
74 template<class T>
75 inline void StPrompt(const char *text, T& var)
76 {
77  string line;
78  char c;
79 
80  cout << text << " [" << var << "]: ";
81  while ((c = cin.get()) && c != '\n') line += c;
82  if (line.length() > 0) {
83  istrstream ist(line.c_str(), line.length());
84  ist >> var;
85  }
86 }
87 
88 inline void StPrompt(const char *text, string& var)
89 {
90  string line;
91  char c;
92 
93  cout << text << " [" << var.c_str() << "]: ";
94  while ((c = cin.get()) && c != '\n') line += c;
95  if (line.length() > 0) var = line;
96 }
97 
98 #if defined (__SUNPRO_CC) && __SUNPRO_CC < 0x500
99 inline void StBoolPrompt(const char *text, bool& var)
100 #else
101 inline void StPrompt(const char *text, bool& var)
102 #endif
103 {
104  string line;
105  char c;
106  string svar = var ? "true" : "false";
107 
108  cout << text << " [" << svar.c_str() << "]: ";
109  while ((c = cin.get()) && c != '\n') line += c;
110  if (line.length() > 0) {
111  if (line == "true")
112  var = true;
113  else if (line == "t")
114  var = true;
115  else if (line == "yes")
116  var = true;
117  else if (line == "y")
118  var = true;
119  else if (line == "on")
120  var = true;
121  else if (line == "1")
122  var = true;
123  else
124  var = false;
125  }
126 }
127 
128 
129 inline void StPrompt(const char *text, char* var, unsigned int maxlength)
130 {
131  string line;
132  unsigned char c;
133 
134  cout << text << " [" << var << "]: ";
135  while ((c = cin.get()) && c != '\n' && line.length() < maxlength)
136  line += c;
137  if (line.length() > 0) strcpy(var, line.c_str());
138 }
139 
140 #endif