1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 1994-1999
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 //      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>
31 
32 // Test the DAS class.
33 // Read attributes from one or more files, printing the resulting table to
34 // stdout. If a file is named `-' read from stdin for that file. The option
35 // `-d' causes new/delete run-time debugging to be turned on.
36 //
37 // jhrg 7/25/94
38 
39 #include "config.h"
40 
41 #include <cstdlib>
42 #include <string>
43 #include <GetOpt.h>
44 
45 #define YYSTYPE char *
46 
47 #include "DAS.h"
48 #include "das.tab.hh"
49 #include "Error.h"
50 
51 using namespace libdap ;
52 
53 void plain_driver(DAS &das, bool deref_alias);
54 void load_attr_table(AttrTable at);
55 void load_attr_table_ptr(AttrTable *atp);
56 void parser_driver(DAS &das, bool deref_alias, bool as_xml);
57 void test_scanner();
58 
59 int daslex();
60 
61 extern int dasdebug;
62 const char *prompt = "das-test: ";
63 const char *version = "version 1.19";
64 
65 using namespace std;
66 
67 void
usage(string name)68 usage(string name)
69 {
70     fprintf( stderr, "usage: %s %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
71 		     name.c_str(),
72 		     "[-v] [-s] [-d] [-c] [-p] [rx] {< in-file > out-file}",
73 		     "s: Test the DAS scanner.",
74 		     "p: Scan and parse from <in-file>; print to <out-file>.",
75 		     "c: Test building the DAS from C++ code.",
76 		     "v: Print the version of das-test and exit.",
77 		     "d: Print parser debugging information.",
78 		     "r: Print the DAS with aliases deReferenced.",
79 		     "x: Print as xml.") ;
80 }
81 
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 
85     GetOpt getopt (argc, argv, "scpvdrx");
86     int option_char;
87     bool parser_test = false;
88     bool scanner_test = false;
89     bool code_test = false;
90     bool deref_alias = false;
91     bool as_xml = false;
92     while ((option_char = getopt ()) != -1)
93 	switch (option_char)
94 	  {
95 	    case 'p':
96 	      parser_test = true;
97 	      break;
98 	    case 's':
99 	      scanner_test = true;
100 	      break;
101 	    case 'c':
102 	      code_test = true;
103 	      break;
104 	    case 'v':
105 	      fprintf( stderr, "%s: %s\n", argv[0], version ) ;
106 	      return 0;
107 	    case 'd':
108 	      dasdebug = 1;
109 	      break;
110 	    case 'r':
111 	      deref_alias = true;
112 	      break;
113 	    case 'x':
114 	        as_xml = true;
115 	        break;
116 	    case '?':
117 	    default:
118 	      usage(argv[0]);
119               return 1;
120 	  }
121 
122     DAS das;
123 
124     if (!parser_test && !scanner_test && !code_test) {
125 	usage(argv[0]);
126 	return 1;
127     }
128 
129     try {
130       if (parser_test)
131 	parser_driver(das, deref_alias, as_xml);
132 
133       if (scanner_test)
134 	test_scanner();
135 
136       if (code_test)
137 	plain_driver(das, deref_alias);
138     }
139     catch (Error &e) {
140 	cerr << "Caught Error object:" << endl;
141 	cerr << e.get_error_message() << endl;
142 	return 1;
143     }
144 
145     return 0;
146 }
147 
148 void
test_scanner()149 test_scanner()
150 {
151     int tok;
152 
153     fprintf( stdout, "%s", prompt ) ; // first prompt
154     fflush( stdout ) ;
155     while ((tok = daslex())) {
156 	switch (tok) {
157 	  case SCAN_ATTR:
158 	    fprintf( stdout, "ATTR\n" ) ;
159 	    break;
160 	  case SCAN_ALIAS:
161 	    fprintf( stdout, "ALIAS\n" ) ;
162 	    break;
163 	  case SCAN_WORD:
164 	    fprintf( stdout, "WORD=%s\n", daslval ) ;
165 	    break;
166 
167 	  case SCAN_BYTE:
168 	    fprintf( stdout, "BYTE\n" ) ;
169 	    break;
170 	  case SCAN_INT16:
171 	    fprintf( stdout, "INT16\n" ) ;
172 	    break;
173 	  case SCAN_UINT16:
174 	    fprintf( stdout, "UINT16\n" ) ;
175 	    break;
176 	  case SCAN_INT32:
177 	    fprintf( stdout, "INT32\n" ) ;
178 	    break;
179 	  case SCAN_UINT32:
180 	    fprintf( stdout, "UINT32\n" ) ;
181 	    break;
182 	  case SCAN_FLOAT32:
183 	    fprintf( stdout, "FLOAT32\n" ) ;
184 	    break;
185 	  case SCAN_FLOAT64:
186 	    fprintf( stdout, "FLOAT64\n" ) ;
187 	    break;
188 	  case SCAN_STRING:
189 	    fprintf( stdout, "STRING\n" ) ;
190 	    break;
191           case SCAN_URL:
192             fprintf( stdout, "URL\n" ) ;
193             break;
194 
195           case SCAN_XML:
196             fprintf( stdout, "OtherXML\n" ) ;
197             break;
198 
199 	  case '{':
200 	    fprintf( stdout, "Left Brace\n" ) ;
201 	    break;
202 	  case '}':
203 	    fprintf( stdout, "Right Brace\n" ) ;
204 	    break;
205 	  case ';':
206 	    fprintf( stdout, "Semicolon\n" ) ;
207 	    break;
208 	  case ',':
209 	    fprintf( stdout, "Comma\n" ) ;
210 	    break;
211 
212 	  default:
213 	    fprintf( stdout, "Error: Unrecognized input\n" ) ;
214 	    break;
215 	}
216 	fprintf( stdout, "%s", prompt ) ; // print prompt after output
217 	fflush( stdout ) ;
218     }
219 }
220 
221 
222 void
parser_driver(DAS & das,bool deref_alias,bool as_xml)223 parser_driver(DAS &das, bool deref_alias, bool as_xml)
224 {
225     das.parse();
226 
227     if (as_xml) {
228         das.get_top_level_attributes()->print_xml(stdout, "    ");
229     }
230     else
231         das.print(stdout, deref_alias);
232 }
233 
234 // Given a DAS, add some stuff to it.
235 
236 void
plain_driver(DAS & das,bool deref_alias)237 plain_driver(DAS &das, bool deref_alias)
238 {
239     string name = "test";
240     AttrTable *atp = new AttrTable;
241     load_attr_table_ptr(atp);
242 #if 0
243     AttrTable *dummy = das.get_table(name);
244 #endif
245     das.add_table(name, atp);
246 
247     name = "test2";
248     atp = new AttrTable;
249     load_attr_table_ptr(atp);
250     das.add_table(name, atp);
251 
252     das.print(stdout, deref_alias);
253 }
254 
255 // stuff an AttrTable full of values. Also, print it out.
256 
257 void
load_attr_table(AttrTable at)258 load_attr_table(AttrTable at)
259 {
260     at.append_attr("month", "String", "Feb");
261     at.append_attr("month", "String", "Feb");
262 
263     at.append_attr("month_a", "String", "Jan");
264     at.append_attr("month_a", "String", "Feb");
265     at.append_attr("month_a", "String", "Mar");
266 
267     at.append_attr("Date", "Int32", "12345");
268     at.append_attr("day", "Int32", "01");
269     at.append_attr("Time", "Float64", "3.1415");
270 
271     fprintf( stdout, "Using the iterator:\n" ) ;
272     for (AttrTable::Attr_iter p2 = at.attr_begin(); p2 != at.attr_end(); p2++)
273     {
274 		fprintf( stdout, "%s %s ", at.get_name(p2).c_str(),
275 			at.get_type(p2).c_str() ) ;
276 		for (unsigned i = 0; i < at.get_attr_num(p2); ++i)
277 			fprintf( stdout, "%s ", at.get_attr(p2, i).c_str() ) ;
278 		fprintf( stdout, "\n" ) ;
279     }
280 
281     string name = "month";
282     fprintf( stdout, "Using String: %s %s %s\n",
283 		     at.get_type(name).c_str(),
284 		     at.get_attr(name, 0).c_str(),
285 		     at.get_attr(name, 1).c_str()) ;
286     fprintf( stdout, "Using char *: %s %s %s\n",
287 		     at.get_type("month").c_str(),
288 		     at.get_attr("month", 0).c_str(),
289 		     at.get_attr("month", 1).c_str() ) ;
290 
291     at.del_attr("month");
292 
293     fprintf( stdout, "After deletion:\n" ) ;
294     for (AttrTable::Attr_iter p3 = at.attr_begin(); p3 != at.attr_end(); p3++)
295     {
296 		fprintf( stdout, "%s %s ", at.get_name(p3).c_str(),
297 			at.get_type(p3).c_str() ) ;
298 		for (unsigned i = 0; i < at.get_attr_num(p3); ++i)
299 			fprintf( stdout, "%s ", at.get_attr(p3, i).c_str() ) ;
300 		fprintf( stdout, "\n" ) ;
301     }
302 
303     at.print(stdout);
304 
305     fprintf( stdout, "After print:\n" ) ;
306     for (AttrTable::Attr_iter p4 = at.attr_begin(); p4 != at.attr_end(); p4++)
307     {
308 	fprintf( stdout, "%s %s ", at.get_name(p4).c_str(),
309 		at.get_type(p4).c_str() ) ;
310 	for (unsigned i = 0; i < at.get_attr_num(p4); ++i)
311 	     fprintf( stdout, "%s ", at.get_attr(p4, i).c_str() ) ;
312 	fprintf( stdout, "\n" ) ;
313     }
314 }
315 
316 // OK, now try it with a dymanic AttrTable
317 
318 void
load_attr_table_ptr(AttrTable * at)319 load_attr_table_ptr(AttrTable *at)
320 {
321     at->append_attr("month", "String", "Feb");
322     at->append_attr("month", "String", "Feb");
323 
324     at->append_attr("month_a", "String", "Jan");
325     at->append_attr("month_a", "String", "Feb");
326     at->append_attr("month_a", "String", "Mar");
327 
328     at->append_attr("Date", "Int32", "12345");
329     at->append_attr("day", "Int32", "01");
330     at->append_attr("Time", "Float64", "3.1415");
331 
332     fprintf( stdout, "Using the iterator:\n" ) ;
333     for (AttrTable::Attr_iter p2 = at->attr_begin(); p2 != at->attr_end(); p2++)
334     {
335 		fprintf( stdout, "%s %s ", at->get_name(p2).c_str(),
336 			at->get_type(p2).c_str() ) ;
337 		for (unsigned i = 0; i < at->get_attr_num(p2); ++i)
338 			fprintf( stdout, "%s ", at->get_attr(p2, i).c_str() ) ;
339 		fprintf( stdout, "\n" ) ;
340     }
341 
342     string name = "month";
343     fprintf( stdout, "Using String: %s %s %s\n",
344 		     at->get_type(name).c_str(),
345 		     at->get_attr(name, 0).c_str(),
346 		     at->get_attr(name, 1).c_str() ) ;
347     fprintf( stdout, "Using char *: %s %s %s\n",
348 		     at->get_type("month").c_str(),
349 		     at->get_attr("month", 0).c_str(),
350 		     at->get_attr("month", 1).c_str() ) ;
351 
352     at->del_attr("month");
353 
354     fprintf( stdout, "After deletion:\n" ) ;
355     for (AttrTable::Attr_iter p3 = at->attr_begin(); p3 != at->attr_end(); p3++)
356     {
357 		fprintf( stdout, "%s %s ", at->get_name(p3).c_str(),
358 			at->get_type(p3).c_str() ) ;
359 		for (unsigned i = 0; i < at->get_attr_num(p3); ++i)
360 			fprintf( stdout, "%s ", at->get_attr(p3, i).c_str() ) ;
361 		fprintf( stdout, "\n" ) ;
362     }
363 
364     at->print(stdout);
365 
366     fprintf( stdout, "After print:\n" ) ;
367     for (AttrTable::Attr_iter p4 = at->attr_begin(); p4 !=at->attr_end(); p4++)
368     {
369 		fprintf( stdout, "%s %s ", at->get_name(p4).c_str(),
370 			at->get_type(p4).c_str() ) ;
371 		for (unsigned i = 0; i < at->get_attr_num(p4); ++i)
372 			fprintf( stdout, "%s ", at->get_attr(p4, i).c_str() ) ;
373 		fprintf( stdout, "\n" ) ;
374     }
375 }
376 
377