35 #include <Structure.h>
37 #include <D4Sequence.h>
42 #include "InternalErr.h"
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() <<
", " ;
73 int end = a->dimension_size(a->dim_begin(),
true) - 1;
75 for (
int i = 0; i < end; ++i) {
76 a->var(i)->print_val(strm,
"",
false );
79 a->var(end)->print_val(strm,
"",
false );
93 static int print_array_row(Array *a, ostream &strm,
int index,
int number)
95 for (
int i = 0; i < number; ++i) {
96 a->var(index++)->print_val(strm,
"",
false );
100 a->var(index++)->print_val(strm,
"",
false );
111 static bool increment_state(vector<int> *state,
const vector<int> &shape)
113 vector < int >::reverse_iterator state_riter;
114 vector < int >::const_reverse_iterator shape_riter;
115 for (state_riter = state->rbegin(), shape_riter = shape.rbegin();
116 state_riter < state->rend(); state_riter++, shape_riter++) {
117 if (*state_riter == *shape_riter - 1) {
121 *state_riter = *state_riter + 1;
129 static vector <int> get_shape_vector(Array *a,
size_t n)
131 if (n < 1 || n > a->dimensions(
true)) {
133 oss <<
"Attempt to get " << n <<
" dimensions from " << a->name() <<
" which has " << a->dimensions(
true) <<
" dimensions";
134 throw InternalErr(__FILE__, __LINE__, oss.str());
138 Array::Dim_iter p = a->dim_begin();
139 for (
unsigned i = 0; i < n && p != a->dim_end(); ++i, ++p) {
140 shape.push_back(a->dimension_size(p,
true));
149 static int get_nth_dim_size(Array *a,
size_t n)
151 if (n > a->dimensions(
true) - 1) {
153 oss <<
"Attempt to get dimension " << n <<
" from " << a->name() <<
" which has " << a->dimensions(
true) <<
" dimensions";
154 throw InternalErr(__FILE__, __LINE__, oss.str());
157 return a->dimension_size(a->dim_begin() + n,
true);
160 static void print_ndim_array(Array *a, ostream &strm,
bool )
163 int dims = a->dimensions(
true);
165 throw InternalErr(__FILE__, __LINE__,
"Dimension count is <= 1 while printing multidimensional array.");
169 vector<int> shape = get_shape_vector(a, dims - 1);
170 int rightmost_dim_size = get_nth_dim_size(a, dims - 1);
176 vector<int> state(dims - 1, 0);
184 for (
int i = 0; i < dims - 1; ++i) {
185 strm <<
"[" << state[i] <<
"]" ;
189 index = print_array_row(a, strm, index, rightmost_dim_size - 1);
190 more_indices = increment_state(&state, shape);
194 }
while (more_indices);
197 static int get_index(Array *a, vector<int> indices)
199 if (indices.size() != a->dimensions(
true))
200 throw InternalErr(__FILE__, __LINE__,
"Index vector is the wrong size!");
206 vector < int >shape = get_shape_vector(a, indices.size());
209 reverse(indices.begin(), indices.end());
210 reverse(shape.begin(), shape.end());
212 vector<int>::iterator indices_iter = indices.begin();
213 vector<int>::iterator shape_iter = shape.begin();
215 int index = *indices_iter++;
217 while (indices_iter != indices.end()) {
218 multiplier *= *shape_iter++;
219 index += multiplier * *indices_iter++;
234 static void print_complex_array(Array *a, ostream &strm,
bool print_name, Crc32 &checksum)
236 int dims = a->dimensions(
true);
238 throw InternalErr(__FILE__, __LINE__,
"Dimension count is <= 1 while printing multidimensional array.");
242 vector<int> shape = get_shape_vector(a, dims);
244 vector<int> state(dims, 0);
251 for (
int i = 0; i < dims; ++i) {
252 strm <<
"[" << state[i] <<
"]" ;
256 print_values_as_ascii(a->var(get_index(a, state)), print_name, strm, checksum);
258 more_indices = increment_state(&state, shape);
263 }
while (more_indices);
275 static void print_values_as_ascii(Array *a,
bool print_name, ostream &strm, Crc32 &checksum)
277 if (a->var()->is_simple_type()) {
278 if (a->dimensions(
true) > 1) {
279 print_ndim_array(a, strm, print_name);
282 print_array_vector(a, strm, print_name);
286 print_complex_array(a, strm, print_name, checksum);
290 static void print_structure_header(Structure *s, ostream &strm)
292 Constructor::Vars_iter p = s->var_begin(), e = s->var_end();
294 if ((*p)->is_simple_type())
296 else if ((*p)->type() == dods_structure_c)
297 print_structure_header(static_cast<Structure*>(*p), strm);
298 else if ((*p)->type() == dods_sequence_c)
299 print_sequence_header(static_cast<D4Sequence*>(*p), strm);
301 throw InternalErr(__FILE__, __LINE__,
"Unknown or unsupported type.");
302 if (++p != e) strm <<
", ";
306 static void print_structure_ascii(Structure *s, ostream &strm,
bool print_name, Crc32 &checksum)
308 if (s->is_linear()) {
310 print_structure_header(s, strm);
314 Constructor::Vars_iter p = s->var_begin(), e = s->var_end();
317 if ((*p)->send_p()) print_values_as_ascii(*p,
false , strm, checksum);
319 if (++p != e) strm <<
", ";
323 for (Constructor::Vars_iter p = s->var_begin(), e = s->var_end(); p != e; ++p) {
324 if ((*p)->send_p()) {
325 print_values_as_ascii(*p, print_name, strm, checksum);
335 static void print_values_as_ascii(Structure *v,
bool print_name, ostream &strm, Crc32 &checksum)
337 print_structure_ascii(v, strm, print_name, checksum);
340 static void print_one_row(
D4Sequence *seq, ostream &strm, Crc32 &checksum,
int row)
342 int elements = seq->element_count();
345 bool first_val =
true;
347 while (j < elements) {
348 btp = seq->var_value(row, j++);
353 if (btp->type() == dods_sequence_c)
354 print_val_by_rows(static_cast<D4Sequence*>(btp), strm, checksum);
356 print_values_as_ascii(btp,
false, strm, checksum);
361 static void print_val_by_rows(
D4Sequence *seq, ostream &strm, Crc32 &checksum)
363 if (seq->length() != 0) {
364 int rows = seq->length() ;
365 for (
int i = 0; i < rows; ++i) {
366 print_one_row(seq, strm, checksum, i);
372 static void print_sequence_header(
D4Sequence *s, ostream &strm)
374 Constructor::Vars_iter p = s->var_begin(), e = s->var_end();
376 if ((*p)->is_simple_type())
378 else if ((*p)->type() == dods_structure_c)
379 print_structure_header(static_cast<Structure*>(*p), strm);
380 else if ((*p)->type() == dods_sequence_c)
381 print_sequence_header(static_cast<D4Sequence*>(*p), strm);
383 throw InternalErr(__FILE__, __LINE__,
"Unknown or unsupported type.");
384 if (++p != e) strm <<
", ";
389 static void print_values_as_ascii(
D4Sequence *v,
bool print_name, ostream &strm, Crc32 &checksum)
392 print_sequence_header(v, strm);
396 print_val_by_rows(v, strm, checksum);
399 static void print_values_as_ascii(D4Opaque *v,
bool print_name, ostream &strm, Crc32 &)
402 strm << v->FQN() <<
", ";
403 strm << v->value().size() <<
" bytes" << endl;
406 static void print_values_as_ascii(D4Group *group,
bool print_name, ostream &strm, Crc32 &checksum)
408 for (D4Group::groupsIter g = group->grp_begin(), e = group->grp_end(); g != e; ++g)
409 print_values_as_ascii(*g, print_name, strm, checksum);
418 for (Constructor::Vars_iter i = group->var_begin(), e = group->var_end(); i != e; ++i) {
420 if ((*i)->send_p()) {
423 (*i)->intern_data(checksum);
426 print_values_as_ascii((*i), print_name, strm, checksum);
431 oss.setf(ios::hex, ios::basefield);
432 oss << setfill(
'0') << setw(8) << checksum.GetCrc32();
433 strm << (*i)->FQN() <<
":DAP4_Checksum_CRC32, " << oss.str() << endl;
447 static void print_values_as_ascii(BaseType *btp,
bool print_name, ostream &strm, Crc32 &checksum)
449 switch (btp->type()) {
451 throw InternalErr(__FILE__, __LINE__,
"Unknown type");
472 if (print_name) strm << btp->FQN() <<
", ";
473 btp->print_val(strm,
"" ,
false );
477 print_values_as_ascii(static_cast<D4Opaque*>(btp), print_name, strm, checksum);
481 print_values_as_ascii(static_cast<Array*>(btp), print_name, strm, checksum);
484 case dods_structure_c:
485 print_values_as_ascii(static_cast<Structure*>(btp), print_name, strm, checksum);
488 case dods_sequence_c:
489 print_values_as_ascii(static_cast<D4Sequence*>(btp), print_name, strm, checksum);
493 print_values_as_ascii(static_cast<D4Group*>(btp), print_name, strm, checksum);
498 throw InternalErr(__FILE__, __LINE__,
"Unsupported type");
509 void print_values_as_ascii(DMR *dmr, ostream &strm)
513 strm <<
"Dataset: " << dmr->name() << endl;
515 print_values_as_ascii(dmr->root(),
true , strm, checksum);
static class NCMLUtil overview