diff options
author | Quincey Koziol <koziol@hdfgroup.org> | 2004-10-06 16:11:28 (GMT) |
---|---|---|
committer | Quincey Koziol <koziol@hdfgroup.org> | 2004-10-06 16:11:28 (GMT) |
commit | 759096a9fea4f644d43ee97bb10a738eda543811 (patch) | |
tree | 139d4ca75dbc97ea0daac7e319e03495d67761d0 /tools/h5ls | |
parent | 0a2afa77ae43bb3cec4c6d45ca03fa8be0026ba9 (diff) | |
download | hdf5-759096a9fea4f644d43ee97bb10a738eda543811.zip hdf5-759096a9fea4f644d43ee97bb10a738eda543811.tar.gz hdf5-759096a9fea4f644d43ee97bb10a738eda543811.tar.bz2 |
[svn-r9372] Purpose:
Refactor code
Description:
Refactor common code for determining the native type for using in the
tools into separate routine.
Also, reduce diffs between the two branches and bring back some fixes from
the development branch to the release branch.
Platforms tested:
FreeBSD 4.10 (sleipnir)
too minor to require h5committest
Diffstat (limited to 'tools/h5ls')
-rw-r--r-- | tools/h5ls/h5ls.c | 545 |
1 files changed, 282 insertions, 263 deletions
diff --git a/tools/h5ls/h5ls.c b/tools/h5ls/h5ls.c index ca0140d..07b53d1 100644 --- a/tools/h5ls/h5ls.c +++ b/tools/h5ls/h5ls.c @@ -711,11 +711,14 @@ display_cmpd_type(hid_t type, int ind) char *name=NULL; /* member name */ size_t size; /* total size of type in bytes */ hid_t subtype; /* member data type */ - int i, n; /* miscellaneous counters */ + unsigned nmembs; /* number of members */ + int n; /* miscellaneous counters */ + unsigned i; /* miscellaneous counters */ if (H5T_COMPOUND!=H5Tget_class(type)) return FALSE; printf("struct {"); - for (i=0; i<H5Tget_nmembers(type); i++) { + nmembs=H5Tget_nmembers(type); + for (i=0; i<nmembs; i++) { /* Name and offset */ name = H5Tget_member_name(type, i); @@ -758,16 +761,17 @@ display_enum_type(hid_t type, int ind) { char **name=NULL; /* member names */ unsigned char *value=NULL; /* value array */ - int nmembs; /* number of members */ + unsigned nmembs; /* number of members */ int nchars; /* number of output characters */ hid_t super; /* enum base integer type */ hid_t native=-1; /* native integer data type */ size_t dst_size; /* destination value type size */ - int i; /* miscellaneous counters */ + unsigned i; /* miscellaneous counters */ size_t j; if (H5T_ENUM!=H5Tget_class(type)) return FALSE; nmembs = H5Tget_nmembers(type); + assert(nmembs>0); super = H5Tget_super(type); printf("enum "); display_type(super, ind+4); @@ -779,23 +783,22 @@ display_enum_type(hid_t type, int ind) * 2. unsigned long_long -- the largest native unsigned integer * 3. raw format */ if (H5Tget_size(type)<=sizeof(long_long)) { - dst_size = sizeof(long_long); - if (H5T_SGN_NONE==H5Tget_sign(type)) { - native = H5T_NATIVE_ULLONG; - } else { - native = H5T_NATIVE_LLONG; - } + dst_size = sizeof(long_long); + if (H5T_SGN_NONE==H5Tget_sign(type)) { + native = H5T_NATIVE_ULLONG; + } else { + native = H5T_NATIVE_LLONG; + } } else { - dst_size = H5Tget_size(type); + dst_size = H5Tget_size(type); } /* Get the names and raw values of all members */ - assert(nmembs>0); - name = calloc((size_t)nmembs, sizeof(char*)); - value = calloc((size_t)nmembs, MAX(H5Tget_size(type), dst_size)); + name = calloc(nmembs, sizeof(char*)); + value = calloc(nmembs, MAX(H5Tget_size(type), dst_size)); for (i=0; i<nmembs; i++) { - name[i] = H5Tget_member_name(type, i); - H5Tget_member_value(type, i, value+i*H5Tget_size(type)); + name[i] = H5Tget_member_name(type, i); + H5Tget_member_value(type, i, value+i*H5Tget_size(type)); } /* Convert values to native data type */ @@ -806,22 +809,21 @@ display_enum_type(hid_t type, int ind) /* Print members */ for (i=0; i<nmembs; i++) { - printf("\n%*s", ind+4, ""); - nchars = display_string(stdout, name[i], TRUE); - printf("%*s = ", MAX(0, 16-nchars), ""); - - if (native<0) { - printf("0x"); - for (j=0; j<dst_size; j++) { - printf("%02x", value[i*dst_size+j]); - } - } else if (H5T_SGN_NONE==H5Tget_sign(native)) { - HDfprintf(stdout,"%"H5_PRINTF_LL_WIDTH"u", - *((unsigned long_long*)((void*)(value+i*dst_size)))); - } else { - HDfprintf(stdout,"%"H5_PRINTF_LL_WIDTH"d", - *((long_long*)((void*)(value+i*dst_size)))); - } + printf("\n%*s", ind+4, ""); + nchars = display_string(stdout, name[i], TRUE); + printf("%*s = ", MAX(0, 16-nchars), ""); + + if (native<0) { + printf("0x"); + for (j=0; j<dst_size; j++) + printf("%02x", value[i*dst_size+j]); + } else if (H5T_SGN_NONE==H5Tget_sign(native)) { + HDfprintf(stdout,"%"H5_PRINTF_LL_WIDTH"u", + *((unsigned long_long*)((void*)(value+i*dst_size)))); + } else { + HDfprintf(stdout,"%"H5_PRINTF_LL_WIDTH"d", + *((long_long*)((void*)(value+i*dst_size)))); + } } /* Release resources */ @@ -1331,11 +1333,11 @@ list_attr (hid_t obj, const char *attr_name, void UNUSED *op_data) n = display_string(stdout, attr_name, TRUE); printf("%*s", MAX(0, 9-n), ""); if ((attr = H5Aopen_name(obj, attr_name))) { - space = H5Aget_space(attr); - type = H5Aget_type(attr); + space = H5Aget_space(attr); + type = H5Aget_type(attr); - /* Data space */ - ndims = H5Sget_simple_extent_dims(space, size, NULL); + /* Data space */ + ndims = H5Sget_simple_extent_dims(space, size, NULL); if (0==ndims) { puts(" scalar"); } else { @@ -1347,71 +1349,66 @@ list_attr (hid_t obj, const char *attr_name, void UNUSED *op_data) puts("}"); } - /* Data type */ - printf(" Type: "); - display_type(type, 15); - putchar('\n'); + /* Data type */ + printf(" Type: "); + display_type(type, 15); + putchar('\n'); + + /* Data */ + memset(&info, 0, sizeof info); + info.line_multi_new = 1; + if (nelmts<5) { + info.idx_fmt = ""; + info.line_1st = " Data: "; + info.line_pre = " "; + info.line_cont = " "; + info.str_repeat = 8; + + } else { + printf(" Data:\n"); + info.idx_fmt = "(%s)"; + info.line_pre = " %s "; + info.line_cont = " %s "; + info.str_repeat = 8; + } + + info.line_ncols = width_g; + if (label_g) info.cmpd_name = "%s="; + if (string_g && 1==H5Tget_size(type) && + H5T_INTEGER==H5Tget_class(type)) { + info.ascii = TRUE; + info.elmt_suf1 = ""; + info.elmt_suf2 = ""; + info.idx_fmt = "(%s)"; + info.line_pre = " %s \""; + info.line_suf = "\""; + } - /* Data */ - memset(&info, 0, sizeof info); - info.line_multi_new = 1; - if (nelmts<5) { - info.idx_fmt = ""; - info.line_1st = " Data: "; - info.line_pre = " "; - info.line_cont = " "; - info.str_repeat = 8; - - } else { - printf(" Data:\n"); - info.idx_fmt = "(%s)"; - info.line_pre = " %s "; - info.line_cont = " %s "; - info.str_repeat = 8; - } - info.line_ncols = width_g; - if (label_g) info.cmpd_name = "%s="; - if (string_g && 1==H5Tget_size(type) && - H5T_INTEGER==H5Tget_class(type)) { - info.ascii = TRUE; - info.elmt_suf1 = ""; - info.elmt_suf2 = ""; - info.idx_fmt = "(%s)"; - info.line_pre = " %s \""; - info.line_suf = "\""; - } - /* values of type reference */ - info.obj_format = "-%lu:%lu:%lu:%lu"; - info.obj_hidefileno = 0; - if (hexdump_g) { - p_type = H5Tcopy(type); - } else { - H5T_class_t type_class; + /* values of type reference */ + info.obj_format = "-%lu:%lu:%lu:%lu"; + info.obj_hidefileno = 0; + if (hexdump_g) + p_type = H5Tcopy(type); + else + p_type = h5tools_get_native_type(type); - type_class = H5Tget_class(type); - if(type_class==H5T_BITFIELD) - p_type=H5Tcopy(type); - else - p_type = H5Tget_native_type(type,H5T_DIR_DEFAULT); - } - if (p_type>=0) { + if (p_type>=0) { temp_need= nelmts * MAX(H5Tget_size(type), H5Tget_size(p_type)); assert(temp_need==(hsize_t)((size_t)temp_need)); need = (size_t)temp_need; - buf = malloc(need); - assert(buf); - if (H5Aread(attr, p_type, buf)>=0) { - h5tools_dump_mem(stdout, &info, attr, p_type, space, buf, -1); - } - free(buf); - H5Tclose(p_type); - } - - H5Sclose(space); - H5Tclose(type); - H5Aclose(attr); + buf = malloc(need); + assert(buf); + if (H5Aread(attr, p_type, buf)>=0) + h5tools_dump_mem(stdout, &info, attr, p_type, space, buf, -1); + free(buf); + H5Tclose(p_type); + } + + H5Sclose(space); + H5Tclose(type); + H5Aclose(attr); } else { - putchar('\n'); + putchar('\n'); } return 0; @@ -1442,8 +1439,8 @@ dataset_list1(hid_t dset) { hsize_t cur_size[64]; /* current dataset dimensions */ hsize_t max_size[64]; /* maximum dataset dimensions */ - hid_t space; /* data space */ - int ndims; /* dimensionality */ + hid_t space; /* data space */ + int ndims; /* dimensionality */ int i; /* Information that goes on the same row as the name. The name has @@ -1970,6 +1967,29 @@ get_width(void) /*------------------------------------------------------------------------- + * Function: leave + * + * Purpose: Close HDF5 and MPI and call exit() + * + * Return: Does not return + * + * Programmer: Quincey Koziol + * Saturday, January 31, 2004 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +static void +leave(int ret) +{ + h5tools_close(); + + exit(ret); +} + + +/*------------------------------------------------------------------------- * Function: main * * Purpose: Opens a file and lists the specified group @@ -1986,7 +2006,7 @@ get_width(void) *------------------------------------------------------------------------- */ int -main (int argc, char *argv[]) +main (int argc, const char *argv[]) { hid_t file=-1, root=-1; char *fname=NULL, *oname=NULL, *x; @@ -2023,132 +2043,132 @@ main (int argc, char *argv[]) /* Switches come before non-switch arguments */ for (argno=1; argno<argc && '-'==argv[argno][0]; argno++) { - if (!strcmp(argv[argno], "--")) { - /* Last switch */ - argno++; - break; - } else if (!strcmp(argv[argno], "--help")) { - usage(progname); - exit(0); - } else if (!strcmp(argv[argno], "--address")) { - address_g = TRUE; - } else if (!strcmp(argv[argno], "--data")) { - data_g = TRUE; - } else if (!strcmp(argv[argno], "--errors")) { - show_errors_g = TRUE; - } else if (!strcmp(argv[argno], "--full")) { - fullname_g = TRUE; - } else if (!strcmp(argv[argno], "--group")) { - grp_literal_g = TRUE; - } else if (!strcmp(argv[argno], "--label")) { - label_g = TRUE; - } else if (!strcmp(argv[argno], "--recursive")) { - recursive_g = TRUE; - fullname_g = TRUE; - } else if (!strcmp(argv[argno], "--simple")) { - simple_output_g = TRUE; - } else if (!strcmp(argv[argno], "--string")) { - string_g = TRUE; - } else if (!strncmp(argv[argno], "--width=", 8)) { - width_g = (int)strtol(argv[argno]+8, &rest, 0); - if (width_g<=0 || *rest) { - usage(progname); - exit(1); - } - } else if (!strcmp(argv[argno], "--width")) { - if (argno+1>=argc) { - usage(progname); - exit(1); - } else { - s = argv[++argno]; - } - width_g = (int)strtol(s, &rest, 0); - if (width_g<=0 || *rest) { - usage(progname); - exit(1); - } - } else if (!strcmp(argv[argno], "--verbose")) { - verbose_g++; - } else if (!strcmp(argv[argno], "--version")) { + if (!strcmp(argv[argno], "--")) { + /* Last switch */ + argno++; + break; + } else if (!strcmp(argv[argno], "--help")) { + usage(progname); + leave(0); + } else if (!strcmp(argv[argno], "--address")) { + address_g = TRUE; + } else if (!strcmp(argv[argno], "--data")) { + data_g = TRUE; + } else if (!strcmp(argv[argno], "--errors")) { + show_errors_g = TRUE; + } else if (!strcmp(argv[argno], "--full")) { + fullname_g = TRUE; + } else if (!strcmp(argv[argno], "--group")) { + grp_literal_g = TRUE; + } else if (!strcmp(argv[argno], "--label")) { + label_g = TRUE; + } else if (!strcmp(argv[argno], "--recursive")) { + recursive_g = TRUE; + fullname_g = TRUE; + } else if (!strcmp(argv[argno], "--simple")) { + simple_output_g = TRUE; + } else if (!strcmp(argv[argno], "--string")) { + string_g = TRUE; + } else if (!strncmp(argv[argno], "--width=", 8)) { + width_g = (int)strtol(argv[argno]+8, &rest, 0); + if (width_g<=0 || *rest) { + usage(progname); + leave(1); + } + } else if (!strcmp(argv[argno], "--width")) { + if (argno+1>=argc) { + usage(progname); + leave(1); + } else { + s = argv[++argno]; + } + width_g = (int)strtol(s, &rest, 0); + if (width_g<=0 || *rest) { + usage(progname); + leave(1); + } + } else if (!strcmp(argv[argno], "--verbose")) { + verbose_g++; + } else if (!strcmp(argv[argno], "--version")) { print_version(progname); - exit(0); - } else if (!strcmp(argv[argno], "--hexdump")) { - hexdump_g = TRUE; - } else if (!strncmp(argv[argno], "-w", 2)) { - if (argv[argno][2]) { - s = argv[argno]+2; - } else if (argno+1>=argc) { - usage(progname); - exit(1); - } else { - s = argv[++argno]; - } - width_g = (int)strtol(s, &rest, 0); - if (width_g<=0 || *rest) { - usage(progname); - exit(1); - } - } else if ('-'!=argv[argno][1]) { - /* Single-letter switches */ - for (s=argv[argno]+1; *s; s++) { - switch (*s) { - case '?': - case 'h': /* --help */ - usage(progname); - exit(0); - case 'a': /* --address */ - address_g = TRUE; - break; - case 'd': /* --data */ - data_g = TRUE; - break; - case 'e': /* --errors */ - show_errors_g = TRUE; - break; - case 'f': /* --full */ - fullname_g = TRUE; - break; - case 'g': /* --group */ - grp_literal_g = TRUE; - break; - case 'l': /* --label */ - label_g = TRUE; - break; - case 'r': /* --recursive */ - recursive_g = TRUE; - fullname_g = TRUE; - break; - case 'S': /* --simple */ - simple_output_g = TRUE; - break; - case 's': /* --string */ - string_g = TRUE; - break; - case 'v': /* --verbose */ - verbose_g++; - break; - case 'V': /* --version */ - print_version(progname); - exit(0); - case 'x': /* --hexdump */ - hexdump_g = TRUE; - break; - default: - usage(progname); - exit(1); - } - } - } else { - usage(progname); - exit(1); - } + leave(0); + } else if (!strcmp(argv[argno], "--hexdump")) { + hexdump_g = TRUE; + } else if (!strncmp(argv[argno], "-w", 2)) { + if (argv[argno][2]) { + s = argv[argno]+2; + } else if (argno+1>=argc) { + usage(progname); + leave(1); + } else { + s = argv[++argno]; + } + width_g = (int)strtol(s, &rest, 0); + if (width_g<=0 || *rest) { + usage(progname); + leave(1); + } + } else if ('-'!=argv[argno][1]) { + /* Single-letter switches */ + for (s=argv[argno]+1; *s; s++) { + switch (*s) { + case '?': + case 'h': /* --help */ + usage(progname); + leave(0); + case 'a': /* --address */ + address_g = TRUE; + break; + case 'd': /* --data */ + data_g = TRUE; + break; + case 'e': /* --errors */ + show_errors_g = TRUE; + break; + case 'f': /* --full */ + fullname_g = TRUE; + break; + case 'g': /* --group */ + grp_literal_g = TRUE; + break; + case 'l': /* --label */ + label_g = TRUE; + break; + case 'r': /* --recursive */ + recursive_g = TRUE; + fullname_g = TRUE; + break; + case 'S': /* --simple */ + simple_output_g = TRUE; + break; + case 's': /* --string */ + string_g = TRUE; + break; + case 'v': /* --verbose */ + verbose_g++; + break; + case 'V': /* --version */ + print_version(progname); + leave(0); + case 'x': /* --hexdump */ + hexdump_g = TRUE; + break; + default: + usage(progname); + leave(1); + } + } + } else { + usage(progname); + leave(1); + } } /* If no arguments remain then print a usage message (instead of doing * absolutely nothing ;-) */ if (argno>=argc) { - usage(progname); - exit(1); + usage(progname); + leave(1); } /* Turn off HDF5's automatic error printing unless you're debugging h5ls */ @@ -2169,58 +2189,57 @@ main (int argc, char *argv[]) * doesn't exist). */ show_file_name_g = (argc-argno > 1); /*show file names if more than one*/ while (argno<argc) { - fname = argv[argno++]; - oname = NULL; - file = -1; - - while (fname && *fname) { - file = h5tools_fopen(fname, NULL, drivername, sizeof drivername); - - if (file>=0) { - if (verbose_g) { - printf("Opened \"%s\" with %s driver.\n", - fname, drivername); - } - break; /*success*/ - } - - /* Shorten the file name; lengthen the object name */ - x = oname; - oname = strrchr(fname, '/'); - if (x) *x = '/'; - if (!oname) break; - *oname = '\0'; - } - if (file<0) { - fprintf(stderr, "%s: unable to open file\n", argv[argno-1]); + fname = HDstrdup(argv[argno++]); + oname = NULL; + file = -1; + + while (fname && *fname) { + file = h5tools_fopen(fname, NULL, drivername, sizeof drivername, argc, argv); + + if (file>=0) { + if (verbose_g) { + printf("Opened \"%s\" with %s driver.\n", + fname, drivername); + } + break; /*success*/ + } + + /* Shorten the file name; lengthen the object name */ + x = oname; + oname = strrchr(fname, '/'); + if (x) *x = '/'; + if (!oname) break; + *oname = '\0'; + } + if (file<0) { + fprintf(stderr, "%s: unable to open file\n", argv[argno-1]); continue; - } - if (oname) oname++; - if (!oname || !*oname) oname = root_name; + } + if (oname) oname++; + if (!oname || !*oname) oname = root_name; - /* Open the object and display it's information */ - if (H5Gget_objinfo(file, oname, TRUE, &sb)>=0 && - H5G_GROUP==sb.type && !grp_literal_g) { + /* Open the object and display it's information */ + if (H5Gget_objinfo(file, oname, TRUE, &sb)>=0 && + H5G_GROUP==sb.type && !grp_literal_g) { /* Specified name is a group. List the complete contents of the * group. */ - sym_insert(&sb, oname); + sym_insert(&sb, oname); iter.container = container = fix_name(show_file_name_g?fname:"", oname); - H5Giterate(file, oname, NULL, list, &iter); - free(container); + H5Giterate(file, oname, NULL, list, &iter); + free(container); - } else if ((root=H5Gopen(file, "/"))<0) { - exit(1); /*major problem!*/ - - } else { + } else if ((root=H5Gopen(file, "/"))<0) { + leave(1); /*major problem!*/ + + } else { /* Specified name is a non-group object -- list that object. The - * container for the object is everything up to the base name. */ + * container for the object is everything up to the base name. */ iter.container = show_file_name_g ? fname : "/"; - list(root, oname, &iter); - if (H5Gclose(root)<0) exit(1); - } - H5Fclose(file); + list(root, oname, &iter); + if (H5Gclose(root)<0) leave(1); + } + H5Fclose(file); + free(fname); } - h5tools_close(); - - return 0; + leave(0); } |