void traverse_product() { int result; result = coda_open(traverse_info.file_name, &traverse_info.pf); if (result != 0 && coda_errno == CODA_ERROR_FILE_OPEN) { /* maybe not enough memory space to map the file in memory => * temporarily disable memory mapping of files and try again */ coda_set_option_use_mmap(0); result = coda_open(traverse_info.file_name, &traverse_info.pf); coda_set_option_use_mmap(1); } if (result != 0) { handle_coda_error(); } if (coda_cursor_set_product(&traverse_info.cursor, traverse_info.pf) != 0) { handle_coda_error(); } if (coda_get_product_root_type(traverse_info.pf, &traverse_info.type[traverse_info.current_depth]) != 0) { handle_coda_error(); } traverse_data(); coda_close(traverse_info.pf); traverse_info.pf = NULL; }
/* If the user explicitly asks for traversal of a hidden record field, * this function will be called with traverse_hidden set to 1. */ static void traverse_record(int index, int traverse_hidden) { int hidden; traverse_info.parent_index[traverse_info.num_records - 1] = index; if (coda_type_get_record_field_hidden_status(traverse_info.type[traverse_info.current_depth - 1], index, &hidden) != 0) { handle_coda_error(); } if (hidden && !traverse_hidden) { /* skip this field */ return; } if (calc_dim) { int available; /* we do not traverse records that are globally not available * (i.e. not available for every element of our parent array(s)) */ if (coda_type_get_record_field_available_status(traverse_info.type[traverse_info.current_depth - 1], index, &available) != 0) { handle_coda_error(); } if (available == -1) { /* traverse all occurences of this field to check whether at least one is available */ if (!dim_record_field_available()) { return; } } traverse_info.field_available_status[traverse_info.current_depth - 1] = available; } if (coda_type_get_record_field_name(traverse_info.type[traverse_info.current_depth - 1], index, &traverse_info.field_name[traverse_info.num_records - 1]) != 0) { handle_coda_error(); } if (coda_type_get_record_field_type(traverse_info.type[traverse_info.current_depth - 1], index, &traverse_info.type[traverse_info.current_depth]) != 0) { handle_coda_error(); } traverse_data(); }
static void get_filter_item(const char *filter_expr, codadump_filter **filter, char **tail) { int n = 0; assert(filter_expr != NULL); assert(filter != NULL); assert(tail != NULL); /* strip leading spaces */ while (filter_expr[0] == ' ' || filter_expr[0] == '\t' || filter_expr[0] == '\n') { filter_expr = &filter_expr[1]; } /* find end of fieldname */ while (filter_expr[n] != '\0' && filter_expr[n] != '.' && filter_expr[n] != ';' && filter_expr[n] != ',') { n++; } if (n > 0) { int p = n; /* create and initialize new filter item */ *filter = (codadump_filter *)malloc(sizeof(codadump_filter)); if (*filter == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", sizeof(codadump_filter), __FILE__, __LINE__); handle_coda_error(); } (*filter)->fieldname = (char *)malloc(n + 1); if ((*filter)->fieldname == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %u bytes) (%s:%u)", n + 1, __FILE__, __LINE__); handle_coda_error(); } strncpy((*filter)->fieldname, filter_expr, n); (*filter)->subfilter = NULL; (*filter)->next = NULL; /* strip trailing spaces of the fieldname */ while ((*filter)->fieldname[n - 1] == ' ' || (*filter)->fieldname[n - 1] == '\t' || (*filter)->fieldname[n - 1] == '\n') { n--; } (*filter)->fieldname[n] = '\0'; if (filter_expr[p] == '.') { get_filter_item(&(filter_expr[p + 1]), &(*filter)->subfilter, tail); if ((*filter)->subfilter == NULL) { /* the subfilter was incorrect, so we remove the complete filter item */ codadump_filter_remove(filter); } } else if (filter_expr[p] == ';' || filter_expr[p] == ',') { *tail = (char *)&(filter_expr[p + 1]); } else { *tail = (char *)&(filter_expr[p]); } } else { *filter = NULL; } }
static void print_data(coda_cursor *cursor, int depth) { coda_type_class type_class; int has_attributes; if (coda_cursor_has_attributes(cursor, &has_attributes) != 0) { handle_coda_error(); } if (has_attributes) { if (coda_cursor_goto_attributes(cursor) != 0) { handle_coda_error(); } fi_printf("{attributes}\n"); INDENT++; print_data(cursor, depth); INDENT--; coda_cursor_goto_parent(cursor); } if (coda_cursor_get_type_class(cursor, &type_class) != 0) { handle_coda_error(); } switch (type_class) { case coda_record_class: { long num_fields; if (coda_cursor_get_num_elements(cursor, &num_fields) != 0) { handle_coda_error(); } if (num_fields > 0) { coda_type *record_type; int is_union; long i; if (coda_cursor_get_type(cursor, &record_type) != 0) { handle_coda_error(); } if (coda_type_get_record_union_status(record_type, &is_union) != 0) { handle_coda_error(); } if (is_union) { const char *field_name; if (coda_cursor_get_available_union_field_index(cursor, &i) != 0) { handle_coda_error(); } if (coda_type_get_record_field_name(record_type, i, &field_name) != 0) { handle_coda_error(); } if (coda_cursor_goto_record_field_by_index(cursor, i) != 0) { handle_coda_error(); } fi_printf("[%s]", field_name); if (print_offsets) { int64_t offset; if (coda_cursor_get_file_bit_offset(cursor, &offset) != 0) { handle_coda_error(); } if (offset >= 0) { char s[21]; coda_str64(offset >> 3, s); ff_printf(":%s", s); if ((offset & 0x7) != 0) { ff_printf(":%d", (int)(offset & 0x7)); } } } ff_printf("\n"); INDENT++; if (max_depth < 0 || depth < max_depth) { print_data(cursor, depth + 1); } else { fi_printf("...\n"); } INDENT--; coda_cursor_goto_parent(cursor); } else { if (coda_cursor_goto_first_record_field(cursor) != 0) { handle_coda_error(); } for (i = 0; i < num_fields; i++) { const char *field_name; if (coda_type_get_record_field_name(record_type, i, &field_name) != 0) { handle_coda_error(); } fi_printf("[%s]", field_name); if (print_offsets) { int64_t offset; if (coda_cursor_get_file_bit_offset(cursor, &offset) != 0) { handle_coda_error(); } if (offset >= 0) { char s[21]; coda_str64(offset >> 3, s); ff_printf(":%s", s); if ((offset & 0x7) != 0) { ff_printf(":%d", (int)(offset & 0x7)); } } } ff_printf("\n"); INDENT++; if (max_depth < 0 || depth < max_depth) { print_data(cursor, depth + 1); } else { fi_printf("...\n"); } INDENT--; if (i < num_fields - 1) { if (coda_cursor_goto_next_record_field(cursor) != 0) { handle_coda_error(); } } } coda_cursor_goto_parent(cursor); }
void print_full_field_name(FILE *f, int print_dims, int compound_as_array) { int i; if (print_dims == 1) { int record_id = 0; int array_id = 0; for (i = 0; i < traverse_info.current_depth; i++) { coda_type_class type_class; if (coda_type_get_class(traverse_info.type[i], &type_class) != 0) { handle_coda_error(); } switch (type_class) { case coda_record_class: fprintf(f, "/%s", traverse_info.field_name[record_id]); record_id++; break; case coda_array_class: if (i == 0) { fprintf(f, "/"); } if (traverse_info.array_info[array_id].num_dims > 0) { fprintf(f, "["); print_array_dim(f, array_id); fprintf(f, "]"); } array_id++; break; default: break; } } if (compound_as_array && array_id < traverse_info.num_arrays) { fprintf(f, "["); print_array_dim(f, array_id); fprintf(f, "]"); } } else { for (i = 0; i < traverse_info.num_records; i++) { if (i > 0) { fprintf(f, "."); } fprintf(f, "%s", traverse_info.field_name[i]); } if (print_dims == 2) { int array_id = 0; for (i = 0; i < traverse_info.current_depth; i++) { coda_type_class type_class; if (coda_type_get_class(traverse_info.type[i], &type_class) != 0) { handle_coda_error(); } if (type_class == coda_array_class) { if (traverse_info.array_info[array_id].num_dims > 0) { if (traverse_info.array_info[array_id].dim_id == 0) { fprintf(f, " ["); } else { fprintf(f, ","); } print_array_dim(f, array_id); array_id++; } } } if (compound_as_array && array_id < traverse_info.num_arrays) { if (traverse_info.array_info[array_id].dim_id == 0) { fprintf(f, " ["); } else { fprintf(f, ","); } print_array_dim(f, array_id); array_id++; } if (array_id > 0) { fprintf(f, "]"); } } } }
static void traverse_data() { coda_type_class type_class; if (coda_type_get_class(traverse_info.type[traverse_info.current_depth], &type_class) != 0) { handle_coda_error(); } switch (type_class) { case coda_record_class: { long num_fields; long index; if (traverse_info.current_depth >= CODA_CURSOR_MAXDEPTH - 1) { /* don't traverse further, since we can't navigate to it with a CODA cursor */ break; } #ifdef HAVE_HDF4 if (run_mode == RUN_MODE_HDF4) { hdf4_enter_record(); } #endif traverse_info.num_records++; if (coda_type_get_num_record_fields(traverse_info.type[traverse_info.current_depth], &num_fields) != 0) { handle_coda_error(); } traverse_info.current_depth++; if (traverse_info.filter[traverse_info.filter_depth] != NULL) { codadump_filter *filter; filter = traverse_info.filter[traverse_info.filter_depth]; while (traverse_info.filter[traverse_info.filter_depth] != NULL) { const char *name; name = codadump_filter_get_fieldname(traverse_info.filter[traverse_info.filter_depth]); assert(name != NULL); if (coda_type_get_record_field_index_from_name (traverse_info.type[traverse_info.current_depth - 1], name, &index) != 0) { if (coda_errno == CODA_ERROR_INVALID_NAME) { fprintf(stderr, "ERROR: incorrect filter - incorrect fieldname (%s)\n", name); exit(1); } handle_coda_error(); } traverse_info.filter[traverse_info.filter_depth + 1] = codadump_filter_get_subfilter(traverse_info.filter[traverse_info.filter_depth]); traverse_info.filter_depth++; traverse_record(index, 1); traverse_info.filter_depth--; traverse_info.filter[traverse_info.filter_depth] = codadump_filter_get_next_filter(traverse_info.filter[traverse_info.filter_depth]); } traverse_info.filter[traverse_info.filter_depth] = filter; } else { for (index = 0; index < num_fields; index++) { traverse_record(index, 0); } } traverse_info.current_depth--; traverse_info.num_records--; #ifdef HAVE_HDF4 if (run_mode == RUN_MODE_HDF4) { hdf4_leave_record(); } #endif } break; case coda_array_class: { if (traverse_info.current_depth >= CODA_CURSOR_MAXDEPTH - 1) { /* don't traverse further, since we can't navigate to it with a CODA cursor */ break; } dim_enter_array(); #ifdef HAVE_HDF4 if (run_mode == RUN_MODE_HDF4) { hdf4_enter_array(); } #endif traverse_info.num_arrays++; traverse_info.current_depth++; if (coda_type_get_array_base_type(traverse_info.type[traverse_info.current_depth - 1], &traverse_info.type[traverse_info.current_depth]) != 0) { handle_coda_error(); } traverse_data(); traverse_info.current_depth--; traverse_info.num_arrays--; #ifdef HAVE_HDF4 if (run_mode == RUN_MODE_HDF4) { hdf4_leave_array(); } #endif dim_leave_array(); } break; case coda_integer_class: case coda_real_class: case coda_text_class: case coda_raw_class: { coda_native_type read_type; if (coda_type_get_read_type(traverse_info.type[traverse_info.current_depth], &read_type) != 0) { handle_coda_error(); } switch (read_type) { case coda_native_type_string: case coda_native_type_bytes: dim_enter_array(); #ifdef HAVE_HDF4 if (run_mode == RUN_MODE_HDF4) { hdf4_enter_array(); } #endif traverse_info.num_arrays++; handle_data_element(); traverse_info.num_arrays--; #ifdef HAVE_HDF4 if (run_mode == RUN_MODE_HDF4) { hdf4_leave_array(); } #endif dim_leave_array(); break; default: handle_data_element(); break; } } break; case coda_special_class: { coda_special_type special_type; if (coda_get_option_bypass_special_types()) { /* use the base type for all special types */ if (coda_type_get_special_base_type(traverse_info.type[traverse_info.current_depth], &traverse_info.type[traverse_info.current_depth]) != 0) { handle_coda_error(); } traverse_data(); return; } if (coda_type_get_special_type(traverse_info.type[traverse_info.current_depth], &special_type) != 0) { handle_coda_error(); } switch (special_type) { case coda_special_no_data: /* ignore */ return; case coda_special_vsf_integer: case coda_special_time: handle_data_element(); break; case coda_special_complex: dim_enter_array(); #ifdef HAVE_HDF4 if (run_mode == RUN_MODE_HDF4) { hdf4_enter_array(); } #endif traverse_info.num_arrays++; handle_data_element(); traverse_info.num_arrays--; #ifdef HAVE_HDF4 if (run_mode == RUN_MODE_HDF4) { hdf4_leave_array(); } #endif dim_leave_array(); break; } } break; } }
static void handle_data_element() { if (run_mode == RUN_MODE_LIST) { print_full_field_name(stdout, 1, 0); if (show_type) { coda_type_class type_class; if (coda_type_get_class(traverse_info.type[traverse_info.current_depth], &type_class) != 0) { handle_coda_error(); } if (type_class == coda_special_class) { coda_special_type special_type; if (coda_type_get_special_type(traverse_info.type[traverse_info.current_depth], &special_type) != 0) { handle_coda_error(); } printf(" %s", coda_type_get_special_type_name(special_type)); } else { coda_native_type read_type; if (coda_type_get_read_type(traverse_info.type[traverse_info.current_depth], &read_type) != 0) { handle_coda_error(); } printf(" %s", coda_type_get_native_type_name(read_type)); if (read_type == coda_native_type_string || read_type == coda_native_type_bytes) { printf("("); assert(traverse_info.num_arrays > 0); print_array_dim(stdout, traverse_info.num_arrays - 1); printf(")"); } } } if (show_unit) { const char *unit; if (coda_type_get_unit(traverse_info.type[traverse_info.current_depth], &unit) != 0) { handle_coda_error(); } if (unit != NULL && unit[0] != '\0') { printf(" [%s]", unit); } } if (show_description) { const char *description; if (coda_type_get_description(traverse_info.type[traverse_info.current_depth], &description) != 0) { handle_coda_error(); } if (description != NULL && description[0] != '\0') { printf(" \"%s\"", description); } } printf("\n"); if (show_dim_vals) { int i; for (i = 0; i < dim_info.num_dims; i++) { print_all_distinct_dims(i); } } } else if (run_mode == RUN_MODE_ASCII) { export_data_element_to_ascii(); } #ifdef HAVE_HDF4 else if (run_mode == RUN_MODE_HDF4) { export_data_element_to_hdf4(); } #endif }