summaryrefslogtreecommitdiffstats
path: root/java/src/jni/h5util.c
blob: 0d2999af0157699b201096b3f7eafe2c4aaf5a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 *  For details of the HDF libraries, see the HDF Documentation at:
 *    http://hdfdfgroup.org/HDF5/doc/
 *
 */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hdf5.h"
#include "h5util.h"

/* size of hyperslab buffer when a dataset is bigger than H5TOOLS_MALLOCSIZE */
hsize_t H5TOOLS_BUFSIZE = (32 * 1024 * 1024);  /* 32 MB */
int     H5TOOLS_TEXT_BLOCK = 16;  /* Number of elements on a line in a text export file */

JavaVM *jvm;
jobject visit_callback;
jobject copy_callback;
jobject close_callback;
jobject create_callback;
jobject compare_callback;
jobject get_callback;
jobject set_callback;
jobject delete_callback;

/********************/
/* Local Prototypes */
/********************/

static int     h5str_dump_region_blocks(h5str_t *str, hid_t region, hid_t region_obj);
static int     h5str_dump_region_points(h5str_t *str, hid_t region, hid_t region_obj);
static int     h5str_is_zero(const void *_mem, size_t size);
static hid_t   h5str_get_native_type(hid_t type);
static hid_t   h5str_get_little_endian_type(hid_t type);
static hid_t   h5str_get_big_endian_type(hid_t type);
static htri_t  h5str_detect_vlen(hid_t tid);
static htri_t  h5str_detect_vlen_str(hid_t tid);
static int     h5tools_dump_simple_data(FILE *stream, hid_t container, hid_t type, void *_mem, hsize_t nelmts);
static int     h5str_render_bin_output(FILE *stream, hid_t container, hid_t tid, void *_mem, hsize_t block_nelmts);
static int     render_bin_output_region_data_blocks(FILE *stream, hid_t region_id,
            hid_t container, int ndims, hid_t type_id, hssize_t nblocks, hsize_t *ptdata);
static int     render_bin_output_region_blocks(FILE *stream, hid_t region_space,
            hid_t region_id, hid_t container);
static int     render_bin_output_region_data_points(FILE *stream, hid_t region_space, hid_t region_id,
            hid_t container, int ndims, hid_t type_id, hssize_t npoints, hsize_t *ptdata);
static int     render_bin_output_region_points(FILE *stream, hid_t region_space,
            hid_t region_id, hid_t container);

/** frees memory held by array of strings */
void
h5str_array_free
    (char **strs, size_t len)
{
    size_t i;

    if (!strs || len <= 0)
        return;

    for (i = 0; i < len; i++) {
        if (*(strs + i))
            HDfree(*(strs + i));
    } /* for (i=0; i<n; i++)*/
    HDfree(strs);
} /* end h5str_array_free */

/** allocate a new str with given length */
void
h5str_new
    (h5str_t *str, size_t len)
{
    if (str && len > 0) {
        str->s = (char *)HDmalloc(len);
        str->max = len;
        str->s[0] = '\0';
    } /* end if */
} /* end h5str_new */

/** free string memory */
void
h5str_free
    (h5str_t *str)
{
    if (str && str->max > 0) {
        HDfree(str->s);
        HDmemset(str, 0, sizeof(h5str_t));
    } /* end if */
} /* end h5str_free */

/** reset the max size of the string */
void
h5str_resize
    (h5str_t *str, size_t new_len)
{
    char *new_str;

    if (!str || new_len <= 0 || str->max == new_len)
        return;

    new_str = (char *)HDmalloc(new_len);
    if (new_len > str->max) /* increase memory */
        HDstrcpy(new_str, str->s);
    else
        HDstrncpy(new_str, str->s, new_len - 1);

    HDfree(str->s);
    str->s = new_str;
    str->max = new_len;
} /* end h5str_resize */

/* appends a copy of the string pointed to by cstr to the h5str.
 Return Value:
 the char string point to str->s
 */
char*
h5str_append
    (h5str_t *str, const char* cstr)
{
    size_t len;

    if (!str)
        return NULL;
    else if (!cstr)
        return str->s;

    len = HDstrlen(str->s) + HDstrlen(cstr);
    while (len >= str->max) /* not enough to hold the new string, double the space */
    {
        h5str_resize(str, str->max * 2);
    }

    return HDstrcat(str->s, cstr);
} /* end h5str_append */

/** print value of a data point into string.
 Return Value:
 On success, the total number of characters printed is returned.
 On error, a negative number is returned.
 */
size_t
h5str_sprintf
    (h5str_t *str, hid_t container, hid_t tid, void *ptr, int expand_data)
{
    unsigned char   tmp_uchar = 0;
    char            tmp_char = 0;
    unsigned short  tmp_ushort = 0;
    short           tmp_short = 0;
    unsigned int    tmp_uint = 0;
    int             tmp_int = 0;
    unsigned long   tmp_ulong = 0;
    long            tmp_long = 0;
    unsigned long long tmp_ullong = 0;
    long long       tmp_llong = 0;
    float           tmp_float = 0.0;
    double          tmp_double = 0.0;
    long double     tmp_ldouble = 0.0;
    static char     fmt_llong[8], fmt_ullong[8];

    hid_t           mtid = -1;
    size_t          offset;
    size_t          nll;
    char           *this_str;
    size_t          this_strlen;
    int             n;
    hvl_t          *vlptr;
    char           *cptr = (char*) ptr;
    unsigned char  *ucptr = (unsigned char*) ptr;
    H5T_class_t     tclass = H5Tget_class(tid);
    size_t          size = H5Tget_size(tid);
    H5T_sign_t      nsign = H5Tget_sign(tid);
    int bdata_print = 0;

    if (!str || !ptr)
        return 0;

    /* Build default formats for long long types */
    if (!fmt_llong[0]) {
        sprintf(fmt_llong, "%%%sd", H5_PRINTF_LL_WIDTH);
        sprintf(fmt_ullong, "%%%su", H5_PRINTF_LL_WIDTH);
    } /* end if */

    this_str = NULL;
    this_strlen = 0;

    switch (tclass) {
    case H5T_FLOAT:
        if (sizeof(float) == size) {
            /* if (H5Tequal(tid, H5T_NATIVE_FLOAT)) */
            HDmemcpy(&tmp_float, ptr, sizeof(float));
            this_str = (char*)HDmalloc(25);
            sprintf(this_str, "%g", tmp_float);
        }
        else if (sizeof(double) == size) {
            /* if (H5Tequal(tid, H5T_NATIVE_DOUBLE)) */
            HDmemcpy(&tmp_double, ptr, sizeof(double));
            this_str = (char*)HDmalloc(25);
            sprintf(this_str, "%g", tmp_double);
        }
#if H5_SIZEOF_LONG_DOUBLE !=0
        else if (sizeof(long double) == size) {
            /* if (H5Tequal(tid, H5T_NATIVE_LDOUBLE)) */
            HDmemcpy(&tmp_ldouble, ptr, sizeof(long double));
            this_str = (char*)HDmalloc(27);
            sprintf(this_str, "%Lf", tmp_ldouble);
        }
#endif
        break;
    case H5T_STRING:
    {
        char *tmp_str;
        size = 0;

        if (H5Tis_variable_str(tid)) {
            tmp_str = *(char**) ptr;
            if (tmp_str != NULL)
                size = HDstrlen(tmp_str);
        }
        else {
            tmp_str = cptr;
        }

        /* Check for NULL pointer for string */
        if (tmp_str == NULL) {
            this_str = (char *)HDmalloc(5);
            HDstrncpy(this_str, "NULL", 4);
        }
        else {
            if (size > 0) {
                this_str = (char *)HDmalloc(size+1);
                HDstrncpy(this_str, tmp_str, size);
            }
        }
    }
    break;
    case H5T_INTEGER:
        if (sizeof(char) == size) {
            if(H5T_SGN_NONE == nsign) {
                /* if (H5Tequal(tid, H5T_NATIVE_UCHAR)) */
                HDmemcpy(&tmp_uchar, ptr, sizeof(unsigned char));
                this_str = (char*)HDmalloc(7);
                sprintf(this_str, "%u", tmp_uchar);
            }
            else {
                /* if (H5Tequal(tid, H5T_NATIVE_SCHAR)) */
                HDmemcpy(&tmp_char, ptr, sizeof(char));
                this_str = (char*)HDmalloc(7);
                sprintf(this_str, "%hhd", tmp_char);
            }
        }
        else if (sizeof(int) == size) {
            if(H5T_SGN_NONE == nsign) {
                /* if (H5Tequal(tid, H5T_NATIVE_UINT)) */
                HDmemcpy(&tmp_uint, ptr, sizeof(unsigned int));
                this_str = (char*)HDmalloc(14);
                sprintf(this_str, "%u", tmp_uint);
            }
            else {
                /* if (H5Tequal(tid, H5T_NATIVE_INT)) */
                HDmemcpy(&tmp_int, ptr, sizeof(int));
                this_str = (char*)HDmalloc(14);
                sprintf(this_str, "%d", tmp_int);
            }
        }
        else if (sizeof(short) == size) {
            if(H5T_SGN_NONE == nsign) {
                /* if (H5Tequal(tid, H5T_NATIVE_USHORT)) */
                HDmemcpy(&tmp_ushort, ptr, sizeof(unsigned short));
                this_str = (char*)HDmalloc(9);
                sprintf(this_str, "%u", tmp_ushort);
            }
            else {
                /* if (H5Tequal(tid, H5T_NATIVE_SHORT)) */
                HDmemcpy(&tmp_short, ptr, sizeof(short));
                this_str = (char*)HDmalloc(9);
                sprintf(this_str, "%d", tmp_short);
            }
        }
        else if (sizeof(long) == size) {
            if(H5T_SGN_NONE == nsign) {
                /* if (H5Tequal(tid, H5T_NATIVE_ULONG)) */
                HDmemcpy(&tmp_ulong, ptr, sizeof(unsigned long));
                this_str = (char*)HDmalloc(23);
                sprintf(this_str, "%lu", tmp_ulong);
            }
            else {
                /* if (H5Tequal(tid, H5T_NATIVE_LONG)) */
                HDmemcpy(&tmp_long, ptr, sizeof(long));
                this_str = (char*)HDmalloc(23);
                sprintf(this_str, "%ld", tmp_long);
            }
        }
        else if (sizeof(long long) == size) {
            if(H5T_SGN_NONE == nsign) {
                /* if (H5Tequal(tid, H5T_NATIVE_ULLONG)) */
                HDmemcpy(&tmp_ullong, ptr, sizeof(unsigned long long));
                this_str = (char*)HDmalloc(25);
                sprintf(this_str, fmt_ullong, tmp_ullong);
            }
            else {
                /* if (H5Tequal(tid, H5T_NATIVE_LLONG)) */
                HDmemcpy(&tmp_llong, ptr, sizeof(long long));
                this_str = (char*)HDmalloc(25);
                sprintf(this_str, fmt_llong, tmp_llong);
            }
        }
        break;
    case H5T_COMPOUND:
    {
        unsigned i;
        n = H5Tget_nmembers(tid);
        h5str_append(str, " {");

        for (i = 0; i < n; i++) {
            offset = H5Tget_member_offset(tid, i);
            mtid = H5Tget_member_type(tid, i);
            h5str_sprintf(str, container, mtid, cptr + offset, expand_data);
            if (i < n - 1)
                h5str_append(str, ", ");
            H5Tclose(mtid);
        }
        h5str_append(str, "} ");
    }
    break;
    case H5T_ENUM:
    {
        char enum_name[1024];
        if (H5Tenum_nameof(tid, ptr, enum_name, sizeof enum_name) >= 0) {
            h5str_append(str, enum_name);
        }
        else {
            size_t i;
            nll = H5Tget_size(tid);
            this_str = (char*)HDmalloc(4 * (nll + 1));

            if (1 == nll) {
                sprintf(this_str, "0x%02x", ucptr[0]);
            }
            else {
                for (i = 0; i < (int)nll; i++)
                    sprintf(this_str, "%s%02x", i ? ":" : "", ucptr[i]);
            }
        }
    }
    break;
    case H5T_REFERENCE:
        if (h5str_is_zero(ptr, size)) {
            h5str_append(str, "NULL");
        }
        else {
            if (H5R_DSET_REG_REF_BUF_SIZE == size) {
                /* if (H5Tequal(tid, H5T_STD_REF_DSETREG)) */
                /*
                 * Dataset region reference --
                 * show the type and the referenced object
                 */
                char         ref_name[1024];
                hid_t        region_obj;
                hid_t        region;
                H5S_sel_type region_type;

                /* get name of the dataset the region reference points to using H5Rget_name */
                region_obj = H5Rdereference2(container, H5P_DEFAULT, H5R_DATASET_REGION, ptr);
                if (region_obj >= 0) {
                    region = H5Rget_region(container, H5R_DATASET_REGION, ptr);
                    if (region >= 0) {
                        if(expand_data) {
                            region_type = H5Sget_select_type(region);
                            if(region_type==H5S_SEL_POINTS) {
                                h5str_dump_region_points_data(str, region, region_obj);
                            }
                            else {
                                h5str_dump_region_blocks_data(str, region, region_obj);
                            }
                        }
                        else {
                            if(H5Rget_name(region_obj, H5R_DATASET_REGION, ptr, (char*)ref_name, 1024) >= 0) {
                                h5str_append(str, ref_name);
                            }

                            region_type = H5Sget_select_type(region);

                            if(region_type==H5S_SEL_POINTS) {
                                h5str_append(str, " REGION_TYPE POINT");
                                h5str_dump_region_points(str, region, region_obj);
                            }
                            else {
                                h5str_append(str, " REGION_TYPE BLOCK");
                                h5str_dump_region_blocks(str, region, region_obj);
                            }
                        }

                        H5Sclose(region);
                    }
                    H5Dclose(region_obj);
                }
            }
            else if (H5R_OBJ_REF_BUF_SIZE == size) {
                /* if (H5Tequal(tid, H5T_STD_REF_OBJ)) */
                /*
                 * Object references -- show the type and OID of the referenced
                 * object.
                 */
                H5O_info_t  oi;
                hid_t       obj;

                this_str = (char*)HDmalloc(64);
                obj = H5Rdereference2(container, H5P_DEFAULT, H5R_OBJECT, ptr);
                H5Oget_info(obj, &oi);

                /* Print object data and close object */
                sprintf(this_str, "%u-%lu", (unsigned) oi.type, oi.addr);
                H5Oclose(obj);
            }
        }
        break;
    case H5T_ARRAY:
    {
        int rank = 0;
        hsize_t i, dims[H5S_MAX_RANK], total_elmts;
        h5str_append(str, "[ ");

        mtid = H5Tget_super(tid);
        size = H5Tget_size(mtid);
        rank = H5Tget_array_ndims(tid);

        H5Tget_array_dims2(tid, dims);

        total_elmts = 1;
        for (i = 0; i < rank; i++)
            total_elmts *= dims[i];

        for (i = 0; i < total_elmts; i++) {
            h5str_sprintf(str, container, mtid, cptr + i * size, expand_data);
            if (i < total_elmts - 1)
                h5str_append(str, ", ");
        }
        H5Tclose(mtid);
        h5str_append(str, "] ");
    }
    break;
    case H5T_VLEN:
    {
        unsigned int i;
        mtid = H5Tget_super(tid);
        size = H5Tget_size(mtid);

        vlptr = (hvl_t *) cptr;

        nll = vlptr->len;
        for (i = 0; i < (int)nll; i++) {
            h5str_sprintf(str, container, mtid, ((char *) (vlptr->p)) + i * size, expand_data);
            if (i < (int)nll - 1)
                h5str_append(str, ", ");
        }
        H5Tclose(mtid);
    }
    break;

    default:
    {
        /* All other types get printed as hexadecimal */
        size_t i;
        nll = H5Tget_size(tid);
        this_str = (char*)HDmalloc(4 * (nll + 1));

        if (1 == nll) {
            sprintf(this_str, "0x%02x", ucptr[0]);
        }
        else {
            for (i = 0; i < (int)nll; i++)
                sprintf(this_str, "%s%02x", i ? ":" : "", ucptr[i]);
        }
    }
    break;
    } /* end switch */

    if (this_str) {
        h5str_append(str, this_str);
        this_strlen = HDstrlen(str->s);
        HDfree(this_str);
    } /* end if */

    return this_strlen;
} /* end h5str_sprintf */

/*-------------------------------------------------------------------------
 * Purpose: Print the data values from a dataset referenced by region blocks.
 *
 * Description:
 *      This is a special case subfunction to print the data in a region reference of type blocks.
 *
 * Return:
 *      The function returns FAIL if there was an error, otherwise SUCEED
 *-------------------------------------------------------------------------
 */
static int
h5str_print_region_data_blocks
    (hid_t region_id, h5str_t *str, int ndims, hid_t type_id, hssize_t nblocks, hsize_t *ptdata)
{
    hsize_t     *dims1 = NULL;
    hsize_t     *start = NULL;
    hsize_t     *count = NULL;
    hsize_t      blkndx;
    hsize_t      total_size[H5S_MAX_RANK];
    hsize_t      numelem;
    hsize_t      numindex;
    size_t       jndx;
    unsigned     indx;
    size_t       type_size;
    int          ret_value = SUCCEED;
    hid_t        mem_space = -1;
    hid_t        sid1 = -1;
    void        *region_buf = NULL;

    /* Get the dataspace of the dataset */
    if((sid1 = H5Dget_space(region_id)) >= 0) {

        /* Allocate space for the dimension array */
        if((dims1 = (hsize_t *)HDmalloc(sizeof(hsize_t) * (size_t)ndims)) != NULL) {

            /* find the dimensions of each data space from the block coordinates */
            numelem = 1;
            for (jndx = 0; jndx < ndims; jndx++) {
                dims1[jndx] = ptdata[jndx + (size_t)ndims] - ptdata[jndx] + 1;
                numelem = dims1[jndx] * numelem;
            } /* end for */

            /* Create dataspace for reading buffer */
            if((mem_space = H5Screate_simple(ndims, dims1, NULL)) >= 0) {
                if((type_size = H5Tget_size(type_id)) > 0) {
                    if((region_buf = HDmalloc(type_size * (size_t)numelem)) != NULL) {
                        /* Select (x , x , ..., x ) x (y , y , ..., y ) hyperslab for reading memory dataset */
                        /*          1   2        n      1   2        n                                       */
                        if((start = (hsize_t *)HDmalloc(sizeof(hsize_t) * (size_t)ndims)) != NULL) {
                            if((count = (hsize_t *)HDmalloc(sizeof(hsize_t) * (size_t)ndims)) != NULL) {
                                for (blkndx = 0; blkndx < nblocks; blkndx++) {
                                    for (indx = 0; indx < ndims; indx++) {
                                        start[indx] = ptdata[indx + blkndx * (hsize_t)ndims * 2];
                                        count[indx] = dims1[indx];
                                    } /* end for */

                                    if(H5Sselect_hyperslab(sid1, H5S_SELECT_SET, start, NULL, count, NULL) >= 0) {
                                        if(H5Dread(region_id, type_id, mem_space, sid1, H5P_DEFAULT, region_buf) >= 0) {
                                            if(H5Sget_simple_extent_dims(mem_space, total_size, NULL) >= 0) {
                                                for (numindex = 0; numindex < numelem; numindex++) {
                                                    h5str_sprintf(str, region_id, type_id, ((char*)region_buf + numindex * type_size), 1);

                                                    if (numindex + 1 < numelem)
                                                        h5str_append(str, ", ");
                                                } /* end for (jndx = 0; jndx < numelem; jndx++, region_elmtno++, ctx.cur_elmt++) */
                                            } /* end if(H5Sget_simple_extent_dims(mem_space, total_size, NULL) >= 0) */
                                        } /* end if(H5Dread(region_id, type_id, mem_space, sid1, H5P_DEFAULT, region_buf) >= 0) */
                                    } /* end if(H5Sselect_hyperslab(sid1, H5S_SELECT_SET, start, NULL, count, NULL) >= 0) */
                                } /* end for (blkndx = 0; blkndx < nblocks; blkndx++) */

                                HDfree(count);
                            } /* end if((count = (hsize_t *) HDmalloc(sizeof(hsize_t) * ndims)) != NULL) */
                            else
                                ret_value = -1;

                            HDfree(start);
                        } /* end if((start = (hsize_t *) HDmalloc(sizeof(hsize_t) * ndims)) != NULL) */
                        else
                            ret_value = -1;

                        HDfree(region_buf);
                    } /* end if((region_buf = HDmalloc(type_size * (size_t)numelem)) != NULL) */
                    else
                        ret_value = -1;
                } /* end if((type_size = H5Tget_size(type_id)) > 0) */
                else
                    ret_value = -1;

                if(H5Sclose(mem_space) < 0)
                    ret_value = -1;
            } /* end if((mem_space = H5Screate_simple(ndims, dims1, NULL)) >= 0) */
            else
                ret_value = -1;

            HDfree(dims1);
        } /* end if((dims1 = (hsize_t *) HDmalloc(sizeof(hsize_t) * ndims)) != NULL) */
        else
            ret_value = -1;

        if(H5Sclose(sid1) < 0)
            ret_value = -1;
    } /* end if((sid1 = H5Dget_space(region_id)) >= 0) */
    else
        ret_value = -1;

    return ret_value;
} /* end h5str_print_region_data_blocks */

int
h5str_dump_region_blocks_data
    (h5str_t *str, hid_t region, hid_t region_id)
{
    int        ret_value = 0;
    hssize_t   nblocks;
    hsize_t    alloc_size;
    hsize_t   *ptdata;
    hid_t      dtype = -1;
    hid_t      type_id = -1;
    int        ndims = H5Sget_simple_extent_ndims(region);

    /*
     * This function fails if the region does not have blocks.
     */
    H5E_BEGIN_TRY {
        nblocks = H5Sget_select_hyper_nblocks(region);
    } H5E_END_TRY;

    /* Print block information */
    if (nblocks > 0) {
        alloc_size = (hsize_t)nblocks * (hsize_t)ndims * 2 * (hsize_t)sizeof(ptdata[0]);
        if (alloc_size == (hsize_t)((size_t) alloc_size)) {
            ptdata = (hsize_t *)HDmalloc((size_t) alloc_size);
            H5Sget_select_hyper_blocklist(region, (hsize_t) 0,
                    (hsize_t) nblocks, ptdata);


            if((dtype = H5Dget_type(region_id)) >= 0) {
                if((type_id = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) >= 0) {

                    h5str_print_region_data_blocks(region_id, str, ndims, type_id, nblocks, ptdata);

                    if(H5Tclose(type_id) < 0)
                        ret_value = -1;
                } /* end if((type_id = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) >= 0) */
                else
                    ret_value = -1;

                if(H5Tclose(dtype) < 0)
                    ret_value = -1;
            } /* end if((dtype = H5Dget_type(region_id)) >= 0) */
            else
                ret_value = -1;
            HDfree(ptdata);
        } /* if (alloc_size == (hsize_t)((size_t)alloc_size)) */
    } /* if (nblocks > 0) */

    return ret_value;
} /* end h5str_dump_region_blocks_data */

static int
h5str_dump_region_blocks
    (h5str_t *str, hid_t region, hid_t region_id)
{
    int        ret_value = 0;
    hssize_t   nblocks;
    hsize_t    alloc_size;
    hsize_t   *ptdata;
    hid_t      dtype = -1;
    hid_t      type_id = -1;
    char       tmp_str[256];
    int        ndims = H5Sget_simple_extent_ndims(region);

    /*
     * This function fails if the region does not have blocks.
     */
    H5E_BEGIN_TRY {
        nblocks = H5Sget_select_hyper_nblocks(region);
    } H5E_END_TRY;

    /* Print block information */
    if (nblocks > 0) {
        int i;

        alloc_size = (hsize_t)nblocks * (hsize_t)ndims * 2 * (hsize_t)sizeof(ptdata[0]);
        if (alloc_size == (hsize_t)((size_t) alloc_size)) {
            ptdata = (hsize_t *)HDmalloc((size_t) alloc_size);
            H5Sget_select_hyper_blocklist(region, (hsize_t) 0,
                    (hsize_t) nblocks, ptdata);

            h5str_append(str, " {");
            for (i = 0; i < nblocks; i++) {
                int j;

                h5str_append(str, " ");

                /* Start coordinates and opposite corner */
                for (j = 0; j < ndims; j++) {
                    tmp_str[0] = '\0';
                    sprintf(tmp_str, "%s%lu", j ? "," : "(",
                            (unsigned long) ptdata[i * 2 * ndims + j]);
                    h5str_append(str, tmp_str);
                }

                for (j = 0; j < ndims; j++) {
                    tmp_str[0] = '\0';
                    sprintf(tmp_str, "%s%lu", j ? "," : ")-(",
                            (unsigned long) ptdata[i * 2 * ndims + j + ndims]);
                    h5str_append(str, tmp_str);
                }
                h5str_append(str, ") ");
                tmp_str[0] = '\0';
            }
            h5str_append(str, " }");

            HDfree(ptdata);
        } /* if (alloc_size == (hsize_t)((size_t)alloc_size)) */
    } /* if (nblocks > 0) */

    return ret_value;
} /* end h5str_dump_region_blocks */

/*-------------------------------------------------------------------------
 * Purpose: Print the data values from a dataset referenced by region points.
 *
 * Description:
 *      This is a special case subfunction to print the data in a region reference of type points.
 *
 * Return:
 *      The function returns FAIL on error, otherwise SUCCEED
 *-------------------------------------------------------------------------
 */
static int
h5str_print_region_data_points
    (hid_t region_space, hid_t region_id, h5str_t *str, int ndims, hid_t type_id, hssize_t npoints, hsize_t *ptdata)
{
    hsize_t        *dims1 = NULL;
    hsize_t         total_size[H5S_MAX_RANK];
    size_t          jndx;
    size_t          type_size;
    int             ret_value = SUCCEED;
    hid_t           mem_space = -1;
    void           *region_buf = NULL;

    /* Allocate space for the dimension array */
    if((dims1 = (hsize_t *)HDmalloc(sizeof(hsize_t) * (size_t)ndims)) != NULL) {

        dims1[0] = (hsize_t)npoints;

        /* Create dataspace for reading buffer */
        if((mem_space = H5Screate_simple(1, dims1, NULL)) >= 0) {

            if((type_size = H5Tget_size(type_id)) > 0) {

                if((region_buf = HDmalloc(type_size * (size_t)npoints)) != NULL) {

                    if(H5Dread(region_id, type_id, mem_space, region_space, H5P_DEFAULT, region_buf) >= 0) {

                        for (jndx = 0; jndx < npoints; jndx++) {
                            if(H5Sget_simple_extent_dims(mem_space, total_size, NULL) >= 0) {

                                h5str_sprintf(str, region_id, type_id, ((char*)region_buf + jndx * type_size), 1);

                                if (jndx + 1 < npoints)
                                    h5str_append(str, ", ");

                            } /* end if(H5Sget_simple_extent_dims(mem_space, total_size, NULL) >= 0) */
                        } /* end for (jndx = 0; jndx < npoints; jndx++, elmtno++) */
                    } /* end if(H5Dread(region_id, type_id, mem_space, region_space, H5P_DEFAULT, region_buf) >= 0) */
                    else
                        ret_value = -1;

                    HDfree(region_buf);
                } /* end if((region_buf = HDmalloc(type_size * (size_t)npoints)) != NULL) */
                else
                    ret_value = -1;
            } /* end if((type_size = H5Tget_size(type_id)) > 0) */
            else
                ret_value = -1;

            if(H5Sclose(mem_space) < 0)
                ret_value = -1;
        } /* end if((mem_space = H5Screate_simple(1, dims1, NULL)) >= 0) */
        else
            ret_value = -1;
        HDfree(dims1);
    } /* end if((dims1 = (hsize_t *) HDmalloc(sizeof(hsize_t) * ndims)) != NULL) */
    else
        ret_value = -1;

    return ret_value;
} /* end h5str_print_region_data_points */

int
h5str_dump_region_points_data
    (h5str_t *str, hid_t region, hid_t region_id)
{
    int        ret_value = 0;
    hssize_t   npoints;
    hsize_t    alloc_size;
    hsize_t   *ptdata;
    hid_t      dtype = -1;
    hid_t      type_id = -1;
    int        ndims = H5Sget_simple_extent_ndims(region);

    /*
     * This function fails if the region does not have points.
     */
    H5E_BEGIN_TRY {
        npoints = H5Sget_select_elem_npoints(region);
    } H5E_END_TRY;

    /* Print point information */
    if (npoints > 0) {
        alloc_size = (hsize_t)npoints * (hsize_t)ndims * (hsize_t)sizeof(ptdata[0]);
        if (alloc_size == (hsize_t)((size_t) alloc_size)) {
            ptdata = (hsize_t *)HDmalloc((size_t) alloc_size);
            H5Sget_select_elem_pointlist(region, (hsize_t) 0,
                    (hsize_t) npoints, ptdata);

            if((dtype = H5Dget_type(region_id)) >= 0) {
                if((type_id = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) >= 0) {

                    h5str_print_region_data_points(region, region_id,
                            str, ndims, type_id, npoints, ptdata);

                    if(H5Tclose(type_id) < 0)
                        ret_value = -1;
                } /* end if((type_id = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) >= 0) */
                else
                    ret_value = -1;

                if(H5Tclose(dtype) < 0)
                    ret_value = -1;
            } /* end if((dtype = H5Dget_type(region_id)) >= 0) */
            else
                ret_value = -1;
            HDfree(ptdata);
        }
    }

    return ret_value;
} /* end h5str_dump_region_points_data */

static int
h5str_dump_region_points
    (h5str_t *str, hid_t region, hid_t region_id)
{
    int        ret_value = 0;
    hssize_t   npoints;
    hsize_t    alloc_size;
    hsize_t   *ptdata;
    char       tmp_str[256];
    hid_t      dtype = -1;
    hid_t      type_id = -1;
    int        ndims = H5Sget_simple_extent_ndims(region);

    /*
     * This function fails if the region does not have points.
     */
    H5E_BEGIN_TRY {
        npoints = H5Sget_select_elem_npoints(region);
    } H5E_END_TRY;

    /* Print point information */
    if (npoints > 0) {
        int i;

        alloc_size = (hsize_t)npoints * (hsize_t)ndims * (hsize_t)sizeof(ptdata[0]);
        if (alloc_size == (hsize_t)((size_t) alloc_size)) {
            ptdata = (hsize_t *)HDmalloc((size_t) alloc_size);
            H5Sget_select_elem_pointlist(region, (hsize_t) 0,
                    (hsize_t) npoints, ptdata);

            h5str_append(str, " {");
            for (i = 0; i < npoints; i++) {
                int j;

                h5str_append(str, " ");

                for (j = 0; j < ndims; j++) {
                    tmp_str[0] = '\0';
                    sprintf(tmp_str, "%s%lu", j ? "," : "(",
                            (unsigned long) (ptdata[i * ndims + j]));
                    h5str_append(str, tmp_str);
                } /* end for (j = 0; j < ndims; j++) */

                h5str_append(str, ") ");
            } /* end for (i = 0; i < npoints; i++) */
            h5str_append(str, " }");

            HDfree(ptdata);
        } /* end if (alloc_size == (hsize_t)((size_t) alloc_size)) */
    } /* end if (npoints > 0) */

    return ret_value;
} /* end h5str_dump_region_points */

static int
h5str_is_zero
    (const void *_mem, size_t size)
{
    const unsigned char *mem = (const unsigned char *) _mem;

    while (size-- > 0)
        if (mem[size])
            return 0;

    return 1;
} /* end h5str_is_zero */

/*-------------------------------------------------------------------------
 * Function: h5str_detect_vlen_str
 *
 * Purpose: Recursive check for variable length string of a datatype.
 *
 * Return:
 *    TRUE : type conatains any variable length string
 *    FALSE : type doesn't contain any variable length string
 *    Negative value: error occur
 *
 *-------------------------------------------------------------------------
 */
static htri_t
h5str_detect_vlen_str
    (hid_t tid)
{
    H5T_class_t tclass = H5T_NO_CLASS;
    htri_t ret = 0;

    ret = H5Tis_variable_str(tid);
    if((ret == 1) || (ret < 0))
        goto done;

    tclass = H5Tget_class(tid);
    if(tclass == H5T_ARRAY || tclass == H5T_VLEN) {
        hid_t btid = H5Tget_super(tid);

        if(btid < 0) {
            ret = (htri_t)btid;
            goto done;
        } /* end if */
        ret = h5str_detect_vlen_str(btid);
        if((ret == 1) || (ret < 0)) {
            H5Tclose(btid);
            goto done;
        } /* end if */
    } /* end if */
    else if(tclass == H5T_COMPOUND) {
        unsigned i = 0;
        int n = H5Tget_nmembers(tid);

        if(n < 0) {
            n = ret;
            goto done;
        } /* end if */

        for(i = 0; i < n; i++) {
            hid_t mtid = H5Tget_member_type(tid, i);

            ret = h5str_detect_vlen_str(mtid);
            if((ret == 1) || (ret < 0)) {
                H5Tclose(mtid);
                goto done;
            }
            H5Tclose(mtid);
        } /* end for */
    } /* end else */

done:
    return ret;
} /* end h5str_detect_vlen_str */

/*-------------------------------------------------------------------------
 * Function: h5str_get_native_type
 *
 * Purpose: Wrapper around H5Tget_native_type() to work around
 *          Problems with bitfields.
 *
 * Return: Success:    datatype ID
 *         Failure:    FAIL
 *-------------------------------------------------------------------------
 */
static hid_t
h5str_get_native_type
    (hid_t type)
{
    hid_t p_type;
    H5T_class_t type_class;

    type_class = H5Tget_class(type);
    if(type_class==H5T_BITFIELD)
        p_type=H5Tcopy(type);
    else
        p_type = H5Tget_native_type(type,H5T_DIR_DEFAULT);

    return(p_type);
} /* end h5str_get_native_type */


/*-------------------------------------------------------------------------
 * Function: h5str_get_little_endian_type
 *
 * Purpose: Get a little endian type from a file type
 *
 * Return: Success:    datatype ID
 *         Failure:    FAIL
 *-------------------------------------------------------------------------
 */
static hid_t
h5str_get_little_endian_type
    (hid_t tid)
{
    hid_t       p_type=-1;
    H5T_class_t type_class;
    size_t      size;
    H5T_sign_t  sign;

    type_class = H5Tget_class(tid);
    size       = H5Tget_size(tid);
    sign       = H5Tget_sign(tid);

    switch( type_class )
    {
    case H5T_INTEGER:
    {
        if ( size == 1 && sign == H5T_SGN_2)
            p_type=H5Tcopy(H5T_STD_I8LE);
        else if ( size == 2 && sign == H5T_SGN_2)
            p_type=H5Tcopy(H5T_STD_I16LE);
        else if ( size == 4 && sign == H5T_SGN_2)
            p_type=H5Tcopy(H5T_STD_I32LE);
        else if ( size == 8 && sign == H5T_SGN_2)
            p_type=H5Tcopy(H5T_STD_I64LE);
        else if ( size == 1 && sign == H5T_SGN_NONE)
            p_type=H5Tcopy(H5T_STD_U8LE);
        else if ( size == 2 && sign == H5T_SGN_NONE)
            p_type=H5Tcopy(H5T_STD_U16LE);
        else if ( size == 4 && sign == H5T_SGN_NONE)
            p_type=H5Tcopy(H5T_STD_U32LE);
        else if ( size == 8 && sign == H5T_SGN_NONE)
            p_type=H5Tcopy(H5T_STD_U64LE);
    }
    break;

    case H5T_FLOAT:
        if ( size == 4)
            p_type=H5Tcopy(H5T_IEEE_F32LE);
        else if ( size == 8)
            p_type=H5Tcopy(H5T_IEEE_F64LE);
        break;

    case H5T_TIME:
    case H5T_BITFIELD:
    case H5T_OPAQUE:
    case H5T_STRING:
    case H5T_COMPOUND:
    case H5T_REFERENCE:
    case H5T_ENUM:
    case H5T_VLEN:
    case H5T_ARRAY:
        break;

    default:
        break;

    }

    return(p_type);
} /* end h5str_get_little_endian_type */

/*-------------------------------------------------------------------------
 * Function: h5str_get_big_endian_type
 *
 * Purpose: Get a big endian type from a file type
 *
 * Return: Success:    datatype ID
 *         Failure:    FAIL
 *-------------------------------------------------------------------------
 */
static hid_t
h5str_get_big_endian_type
    (hid_t tid)
{
    hid_t       p_type=-1;
    H5T_class_t type_class;
    size_t      size;
    H5T_sign_t  sign;

    type_class = H5Tget_class(tid);
    size       = H5Tget_size(tid);
    sign       = H5Tget_sign(tid);

    switch( type_class )
    {
    case H5T_INTEGER:
    {
        if ( size == 1 && sign == H5T_SGN_2)
            p_type=H5Tcopy(H5T_STD_I8BE);
        else if ( size == 2 && sign == H5T_SGN_2)
            p_type=H5Tcopy(H5T_STD_I16BE);
        else if ( size == 4 && sign == H5T_SGN_2)
            p_type=H5Tcopy(H5T_STD_I32BE);
        else if ( size == 8 && sign == H5T_SGN_2)
            p_type=H5Tcopy(H5T_STD_I64BE);
        else if ( size == 1 && sign == H5T_SGN_NONE)
            p_type=H5Tcopy(H5T_STD_U8BE);
        else if ( size == 2 && sign == H5T_SGN_NONE)
            p_type=H5Tcopy(H5T_STD_U16BE);
        else if ( size == 4 && sign == H5T_SGN_NONE)
            p_type=H5Tcopy(H5T_STD_U32BE);
        else if ( size == 8 && sign == H5T_SGN_NONE)
            p_type=H5Tcopy(H5T_STD_U64BE);
    }
    break;

    case H5T_FLOAT:
        if ( size == 4)
            p_type=H5Tcopy(H5T_IEEE_F32BE);
        else if ( size == 8)
            p_type=H5Tcopy(H5T_IEEE_F64BE);
        break;

    case H5T_TIME:
    case H5T_BITFIELD:
    case H5T_OPAQUE:
    case H5T_STRING:
    case H5T_COMPOUND:
    case H5T_REFERENCE:
    case H5T_ENUM:
    case H5T_VLEN:
    case H5T_ARRAY:
        break;

    default:
        break;

    }

    return(p_type);
} /* end h5str_get_big_endian_type */

/*-------------------------------------------------------------------------
 * Function: h5str_detect_vlen
 *
 * Purpose: Recursive check for any variable length data in given type.
 *
 * Return:
 *    1 : type conatains any variable length data
 *    0 : type doesn't contain any variable length data
 *    Negative value: error occur
 *-------------------------------------------------------------------------
 */
static htri_t
h5str_detect_vlen
    (hid_t tid)
{
    htri_t ret;

    /* recursive detect any vlen data values in type (compound, array ...) */
    ret = H5Tdetect_class(tid, H5T_VLEN);
    if((ret == 1) || (ret < 0))
        goto done;

    /* recursive detect any vlen string in type (compound, array ...) */
    ret = h5str_detect_vlen_str(tid);
    if((ret == 1) || (ret < 0))
        goto done;

done:
    return ret;
} /* end h5str_detect_vlen */

/*-------------------------------------------------------------------------
 * Function: render_bin_output
 *
 * Purpose: Write one element of memory buffer to a binary file stream
 *
 * Return: Success:    SUCCEED
 *         Failure:    FAIL
 *-------------------------------------------------------------------------
 */
static int
h5str_render_bin_output
    (FILE *stream, hid_t container, hid_t tid, void *_mem, hsize_t block_nelmts)
{
    int                ret_value = 0;
    unsigned char     *mem  = (unsigned char*)_mem;
    size_t             size;   /* datum size */
    hsize_t            block_index;
    H5T_class_t        type_class;

    if((size = H5Tget_size(tid)) > 0) {

        if((type_class = H5Tget_class(tid)) >= 0) {

            switch (type_class) {
                case H5T_INTEGER:
                case H5T_FLOAT:
                case H5T_ENUM:
                    block_index = block_nelmts * size;
                    while(block_index > 0) {
                        size_t bytes_in        = 0;    /* # of bytes to write  */
                        size_t bytes_wrote     = 0;    /* # of bytes written   */
                        size_t item_size       = size;    /* size of items in bytes */

                        if(block_index > sizeof(size_t))
                            bytes_in = sizeof(size_t);
                        else
                            bytes_in = (size_t)block_index;

                        bytes_wrote = fwrite(mem, 1, bytes_in, stream);

                        if(bytes_wrote != bytes_in || (0 == bytes_wrote && ferror(stream))) {
                            ret_value = -1;
                            break;
                        }

                        block_index -= (hsize_t)bytes_wrote;
                        mem = mem + bytes_wrote;
                    }
                    break;
                case H5T_STRING:
                {
                    unsigned int    i;
                    H5T_str_t       pad;
                    char           *s;
                    unsigned char   tempuchar;

                    pad = H5Tget_strpad(tid);

                    for (block_index = 0; block_index < block_nelmts; block_index++) {
                        mem = ((unsigned char*)_mem) + block_index * size;

                        if (H5Tis_variable_str(tid)) {
                            s = *(char**) mem;
                            if (s != NULL)
                                size = strlen(s);
                        }
                        else {
                            s = (char *) mem;
                        }
                        for (i = 0; i < size && (s[i] || pad != H5T_STR_NULLTERM); i++) {
                            HDmemcpy(&tempuchar, &s[i], sizeof(unsigned char));
                            if (1 != fwrite(&tempuchar, sizeof(unsigned char), 1, stream)) {
                                ret_value = -1;
                                break;
                            }
                        } /* i */
                        if(ret_value < 0)
                            break;
                    } /* for (block_index = 0; block_index < block_nelmts; block_index++) */
                }
                break;
                case H5T_COMPOUND:
                {
                    unsigned j;
                    hid_t    memb;
                    unsigned nmembs;
                    size_t   offset;

                    nmembs = (unsigned)H5Tget_nmembers(tid);

                    for (block_index = 0; block_index < block_nelmts; block_index++) {
                        mem = ((unsigned char*)_mem) + block_index * size;
                        for (j = 0; j < nmembs; j++) {
                            offset = H5Tget_member_offset(tid, j);
                            memb   = H5Tget_member_type(tid, j);

                            if (h5str_render_bin_output(stream, container, memb, mem + offset, 1) < 0) {
                                H5Tclose(memb);
                                ret_value = -1;
                                break;
                            }

                            H5Tclose(memb);
                        }
                        if(ret_value < 0)
                            break;
                    }
                }
                break;
                case H5T_ARRAY:
                {
                    int     k, ndims;
                    hsize_t dims[H5S_MAX_RANK], temp_nelmts, nelmts;
                    hid_t   memb;

                    /* get the array's base datatype for each element */
                    memb = H5Tget_super(tid);
                    ndims = H5Tget_array_ndims(tid);
                    H5Tget_array_dims2(tid, dims);

                    /* calculate the number of array elements */
                    for (k = 0, nelmts = 1; k < ndims; k++) {
                        temp_nelmts = nelmts;
                        temp_nelmts *= dims[k];
                        nelmts = (size_t) temp_nelmts;
                    }

                    for (block_index = 0; block_index < block_nelmts; block_index++) {
                        mem = ((unsigned char*)_mem) + block_index * size;
                        /* dump the array element */
                        if (h5str_render_bin_output(stream, container, memb, mem, nelmts) < 0) {
                            ret_value = -1;
                            break;
                        }
                    }
                    H5Tclose(memb);
                }
                break;
                case H5T_VLEN:
                {
                    hsize_t      nelmts;
                    hid_t        memb;

                    /* get the VL sequences's base datatype for each element */
                    memb = H5Tget_super(tid);

                    for (block_index = 0; block_index < block_nelmts; block_index++) {
                        mem = ((unsigned char*)_mem) + block_index * size;
                        /* Get the number of sequence elements */
                        nelmts = ((hvl_t *) mem)->len;

                        /* dump the array element */
                        if (h5str_render_bin_output(stream, container, memb, ((char *) (((hvl_t *) mem)->p)), nelmts) < 0) {
                            ret_value = -1;
                            break;
                        }
                    }
                    H5Tclose(memb);
                }
                break;
                case H5T_REFERENCE:
                {
                    if (H5Tequal(tid, H5T_STD_REF_DSETREG)) {
                        /* region data */
                        hid_t   region_id, region_space;
                        H5S_sel_type region_type;

                        for (block_index = 0; block_index < block_nelmts; block_index++) {
                            mem = ((unsigned char*)_mem) + block_index * size;
                            region_id = H5Rdereference2(container, H5P_DEFAULT, H5R_DATASET_REGION, mem);
                            if (region_id >= 0) {
                                region_space = H5Rget_region(container, H5R_DATASET_REGION, mem);
                                if (region_space >= 0) {
                                    region_type = H5Sget_select_type(region_space);
                                    if(region_type == H5S_SEL_POINTS)
                                        ret_value = render_bin_output_region_points(stream, region_space, region_id, container);
                                    else
                                        ret_value = render_bin_output_region_blocks(stream, region_space, region_id, container);
                                    H5Sclose(region_space);
                                } /* end if (region_space >= 0) */
                                H5Dclose(region_id);
                            } /* end if (region_id >= 0) */
                            if(ret_value < 0)
                                break;
                        }
                    }
                    else if (H5Tequal(tid, H5T_STD_REF_OBJ)) {
                        ;
                    }
                }
                break;
                default:
                    for (block_index = 0; block_index < block_nelmts; block_index++) {
                        mem = ((unsigned char*)_mem) + block_index * size;
                        if (size != fwrite(mem, sizeof(char), size, stream)) {
                            ret_value = -1;
                            break;
                        }
                    }
                    break;
            }
        } /* end if((type_class = H5Tget_class(tid)) >= 0) */
        else
            ret_value = -1;
    } /* end if((size = H5Tget_size(tid)) > 0) */
    else
        ret_value = -1;

    return ret_value;
} /* end h5str_render_bin_output */

/*-------------------------------------------------------------------------
 * Purpose: Print the data values from a dataset referenced by region blocks.
 *
 * Description:
 *      This is a special case subfunction to print the data in a region reference of type blocks.
 *
 * Return:
 *      The function returns FAIL if there was an error, otherwise SUCEED
 *
 *-------------------------------------------------------------------------
 */
static int
render_bin_output_region_data_blocks
    (FILE *stream, hid_t region_id, hid_t container, int ndims, hid_t type_id, hssize_t nblocks, hsize_t *ptdata)
{
    hsize_t     *dims1 = NULL;
    hsize_t     *start = NULL;
    hsize_t     *count = NULL;
    hsize_t      numelem;
    hsize_t      total_size[H5S_MAX_RANK];
    int          jndx;
    size_t       type_size;
    hid_t        mem_space = -1;
    void        *region_buf = NULL;
    int          blkndx;
    hid_t        sid1 = -1;
    int          ret_value = SUCCEED;

    /* Get the dataspace of the dataset */
    if((sid1 = H5Dget_space(region_id)) >= 0) {
        /* Allocate space for the dimension array */
        if((dims1 = (hsize_t *)HDmalloc(sizeof(hsize_t) * (size_t)ndims)) != NULL) {
            /* find the dimensions of each data space from the block coordinates */
            numelem = 1;
            for (jndx = 0; jndx < ndims; jndx++) {
                dims1[jndx] = ptdata[jndx + ndims] - ptdata[jndx] + 1;
                numelem = dims1[jndx] * numelem;
            }

            /* Create dataspace for reading buffer */
            if((mem_space = H5Screate_simple(ndims, dims1, NULL)) >= 0) {
                if((type_size = H5Tget_size(type_id)) > 0) {
                    if((region_buf = HDmalloc(type_size * (size_t)numelem)) != NULL) {
                        /* Select (x , x , ..., x ) x (y , y , ..., y ) hyperslab for reading memory dataset */
                        /*          1   2        n      1   2        n                                       */
                        if((start = (hsize_t *)HDmalloc(sizeof(hsize_t) * (size_t)ndims)) != NULL) {
                            if((count = (hsize_t *)HDmalloc(sizeof(hsize_t) * (size_t)ndims)) != NULL) {
                                for (blkndx = 0; blkndx < nblocks; blkndx++) {
                                    for (jndx = 0; jndx < ndims; jndx++) {
                                        start[jndx] = ptdata[jndx + blkndx * ndims * 2];
                                        count[jndx] = dims1[jndx];
                                    }

                                    if(H5Sselect_hyperslab(sid1, H5S_SELECT_SET, start, NULL, count, NULL) >= 0) {
                                        if(H5Dread(region_id, type_id, mem_space, sid1, H5P_DEFAULT, region_buf) >= 0) {
                                            if(H5Sget_simple_extent_dims(mem_space, total_size, NULL) >= 0) {
                                                ret_value = h5str_render_bin_output(stream, container, type_id, (char*)region_buf, numelem);
                                            } /* end if(H5Sget_simple_extent_dims(mem_space, total_size, NULL) >= 0) */
                                            else {
                                                ret_value = -1;
                                                break;
                                            }
                                        } /* end if(H5Dread(region_id, type_id, mem_space, sid1, H5P_DEFAULT, region_buf) >= 0) */
                                        else {
                                            ret_value = -1;
                                            break;
                                        }
                                    } /* end if(H5Sselect_hyperslab(sid1, H5S_SELECT_SET, start, NULL, count, NULL) >= 0) */
                                    else {
                                        ret_value = -1;
                                        break;
                                    }
                                    /* Render the region data element end */
                                } /* end for (blkndx = 0; blkndx < nblocks; blkndx++) */

                                HDfree(count);
                            } /* end if((count = (hsize_t *) HDmalloc(sizeof(hsize_t) * ndims)) != NULL) */
                            else
                                ret_value = -1;
                            HDfree(start);
                        } /* end if((start = (hsize_t *) HDmalloc(sizeof(hsize_t) * ndims)) != NULL) */
                        else
                            ret_value = -1;
                        HDfree(region_buf);
                    } /* end if((region_buf = HDmalloc(type_size * (size_t)numelem)) != NULL) */
                    else
                        ret_value = -1;
                } /* end if((type_size = H5Tget_size(type_id)) > 0) */
                else
                    ret_value = -1;

                if(H5Sclose(mem_space) < 0)
                    ret_value = -1;
            } /* end if((mem_space = H5Screate_simple(ndims, dims1, NULL)) >= 0) */
            else
                ret_value = -1;
            HDfree(dims1);
        } /* end if((dims1 = (hsize_t *) HDmalloc(sizeof(hsize_t) * ndims)) != NULL) */
        else
            ret_value = -1;
        if(H5Sclose(sid1) < 0)
            ret_value = -1;
    } /* end if((sid1 = H5Dget_space(region_id)) >= 0) */
    else
        ret_value = -1;

    return ret_value;
} /* end render_bin_output_region_data_blocks */

/*-------------------------------------------------------------------------
 * Purpose: Print some values from a dataset referenced by region blocks.
 *
 * Description:
 *      This is a special case subfunction to dump a region reference using blocks.
 *
 * Return:
 *      The function returns False if ERROR, otherwise True
 *
 *-------------------------------------------------------------------------
 */
static int
render_bin_output_region_blocks
    (FILE *stream, hid_t region_space, hid_t region_id, hid_t container)
{
    int          ret_value = SUCCEED;
    hssize_t     nblocks;
    hsize_t      alloc_size;
    hsize_t     *ptdata = NULL;
    int          ndims;
    hid_t        dtype;
    hid_t        type_id;

    if((nblocks = H5Sget_select_hyper_nblocks(region_space)) > 0) {
        /* Print block information */
        if((ndims = H5Sget_simple_extent_ndims(region_space)) >= 0) {
            alloc_size = (hsize_t)nblocks * (hsize_t)ndims * 2 * (hsize_t)sizeof(ptdata[0]);
            if((ptdata = (hsize_t*)HDmalloc((size_t)alloc_size)) != NULL) {
                if(H5Sget_select_hyper_blocklist(region_space, (hsize_t)0, (hsize_t)nblocks, ptdata) >= 0) {
                    if((dtype = H5Dget_type(region_id)) >= 0) {
                        if((type_id = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) >= 0) {
                            ret_value = render_bin_output_region_data_blocks(stream, region_id, container, ndims,
                                    type_id, nblocks, ptdata);

                            if(H5Tclose(type_id) < 0)
                                ret_value = -1;
                        } /* end if((type_id = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) >= 0) */
                        else
                            ret_value = -1;

                        if(H5Tclose(dtype) < 0)
                            ret_value = -1;
                    } /* end if((dtype = H5Dget_type(region_id)) >= 0) */
                    else
                        ret_value = -1;
                } /* end if(H5Sget_select_hyper_blocklist(region_space, (hsize_t) 0, (hsize_t) nblocks, ptdata) >= 0) */
                else
                    ret_value = -1;

                HDfree(ptdata);
            } /* end if((ptdata = (hsize_t*) HDmalloc((size_t) alloc_size)) != NULL) */
            else
                ret_value = -1;
        } /* end if((ndims = H5Sget_simple_extent_ndims(region_space)) >= 0) */
        else
            ret_value = -1;
    } /* end if((nblocks = H5Sget_select_hyper_nblocks(region_space)) > 0) */
    else
        ret_value = -1;

    return ret_value;
} /* end render_bin_output_region_blocks */

/*-------------------------------------------------------------------------
 * Purpose: Print the data values from a dataset referenced by region points.
 *
 * Description:
 *      This is a special case subfunction to print the data in a region reference of type points.
 *
 * Return:
 *      The function returns FAIL on error, otherwise SUCCEED
 *
 *-------------------------------------------------------------------------
 */
static int
render_bin_output_region_data_points
    (FILE *stream, hid_t region_space, hid_t region_id,
        hid_t container, int ndims, hid_t type_id, hssize_t npoints, hsize_t *ptdata)
{
    hsize_t *dims1 = NULL;
    size_t   type_size;
    hid_t    mem_space = -1;
    void    *region_buf = NULL;
    int      ret_value = SUCCEED;

    if((type_size = H5Tget_size(type_id)) > 0) {
        if((region_buf = HDmalloc(type_size * (size_t)npoints)) != NULL) {
            /* Allocate space for the dimension array */
            if((dims1 = (hsize_t *)HDmalloc(sizeof(hsize_t) * (size_t)ndims)) != NULL) {
                dims1[0] = (hsize_t)npoints;
                if((mem_space = H5Screate_simple(1, dims1, NULL)) >= 0) {
                    if(H5Dread(region_id, type_id, mem_space, region_space, H5P_DEFAULT, region_buf) >= 0) {
                        if(H5Sget_simple_extent_dims(region_space, dims1, NULL) >= 0) {
                            ret_value = h5str_render_bin_output(stream, container, type_id, (char*)region_buf, (hsize_t)npoints);
                        } /* end if(H5Sget_simple_extent_dims(region_space, dims1, NULL) >= 0) */
                        else
                            ret_value = -1;
                    } /* end if(H5Dread(region_id, type_id, mem_space, region_space, H5P_DEFAULT, region_buf) >= 0) */
                    else
                        ret_value = -1;
                } /* end if((mem_space = H5Screate_simple(1, dims1, NULL)) >= 0) */
                else
                    ret_value = -1;

                HDfree(dims1);
            } /* end if((dims1 = (hsize_t *) malloc(sizeof(hsize_t) * ndims)) != NULL) */
            else
                ret_value = -1;
            HDfree(region_buf);
        } /* end if((region_buf = malloc(type_size * (size_t)npoints)) != NULL) */
        else
            ret_value = -1;

        if(H5Sclose(mem_space) < 0)
            ret_value = -1;
    } /* end if((type_size = H5Tget_size(type_id)) > 0) */
    else
        ret_value = -1;

    return ret_value;
} /* end render_bin_output_region_data_points */

/*-------------------------------------------------------------------------
 * Purpose: Print some values from a dataset referenced by region points.
 *
 * Description:
 *      This is a special case subfunction to dump a region reference using points.
 *
 * Return:
 *      The function returns False if the last dimension has been reached, otherwise True
 *
 *-------------------------------------------------------------------------
 */
static int
render_bin_output_region_points
    (FILE *stream, hid_t region_space, hid_t region_id, hid_t container)
{
    int      ret_value = SUCCEED;
    hssize_t npoints;
    hsize_t  alloc_size;
    hsize_t *ptdata;
    int      ndims;
    hid_t    dtype;
    hid_t    type_id;

    if((npoints = H5Sget_select_elem_npoints(region_space)) > 0) {
        /* Allocate space for the dimension array */
        if((ndims = H5Sget_simple_extent_ndims(region_space)) >= 0) {
            alloc_size = (hsize_t)npoints * (hsize_t)ndims * (hsize_t)sizeof(ptdata[0]);
            if(NULL != (ptdata = (hsize_t *)HDmalloc((size_t)alloc_size))) {
                if(H5Sget_select_elem_pointlist(region_space, (hsize_t)0, (hsize_t)npoints, ptdata) >= 0) {
                    if((dtype = H5Dget_type(region_id)) >= 0) {
                        if((type_id = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) >= 0) {
                            ret_value = render_bin_output_region_data_points(stream, region_space, region_id,
                                    container, ndims, type_id, npoints, ptdata);

                            if(H5Tclose(type_id) < 0)
                                ret_value = -1;
                        } /* end if((type_id = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) >= 0) */
                        else
                            ret_value = -1;

                        if(H5Tclose(dtype) < 0)
                            ret_value = -1;
                    } /* end if((dtype = H5Dget_type(region_id)) >= 0) */
                    else
                        ret_value = -1;
                } /* end if(H5Sget_select_elem_pointlist(region_space, (hsize_t) 0, (hsize_t) npoints, ptdata) >= 0) */
                else
                    ret_value = -1;

                HDfree(ptdata);
            } /* end if(NULL != (ptdata = (hsize_t *)HDmalloc((size_t) alloc_size))) */
            else
                ret_value = -1;
        } /* end if((ndims = H5Sget_simple_extent_ndims(region_space)) >= 0) */
        else
            ret_value = -1;

    } /* end if((npoints = H5Sget_select_elem_npoints(region_space)) > 0) */
    else
        ret_value = -1;

    return ret_value;
} /* end render_bin_output_region_points */

int
h5str_dump_simple_dset
    (FILE *stream, hid_t dset, int binary_order)
{
    int      ret_value = SUCCEED;
    hid_t               f_space = -1;                  /* file data space */
    hsize_t             elmtno;                   /* counter  */
    size_t              i;                        /* counter  */
    int                 ndims;
    int                 carry;                    /* counter carry value */
    hsize_t             zero[8];                  /* vector of zeros */
    hsize_t             total_size[H5S_MAX_RANK]; /* total size of dataset*/

    /* Print info */
    size_t              p_type_nbytes;            /* size of memory type */
    hsize_t             p_nelmts;                 /* total selected elmts */

    /* Stripmine info */
    hsize_t             sm_size[H5S_MAX_RANK];    /* stripmine size */
    hsize_t             sm_nbytes;                /* bytes per stripmine */
    hsize_t             sm_nelmts;                /* elements per stripmine*/
    unsigned char      *sm_buf = NULL;            /* buffer for raw data */
    hid_t               sm_space = -1;                 /* stripmine data space */

    /* Hyperslab info */
    hsize_t             hs_offset[H5S_MAX_RANK];  /* starting offset */
    hsize_t             hs_size[H5S_MAX_RANK];    /* size this pass */
    hsize_t             hs_nelmts;                /* elements in request */

    /* VL data special information */
    unsigned int        vl_data = 0; /* contains VL datatypes */
    hid_t               p_type = -1;
    hid_t               f_type = -1;

    if(dset < 0) return -1;
    f_type = H5Dget_type(dset);
    if (binary_order == 1)
        p_type = h5str_get_native_type(f_type);
    else if (binary_order == 2)
        p_type = h5str_get_little_endian_type(f_type);
    else if (binary_order == 3)
        p_type = h5str_get_big_endian_type(f_type);
    else
        p_type = H5Tcopy(f_type);

    H5Tclose(f_type);

    if (p_type >= 0) {
        if((f_space = H5Dget_space(dset)) >= 0) {
            ndims = H5Sget_simple_extent_ndims(f_space);

            if ((size_t)ndims <= (sizeof(sm_size)/sizeof(sm_size[0]))) {
                H5Sget_simple_extent_dims(f_space, total_size, NULL);

                /* calculate the number of elements we're going to print */
                p_nelmts = 1;

                if (ndims > 0) {
                    for (i = 0; i < ndims; i++)
                        p_nelmts *= total_size[i];
                } /* end if */

                if (p_nelmts > 0) {
                    /* Check if we have VL data in the dataset's datatype */
                    if (h5str_detect_vlen(p_type) != 0)
                        vl_data = 1;

                    /*
                     * Determine the strip mine size and allocate a buffer. The strip mine is
                     * a hyperslab whose size is manageable.
                     */
                    sm_nbytes = p_type_nbytes = H5Tget_size(p_type);

                    if (ndims > 0) {
                        for (i = (size_t)ndims; i > 0; --i) {
                            hsize_t size = H5TOOLS_BUFSIZE / sm_nbytes;
                            if ( size == 0) /* datum size > H5TOOLS_BUFSIZE */
                                size = 1;
                            sm_size[i - 1] = (((total_size[i - 1]) < (size)) ? (total_size[i - 1]) : (size));
                            sm_nbytes *= sm_size[i - 1];
                        }
                    }

                    if(sm_nbytes > 0) {
                        sm_buf = (unsigned char *)HDmalloc((size_t)sm_nbytes);

                        sm_nelmts = sm_nbytes / p_type_nbytes;
                        sm_space = H5Screate_simple(1, &sm_nelmts, NULL);

                        /* The stripmine loop */
                        HDmemset(hs_offset, 0, sizeof hs_offset);
                        HDmemset(zero, 0, sizeof zero);

                        for (elmtno = 0; elmtno < p_nelmts; elmtno += hs_nelmts) {
                            /* Calculate the hyperslab size */
                            if (ndims > 0) {
                                for (i = 0, hs_nelmts = 1; i < ndims; i++) {
                                    hs_size[i] = (((total_size[i] - hs_offset[i]) < (sm_size[i])) ? (total_size[i] - hs_offset[i]) : (sm_size[i]));
                                    hs_nelmts *= hs_size[i];
                                }

                                H5Sselect_hyperslab(f_space, H5S_SELECT_SET, hs_offset, NULL, hs_size, NULL);
                                H5Sselect_hyperslab(sm_space, H5S_SELECT_SET, zero, NULL, &hs_nelmts, NULL);
                            }
                            else {
                                H5Sselect_all(f_space);
                                H5Sselect_all(sm_space);
                                hs_nelmts = 1;
                            }

                            /* Read the data */
                            if (H5Dread(dset, p_type, sm_space, f_space, H5P_DEFAULT, sm_buf) >= 0) {

                                if (binary_order == 99)
                                    ret_value = h5tools_dump_simple_data(stream, dset, p_type, sm_buf, hs_nelmts);
                                else
                                    ret_value = h5str_render_bin_output(stream, dset, p_type, sm_buf, hs_nelmts);

                                /* Reclaim any VL memory, if necessary */
                                if (vl_data)
                                    H5Dvlen_reclaim(p_type, sm_space, H5P_DEFAULT, sm_buf);
                            }
                            else {
                                ret_value = -1;
                                break;
                            }

                            if(ret_value < 0) break;

                            /* Calculate the next hyperslab offset */
                            for (i = (size_t)ndims, carry = 1; i > 0 && carry; --i) {
                                hs_offset[i - 1] += hs_size[i - 1];

                                if (hs_offset[i - 1] == total_size[i - 1])
                                    hs_offset[i - 1] = 0;
                                else
                                    carry = 0;
                            }
                        }

                        if(sm_buf)
                            HDfree(sm_buf);
                    }
                    if(sm_space >= 0 && H5Sclose(sm_space) < 0)
                        ret_value = -1;
                }
            }
            if(f_space >= 0 && H5Sclose(f_space) < 0)
                ret_value = -1;
        } /* end if((f_space = H5Dget_space(dset)) >= 0) */
        else
            ret_value = -1;

        if (p_type >= 0)
            H5Tclose(p_type);
    }
    return ret_value;
} /* end h5str_dump_simple_dset */

static int
h5tools_dump_simple_data
    (FILE *stream, hid_t container, hid_t type, void *_mem, hsize_t nelmts)
{
    int                ret_value = 0;
    int                line_count;
    unsigned char     *mem  = (unsigned char*)_mem;
    size_t             size;   /* datum size */
    hsize_t            i;         /*element counter  */
    h5str_t            buffer;    /*string into which to render */

    if((size = H5Tget_size(type)) > 0) {
        for (i = 0, line_count = 0; i < nelmts; i++, line_count++) {
            size_t bytes_in        = 0;    /* # of bytes to write  */
            size_t bytes_wrote     = 0;    /* # of bytes written   */
            void* memref = mem + i * size;

            /* Render the data element*/
            h5str_new(&buffer, 32 * size);
            bytes_in = h5str_sprintf(&buffer, container, type, memref, 1);
            if(i > 0) {
                HDfprintf(stream, ", ");
                if (line_count >= H5TOOLS_TEXT_BLOCK) {
                    line_count = 0;
                    HDfprintf(stream, "\n");
                }
            }
            HDfprintf(stream, "%s", buffer.s);
            h5str_free(&buffer);
        } /* end for (i = 0; i < nelmts... */
        HDfprintf(stream, "\n");
    } /* end if((size = H5Tget_size(tid)) > 0) */
    else
        ret_value = -1;

    return ret_value;
} /* end h5tools_dump_simple_data */

/*
 * Utility Java APIs
 * Functions designed to workaround issues with the Java-C interface
 */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5AwriteVL
 * Signature: (JJ[Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5AwriteVL
    (JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jobjectArray buf)
{
    herr_t  status = -1;
    char  **wdata;
    jsize   size;
    jint    i;

    size = ENVPTR->GetArrayLength(ENVPAR (jarray) buf);

    wdata = (char**)HDcalloc((size_t)size + 1, sizeof(char*));
    if (!wdata) {
        h5JNIFatalError(env, "H5AwriteVL:  cannot allocate buffer");
    } /* end if */
    else {
        HDmemset(wdata, 0, (size_t)size * sizeof(char*));
        for (i = 0; i < size; ++i) {
            jstring obj = (jstring) ENVPTR->GetObjectArrayElement(ENVPAR (jobjectArray) buf, i);
            if (obj != 0) {
                jsize length = ENVPTR->GetStringUTFLength(ENVPAR obj);
                const char *utf8 = ENVPTR->GetStringUTFChars(ENVPAR obj, 0);

                if (utf8) {
                    wdata[i] = (char*)HDmalloc((size_t)length + 1);
                    if (wdata[i]) {
                        HDmemset(wdata[i], 0, ((size_t)length + 1));
                        HDstrncpy(wdata[i], utf8, (size_t)length);
                    } /* end if */
                } /* end if */

                ENVPTR->ReleaseStringUTFChars(ENVPAR obj, utf8);
                ENVPTR->DeleteLocalRef(ENVPAR obj);
            } /* end if */
        } /* end for (i = 0; i < size; ++i) */

        status = H5Awrite((hid_t)attr_id, (hid_t)mem_type_id, wdata);

        for (i = 0; i < size; i++) {
            if(wdata[i]) {
                HDfree(wdata[i]);
            } /* end if */
        } /* end for */
        HDfree(wdata);

        if (status < 0)
            h5libraryError(env);
    } /* end else */

    return (jint)status;
} /* end Java_hdf_hdf5lib_H5_H5AwriteVL */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5AreadVL
 * Signature: (JJ[Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5AreadVL
    (JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jobjectArray buf)
{
    herr_t  status = -1;
    jstring jstr;
    char  **strs;
    int     i, n;
    hid_t   sid;
    hsize_t dims[H5S_MAX_RANK];

    n = ENVPTR->GetArrayLength(ENVPAR buf);

    strs =(char **)HDmalloc((size_t)n * sizeof(char *));
    if (strs == NULL) {
        h5JNIFatalError( env, "H5AreadVL:  failed to allocate buff for read variable length strings");
    } /* end if */
    else {
        status = H5Aread(attr_id, mem_type_id, strs);
        if (status < 0) {
            dims[0] = (hsize_t)n;
            sid = H5Screate_simple(1, dims, NULL);
            H5Dvlen_reclaim(mem_type_id, sid, H5P_DEFAULT, strs);
            H5Sclose(sid);
            HDfree(strs);
            h5JNIFatalError(env, "H5AreadVL: failed to read variable length strings");
        } /* end if */
        else {
            for (i=0; i<n; i++) {
                jstr = ENVPTR->NewStringUTF(ENVPAR strs[i]);
                ENVPTR->SetObjectArrayElement(ENVPAR buf, i, jstr);
                HDfree (strs[i]);
            } /* end for */

            /*
                for repeatedly reading an attribute with a large number of strs (e.g., 1,000,000 strings,
                H5Dvlen_reclaim() may crash on Windows because the Java GC will not be able to collect
                free space in time. Instead, use "free(strs[i])" to free individual strings
                after it is done.
                H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, strs);
            */

            HDfree(strs);
        }  /* end else */
    }  /* end else */
    return status;
} /* end Java_hdf_hdf5lib_H5_H5AreadVL */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5AreadComplex
 * Signature: (JJ[Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5AreadComplex
(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jobjectArray buf)
{
    herr_t      status = -1;
    int         i;
    int         n;
    char       *rdata;
    size_t      max_len = 0;
    size_t      size;
    h5str_t     h5str;
    hid_t       p_type = -1;
    jstring     jstr;

    p_type = H5Tget_native_type(mem_type_id, H5T_DIR_DEFAULT);
    size = (((H5Tget_size(mem_type_id))>(H5Tget_size(p_type))) ? (H5Tget_size(mem_type_id)) : (H5Tget_size(p_type)));
    H5Tclose(p_type);

    n = ENVPTR->GetArrayLength(ENVPAR buf);
    rdata = (char *)malloc((size_t)n * size);
    if (rdata == NULL) {
        h5JNIFatalError(env, "H5AreadComplex:  failed to allocate buff for read");
    } /* end if */
    else {
        status = H5Aread(attr_id, mem_type_id, rdata);
        if (status < 0) {
            h5JNIFatalError(env, "H5AreadComplex: failed to read data");
        } /* end if */
        else {
            HDmemset(&h5str, 0, sizeof(h5str_t));
            h5str_new(&h5str, 4 * size);
            if (h5str.s == NULL) {
                h5JNIFatalError(env, "H5AreadComplex:  failed to allocate string buf");
            } /* end if */
            else {
                for (i = 0; i < n; i++) {
                    h5str.s[0] = '\0';
                    h5str_sprintf(&h5str, attr_id, mem_type_id, rdata + ((size_t)i * size), 0);
                    jstr = ENVPTR->NewStringUTF(ENVPAR h5str.s);
                    ENVPTR->SetObjectArrayElement(ENVPAR buf, i, jstr);
                } /* end for */
            }  /* end else */
            h5str_free(&h5str);
        }  /* end else */
        HDfree(rdata);
    }  /* end else */
    return status;
}

/*
 * Copies the content of one dataset to another dataset
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Acopy
 * Signature: (JJ)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Acopy
    (JNIEnv *env, jclass clss, jlong src_id, jlong dst_id)
{
    jbyte  *buf;
    herr_t  retVal = -1;
    hid_t   src_did = (hid_t)src_id;
    hid_t   dst_did = (hid_t)dst_id;
    hid_t   tid = -1;
    hid_t   sid = -1;
    hsize_t total_size = 0;


    sid = H5Aget_space(src_did);
    if (sid < 0) {
        h5libraryError(env);
    } /* end if */
    else {
        tid = H5Aget_type(src_did);
        if (tid < 0) {
            H5Sclose(sid);
            h5libraryError(env);
        } /* end if */
        else {
            total_size = (hsize_t)H5Sget_simple_extent_npoints(sid) * (hsize_t)H5Tget_size(tid);

            H5Sclose(sid);

            buf = (jbyte *)HDmalloc( (size_t)total_size * sizeof(jbyte));
            if (buf == NULL) {
                H5Tclose(tid);
                h5outOfMemory( env, "H5Acopy:  malloc failed");
            } /* end if */
            else {
                retVal = H5Aread(src_did, tid, buf);
                H5Tclose(tid);

                if (retVal < 0) {
                    HDfree(buf);
                    h5libraryError(env);
                } /* end if */
                else {
                    tid = H5Aget_type(dst_did);
                    if (tid < 0) {
                        HDfree(buf);
                        h5libraryError(env);
                    } /* end if */
                    else {
                        retVal = H5Awrite(dst_did, tid, buf);

                        H5Tclose(tid);
                        HDfree(buf);

                        if (retVal < 0)
                            h5libraryError(env);
                    } /* end else */
                } /* end else */
            } /* end else */
        } /* end else */
    } /* end else */

    return (jint)retVal;
} /* end Java_hdf_hdf5lib_H5_H5Acopy */

/*
 * Copies the content of one dataset to another dataset
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Dcopy
 * Signature: (JJ)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dcopy
    (JNIEnv *env, jclass clss, jlong src_id, jlong dst_id)
{
    jbyte  *buf;
    herr_t  retVal = -1;
    hid_t   src_did = (hid_t)src_id;
    hid_t   dst_did = (hid_t)dst_id;
    hid_t   tid = -1;
    hid_t   sid = -1;
    hsize_t total_size = 0, total_allocated_size;

    total_allocated_size = H5Dget_storage_size(src_did);
    if (total_allocated_size <=0)
      return 0; // nothing to write;

    sid = H5Dget_space(src_did);
    if (sid < 0) {
        h5libraryError(env);
        return -1;
    } /* end if */

    tid = H5Dget_type(src_did);
    if (tid < 0) {
        H5Sclose(sid);
        h5libraryError(env);
        return -1;
    } /* end if */

    total_size = (hsize_t)H5Sget_simple_extent_npoints(sid) * (hsize_t)H5Tget_size(tid);

    H5Sclose(sid);

    buf = (jbyte*)HDmalloc((size_t)total_size * sizeof(jbyte));
    if (buf == NULL) {
        H5Tclose(tid);
        h5outOfMemory(env, "H5Dcopy:  malloc failed");
        return -1;
    } /* end if */

    retVal = H5Dread(src_did, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
    H5Tclose(tid);

    if (retVal < 0) {
        HDfree(buf);
        h5libraryError(env);
        return (jint)retVal;
    } /* end if */

    tid = H5Dget_type(dst_did);
    if (tid < 0) {
        HDfree(buf);
        h5libraryError(env);
        return -1;
    } /* end if */
    retVal = H5Dwrite(dst_did, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
    H5Tclose(tid);
    HDfree(buf);

    if (retVal < 0) {
        h5libraryError(env);
    } /* end if */

    return (jint)retVal;
} /* end Java_hdf_hdf5lib_H5_H5Dcopy */

/*
/////////////////////////////////////////////////////////////////////////////////
//
//
// Add these methods so that we don't need to call H5Gget_objtype_by_idx
// in a loop to get information for all the object in a group, which takes
// a lot of time to finish if the number of objects is more than 10,000
//
/////////////////////////////////////////////////////////////////////////////////
*/

#ifdef __cplusplus
    herr_t obj_info_all(hid_t g_id, const char *name, const H5L_info_t *linfo, void *op_data);
    herr_t obj_info_max(hid_t g_id, const char *name, const H5L_info_t *linfo, void *op_data);
    int H5Gget_obj_info_max(hid_t, char **, int *, int *, unsigned long *, long);
    int H5Gget_obj_info_full( hid_t loc_id, char **objname, int *otype, int *ltype, unsigned long *fno, unsigned long *objno, int indexType, int indexOrder);
#else
    static herr_t obj_info_all(hid_t g_id, const char *name, const H5L_info_t *linfo, void *op_data);
    static herr_t obj_info_max(hid_t g_id, const char *name, const H5L_info_t *linfo, void *op_data);
    static int H5Gget_obj_info_max(hid_t, char **, int *, int *, unsigned long *, long);
    static int H5Gget_obj_info_full( hid_t loc_id, char **objname, int *otype, int *ltype, unsigned long *fno, unsigned long *objno, int indexType, int indexOrder);
#endif

typedef struct info_all
{
    char **objname;
    int *otype;
    int *ltype;
    unsigned long *objno;
    unsigned long *fno;
    unsigned long idxnum;
    int count;
} info_all_t;


/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Gget_obj_info_full
 * Signature: (JLjava/lang/String;[Ljava/lang/String;[I[I[J[JIII)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Gget_1obj_1info_1full
    (JNIEnv *env, jclass clss, jlong loc_id, jstring group_name,
        jobjectArray objName, jintArray oType, jintArray lType, jlongArray fNo,
        jlongArray oRef, jint n, jint indx_type, jint indx_order)
{
    herr_t        ret_val = -1;
    const char   *gName = NULL;
    char        **oName = NULL;
    jboolean      isCopy;
    jstring       str;
    jint          *otarr;
    jint          *ltarr;
    jlong         *refP;
    jlong         *fnoP;
    unsigned long *refs=NULL;
    unsigned long *fnos=NULL;
    hid_t          gid = (hid_t)loc_id;
    int            i;
    int            indexType = indx_type;
    int            indexOrder = indx_order;

    if (oType == NULL) {
        h5nullArgument(env, "H5Gget_obj_info_full:  oType is NULL");
    }
    else if (lType == NULL) {
        h5nullArgument(env, "H5Gget_obj_info_full:  lType is NULL");
    }
    else if (oRef == NULL) {
        h5nullArgument(env, "H5Gget_obj_info_full:  oRef is NULL");
    }
    else if (fNo == NULL) {
        h5nullArgument(env, "H5Gget_obj_info_full:  fNo is NULL");
    }
    else {
        otarr = ENVPTR->GetIntArrayElements(ENVPAR oType, &isCopy);
        if (otarr == NULL) {
            h5JNIFatalError(env, "H5Gget_obj_info_full:  otype not pinned");
            return -1;
        } /* end if */
        ltarr = ENVPTR->GetIntArrayElements(ENVPAR lType, &isCopy);
        if (ltarr == NULL) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            h5JNIFatalError(env, "H5Gget_obj_info_full:  ltype not pinned");
            return -1;
        } /* end if */
        refP = ENVPTR->GetLongArrayElements(ENVPAR oRef, &isCopy);
        if (refP == NULL) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, JNI_ABORT);
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            h5JNIFatalError(env, "H5Gget_obj_info_full:  oRef not pinned");
            return -1;
        } /* end if */
        fnoP = ENVPTR->GetLongArrayElements(ENVPAR fNo, &isCopy);
        if (fnoP == NULL) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, JNI_ABORT);
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, JNI_ABORT);
            h5JNIFatalError(env, "H5Gget_obj_info_full:  fNo not pinned");
            return -1;
        } /* end if */
        oName = (char **)HDcalloc((size_t)n, sizeof(*oName));
        if (!oName) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, JNI_ABORT);
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, JNI_ABORT);
            ENVPTR->ReleaseLongArrayElements(ENVPAR fNo, fnoP, JNI_ABORT);
            h5JNIFatalError(env, "H5Gget_obj_info_full:  oName not allocated");
            return -1;
        } /* end if */

        refs = (unsigned long *)HDcalloc((size_t)n, sizeof(unsigned long));
        fnos = (unsigned long *)HDcalloc((size_t)n, sizeof(unsigned long));
        if (!refs || !fnos) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, JNI_ABORT);
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, JNI_ABORT);
            ENVPTR->ReleaseLongArrayElements(ENVPAR fNo, fnoP, JNI_ABORT);
            h5str_array_free(oName, (size_t)n);
            if (refs)
                HDfree(refs);
            if (fnos)
                HDfree(fnos);
            h5JNIFatalError(env, "H5Gget_obj_info_full:  result arrays not allocated");
            return -1;
        } /* end if */

        if (group_name != NULL) {
            gid = -1;
            gName = ENVPTR->GetStringUTFChars(ENVPAR group_name, &isCopy);
            if (gName != NULL) {
                gid = H5Gopen2((hid_t)loc_id, gName, H5P_DEFAULT);

                ENVPTR->ReleaseStringUTFChars(ENVPAR group_name, gName);
            } /* end if */
            if(gid < 0) {
                ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, JNI_ABORT);
                ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
                ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, JNI_ABORT);
                ENVPTR->ReleaseLongArrayElements(ENVPAR fNo, fnoP, JNI_ABORT);
                h5str_array_free(oName, (size_t)n);
                HDfree(refs);
                HDfree(fnos);
                h5JNIFatalError(env, "H5Gget_obj_info_full: could not get group identifier");
                return -1;
            } /* end if */
        } /* end if */

        ret_val = H5Gget_obj_info_full(gid, oName, (int *)otarr, (int *)ltarr, fnos, refs, indexType, indexOrder);

        ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, 0);
        ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, 0);

        if (group_name != NULL)
            H5Gclose(gid);

        if (ret_val < 0) {
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, JNI_ABORT);
            ENVPTR->ReleaseLongArrayElements(ENVPAR fNo, fnoP, JNI_ABORT);
            h5str_array_free(oName, (size_t)n);
            HDfree(refs);
            HDfree(fnos);
            h5libraryError(env);
        } /* end if */
        else {
            for (i=0; i<n; i++) {
                refP[i] = (jlong)refs[i];
            } /* end for */
            HDfree(refs);
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, 0);

            for (i=0; i<n; i++) {
                fnoP[i] = (jlong)fnos[i];
            } /* end for */
            HDfree(fnos);
            ENVPTR->ReleaseLongArrayElements(ENVPAR fNo, fnoP, 0);

            for (i=0; i<n; i++) {
                if (*(oName+i)) {
                    str = ENVPTR->NewStringUTF(ENVPAR *(oName+i));
                    ENVPTR->SetObjectArrayElement(ENVPAR objName, i, (jobject)str);
                } /* end if */
            } /* for (i=0; i<n; i++)*/
            h5str_array_free(oName, (size_t)n);
        } /* end else */
    } /* end else */
    return ret_val;
} /* end Java_hdf_hdf5lib_H5_H5Gget_1obj_1info_1full */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Gget_obj_info_max
 * Signature: (J[Ljava/lang/String;[I[I[JJI)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Gget_1obj_1info_1max
    (JNIEnv *env, jclass clss, jlong loc_id, jobjectArray objName,
          jintArray oType, jintArray lType, jlongArray oRef, jlong maxnum, jint n)
{
    herr_t         ret_val = -1;
    char         **oName=NULL;
    jboolean       isCopy;
    jstring        str;
    jint          *otarr;
    jint          *ltarr;
    jlong         *refP;
    unsigned long *refs;
    int            i;

    if (oType == NULL) {
        h5nullArgument(env, "H5Gget_obj_info_max:  oType is NULL");
    } /* end if */
    else if (lType == NULL) {
        h5nullArgument(env, "H5Gget_obj_info_max:  lType is NULL");
    } /* end else if */
    else if (oRef == NULL) {
        h5nullArgument(env, "H5Gget_obj_info_max:  oRef is NULL");
    } /* end else if */
    else {
        otarr = ENVPTR->GetIntArrayElements(ENVPAR oType, &isCopy);
        if (otarr == NULL) {
            h5JNIFatalError(env, "H5Gget_obj_info_max:  otype not pinned");
            return -1;
        } /* end if */

        ltarr = ENVPTR->GetIntArrayElements(ENVPAR lType, &isCopy);
        if (ltarr == NULL) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            h5JNIFatalError(env, "H5Gget_obj_info_max:  ltype not pinned");
            return -1;
        } /* end if */

        refP = ENVPTR->GetLongArrayElements(ENVPAR oRef, &isCopy);
        if (refP == NULL) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, JNI_ABORT);
            h5JNIFatalError(env, "H5Gget_obj_info_max:  oRef not pinned");
            return -1;
        } /* end if */

        oName = (char **)HDcalloc((size_t)n, sizeof(*oName));
        if (!oName) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, JNI_ABORT);
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, JNI_ABORT);
            h5JNIFatalError(env, "H5Gget_obj_info_max:  oName not allocated");
            return -1;
        } /* end if */
        refs = (unsigned long *)HDcalloc((size_t)n, sizeof(unsigned long));
        if (!refs) {
            ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, JNI_ABORT);
            ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, JNI_ABORT);
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, JNI_ABORT);
            h5str_array_free(oName, (size_t)n);
            h5JNIFatalError(env, "H5Gget_obj_info_max:  result array not allocated");
            return -1;
        } /* end if */

        ret_val = H5Gget_obj_info_max((hid_t)loc_id, oName, (int*)otarr, (int*)ltarr, refs, maxnum );
        ENVPTR->ReleaseIntArrayElements(ENVPAR lType, ltarr, 0);
        ENVPTR->ReleaseIntArrayElements(ENVPAR oType, otarr, 0);

        if (ret_val < 0) {
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, JNI_ABORT);
            h5str_array_free(oName, (size_t)n);
            HDfree(refs);
            h5libraryError(env);
        } /* end if */
        else {
            for (i=0; i<n; i++) {
                refP[i] = (jlong) refs[i];
            } /* end for */
            HDfree(refs);
            ENVPTR->ReleaseLongArrayElements(ENVPAR oRef, refP, 0);

            for (i=0; i<n; i++) {
                if (*(oName+i)) {
                    str = ENVPTR->NewStringUTF(ENVPAR *(oName+i));
                    ENVPTR->SetObjectArrayElement(ENVPAR objName, i, (jobject)str);
                }
            } /* for (i=0; i<n; i++)*/

            h5str_array_free(oName, (size_t)n);
        } /* end else */
    } /* end else */

    return ret_val;
} /* end Java_hdf_hdf5lib_H5_H5Gget_1obj_1info_1max */

int
H5Gget_obj_info_full
    (hid_t loc_id, char **objname, int *otype, int *ltype, unsigned long *fno, unsigned long *objno, int indexType, int indexOrder)
{
    info_all_t info;
    info.objname = objname;
    info.otype = otype;
    info.ltype = ltype;
    info.idxnum = 0;
    info.fno = fno;
    info.objno = objno;
    info.count = 0;

    if(H5Literate(loc_id, (H5_index_t)indexType, (H5_iter_order_t)indexOrder, NULL, obj_info_all, (void *)&info) < 0) {
        /* iterate failed, try normal alphabetical order */
        if(H5Literate(loc_id, H5_INDEX_NAME, H5_ITER_INC, NULL, obj_info_all, (void *)&info) < 0)
            return -1;
    } /* end if */

    return info.count;
} /* end H5Gget_obj_info_full */

int
H5Gget_obj_info_max
    (hid_t loc_id, char **objname, int *otype, int *ltype, unsigned long *objno, long maxnum)
{
    info_all_t info;
    info.objname = objname;
    info.otype = otype;
    info.ltype = ltype;
    info.idxnum = (unsigned long)maxnum;
    info.objno = objno;
    info.count = 0;

    if(H5Lvisit(loc_id, H5_INDEX_NAME, H5_ITER_NATIVE, obj_info_max, (void *)&info) < 0)
        return -1;

    return info.count;
} /* end H5Gget_obj_info_max */

herr_t
obj_info_all
    (hid_t loc_id, const char *name, const H5L_info_t *info, void *op_data)
{
    int         type = -1;
    hid_t       oid = -1;
    herr_t      retVal = -1;
    info_all_t *datainfo = (info_all_t*)op_data;
    H5O_info_t  object_info;

    retVal = H5Oget_info_by_name(loc_id, name, &object_info, H5P_DEFAULT);

    if (retVal < 0) {
        *(datainfo->otype+datainfo->count) = -1;
        *(datainfo->ltype+datainfo->count) = -1;
        *(datainfo->objname+datainfo->count) = (char *)HDmalloc(strlen(name)+1);
        HDstrcpy(*(datainfo->objname+datainfo->count), name);
        *(datainfo->objno+datainfo->count) = (unsigned long)-1;
    } /* end if */
    else {
        *(datainfo->otype+datainfo->count) = object_info.type;
        *(datainfo->ltype+datainfo->count) = info->type;
        *(datainfo->objname+datainfo->count) = (char *)HDmalloc(HDstrlen(name)+1);
        HDstrcpy(*(datainfo->objname+datainfo->count), name);

        *(datainfo->fno+datainfo->count) = object_info.fileno;
        *(datainfo->objno+datainfo->count) = (unsigned long)object_info.addr;
        /*
        if(info->type==H5L_TYPE_HARD)
            *(datainfo->objno+datainfo->count) = (unsigned long)info->u.address;
        else
            *(datainfo->objno+datainfo->count) = info->u.val_size;
        */
    } /* end else */

    datainfo->count++;

    return 0;
} /* end obj_info_all */

herr_t
obj_info_max
    (hid_t loc_id, const char *name, const H5L_info_t *info, void *op_data)
{
    int         type = -1;
    herr_t      retVal = 0;
    info_all_t *datainfo = (info_all_t*)op_data;
    H5O_info_t  object_info;

    retVal = H5Oget_info(loc_id, &object_info);
    if (retVal < 0) {
        *(datainfo->otype+datainfo->count) = -1;
        *(datainfo->ltype+datainfo->count) = -1;
        *(datainfo->objname+datainfo->count) = NULL;
        *(datainfo->objno+datainfo->count) = (unsigned long)-1;
        return 1;
    } /* end if */
    else {
        *(datainfo->otype+datainfo->count) = object_info.type;
        *(datainfo->ltype+datainfo->count) = info->type;
        /* this will be freed by h5str_array_free(oName, n)*/
        *(datainfo->objname+datainfo->count) = (char *)HDmalloc(HDstrlen(name)+1);
        strcpy(*(datainfo->objname+datainfo->count), name);
        if(info->type==H5L_TYPE_HARD)
            *(datainfo->objno+datainfo->count) = (unsigned long)info->u.address;
        else
            *(datainfo->objno+datainfo->count) = info->u.val_size;
    } /* end else */
    datainfo->count++;
    if(datainfo->count < (int)datainfo->idxnum)
        return 0;
    else
        return 1;
} /* end obj_info_max */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5export_dataset
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V
 */
JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5export_1dataset
    (JNIEnv *env, jclass cls, jstring file_export_name, jstring file_name, jstring object_path, jint binary_order)
{
    herr_t      status = -1;
    herr_t      ret_val = -1;
    hid_t       file_id = -1;
    hid_t       dataset_id = -1;
    FILE       *stream;
    const char *file_export;
    const char *object_name;
    const char *fileName;
    jboolean    isCopy2;

    if (file_export_name == NULL) {
        h5nullArgument(env, "HDF5Library_export_data:  file_export_name is NULL");
    } /* end if */
    else if (object_path == NULL) {
        h5nullArgument(env, "HDF5Library_export_data:  object_path is NULL");
    } /* end else if */
    else {
        PIN_JAVA_STRING(file_name, fileName);
        if (fileName != NULL) {
            file_id = H5Fopen(fileName, (unsigned)H5F_ACC_RDWR, (hid_t)H5P_DEFAULT);

            UNPIN_JAVA_STRING(file_name, fileName);

            if (file_id < 0) {
                /* throw exception */
                h5libraryError(env);
            } /* end if */
            else {
                object_name = ENVPTR->GetStringUTFChars(ENVPAR object_path, &isCopy2);
                if (object_name == NULL) {
                    h5JNIFatalError( env, "H5Dopen:  object name not pinned");
                } /* end if */
                else {
                    dataset_id = H5Dopen2(file_id, object_name, H5P_DEFAULT);

                    ENVPTR->ReleaseStringUTFChars(ENVPAR object_path, object_name);

                    if (dataset_id < 0) {
                        H5Fclose(file_id);
                        h5libraryError(env);
                    } /* end if */
                    else {
                        file_export = ENVPTR->GetStringUTFChars(ENVPAR file_export_name, 0);
                        stream = HDfopen(file_export, "w+");
                        ENVPTR->ReleaseStringUTFChars(ENVPAR file_export_name, file_export);

                        ret_val = h5str_dump_simple_dset(stream, dataset_id, binary_order);

                        if (stream)
                            HDfclose(stream);

                        H5Dclose(dataset_id);

                        H5Fclose(file_id);

                        if (ret_val < 0)
                            h5libraryError(env);
                    } /* end else */
                } /* end else */
            } /* end else */
        }
    } /* end else */
} /* end Java_hdf_hdf5lib_H5_H5export_1dataset */

#ifdef __cplusplus
}
#endif