35 #include <Structure.h>
37 #include <D4Sequence.h>
42 #include "InternalErr.h"
44 #include "get_ascii_dap4.h"
46 namespace dap_asciival {
53 static void print_values_as_ascii(BaseType *btp,
bool print_name, ostream &strm, Crc32 &checksum);
54 static void print_sequence_header(D4Sequence *s, ostream &strm);
55 static void print_val_by_rows(D4Sequence *seq, ostream &strm, Crc32 &checksum);
67 static void print_array_vector(Array *a, ostream &strm,
bool print_name)
70 strm << a->FQN() <<
", " ;
74 if (a->dimension_size(a->dim_begin(),
true) > 0) {
75 int end = a->dimension_size(a->dim_begin(),
true) - 1;
77 for (
int i = 0; i < end; ++i) {
78 a->var(i)->print_val(strm,
"",
false );
81 a->var(end)->print_val(strm,
"",
false );
96 static int print_array_row(Array *a, ostream &strm,
int index,
int number)
100 for (
int i = 0; i < number; ++i) {
101 a->var(index++)->print_val(strm,
"",
false );
105 a->var(index++)->print_val(strm,
"",
false );
117 static bool increment_state(vector<int> *state,
const vector<int> &shape)
119 vector < int >::reverse_iterator state_riter;
120 vector < int >::const_reverse_iterator shape_riter;
121 for (state_riter = state->rbegin(), shape_riter = shape.rbegin();
122 state_riter < state->rend(); state_riter++, shape_riter++) {
123 if (*state_riter == *shape_riter - 1) {
127 *state_riter = *state_riter + 1;
135 static vector <int> get_shape_vector(Array *a,
size_t n)
137 if (n < 1 || n > a->dimensions(
true)) {
139 oss <<
"Attempt to get " << n <<
" dimensions from " << a->name() <<
" which has " << a->dimensions(
true) <<
" dimensions";
140 throw InternalErr(__FILE__, __LINE__, oss.str());
144 Array::Dim_iter p = a->dim_begin();
145 for (
unsigned i = 0; i < n && p != a->dim_end(); ++i, ++p) {
146 shape.push_back(a->dimension_size(p,
true));
155 static int get_nth_dim_size(Array *a,
size_t n)
157 if (n > a->dimensions(
true) - 1) {
159 oss <<
"Attempt to get dimension " << n <<
" from " << a->name() <<
" which has " << a->dimensions(
true) <<
" dimensions";
160 throw InternalErr(__FILE__, __LINE__, oss.str());
163 return a->dimension_size(a->dim_begin() + n,
true);
166 static void print_ndim_array(Array *a, ostream &strm,
bool )
169 int dims = a->dimensions(
true);
171 throw InternalErr(__FILE__, __LINE__,
"Dimension count is <= 1 while printing multidimensional array.");
175 vector<int> shape = get_shape_vector(a, dims - 1);
176 int rightmost_dim_size = get_nth_dim_size(a, dims - 1);
182 vector<int> state(dims - 1, 0);
190 for (
int i = 0; i < dims - 1; ++i) {
191 strm <<
"[" << state[i] <<
"]" ;
195 index = print_array_row(a, strm, index, rightmost_dim_size - 1);
196 more_indices = increment_state(&state, shape);
200 }
while (more_indices);
203 static int get_index(Array *a, vector<int> indices)
205 if (indices.size() != a->dimensions(
true))
206 throw InternalErr(__FILE__, __LINE__,
"Index vector is the wrong size!");
212 vector < int >shape = get_shape_vector(a, indices.size());
215 reverse(indices.begin(), indices.end());
216 reverse(shape.begin(), shape.end());
218 vector<int>::iterator indices_iter = indices.begin();
219 vector<int>::iterator shape_iter = shape.begin();
221 int index = *indices_iter++;
223 while (indices_iter != indices.end()) {
224 multiplier *= *shape_iter++;
225 index += multiplier * *indices_iter++;
240 static void print_complex_array(Array *a, ostream &strm,
bool print_name, Crc32 &checksum)
242 int dims = a->dimensions(
true);
244 throw InternalErr(__FILE__, __LINE__,
"Dimension count is <= 1 while printing multidimensional array.");
248 vector<int> shape = get_shape_vector(a, dims);
250 vector<int> state(dims, 0);
257 for (
int i = 0; i < dims; ++i) {
258 strm <<
"[" << state[i] <<
"]" ;
262 print_values_as_ascii(a->var(get_index(a, state)), print_name, strm, checksum);
264 more_indices = increment_state(&state, shape);
269 }
while (more_indices);
281 static void print_values_as_ascii(Array *a,
bool print_name, ostream &strm, Crc32 &checksum)
283 if (a->var()->is_simple_type()) {
284 if (a->dimensions(
true) > 1) {
285 print_ndim_array(a, strm, print_name);
288 print_array_vector(a, strm, print_name);
292 print_complex_array(a, strm, print_name, checksum);
296 static void print_structure_header(Structure *s, ostream &strm)
298 Constructor::Vars_iter p = s->var_begin(), e = s->var_end();
299 bool needs_comma =
false;
302 if ((*p)->is_simple_type())
303 strm << (needs_comma?
", ":
"") << (*p)->FQN();
304 else if ((*p)->type() == dods_structure_c)
305 print_structure_header(
static_cast<Structure*
>(*p), strm);
306 else if ((*p)->type() == dods_sequence_c)
307 print_sequence_header(
static_cast<D4Sequence*
>(*p), strm);
309 throw InternalErr(__FILE__, __LINE__,
"Unknown or unsupported type.");
316 static void print_structure_ascii(Structure *s, ostream &strm,
bool print_name, Crc32 &checksum)
318 if (s->is_linear()) {
320 print_structure_header(s, strm);
324 Constructor::Vars_iter p = s->var_begin(), e = s->var_end();
327 if ((*p)->send_p()) print_values_as_ascii(*p,
false , strm, checksum);
329 if (++p != e) strm <<
", ";
333 for (Constructor::Vars_iter p = s->var_begin(), e = s->var_end(); p != e; ++p) {
334 if ((*p)->send_p()) {
335 print_values_as_ascii(*p, print_name, strm, checksum);
345 static void print_values_as_ascii(Structure *v,
bool print_name, ostream &strm, Crc32 &checksum)
347 print_structure_ascii(v, strm, print_name, checksum);
350 static void print_one_row(D4Sequence *seq, ostream &strm, Crc32 &checksum,
int row)
352 int elements = seq->element_count();
355 bool first_val =
true;
357 while (j < elements) {
358 btp = seq->var_value(row, j++);
363 if (btp->type() == dods_sequence_c)
364 print_val_by_rows(
static_cast<D4Sequence*
>(btp), strm, checksum);
366 print_values_as_ascii(btp,
false, strm, checksum);
371 static void print_val_by_rows(D4Sequence *seq, ostream &strm, Crc32 &checksum)
373 if (seq->length() != 0) {
374 int rows = seq->length() ;
375 for (
int i = 0; i < rows; ++i) {
376 print_one_row(seq, strm, checksum, i);
382 static void print_sequence_header(D4Sequence *s, ostream &strm)
384 Constructor::Vars_iter p = s->var_begin(), e = s->var_end();
385 bool needs_comma =
false;
388 if((*p)->is_simple_type())
389 strm << (needs_comma?
", ":
"") << (*p)->FQN();
390 else if ((*p)->type() == dods_structure_c)
391 print_structure_header(
static_cast<Structure*
>((*p)), strm);
392 else if ((*p)->type() == dods_sequence_c)
393 print_sequence_header(
static_cast<D4Sequence*
>((*p)), strm);
395 throw InternalErr(__FILE__, __LINE__,
"Unknown or unsupported type.");
404 static void print_values_as_ascii(D4Sequence *v,
bool print_name, ostream &strm, Crc32 &checksum)
407 print_sequence_header(v, strm);
411 print_val_by_rows(v, strm, checksum);
414 static void print_values_as_ascii(D4Opaque *v,
bool print_name, ostream &strm, Crc32 &)
417 strm << v->FQN() <<
", ";
418 strm << v->value().size() <<
" bytes" << endl;
421 static void print_values_as_ascii(D4Group *group,
bool print_name, ostream &strm, Crc32 &checksum)
423 for (D4Group::groupsIter g = group->grp_begin(), e = group->grp_end(); g != e; ++g)
424 print_values_as_ascii(*g, print_name, strm, checksum);
433 for (Constructor::Vars_iter i = group->var_begin(), e = group->var_end(); i != e; ++i) {
435 if ((*i)->send_p()) {
439 print_values_as_ascii((*i), print_name, strm, checksum);
454 static void print_values_as_ascii(BaseType *btp,
bool print_name, ostream &strm, Crc32 &checksum)
456 switch (btp->type()) {
458 throw InternalErr(__FILE__, __LINE__,
"Unknown type");
479 if (print_name) strm << btp->FQN() <<
", ";
480 btp->print_val(strm,
"" ,
false );
484 print_values_as_ascii(
static_cast<D4Opaque*
>(btp), print_name, strm, checksum);
488 print_values_as_ascii(
static_cast<Array*
>(btp), print_name, strm, checksum);
491 case dods_structure_c:
492 print_values_as_ascii(
static_cast<Structure*
>(btp), print_name, strm, checksum);
495 case dods_sequence_c:
496 print_values_as_ascii(
static_cast<D4Sequence*
>(btp), print_name, strm, checksum);
500 print_values_as_ascii(
static_cast<D4Group*
>(btp), print_name, strm, checksum);
505 throw InternalErr(__FILE__, __LINE__,
"Unsupported type");
516 void print_values_as_ascii(DMR *dmr, ostream &strm)
520 strm <<
"Dataset: " << dmr->name() << endl;
522 print_values_as_ascii(dmr->root(),
true , strm, checksum);