54#include "StdinResponse.h"
62const char *version = CVER
" (" DVR
" DAP/" DAP_PROTOCOL_VERSION
")";
64extern int libdap::dods_keep_temps;
65extern int libdap::www_trace;
66extern int libdap::www_trace_extensive;
68void usage(
string name) {
69 cerr <<
"Usage: " << name << endl;
70 cerr <<
" [idaDxBzp vVkms][-c <expr>][-m <num>] <url> [<url> ...]" << endl;
71 cerr <<
" [M vVkms] <file> [<file> ...]" << endl;
73 cerr <<
"In the first form of the command, dereference the URL and" << endl;
74 cerr <<
"perform the requested operations. This includes routing" << endl;
75 cerr <<
"the returned information through the DAP processing" << endl;
76 cerr <<
"library (parsing the returned objects, et c.). If none" << endl;
77 cerr <<
"of a, d, or D are used with a URL, then the DAP library" << endl;
78 cerr <<
"routines are NOT used and the URLs contents are dumped" << endl;
79 cerr <<
"to standard output." << endl;
81 cerr <<
"In the second form of the command, assume the files are" << endl;
82 cerr <<
"DataDDS objects (stored in files or read from pipes)" << endl;
83 cerr <<
"and process them as if -D were given. In this case the" << endl;
84 cerr <<
"information *must* contain valid MIME header in order" << endl;
85 cerr <<
"to be processed." << endl;
87 cerr <<
"Options:" << endl;
88 cerr <<
" i: For each URL, get the server version." << endl;
89 cerr <<
" d: For each URL, get the the DDS." << endl;
90 cerr <<
" a: For each URL, get the the DAS." << endl;
91 cerr <<
" D: For each URL, get the the DataDDS." << endl;
92 cerr <<
" x: For each URL, get the (DAP2) DDX object. Does not get data." << endl;
93 cerr <<
" B: Build a DDX in getdap using the DDS and DAS." << endl;
94 cerr <<
" v: Verbose output." << endl;
95 cerr <<
" e: Extensive (or very) verbose." << endl;
96 cerr <<
" V: Version of this client; see 'i' for server version." << endl;
97 cerr <<
" c: <expr> is a constraint expression. Used with -D/X and -d/r" << endl;
98 cerr <<
" NB: You can use a `?' for the CE also." << endl;
99 cerr <<
" k: Keep temporary files created by libdap." << endl;
100 cerr <<
" m: Request the same URL <num> times." << endl;
101 cerr <<
" z: Ask the server to compress data." << endl;
102 cerr <<
" s: Print Sequences using numbered rows." << endl;
103 cerr <<
" M: Assume data read from a file has no MIME headers" << endl;
104 cerr <<
" (the default is to assume the headers are present)." << endl;
105 cerr <<
" p: Set DAP protocol to x.y" << endl;
108bool read_data(FILE *fp) {
110 fprintf(stderr,
"getdap: Whoa!!! Null stream pointer.\n");
117 while (fp && !feof(fp) && fread(&c, 1, 1, fp))
123static void print_data(
DDS &dds,
bool print_rows =
false) {
124 cout <<
"The data:" << endl;
128 if (print_rows && (*i)->type() == dods_sequence_c)
129 dynamic_cast<Sequence *
>(*i)->print_val_by_rows(cout);
134 cout << endl << flush;
137int main(
int argc,
char *argv[]) {
138 GetOpt getopt(argc, argv,
"idaDxrXBVvekc:m:zshM?Hp:t");
141 bool get_das =
false;
142 bool get_dds =
false;
143 bool get_data =
false;
144 bool get_ddx =
false;
145 bool build_ddx =
false;
146 bool get_version =
false;
147 bool verbose =
false;
148 bool accept_deflate =
false;
149 bool print_rows =
false;
150 bool mime_headers =
true;
152 int dap_client_major = 2;
153 int dap_client_minor = 0;
157 _setmode(_fileno(stdout), _O_BINARY);
160 while ((option_char = getopt()) != -1)
161 switch (option_char) {
175 fprintf(stderr,
"getdap version: %s\n", version);
187 www_trace_extensive = 1;
193 expr = getopt.optarg;
196 times = atoi(getopt.optarg);
202 accept_deflate =
true;
208 mime_headers =
false;
211 istringstream iss(getopt.optarg);
213 iss >> dap_client_major;
215 iss >> dap_client_minor;
231 for (
int i = getopt.optind; i < argc; ++i) {
233 fprintf(stderr,
"Fetching: %s\n", argv[i]);
235 string name = argv[i];
244 if (dap_client_major > 2)
247 if (url->is_local()) {
250 "Assuming that the argument %s is a file that contains a response object; decoding.\n",
260 if (strcmp(argv[i],
"-") == 0) {
263 if (!r->get_stream())
264 throw Error(
"Could not open standard input.");
271 r =
new Response(fopen(argv[i],
"r"), 0);
273 if (!r->get_stream())
274 throw Error(
string(
"The input source: ") +
string(argv[i]) +
275 string(
" could not be opened"));
289 fprintf(stderr,
"DAP version: %s, Server version: %s\n", url->
get_protocol().c_str(),
292 print_data(dds, print_rows);
296 else if (get_version) {
297 fprintf(stderr,
"DAP version: %s, Server version: %s\n", url->
request_protocol().c_str(),
302 for (
int j = 0; j < times; ++j) {
314 fprintf(stderr,
"DAP version: %s, Server version: %s\n", url->
get_protocol().c_str(),
317 fprintf(stderr,
"DAS:\n");
325 for (
int j = 0; j < times; ++j) {
338 fprintf(stderr,
"DAP version: %s, Server version: %s\n", url->
get_protocol().c_str(),
341 fprintf(stderr,
"DDS:\n");
349 for (
int j = 0; j < times; ++j) {
360 fprintf(stderr,
"DAP version: %s, Server version: %s\n", url->
get_protocol().c_str(),
363 fprintf(stderr,
"DDX:\n");
370 else if (build_ddx) {
371 for (
int j = 0; j < times; ++j) {
385 fprintf(stderr,
"DAP version: %s, Server version: %s\n", url->
get_protocol().c_str(),
388 fprintf(stderr,
"Client-built DDX:\n");
396 for (
int j = 0; j < times; ++j) {
400 DBG(cerr <<
"URL: " << url->
URL(
false) << endl);
401 DBG(cerr <<
"CE: " << expr << endl);
405 fprintf(stderr,
"DAP version: %s, Server version: %s\n", url->
get_protocol().c_str(),
408 print_data(dds, print_rows);
426 http.set_accept_deflate(accept_deflate);
428 if (dap_client_major > 2)
431 string url_string = argv[i];
432 for (
int j = 0; j < times; ++j) {
434 Response *r = http.fetch_url(url_string);
435 if (!read_data(r->get_stream())) {
454 }
catch (exception &e) {
455 cerr <<
"C++ library exception: " << e.what() << endl;
The basic data type for the DODS DAP types.
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Holds information about the link from a DAP2 client to a dataset.
void set_accept_deflate(bool deflate)
virtual void request_ddx(DDS &dds, string expr="")
Get the DDX from a server.
virtual void read_data_no_mime(DataDDS &data, Response *rs)
Read data from a file which does not have response MIME headers. This method is a companion to read_d...
void set_xdap_protocol(int major, int minor)
virtual string URL(bool CE=true)
Get the object's URL.
virtual void request_data(DataDDS &data, string expr="")
Get the DAS from a server.
virtual void request_das(DAS &das)
Get the DAS from a server.
virtual string request_protocol()
virtual void request_dds(DDS &dds, string expr="")
Get the DDS from a server.
virtual void read_data(DataDDS &data, Response *rs)
Read data which is preceded by MIME headers. This method works for both data dds and data ddx respons...
Hold attribute data for a DAP2 dataset.
virtual void print(FILE *out, bool dereference=false)
virtual void transfer_attributes(DAS *das)
void print(FILE *out)
Print the entire DDS to the specified file.
void print_xml(FILE *out, bool constrained, const string &blob="")
Vars_iter var_end()
Return an iterator.
A class for error processing.
std::string get_error_message() const
Encapsulate a response read from stdin.
top level DAP object to house generic methods