File: das-test.cc

package info (click to toggle)
libdap 3.21.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,252 kB
  • sloc: cpp: 46,310; sh: 44,047; xml: 23,535; ansic: 19,973; yacc: 2,505; exp: 1,544; makefile: 581; lex: 309; fortran: 8
file content (333 lines) | stat: -rw-r--r-- 10,322 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// (c) COPYRIGHT URI/MIT 1994-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
//      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>

// Test the DAS class.
// Read attributes from one or more files, printing the resulting table to
// stdout. If a file is named `-' read from stdin for that file. The option
// `-d' causes new/delete run-time debugging to be turned on.
//
// jhrg 7/25/94

#include "config.h"

#include <GetOpt.h>
#include <cstdlib>
#include <string>

#define YYSTYPE char *

#include "DAS.h"
#include "Error.h"
#include "das.tab.hh"

using namespace libdap;

void plain_driver(DAS &das, bool deref_alias);
void load_attr_table(AttrTable at);
void load_attr_table_ptr(AttrTable *atp);
void parser_driver(DAS &das, bool deref_alias, bool as_xml);
void test_scanner();

int daslex();

extern int dasdebug;
const char *prompt = "das-test: ";
const char *version = "version 1.19";

using namespace std;

void usage(string name) {
    fprintf(stderr, "usage: %s %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n", name.c_str(),
            "[-v] [-s] [-d] [-c] [-p] [rx] {< in-file > out-file}", "s: Test the DAS scanner.",
            "p: Scan and parse from <in-file>; print to <out-file>.", "c: Test building the DAS from C++ code.",
            "v: Print the version of das-test and exit.", "d: Print parser debugging information.",
            "r: Print the DAS with aliases deReferenced.", "x: Print as xml.");
}

int main(int argc, char *argv[]) {

    GetOpt getopt(argc, argv, "scpvdrx");
    int option_char;
    bool parser_test = false;
    bool scanner_test = false;
    bool code_test = false;
    bool deref_alias = false;
    bool as_xml = false;
    while ((option_char = getopt()) != -1)
        switch (option_char) {
        case 'p':
            parser_test = true;
            break;
        case 's':
            scanner_test = true;
            break;
        case 'c':
            code_test = true;
            break;
        case 'v':
            fprintf(stderr, "%s: %s\n", argv[0], version);
            return 0;
        case 'd':
            dasdebug = 1;
            break;
        case 'r':
            deref_alias = true;
            break;
        case 'x':
            as_xml = true;
            break;
        case '?':
        default:
            usage(argv[0]);
            return 1;
        }

    DAS das;

    if (!parser_test && !scanner_test && !code_test) {
        usage(argv[0]);
        return 1;
    }

    try {
        if (parser_test)
            parser_driver(das, deref_alias, as_xml);

        if (scanner_test)
            test_scanner();

        if (code_test)
            plain_driver(das, deref_alias);
    } catch (Error &e) {
        cerr << "Caught Error object:" << endl;
        cerr << e.get_error_message() << endl;
        return 1;
    }

    return 0;
}

void test_scanner() {
    int tok;

    fprintf(stdout, "%s", prompt); // first prompt
    fflush(stdout);
    while ((tok = daslex())) {
        switch (tok) {
        case SCAN_ATTR:
            fprintf(stdout, "ATTR\n");
            break;
        case SCAN_ALIAS:
            fprintf(stdout, "ALIAS\n");
            break;
        case SCAN_WORD:
            fprintf(stdout, "WORD=%s\n", daslval);
            break;

        case SCAN_BYTE:
            fprintf(stdout, "BYTE\n");
            break;
        case SCAN_INT16:
            fprintf(stdout, "INT16\n");
            break;
        case SCAN_UINT16:
            fprintf(stdout, "UINT16\n");
            break;
        case SCAN_INT32:
            fprintf(stdout, "INT32\n");
            break;
        case SCAN_UINT32:
            fprintf(stdout, "UINT32\n");
            break;
        case SCAN_FLOAT32:
            fprintf(stdout, "FLOAT32\n");
            break;
        case SCAN_FLOAT64:
            fprintf(stdout, "FLOAT64\n");
            break;
        case SCAN_STRING:
            fprintf(stdout, "STRING\n");
            break;
        case SCAN_URL:
            fprintf(stdout, "URL\n");
            break;

        case SCAN_XML:
            fprintf(stdout, "OtherXML\n");
            break;

        case '{':
            fprintf(stdout, "Left Brace\n");
            break;
        case '}':
            fprintf(stdout, "Right Brace\n");
            break;
        case ';':
            fprintf(stdout, "Semicolon\n");
            break;
        case ',':
            fprintf(stdout, "Comma\n");
            break;

        default:
            fprintf(stdout, "Error: Unrecognized input\n");
            break;
        }
        fprintf(stdout, "%s", prompt); // print prompt after output
        fflush(stdout);
    }
}

void parser_driver(DAS &das, bool deref_alias, bool as_xml) {
    das.parse();

    if (as_xml) {
        das.get_top_level_attributes()->print_xml(stdout, "    ");
    } else
        das.print(stdout, deref_alias);
}

// Given a DAS, add some stuff to it.

void plain_driver(DAS &das, bool deref_alias) {
    string name = "test";
    AttrTable *atp = new AttrTable;
    load_attr_table_ptr(atp);
#if 0
    AttrTable *dummy = das.get_table(name);
#endif
    das.add_table(name, atp);

    name = "test2";
    atp = new AttrTable;
    load_attr_table_ptr(atp);
    das.add_table(name, atp);

    das.print(stdout, deref_alias);
}

// stuff an AttrTable full of values. Also, print it out.

void load_attr_table(AttrTable at) {
    at.append_attr("month", "String", "Feb");
    at.append_attr("month", "String", "Feb");

    at.append_attr("month_a", "String", "Jan");
    at.append_attr("month_a", "String", "Feb");
    at.append_attr("month_a", "String", "Mar");

    at.append_attr("Date", "Int32", "12345");
    at.append_attr("day", "Int32", "01");
    at.append_attr("Time", "Float64", "3.1415");

    fprintf(stdout, "Using the iterator:\n");
    for (AttrTable::Attr_iter p2 = at.attr_begin(); p2 != at.attr_end(); p2++) {
        fprintf(stdout, "%s %s ", at.get_name(p2).c_str(), at.get_type(p2).c_str());
        for (unsigned i = 0; i < at.get_attr_num(p2); ++i)
            fprintf(stdout, "%s ", at.get_attr(p2, i).c_str());
        fprintf(stdout, "\n");
    }

    string name = "month";
    fprintf(stdout, "Using String: %s %s %s\n", at.get_type(name).c_str(), at.get_attr(name, 0).c_str(),
            at.get_attr(name, 1).c_str());
    fprintf(stdout, "Using char *: %s %s %s\n", at.get_type("month").c_str(), at.get_attr("month", 0).c_str(),
            at.get_attr("month", 1).c_str());

    at.del_attr("month");

    fprintf(stdout, "After deletion:\n");
    for (AttrTable::Attr_iter p3 = at.attr_begin(); p3 != at.attr_end(); p3++) {
        fprintf(stdout, "%s %s ", at.get_name(p3).c_str(), at.get_type(p3).c_str());
        for (unsigned i = 0; i < at.get_attr_num(p3); ++i)
            fprintf(stdout, "%s ", at.get_attr(p3, i).c_str());
        fprintf(stdout, "\n");
    }

    at.print(stdout);

    fprintf(stdout, "After print:\n");
    for (AttrTable::Attr_iter p4 = at.attr_begin(); p4 != at.attr_end(); p4++) {
        fprintf(stdout, "%s %s ", at.get_name(p4).c_str(), at.get_type(p4).c_str());
        for (unsigned i = 0; i < at.get_attr_num(p4); ++i)
            fprintf(stdout, "%s ", at.get_attr(p4, i).c_str());
        fprintf(stdout, "\n");
    }
}

// OK, now try it with a dymanic AttrTable

void load_attr_table_ptr(AttrTable *at) {
    at->append_attr("month", "String", "Feb");
    at->append_attr("month", "String", "Feb");

    at->append_attr("month_a", "String", "Jan");
    at->append_attr("month_a", "String", "Feb");
    at->append_attr("month_a", "String", "Mar");

    at->append_attr("Date", "Int32", "12345");
    at->append_attr("day", "Int32", "01");
    at->append_attr("Time", "Float64", "3.1415");

    fprintf(stdout, "Using the iterator:\n");
    for (AttrTable::Attr_iter p2 = at->attr_begin(); p2 != at->attr_end(); p2++) {
        fprintf(stdout, "%s %s ", at->get_name(p2).c_str(), at->get_type(p2).c_str());
        for (unsigned i = 0; i < at->get_attr_num(p2); ++i)
            fprintf(stdout, "%s ", at->get_attr(p2, i).c_str());
        fprintf(stdout, "\n");
    }

    string name = "month";
    fprintf(stdout, "Using String: %s %s %s\n", at->get_type(name).c_str(), at->get_attr(name, 0).c_str(),
            at->get_attr(name, 1).c_str());
    fprintf(stdout, "Using char *: %s %s %s\n", at->get_type("month").c_str(), at->get_attr("month", 0).c_str(),
            at->get_attr("month", 1).c_str());

    at->del_attr("month");

    fprintf(stdout, "After deletion:\n");
    for (AttrTable::Attr_iter p3 = at->attr_begin(); p3 != at->attr_end(); p3++) {
        fprintf(stdout, "%s %s ", at->get_name(p3).c_str(), at->get_type(p3).c_str());
        for (unsigned i = 0; i < at->get_attr_num(p3); ++i)
            fprintf(stdout, "%s ", at->get_attr(p3, i).c_str());
        fprintf(stdout, "\n");
    }

    at->print(stdout);

    fprintf(stdout, "After print:\n");
    for (AttrTable::Attr_iter p4 = at->attr_begin(); p4 != at->attr_end(); p4++) {
        fprintf(stdout, "%s %s ", at->get_name(p4).c_str(), at->get_type(p4).c_str());
        for (unsigned i = 0; i < at->get_attr_num(p4); ++i)
            fprintf(stdout, "%s ", at->get_attr(p4, i).c_str());
        fprintf(stdout, "\n");
    }
}