forked from Unidata/netcdf-c
/
tst_h_strings.c
340 lines (279 loc) · 11.2 KB
/
tst_h_strings.c
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
/* This is part of the netCDF package. Copyright 2005 University
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
conditions of use.
This program does HDF5 string stuff.
Here's a HDF5 sample programs:
http://hdf.ncsa.uiuc.edu/training/other-ex5/sample-programs/strings.c
*/
#include <string.h>
#include "h5_err_macros.h"
#include <hdf5.h>
#define FILE_NAME "tst_h_strings.h5"
#define DIM1_LEN 3
#define ATT_NAME "Stooge_Statements"
#define GRP_NAME "Stooge_Antic_Metrics"
int
main()
{
printf("\n*** Checking HDF5 string types.\n");
printf("*** Checking scalar string attribute...");
{
hid_t fileid, grpid, spaceid, typeid, attid;
hid_t class;
size_t type_size;
htri_t is_str;
const char *data = "The art of war is of vital "
"importance to the State. It is a matter of life and death, a road either"
"to safety or to ruin. Hence it is a subject of inquiry"
"which can on no account be neglected.";
char *data_in = NULL;
/* Open file. */
if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR;
/* Create string type. */
if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
if (H5Tset_size(typeid, H5T_VARIABLE) < 0) ERR;
/* Write an attribute of this type. */
if ((spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
if ((attid = H5Acreate(grpid, ATT_NAME, typeid, spaceid,
H5P_DEFAULT)) < 0) ERR;
if (H5Awrite(attid, typeid, &data) < 0) ERR;
/* Close up. */
if (H5Aclose(attid) < 0) ERR;
if (H5Tclose(typeid) < 0) ERR;
if (H5Sclose(spaceid) < 0) ERR;
if (H5Gclose(grpid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
/* Now reopen the file and check it out. */
if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR;
if ((attid = H5Aopen_name(grpid, ATT_NAME)) < 0) ERR;
if ((typeid = H5Aget_type(attid)) < 0) ERR;
if ((spaceid = H5Aget_space(attid)) < 0) ERR;
/* Given this type id, how would we know this is a string
* attribute? */
if ((class = H5Tget_class(typeid)) < 0) ERR;
if (class != H5T_STRING) ERR;
if (!(type_size = H5Tget_size(typeid))) ERR;
if (type_size != sizeof(char *)) ERR;
if ((is_str = H5Tis_variable_str(typeid)) < 0) ERR;
if (!is_str) ERR;
/* Make sure this is a scalar. */
if (H5Sget_simple_extent_type(spaceid) != H5S_SCALAR) ERR;
/* Read the attribute. */
if (H5Aread(attid, typeid, &data_in) < 0) ERR;
/* Check the data. */
if (strcmp(data, data_in)) ERR;
/* Free the memory returned by H5Aread */
free(data_in);
/* Close HDF5 stuff. */
if (H5Aclose(attid) < 0) ERR;
if (H5Tclose(typeid) < 0) ERR;
if (H5Sclose(spaceid) < 0) ERR;
if (H5Gclose(grpid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking simple HDF5 string types...");
{
hid_t fileid, grpid, spaceid, typeid, attid;
hsize_t dims[1] = {DIM1_LEN};
/* size_t type_size;
htri_t is_str;*/
hid_t class;
char *data[DIM1_LEN] = {"Ohhh!", "Ahhh!", "Wub-wub-wub!"};
char **data_in;
int i;
/* Open file. */
if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR;
/* Create string type. */
if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
if (H5Tset_size(typeid, H5T_VARIABLE) < 0) ERR;
/* I thought the following should work instead of the H5Tcopy and
* H5Tset_size functions above, but it doesn't. */
/*if ((typeid = H5Tvlen_create(H5T_NATIVE_CHAR)) < 0) ERR;*/
/* Write an attribute of this (string) type. */
if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR;
if ((attid = H5Acreate(grpid, ATT_NAME, typeid, spaceid,
H5P_DEFAULT)) < 0) ERR;
if (H5Awrite(attid, typeid, data) < 0) ERR;
if (H5Aclose(attid) < 0) ERR;
if (H5Tclose(typeid) < 0) ERR;
if (H5Gclose(grpid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
/* Now reopen the file and check it out. */
if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR;
if ((attid = H5Aopen_name(grpid, ATT_NAME)) < 0) ERR;
if ((typeid = H5Aget_type(attid)) < 0) ERR;
/* Given this type id, how would we know this is a string
* attribute? */
if ((class = H5Tget_class(typeid)) < 0) ERR;
if (class != H5T_STRING) ERR;
/* if (!(type_size = H5Tget_size(typeid))) ERR;
if ((is_str = H5Tis_variable_str(typeid)) < 0) ERR;*/
/* How many strings are in the string array? */
if ((spaceid = H5Aget_space(attid)) < 0) ERR;
if (H5Sget_simple_extent_dims(spaceid, dims, NULL) < 0) ERR;
if (!(data_in = malloc(dims[0] * sizeof(char *)))) ERR;
/* Now read the array of strings. The HDF5 library will allocate
* space for each string. */
if (H5Aread(attid, typeid, data_in) < 0) ERR;
/* Compare the values to make sure it worked... */
for (i = 0; i < DIM1_LEN; i++)
if (strcmp(data[i], data_in[i])) ERR;
/* Free the memory that HDF5 allocated. */
for (i = 0; i < DIM1_LEN; i++)
free(data_in[i]);
/* Free the memory that we allocated. */
free(data_in);
/* Close everything up. */
if (H5Aclose(attid) < 0) ERR;
if (H5Tclose(typeid) < 0) ERR;
if (H5Gclose(grpid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking G&S compliance...");
{
#define S1 "When Frederick was a little lad"
#define S2 "He proved so brave and daring..."
char **data;
char **data_in;
hsize_t dims_in[1], dims[1] = {2};
hid_t fileid, grpid, spaceid, typeid, attid;
hid_t class;
int i;
/* Allocate space and copy strings. */
data = malloc(sizeof(char *) * 2);
data[0] = malloc(strlen(S1) + 1);
strcpy(data[0], S1);
data[1] = malloc(strlen(S2) + 1);
strcpy(data[1], S2);
/* Open file. */
if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR;
/* Create string type. */
if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
if (H5Tset_size(typeid, H5T_VARIABLE) < 0) ERR;
/* I thought the following should work instead of the H5Tcopy and
* H5Tset_size functions above, but it doesn't. */
/*if ((typeid = H5Tvlen_create(H5T_NATIVE_CHAR)) < 0) ERR;*/
/* Write an attribute of this type. */
if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR;
if ((attid = H5Acreate(grpid, ATT_NAME, typeid, spaceid,
H5P_DEFAULT)) < 0) ERR;
if (H5Awrite(attid, typeid, data) < 0) ERR;
/* Close up. */
if (H5Aclose(attid) < 0) ERR;
if (H5Tclose(typeid) < 0) ERR;
if (H5Gclose(grpid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
/* Now reopen the file and check it out. */
if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR;
if ((attid = H5Aopen_name(grpid, ATT_NAME)) < 0) ERR;
if ((typeid = H5Aget_type(attid)) < 0) ERR;
/* Given this type id, how would we know this is a string
* attribute? */
if ((class = H5Tget_class(typeid)) < 0) ERR;
if (class != H5T_STRING) ERR;
/* How many strings are in the array? */
if (H5Sget_simple_extent_dims(spaceid, dims_in, NULL) != 1) ERR;
if (dims_in[0] != dims[0]) ERR;
/* Allocate enough pointers to read the data. The HDF5 library
* will allocate the space needed for each string. */
if (!(data_in = malloc(dims_in[0] * sizeof(char *)))) ERR;
/* Read the data. */
if (H5Aread(attid, typeid, data_in) < 0) ERR;
/* Check the data. */
for (i = 0; i < 2; i++)
if (strcmp(data[i], data_in[i])) ERR;
/* Free our memory. */
for (i = 0; i < 2; i++)
free(data_in[i]);
free(data_in);
free(data[0]);
free(data[1]);
free(data);
/* Close HDF5 stuff. */
if (H5Aclose(attid) < 0) ERR;
if (H5Tclose(typeid) < 0) ERR;
if (H5Gclose(grpid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking empty strings...");
{
char **data;
char **data_in;
hsize_t dims_in[1], dims[1] = {2};
hid_t fileid, grpid, spaceid, typeid, attid;
hid_t class;
int i;
/* Allocate space and copy strings. */
data = malloc(sizeof(char *) * 2);
data[0] = malloc(strlen(S1) + 1);
strcpy(data[0], "");
data[1] = malloc(strlen(S2) + 1);
strcpy(data[1], "");
/* Open file. */
if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR;
/* Create string type. */
if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
if (H5Tset_size(typeid, H5T_VARIABLE) < 0) ERR;
/* I thought the following should work instead of the H5Tcopy and
* H5Tset_size functions above, but it doesn't. */
/*if ((typeid = H5Tvlen_create(H5T_NATIVE_CHAR)) < 0) ERR;*/
/* Write an attribute of this type. */
if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR;
if ((attid = H5Acreate(grpid, ATT_NAME, typeid, spaceid,
H5P_DEFAULT)) < 0) ERR;
if (H5Awrite(attid, typeid, data) < 0) ERR;
/* Close up. */
if (H5Aclose(attid) < 0) ERR;
if (H5Tclose(typeid) < 0) ERR;
if (H5Gclose(grpid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
/* Now reopen the file and check it out. */
if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR;
if ((attid = H5Aopen_name(grpid, ATT_NAME)) < 0) ERR;
if ((typeid = H5Aget_type(attid)) < 0) ERR;
/* Given this type id, how would we know this is a string
* attribute? */
if ((class = H5Tget_class(typeid)) < 0) ERR;
if (class != H5T_STRING) ERR;
/* How many strings are in the array? */
if (H5Sget_simple_extent_dims(spaceid, dims_in, NULL) != 1) ERR;
if (dims_in[0] != dims[0]) ERR;
/* Allocate enough pointers to read the data. The HDF5 library
* will allocate the space needed for each string. */
if (!(data_in = malloc(dims_in[0] * sizeof(char *)))) ERR;
/* Read the data. */
if (H5Aread(attid, typeid, data_in) < 0) ERR;
/* Check the data. */
for (i = 0; i < 2; i++)
if (strcmp(data[i], data_in[i])) ERR;
/* Free our memory. */
for (i = 0; i < 2; i++)
free(data_in[i]);
free(data_in);
free(data[0]);
free(data[1]);
free(data);
/* Close HDF5 stuff. */
if (H5Aclose(attid) < 0) ERR;
if (H5Tclose(typeid) < 0) ERR;
if (H5Gclose(grpid) < 0) ERR;
if (H5Fclose(fileid) < 0) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}