1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: ftest_io.cc,v 1.13 1998-04-06 11:08:28 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 | //{{{ void ftestReadString ( const char * canFormSpec, CanonicalForm & f ) |
---|
25 | //{{{ docu |
---|
26 | // |
---|
27 | // ftestReadString() - read canonical form `f' from `canFormSpec'. |
---|
28 | // |
---|
29 | //}}} |
---|
30 | void |
---|
31 | ftestReadString ( const char * canFormSpec, CanonicalForm & f ) |
---|
32 | { |
---|
33 | canFormSpec = ftestSkipBlancs( canFormSpec ); |
---|
34 | |
---|
35 | // check for a single minus and read from stdin |
---|
36 | if ( *canFormSpec == '-' ) { |
---|
37 | const char * tokenCursor = ftestSkipBlancs( canFormSpec+1 ); |
---|
38 | if ( ! *tokenCursor ) { |
---|
39 | cin >> f; |
---|
40 | return; |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | // create terminated CanonicalForm |
---|
45 | char * terminatedSpec = new char[ strlen( canFormSpec )+2 ]; |
---|
46 | char * cursor = terminatedSpec; |
---|
47 | while ( *canFormSpec ) { |
---|
48 | switch ( *canFormSpec ) { |
---|
49 | case '.': *cursor = '*'; break; |
---|
50 | case '{': *cursor = '('; break; |
---|
51 | case '}': *cursor = ')'; break; |
---|
52 | default: *cursor = *canFormSpec; break; |
---|
53 | } |
---|
54 | canFormSpec++; cursor++; |
---|
55 | } |
---|
56 | *cursor++ = ';'; |
---|
57 | *cursor = '\0'; |
---|
58 | |
---|
59 | // read f from string |
---|
60 | istrstream( terminatedSpec ) >> f; |
---|
61 | delete [] terminatedSpec; |
---|
62 | } |
---|
63 | //}}} |
---|
64 | |
---|
65 | //{{{ void ftestReadString ( const char * varSpec, Variable & v ) |
---|
66 | //{{{ docu |
---|
67 | // |
---|
68 | // ftestReadString() - read variable `v' from `varSpec'. |
---|
69 | // |
---|
70 | //}}} |
---|
71 | void |
---|
72 | ftestReadString ( const char * varSpec, Variable & v ) |
---|
73 | { |
---|
74 | varSpec = ftestSkipBlancs( varSpec ); |
---|
75 | |
---|
76 | if ( isalpha( *varSpec ) ) |
---|
77 | v = Variable( *varSpec ); |
---|
78 | else if ( isdigit( *varSpec ) ) |
---|
79 | v = Variable(); |
---|
80 | else |
---|
81 | ftestError( CommandlineError, |
---|
82 | "variable expected at `%s'\n", varSpec ); |
---|
83 | |
---|
84 | varSpec = ftestSkipBlancs( varSpec+1 ); |
---|
85 | if ( *varSpec ) |
---|
86 | ftestError( CommandlineError, |
---|
87 | "extra characters after var spec `%s'\n", varSpec ); |
---|
88 | } |
---|
89 | //}}} |
---|
90 | |
---|
91 | //{{{ void ftestReadString ( const char * intSpec, int & i ) |
---|
92 | //{{{ docu |
---|
93 | // |
---|
94 | // ftestReadString() - read integer `i' from `intSpec'. |
---|
95 | // |
---|
96 | //}}} |
---|
97 | void |
---|
98 | ftestReadString ( const char * intSpec, int & i ) |
---|
99 | { |
---|
100 | const char * tokenCursor; |
---|
101 | |
---|
102 | i = (int)strtol( intSpec, (char**)&tokenCursor, 0 ); |
---|
103 | |
---|
104 | // do error checks |
---|
105 | if ( intSpec == tokenCursor ) |
---|
106 | ftestError( CommandlineError, |
---|
107 | "integer expected at `%s'\n", intSpec ); |
---|
108 | |
---|
109 | // check for extra characters after skipping blancs |
---|
110 | intSpec = ftestSkipBlancs( tokenCursor ); |
---|
111 | if ( *intSpec ) |
---|
112 | ftestError( CommandlineError, |
---|
113 | "extra characters after int spec `%s'\n", intSpec ); |
---|
114 | } |
---|
115 | //}}} |
---|
116 | |
---|
117 | //{{{ void ftestReadString ( const char * boolSpec, bool & b ) |
---|
118 | //{{{ docu |
---|
119 | // |
---|
120 | // ftestReadString() - read boolean `b' from `boolSpec'. |
---|
121 | // |
---|
122 | //}}} |
---|
123 | void |
---|
124 | ftestReadString ( const char * boolSpec, bool & b ) |
---|
125 | { |
---|
126 | // skip blancs |
---|
127 | boolSpec = ftestSkipBlancs( boolSpec ); |
---|
128 | |
---|
129 | // look for "true" or "false" |
---|
130 | const char * tokenCursor = ftestSubStr( "true", boolSpec ); |
---|
131 | if ( boolSpec != tokenCursor ) |
---|
132 | b = true; |
---|
133 | else { |
---|
134 | tokenCursor = ftestSubStr( "false", boolSpec ); |
---|
135 | b = false; |
---|
136 | } |
---|
137 | |
---|
138 | // do error checks |
---|
139 | if ( boolSpec == tokenCursor ) |
---|
140 | ftestError( CommandlineError, |
---|
141 | "bool expected at `%s'\n", boolSpec ); |
---|
142 | |
---|
143 | // check for extra characters after skipping blancs |
---|
144 | boolSpec = ftestSkipBlancs( tokenCursor ); |
---|
145 | if ( *boolSpec ) |
---|
146 | ftestError( CommandlineError, |
---|
147 | "extra characters after bool spec `%s'\n", boolSpec ); |
---|
148 | } |
---|
149 | //}}} |
---|
150 | |
---|
151 | //{{{ void ftestPrintResult ( const char * resultName, const CanonicalForm & result ) |
---|
152 | //{{{ docu |
---|
153 | // |
---|
154 | // ftestPrintResult() - print a canonical form. |
---|
155 | // |
---|
156 | //}}} |
---|
157 | void |
---|
158 | ftestPrintResult ( const char * resultName, const CanonicalForm & result ) |
---|
159 | { |
---|
160 | if ( ftestPrintResultFlag ) { |
---|
161 | cout << "Result:\t\t" << resultName << ":" << endl; |
---|
162 | cout << result << endl; |
---|
163 | } else if ( ! ftestPrintFlag ) |
---|
164 | cout << "(" << result << ")" << endl; |
---|
165 | } |
---|
166 | //}}} |
---|
167 | |
---|
168 | //{{{ void ftestPrintResult ( const char * resultName, const CFFList & result ) |
---|
169 | //{{{ docu |
---|
170 | // |
---|
171 | // ftestPrintResult() - print a list of canonical form factors |
---|
172 | // |
---|
173 | //}}} |
---|
174 | void |
---|
175 | ftestPrintResult ( const char * resultName, const CFFList & result ) |
---|
176 | { |
---|
177 | CFFListIterator I; |
---|
178 | |
---|
179 | if ( ftestPrintResultFlag ) { |
---|
180 | cout << "Result:\t\t" << resultName << ":" << endl; |
---|
181 | for ( I = result; I.hasItem(); I++ ) |
---|
182 | cout << I.getItem() << endl; |
---|
183 | } else if ( ! ftestPrintFlag ) |
---|
184 | for ( I = result; I.hasItem(); I++ ) |
---|
185 | cout << "(" << I.getItem() << ")" << endl; |
---|
186 | } |
---|
187 | //}}} |
---|
188 | |
---|
189 | //{{{ void ftestPrintResult ( const char * resultName, const int result ) |
---|
190 | //{{{ docu |
---|
191 | // |
---|
192 | // ftestPrintResult() - print an integer. |
---|
193 | // |
---|
194 | //}}} |
---|
195 | void |
---|
196 | ftestPrintResult ( const char * resultName, const int result ) |
---|
197 | { |
---|
198 | if ( ftestPrintResultFlag ) |
---|
199 | cout << "Result:\t\t" << resultName << ": " << result << endl; |
---|
200 | else if ( ! ftestPrintFlag ) |
---|
201 | cout << result << endl; |
---|
202 | } |
---|
203 | //}}} |
---|
204 | |
---|
205 | //{{{ void ftestPrintResult ( const char * resultName, const bool result ) |
---|
206 | //{{{ docu |
---|
207 | // |
---|
208 | // ftestPrintResult() - print a boolean. |
---|
209 | // |
---|
210 | //}}} |
---|
211 | void |
---|
212 | ftestPrintResult ( const char * resultName, const bool result ) |
---|
213 | { |
---|
214 | const char * stringResult = result ? "true" : "false"; |
---|
215 | |
---|
216 | if ( ftestPrintResultFlag ) |
---|
217 | cout << "Result:\t\t" << resultName << ": " << stringResult << endl; |
---|
218 | else if ( ! ftestPrintFlag ) |
---|
219 | cout << stringResult << endl; |
---|
220 | } |
---|
221 | //}}} |
---|