64static void usage(
const string &) {
65 const char *message = R
"(
66 Usage: getdap4 [dD vVmzsM][-c <expr>][-m <num>] <url> [<url> ...]
67 getdap4 [dD vVmzsM][-c <expr>][-m <num>] <file> [<file> ...]
69 In the first form of the command, dereference the URL and perform
70 the requested operations. This includes routing the returned
71 information through the DAP processing library (parsing the
72 returned objects, et c.). If none of d, or D are used with a URL,
73 then the DAP library routines are NOT used and the URLs contents
74 are dumped to standard output.
76 Note: If the URL contains a query string the query string will be
77 preserved in the request. However, if the query string contains
78 DAP4 keys they may interfere with the operation of getdap4. A
79 warning will be written to stderr when getdap4 identifies the
80 presence of a DAP4 query key in the submitted URL's query string.
82 In the second form of the command, assume the files are DAP4 data
83 responses (stored in files or read from pipes)
86 d: For each URL, get the (DAP4) DMR object. Does not get data.
87 D: For each URL, get the DAP4 Data response.
90 V: Version of this client
91 i: For each URL, get the server version.
92 m: Request the same URL <num> times.
93 z: Ask the server to compress data.
94 s: Print Sequences using numbered rows.
95 M: Assume data read from a file has no MIME headers; use only
98 c: <expr> is a constraint expression. Used with -d/D
99 NB: You can use a `?' for the CE also.
100 S: Used in conjunction with -d and will report the total size
101 of the data referenced in the DMR.)";
103 cerr << message << endl;
109 fprintf(stderr,
"getdap4: Whoa!!! Null stream pointer.\n");
116 while (fp && !feof(fp) && fread(&c, 1, 1, fp))
122static void read_response_from_file(
D4Connect *url,
DMR &dmr,
Response &r,
bool mime_headers,
bool get_dap4_data,
130 throw Error(
"Only supports Data or DMR responses");
137 throw Error(
"Only supports Data or DMR responses");
141static void print_group_data(
D4Group *g,
bool print_rows =
false) {
146 (*i)->print_val(cout);
150 print_group_data(*gi, print_rows);
154static void print_data(
DMR &dmr,
bool print_rows =
false) {
155 cout <<
"The data:" << endl;
159 print_group_data(g, print_rows);
161 cout << endl << flush;
175 unsigned long long w = 0;
177 for (
auto var_itr = grp->
var_begin(); var_itr != grp->
var_end(); var_itr++) {
179 if ((*var_itr)->send_p())
180 w += (*var_itr)->width(constrained);
182 w += (*var_itr)->width(constrained);
185 for (
auto grp_itr = grp->
grp_begin(); grp_itr != grp->
grp_end(); grp_itr++) {
186 w +=
get_size(*grp_itr, constrained);
194int main(
int argc,
char *argv[]) {
197 bool get_dmr =
false;
198 bool get_dap4_data =
false;
199 bool verbose =
false;
200 bool accept_deflate =
false;
201 bool print_rows =
false;
202 bool mime_headers =
true;
203 bool report_errors =
false;
205 int dap_client_major = 4;
206 int dap_client_minor = 0;
208 bool compute_size =
false;
211 _setmode(_fileno(stdout), _O_BINARY);
214 while ((option_char = getopt(argc, argv,
"dDvVrm:Mzsc:S")) != -1) {
215 switch (option_char) {
220 get_dap4_data =
true;
226 cerr <<
"getdap4 version: " <<
version << endl;
232 report_errors =
true;
235 times = atoi(optarg);
238 accept_deflate =
true;
244 mime_headers =
false;
261 for (
int i = optind; i < argc; ++i) {
263 cerr <<
"Fetching: " << argv[i] << endl;
265 string name = argv[i];
272 if (dap_client_major > 2)
277 cerr <<
"Assuming " << argv[i] <<
" is a file that contains a response object; decoding." << endl;
283 if (strcmp(argv[i],
"-") == 0) {
287 throw Error(
"Could not open standard input.");
289 read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
291 fstream f(argv[i], std::ios_base::in);
292 if (!f.is_open() || f.bad() || f.eof())
293 throw Error((
string)
"Could not open: " + argv[i]);
297 read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
302 <<
" Server version: " << url->
get_version().c_str() << endl;
310 print_data(dmr, print_rows);
318 }
else if (get_dmr) {
319 for (
int j = 0; j < times; ++j) {
328 cout <<
"DMR:" << endl;
335 cout <<
"DMR References " <<
get_size(dmr) <<
" bytes of data," << endl;
344 }
else if (get_dap4_data) {
345 for (
int j = 0; j < times; ++j) {
354 cout <<
"DMR:" << endl;
361 print_data(dmr, print_rows);
376 if (dap_client_major > 2)
379 string url_string = argv[i];
380 for (
int j = 0; j < times; ++j) {
385 copy(headers.begin(), headers.end(), ostream_iterator<string>(cout,
"\n"));
412 cerr <<
"Exiting." << endl;
414 }
catch (exception &e) {
416 cerr <<
"C++ library exception: " << e.
what() << endl;
417 cerr <<
"Exiting." << endl;
#define malformed_expr
(400)
std::vector< BaseType * >::iterator Vars_iter
virtual void read_dmr_no_mime(DMR &dmr, Response &rs)
std::string get_protocol()
virtual void read_data_no_mime(DMR &data, Response &rs)
virtual void request_dap4_data(DMR &dmr, const std::string expr="")
virtual void read_data(DMR &data, Response &rs)
void set_accept_deflate(bool deflate)
virtual void request_dmr(DMR &dmr, const std::string expr="")
virtual void read_dmr(DMR &dmr, Response &rs)
void set_xdap_protocol(int major, int minor)
std::string get_version()
groupsIter grp_begin()
Get an iterator to the start of the values.
groupsIter grp_end()
Get an iterator to the end of the values.
vector< D4Group * >::iterator groupsIter
virtual void print_val_by_rows(ostream &out, string space="", bool print_decl_p=true, bool print_row_numbers=true)
void print_dap4(XMLWriter &xml, bool constrained=false)
A class for error processing.
const char * what() const noexcept override
The pointer is valid only for the lifetime of the Error instance. jhrg 9/22/20.
ErrorCode get_error_code() const
std::string get_error_message() const
void set_accept_deflate(bool deflate)
HTTPResponse * fetch_url(const std::string &url)
virtual std::vector< std::string > & get_headers()
static RCReader * instance()
virtual FILE * get_stream() const
Encapsulate a response read from stdin.
virtual std::istream * get_cpp_stream() const
#define DAP_PROTOCOL_VERSION
unsigned long long get_size(D4Group *grp, bool constrained=false)
int main(int argc, char *argv[])
top level DAP object to house generic methods