Skip to content

Commit 192d630

Browse files
authored
Add long double format option to h5dump (#5025)
1 parent 7209383 commit 192d630

23 files changed

+538
-1151
lines changed

hl/tools/h5watch/h5watch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ doprint(hid_t did, const hsize_t *start, const hsize_t *block, int rank)
9696
{
9797
h5tools_context_t ctx; /* print context */
9898
h5tool_format_t info; /* Format info for the tools library */
99+
static char fmt_ldouble[16]; /* Format info */
99100
static char fmt_double[16], fmt_float[16]; /* Format info */
100101
struct subset_t subset; /* Subsetting info */
101102
hsize_t ss_start[H5S_MAX_RANK]; /* Info for hyperslab */
@@ -175,6 +176,8 @@ doprint(hid_t did, const hsize_t *start, const hsize_t *block, int rank)
175176
info.fmt_float = fmt_float;
176177
snprintf(fmt_double, sizeof(fmt_double), "%%1.%dg", DBL_DIG);
177178
info.fmt_double = fmt_double;
179+
snprintf(fmt_ldouble, sizeof(fmt_ldouble), "%%1.%dLg", DBL_DIG);
180+
info.fmt_ldouble = fmt_ldouble;
178181

179182
info.dset_format = "DSET-%s ";
180183
info.dset_hidefileno = 0;

release_docs/RELEASE.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ New Features
198198

199199
Tools:
200200
------
201+
- Added h5dump command option to set the floating point format for long double
202+
203+
The new option is --lformat, which allows the user to set the
204+
floating point format for long double. The default format is %Lg.
205+
There is already an option --format to set the floating point format
206+
for double and float. The default format is %g.
207+
201208
- Remove the high-level GIF tools
202209

203210
The high-level GIF tools, h52gif and gif2h5, have unfixed CVE issues

tools/lib/h5tools.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ typedef struct h5tool_format_t {
267267
* typed `unsigned long long'. The default depends on what
268268
* printf() format is available to print this datatype.
269269
*
270+
* fmt_ldouble: The printf() format to use when rendering data which is
271+
* typed `long double'. The default is `%Lg'.
272+
*
270273
* fmt_double: The printf() format to use when rendering data which is
271274
* typed `double'. The default is `%g'.
272275
*
@@ -303,6 +306,7 @@ typedef struct h5tool_format_t {
303306
const char *fmt_ulong;
304307
const char *fmt_llong;
305308
const char *fmt_ullong;
309+
const char *fmt_ldouble;
306310
const char *fmt_double;
307311
const char *fmt_float;
308312
int ascii;

tools/lib/h5tools_dump.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ h5tool_format_t h5tools_dataformat = {
3535
"%lu", /*fmt_ulong */
3636
NULL, /*fmt_llong */
3737
NULL, /*fmt_ullong */
38+
"%Lg", /*fmt_ldouble */
3839
"%g", /*fmt_double */
3940
"%g", /*fmt_float */
4041

tools/lib/h5tools_str.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ h5tools_str_sprint(h5tools_str_t *str, const h5tool_format_t *info, hid_t contai
729729
long double templdouble;
730730

731731
memcpy(&templdouble, vp, sizeof(long double));
732-
h5tools_str_append(str, "%Lg", templdouble);
732+
h5tools_str_append(str, OPT(info->fmt_ldouble, "%Lg"), templdouble);
733733
}
734734
else {
735735
size_t i;

tools/src/h5dump/h5dump.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct handler_t {
9898
*/
9999
/* The following initialization makes use of C language concatenating */
100100
/* "xxx" "yyy" into "xxxyyy". */
101-
static const char *s_opts = "a:b*c:d:ef:g:hik:l:m:n*o*pq:rs:t:uvw:xyz:A*BCD:E*F:G:HK:M:N:O*RS:VX:";
101+
static const char *s_opts = "a:b*c:d:ef:g:hik:l:m:n*o*pq:rs:t:uvw:xyz:A*BCD:E*F:G:HK:L:M:N:O*RS:VX:";
102102
static struct h5_long_options l_opts[] = {{"attribute", require_arg, 'a'},
103103
{"binary", optional_arg, 'b'},
104104
{"count", require_arg, 'c'},
@@ -134,6 +134,7 @@ static struct h5_long_options l_opts[] = {{"attribute", require_arg, 'a'},
134134
{"vds-gap-size", require_arg, 'G'},
135135
{"header", no_arg, 'H'},
136136
{"page-buffer-size", require_arg, 'K'},
137+
{"lformat", require_arg, 'L'},
137138
{"packed-bits", require_arg, 'M'},
138139
{"any_path", require_arg, 'N'},
139140
{"ddl", optional_arg, 'O'},
@@ -285,6 +286,8 @@ usage(const char *prog)
285286
PRINTVALSTREAM(rawoutstream, " -r, --string Print 1-byte integer datasets as ASCII\n");
286287
PRINTVALSTREAM(rawoutstream, " -y, --noindex Do not print array indices with the data\n");
287288
PRINTVALSTREAM(rawoutstream, " -m T, --format=T Set the floating point output format\n");
289+
PRINTVALSTREAM(rawoutstream,
290+
" -L T, --lformat=T Set the floating point long double output format\n");
288291
PRINTVALSTREAM(rawoutstream, " -q Q, --sort_by=Q Sort groups and attributes by index Q\n");
289292
PRINTVALSTREAM(rawoutstream, " -z Z, --sort_order=Z Sort groups and attributes by order Z\n");
290293
PRINTVALSTREAM(rawoutstream,
@@ -346,7 +349,9 @@ usage(const char *prog)
346349
PRINTVALSTREAM(rawoutstream, " F - is a filename.\n");
347350
PRINTVALSTREAM(rawoutstream, " P - is the full path from the root group to the object.\n");
348351
PRINTVALSTREAM(rawoutstream, " N - is an integer greater than 1.\n");
349-
PRINTVALSTREAM(rawoutstream, " T - is a string containing the floating point format, e.g '%%.3f'\n");
352+
PRINTVALSTREAM(rawoutstream, " T - is a string containing the floating point format, e.g '%%.3g'\n");
353+
PRINTVALSTREAM(rawoutstream,
354+
" T - is a string containing the floating point long double format, e.g '%%.3Lg'\n");
350355
PRINTVALSTREAM(rawoutstream, " U - is a URI reference (as defined in [IETF RFC 2396],\n");
351356
PRINTVALSTREAM(rawoutstream, " updated by [IETF RFC 2732])\n");
352357
PRINTVALSTREAM(rawoutstream,
@@ -1056,6 +1061,12 @@ parse_command_line(int argc, const char *const *argv)
10561061
h5tools_nCols = 0;
10571062
break;
10581063

1064+
case 'L':
1065+
/* specify alternative floating point long double printing format */
1066+
fp_lformat = H5_optarg;
1067+
h5tools_nCols = 0;
1068+
break;
1069+
10591070
case 'X':
10601071
/* specify XML namespace (default="hdf5:"), or none */
10611072
/* To Do: check format of this value? */

tools/src/h5dump/h5dump.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
* \li <strong>--string</strong> Print 1-byte integer datasets as ASCII
9696
* \li <strong>--noindex</strong> Do not print array indices with the data
9797
* \li <strong>--format=T</strong> Set the floating point output format
98+
* \li <strong>--lformat=T</strong> Set the floating point long double output format
9899
* \li <strong>--sort_by=Q</strong> Sort groups and attributes by index Q
99100
* \li <strong>--sort_order=Z</strong> Sort groups and attributes by order Z
100101
* \li <strong>--no-compact-subset</strong> Disable compact form of subsetting and allow the use
@@ -143,7 +144,8 @@
143144
* \li <strong>F</strong> - is a filename.
144145
* \li <strong>P</strong> - is the full path from the root group to the object.
145146
* \li <strong>N</strong> - is an integer greater than 1.
146-
* \li <strong>T</strong> - is a string containing the floating point format, e.g '%.3f'
147+
* \li <strong>T</strong> - is a string containing the floating point format, e.g '%.3g'
148+
* \li <strong>T</strong> - is a string containing the floating point long double format, e.g '%.3Lg'
147149
* \li <strong>U</strong> - is a URI reference (as defined in [IETF RFC 2396],
148150
* updated by [IETF RFC 2732])
149151
* \li <strong>B</strong> - is the form of binary output: NATIVE for a memory type, FILE for the
@@ -239,6 +241,7 @@ bool hit_elink = false; /* whether we have traversed an external link *
239241
size_t prefix_len = 1024;
240242
char *prefix = NULL;
241243
const char *fp_format = NULL;
244+
const char *fp_lformat = NULL;
242245

243246
/* things to display or which are set via command line parameters */
244247
typedef struct {

tools/src/h5dump/h5dump_ddl.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ dump_attr_cb(hid_t oid, const char *attr_name, const H5A_info_t H5_ATTR_UNUSED *
116116
string_dataformat.fmt_double = fp_format;
117117
string_dataformat.fmt_float = fp_format;
118118
}
119+
if (fp_lformat) {
120+
string_dataformat.fmt_ldouble = fp_lformat;
121+
}
119122

120123
if (h5tools_nCols == 0) {
121124
string_dataformat.line_ncols = 65535;
@@ -175,6 +178,9 @@ dump_all_cb(hid_t group, const char *name, const H5L_info2_t *linfo, void H5_ATT
175178
string_dataformat.fmt_double = fp_format;
176179
string_dataformat.fmt_float = fp_format;
177180
}
181+
if (fp_lformat) {
182+
string_dataformat.fmt_ldouble = fp_lformat;
183+
}
178184

179185
if (h5tools_nCols == 0) {
180186
string_dataformat.line_ncols = 65535;
@@ -646,6 +652,9 @@ dump_named_datatype(hid_t tid, const char *name)
646652
string_dataformat.fmt_double = fp_format;
647653
string_dataformat.fmt_float = fp_format;
648654
}
655+
if (fp_lformat) {
656+
string_dataformat.fmt_ldouble = fp_lformat;
657+
}
649658

650659
if (h5tools_nCols == 0) {
651660
string_dataformat.line_ncols = 65535;
@@ -802,6 +811,9 @@ dump_group(hid_t gid, const char *name)
802811
string_dataformat.fmt_double = fp_format;
803812
string_dataformat.fmt_float = fp_format;
804813
}
814+
if (fp_lformat) {
815+
string_dataformat.fmt_ldouble = fp_lformat;
816+
}
805817

806818
if (h5tools_nCols == 0) {
807819
string_dataformat.line_ncols = 65535;
@@ -928,6 +940,9 @@ dump_dataset(hid_t did, const char *name, struct subset_t *sset)
928940
string_dataformat.fmt_double = fp_format;
929941
string_dataformat.fmt_float = fp_format;
930942
}
943+
if (fp_lformat) {
944+
string_dataformat.fmt_ldouble = fp_lformat;
945+
}
931946

932947
if (h5tools_nCols == 0) {
933948
string_dataformat.line_ncols = 65535;
@@ -1098,6 +1113,9 @@ dump_data(hid_t obj_id, int obj_data, struct subset_t *sset, int display_index)
10981113
string_dataformat.fmt_double = fp_format;
10991114
string_dataformat.fmt_float = fp_format;
11001115
}
1116+
if (fp_lformat) {
1117+
string_dataformat.fmt_ldouble = fp_lformat;
1118+
}
11011119

11021120
if (h5tools_nCols == 0) {
11031121
string_dataformat.line_ncols = 65535;
@@ -1563,6 +1581,9 @@ handle_attributes(hid_t fid, const char *attr, void H5_ATTR_UNUSED *data, int H5
15631581
string_dataformat.fmt_double = fp_format;
15641582
string_dataformat.fmt_float = fp_format;
15651583
}
1584+
if (fp_lformat) {
1585+
string_dataformat.fmt_ldouble = fp_lformat;
1586+
}
15661587

15671588
if (h5tools_nCols == 0) {
15681589
string_dataformat.line_ncols = 65535;

tools/src/h5dump/h5dump_extern.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ extern bool hit_elink; /* whether we have traversed an external link */
5656
extern size_t prefix_len;
5757
extern char *prefix;
5858
extern const char *fp_format;
59+
extern const char *fp_lformat;
5960

6061
/* things to display or which are set via command line parameters */
6162
typedef struct {

tools/src/h5dump/h5dump_xml.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static h5tool_format_t xml_dataformat = {
4747
"%lu", /*fmt_ulong */
4848
NULL, /*fmt_llong */
4949
NULL, /*fmt_ullong */
50+
"%Lg", /*fmt_ldouble */
5051
"%g", /*fmt_double */
5152
"%g", /*fmt_float */
5253

@@ -152,6 +153,9 @@ xml_dump_all_cb(hid_t group, const char *name, const H5L_info2_t *linfo, void H5
152153
string_dataformat.fmt_double = fp_format;
153154
string_dataformat.fmt_float = fp_format;
154155
}
156+
if (fp_lformat) {
157+
string_dataformat.fmt_ldouble = fp_lformat;
158+
}
155159

156160
if (h5tools_nCols == 0) {
157161
string_dataformat.line_ncols = 65535;
@@ -885,6 +889,9 @@ xml_print_datatype(hid_t type, unsigned in_group)
885889
string_dataformat.fmt_double = fp_format;
886890
string_dataformat.fmt_float = fp_format;
887891
}
892+
if (fp_lformat) {
893+
string_dataformat.fmt_ldouble = fp_lformat;
894+
}
888895

889896
if (h5tools_nCols == 0) {
890897
string_dataformat.line_ncols = 65535;
@@ -1582,6 +1589,9 @@ xml_dump_datatype(hid_t type)
15821589
string_dataformat.fmt_double = fp_format;
15831590
string_dataformat.fmt_float = fp_format;
15841591
}
1592+
if (fp_lformat) {
1593+
string_dataformat.fmt_ldouble = fp_lformat;
1594+
}
15851595

15861596
if (h5tools_nCols == 0) {
15871597
string_dataformat.line_ncols = 65535;
@@ -1716,6 +1726,9 @@ xml_dump_dataspace(hid_t space)
17161726
string_dataformat.fmt_double = fp_format;
17171727
string_dataformat.fmt_float = fp_format;
17181728
}
1729+
if (fp_lformat) {
1730+
string_dataformat.fmt_ldouble = fp_lformat;
1731+
}
17191732

17201733
if (h5tools_nCols == 0) {
17211734
string_dataformat.line_ncols = 65535;
@@ -1889,6 +1902,9 @@ xml_dump_data(hid_t obj_id, int obj_data, struct subset_t H5_ATTR_UNUSED *sset,
18891902
string_dataformat.fmt_double = fp_format;
18901903
string_dataformat.fmt_float = fp_format;
18911904
}
1905+
if (fp_lformat) {
1906+
string_dataformat.fmt_ldouble = fp_lformat;
1907+
}
18921908

18931909
if (h5tools_nCols == 0) {
18941910
string_dataformat.line_ncols = 65535;
@@ -2056,6 +2072,9 @@ xml_dump_attr(hid_t attr, const char *attr_name, const H5A_info_t H5_ATTR_UNUSED
20562072
string_dataformat.fmt_double = fp_format;
20572073
string_dataformat.fmt_float = fp_format;
20582074
}
2075+
if (fp_lformat) {
2076+
string_dataformat.fmt_ldouble = fp_lformat;
2077+
}
20592078

20602079
if (h5tools_nCols == 0) {
20612080
string_dataformat.line_ncols = 65535;
@@ -2387,6 +2406,9 @@ xml_dump_named_datatype(hid_t type, const char *name)
23872406
string_dataformat.fmt_double = fp_format;
23882407
string_dataformat.fmt_float = fp_format;
23892408
}
2409+
if (fp_lformat) {
2410+
string_dataformat.fmt_ldouble = fp_lformat;
2411+
}
23902412

23912413
if (h5tools_nCols == 0) {
23922414
string_dataformat.line_ncols = 65535;
@@ -2606,6 +2628,9 @@ xml_dump_group(hid_t gid, const char *name)
26062628
string_dataformat.fmt_double = fp_format;
26072629
string_dataformat.fmt_float = fp_format;
26082630
}
2631+
if (fp_lformat) {
2632+
string_dataformat.fmt_ldouble = fp_lformat;
2633+
}
26092634

26102635
if (h5tools_nCols == 0) {
26112636
string_dataformat.line_ncols = 65535;
@@ -3010,6 +3035,9 @@ xml_print_refs(hid_t did, int source)
30103035
string_dataformat.fmt_double = fp_format;
30113036
string_dataformat.fmt_float = fp_format;
30123037
}
3038+
if (fp_lformat) {
3039+
string_dataformat.fmt_ldouble = fp_lformat;
3040+
}
30133041

30143042
if (h5tools_nCols == 0) {
30153043
string_dataformat.line_ncols = 65535;
@@ -3166,6 +3194,9 @@ xml_print_strs(hid_t did, int source)
31663194
string_dataformat.fmt_double = fp_format;
31673195
string_dataformat.fmt_float = fp_format;
31683196
}
3197+
if (fp_lformat) {
3198+
string_dataformat.fmt_ldouble = fp_lformat;
3199+
}
31693200

31703201
if (h5tools_nCols == 0) {
31713202
string_dataformat.line_ncols = 65535;
@@ -3281,6 +3312,9 @@ check_filters(hid_t dcpl)
32813312
string_dataformat.fmt_double = fp_format;
32823313
string_dataformat.fmt_float = fp_format;
32833314
}
3315+
if (fp_lformat) {
3316+
string_dataformat.fmt_ldouble = fp_lformat;
3317+
}
32843318

32853319
if (h5tools_nCols == 0) {
32863320
string_dataformat.line_ncols = 65535;
@@ -3422,6 +3456,9 @@ xml_dump_fill_value(hid_t dcpl, hid_t type)
34223456
string_dataformat.fmt_double = fp_format;
34233457
string_dataformat.fmt_float = fp_format;
34243458
}
3459+
if (fp_lformat) {
3460+
string_dataformat.fmt_ldouble = fp_lformat;
3461+
}
34253462

34263463
if (h5tools_nCols == 0) {
34273464
string_dataformat.line_ncols = 65535;
@@ -3803,6 +3840,9 @@ xml_dump_dataset(hid_t did, const char *name, struct subset_t H5_ATTR_UNUSED *ss
38033840
string_dataformat.fmt_double = fp_format;
38043841
string_dataformat.fmt_float = fp_format;
38053842
}
3843+
if (fp_lformat) {
3844+
string_dataformat.fmt_ldouble = fp_lformat;
3845+
}
38063846

38073847
if (h5tools_nCols == 0) {
38083848
string_dataformat.line_ncols = 65535;
@@ -4390,6 +4430,9 @@ xml_print_enum(hid_t type)
43904430
string_dataformat.fmt_double = fp_format;
43914431
string_dataformat.fmt_float = fp_format;
43924432
}
4433+
if (fp_lformat) {
4434+
string_dataformat.fmt_ldouble = fp_lformat;
4435+
}
43934436

43944437
if (h5tools_nCols == 0) {
43954438
string_dataformat.line_ncols = 65535;

0 commit comments

Comments
 (0)