1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: ftest_io.cc,v 1.12 1998-03-31 10:36:02 schmidt Exp $ */ |
---|
3 | |
---|
4 | //{{{ docu |
---|
5 | // |
---|
6 | // ftest_io.cc - io utilities for the factory test environment. |
---|
7 | // |
---|
8 | //}}} |
---|
9 | |
---|
10 | #include <ctype.h> |
---|
11 | #include <string.h> |
---|
12 | #include <stdlib.h> |
---|
13 | #include <iostream.h> |
---|
14 | #include <strstream.h> |
---|
15 | |
---|
16 | #include <factory.h> |
---|
17 | |
---|
18 | #include "ftest_util.h" |
---|
19 | |
---|
20 | // |
---|
21 | // - external functions. |
---|
22 | // |
---|
23 | |
---|
24 | //{{{ CanonicalForm ftestGetCanonicalForm ( const char * canFormSpec ) |
---|
25 | //{{{ docu |
---|
26 | // |
---|
27 | // ftestGetCanonicalForm() - read a canonical form from canFormSpec, |
---|
28 | // return it. |
---|
29 | // |
---|
30 | //}}} |
---|
31 | CanonicalForm |
---|
32 | ftestGetCanonicalForm ( const char * canFormSpec ) |
---|
33 | { |
---|
34 | const char * stringF = canFormSpec; |
---|
35 | stringF = ftestSkipBlancs( stringF ); |
---|
36 | |
---|
37 | // check for a single minus |
---|
38 | if ( stringF[0] == '-' ) { |
---|
39 | const char * tokenCursor = ftestSkipBlancs( stringF+1 ); |
---|
40 | |
---|
41 | if ( ! *tokenCursor ) { |
---|
42 | CanonicalForm f; |
---|
43 | cin >> f; |
---|
44 | return f; |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | // get string to read canonical form from |
---|
49 | if ( *stringF == '$' ) { |
---|
50 | const char * tokenCursor = ftestSkipBlancs( stringF+1 ); |
---|
51 | // read canonical form from environment |
---|
52 | stringF = getenv( tokenCursor ); |
---|
53 | if ( ! stringF ) |
---|
54 | ftestError( CanFormSpecError, |
---|
55 | "no environment variable `$%s' set\n", |
---|
56 | tokenCursor ); |
---|
57 | } |
---|
58 | |
---|
59 | // create terminated CanonicalForm |
---|
60 | int i = strlen( stringF ); |
---|
61 | char * terminatedStringF = new char[i+2]; |
---|
62 | char * stringCursor = terminatedStringF; |
---|
63 | while ( *stringF ) { |
---|
64 | switch ( *stringF ) { |
---|
65 | case '.': *stringCursor = '*'; break; |
---|
66 | case '{': *stringCursor = '('; break; |
---|
67 | case '}': *stringCursor = ')'; break; |
---|
68 | default: *stringCursor = *stringF; break; |
---|
69 | } |
---|
70 | stringF++; stringCursor++; |
---|
71 | } |
---|
72 | *stringCursor++ = ';'; |
---|
73 | *stringCursor = '\0'; |
---|
74 | |
---|
75 | // read f and return it |
---|
76 | CanonicalForm f; |
---|
77 | istrstream( terminatedStringF ) >> f; |
---|
78 | delete [] terminatedStringF; |
---|
79 | return f; |
---|
80 | } |
---|
81 | //}}} |
---|
82 | |
---|
83 | //{{{ Variable ftestGetVariable ( const char * stringVariable ) |
---|
84 | //{{{ docu |
---|
85 | // |
---|
86 | // ftestGetVariable() - read a variable from stringVariable, |
---|
87 | // return it. |
---|
88 | // |
---|
89 | //}}} |
---|
90 | Variable |
---|
91 | ftestGetVariable ( const char * stringVariable ) |
---|
92 | { |
---|
93 | Variable v; |
---|
94 | stringVariable = ftestSkipBlancs( stringVariable ); |
---|
95 | |
---|
96 | if ( isalpha( *stringVariable ) ) |
---|
97 | v = Variable( *stringVariable ); |
---|
98 | else if ( isdigit( *stringVariable ) ) |
---|
99 | v = Variable(); |
---|
100 | else |
---|
101 | ftestError( CommandlineError, |
---|
102 | "variable expected at `%s'\n", stringVariable ); |
---|
103 | |
---|
104 | stringVariable = ftestSkipBlancs( stringVariable+1 ); |
---|
105 | if ( *stringVariable ) |
---|
106 | ftestError( CommandlineError, |
---|
107 | "extra characters after var spec `%s'\n", stringVariable ); |
---|
108 | |
---|
109 | return v; |
---|
110 | } |
---|
111 | //}}} |
---|
112 | |
---|
113 | //{{{ int ftestGetint ( const char * stringInt ) |
---|
114 | //{{{ docu |
---|
115 | // |
---|
116 | // ftestGetint() - read an integer from stringInt, |
---|
117 | // return it. |
---|
118 | // |
---|
119 | //}}} |
---|
120 | int |
---|
121 | ftestGetint ( const char * stringInt ) |
---|
122 | { |
---|
123 | const char * tokenCursor; |
---|
124 | |
---|
125 | int i = (int)strtol( stringInt, (char**)&tokenCursor, 0 ); |
---|
126 | |
---|
127 | // do error checks |
---|
128 | if ( stringInt == tokenCursor ) |
---|
129 | ftestError( CommandlineError, |
---|
130 | "integer expected at `%s'\n", stringInt ); |
---|
131 | |
---|
132 | stringInt = ftestSkipBlancs( tokenCursor ); |
---|
133 | if ( *stringInt ) |
---|
134 | ftestError( CommandlineError, |
---|
135 | "extra characters after int spec `%s'\n", stringInt ); |
---|
136 | |
---|
137 | return i; |
---|
138 | } |
---|
139 | //}}} |
---|
140 | |
---|
141 | //{{{ bool ftestGetbool ( const char * stringBool ) |
---|
142 | //{{{ docu |
---|
143 | // |
---|
144 | // ftestGetbool() - read an boolean from stringBool, |
---|
145 | // return it. |
---|
146 | // |
---|
147 | //}}} |
---|
148 | bool |
---|
149 | ftestGetbool ( const char * stringBool ) |
---|
150 | { |
---|
151 | const char * tokenCursor; |
---|
152 | |
---|
153 | bool b; |
---|
154 | // skip blancs |
---|
155 | stringBool = ftestSkipBlancs( stringBool ); |
---|
156 | |
---|
157 | // look for "true" or "false" |
---|
158 | tokenCursor = ftestSubStr( "true", stringBool ); |
---|
159 | if ( stringBool != tokenCursor ) |
---|
160 | b = true; |
---|
161 | else { |
---|
162 | tokenCursor = ftestSubStr( "false", stringBool ); |
---|
163 | b = false; |
---|
164 | } |
---|
165 | |
---|
166 | // do error checks |
---|
167 | if ( stringBool == tokenCursor ) |
---|
168 | ftestError( CommandlineError, |
---|
169 | "bool expected at `%s'\n", stringBool ); |
---|
170 | |
---|
171 | // check for extra characters |
---|
172 | stringBool = ftestSkipBlancs( tokenCursor ); |
---|
173 | if ( *stringBool ) |
---|
174 | ftestError( CommandlineError, |
---|
175 | "extra characters after bool spec `%s'\n", stringBool ); |
---|
176 | |
---|
177 | return b; |
---|
178 | } |
---|
179 | //}}} |
---|
180 | |
---|
181 | //{{{ void ftestPrintResult ( const char * resultName, const CanonicalForm & result ) |
---|
182 | //{{{ docu |
---|
183 | // |
---|
184 | // ftestPrintResult() - print a canonical form. |
---|
185 | // |
---|
186 | //}}} |
---|
187 | void |
---|
188 | ftestPrintResult ( const char * resultName, const CanonicalForm & result ) |
---|
189 | { |
---|
190 | if ( ftestPrintResultFlag ) { |
---|
191 | cout << "Result:\t\t" << resultName << ":" << endl; |
---|
192 | cout << result << endl; |
---|
193 | } else if ( ! ftestPrintFlag ) |
---|
194 | cout << "(" << result << ")" << endl; |
---|
195 | } |
---|
196 | //}}} |
---|
197 | |
---|
198 | //{{{ void ftestPrintResult ( const char * resultName, const CFFList & result ) |
---|
199 | //{{{ docu |
---|
200 | // |
---|
201 | // ftestPrintResult() - print a list of canonical form factors |
---|
202 | // |
---|
203 | //}}} |
---|
204 | void |
---|
205 | ftestPrintResult ( const char * resultName, const CFFList & result ) |
---|
206 | { |
---|
207 | CFFListIterator I; |
---|
208 | |
---|
209 | if ( ftestPrintResultFlag ) { |
---|
210 | cout << "Result:\t\t" << resultName << ":" << endl; |
---|
211 | for ( I = result; I.hasItem(); I++ ) |
---|
212 | cout << I.getItem() << endl; |
---|
213 | } else if ( ! ftestPrintFlag ) |
---|
214 | for ( I = result; I.hasItem(); I++ ) |
---|
215 | cout << "(" << I.getItem() << ")" << endl; |
---|
216 | } |
---|
217 | //}}} |
---|
218 | |
---|
219 | //{{{ void ftestPrintResult ( const char * resultName, const int result ) |
---|
220 | //{{{ docu |
---|
221 | // |
---|
222 | // ftestPrintResult() - print an integer. |
---|
223 | // |
---|
224 | //}}} |
---|
225 | void |
---|
226 | ftestPrintResult ( const char * resultName, const int result ) |
---|
227 | { |
---|
228 | if ( ftestPrintResultFlag ) |
---|
229 | cout << "Result:\t\t" << resultName << ": " << result << endl; |
---|
230 | else if ( ! ftestPrintFlag ) |
---|
231 | cout << result << endl; |
---|
232 | } |
---|
233 | //}}} |
---|