File: tst_h_vl.c

package info (click to toggle)
netcdf 1:4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 104,952 kB
  • sloc: ansic: 228,683; sh: 10,980; yacc: 2,561; makefile: 1,319; lex: 1,173; xml: 173; awk: 2
file content (150 lines) | stat: -rw-r--r-- 5,046 bytes parent folder | download | duplicates (2)
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
/* This is part of the netCDF package.  Copyright 2018 University
   Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
   conditions of use.

   This program excersizes HDF5 variable length array code.
*/

#include "h5_err_macros.h"
#include <hdf5.h>

#define FILE_NAME "tst_h_vl.h5"
#define DIM1_LEN 3
#define ATT_NAME "att_name"
#define GROUP_NAME "grp1"

int
main()
{
   printf("\n*** Checking HDF5 VLEN types.\n");
   printf("*** Checking simple HDF5 variable length types...");
   {
      hid_t fileid, grpid, spaceid, typeid, attid;
      hsize_t dims[1] = {DIM1_LEN};
      hvl_t data[DIM1_LEN], data_in[DIM1_LEN];
      int *phoney;
      int i, j;
      size_t size;
      
      /* Create some phoney data, an array of struct s1, which holds a
       * pointer to a variable length array of int. */
      for (i=0; i<DIM1_LEN; i++)
      {
	 if (!(phoney = malloc(sizeof(int) * (i+1)))) ERR;
	 for (j=0; j<i+1; j++)
	    phoney[j] = -99;
	 data[i].p = phoney;
	 data[i].len = i+1;
      }
      
      /* Open file. */
      if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, 
			      H5P_DEFAULT)) < 0) ERR;
      if ((grpid = H5Gcreate(fileid, GROUP_NAME, 0)) < 0) ERR;

      /* Create VLEN type. */
      if ((typeid =  H5Tvlen_create(H5T_NATIVE_INT)) < 0) ERR;

      /* Although it's a vlen of ints, the size is rouned up to 8. */
      if (!(size = H5Tget_size(typeid))) ERR;
      if (size < 8) ERR;

      /* Write an attribute of this vlen 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;

      /* Reopen the file and read the vlen data. */
      if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) ERR;
      if ((grpid = H5Gopen(fileid, GROUP_NAME)) < 0) ERR;
      if ((attid = H5Aopen_name(grpid, ATT_NAME)) < 0) ERR;
      if ((spaceid = H5Aget_space(attid)) < 0) ERR;
      if ((typeid = H5Aget_type(attid)) < 0) ERR;
      if (H5Aread(attid, typeid, data_in) < 0) ERR;

      /* Check the data */
      for (i=0; i<DIM1_LEN; i++)
      {
	 if (data[i].len != i+1) ERR;
	 for (j = 0; j < i+1; j++)
	    if (((int *)(data[i].p))[j] != -99) ERR;
      }

      /* Free the memory allocated above. */
      for (i=0; i<DIM1_LEN; i++)
	 free(data[i].p);

      /* HDF5 allocated memory to store the data. Free that memory. */
      if (H5Dvlen_reclaim(typeid, spaceid, H5P_DEFAULT, data_in) < 0) ERR;

      /* Close everything. */
      if (H5Aclose(attid) < 0 ||
	  H5Sclose(spaceid) < 0 || 
	  H5Tclose(typeid) < 0 || 
	  H5Gclose(grpid) < 0 || 
	  H5Fclose(fileid) < 0) ERR;
   }

   SUMMARIZE_ERR;
/*    printf("*** Checking array of compound holding a vlen..."); */

/*    { */
/*       hid_t vlen_typeid, compound_typeid, spaceid, datasetid; */
/*       struct sea_sounding */
/*       { */
/* 	    int sounding_no; */
/* 	    hvl_t temp_vl; */
/*       } data[DIM1_LEN]; */
/*       float *phoney; */
/*       int i, j; */
      
/*       /\* Create phoney data. *\/ */
/*       for (i=0; i<DIM1_LEN; i++) */
/*       { */
/* 	  if (!(phoney = malloc(sizeof(float) * (i+1)))) ERR; */
/* 	 for (j=0; j<i+1; j++) */
/* 	    phoney[j] = 23.5 - j; */
/* 	 data[i].temp_vl.p = phoney; */
/* 	 data[i].temp_vl.len = i+1; */
/*       } */

/*       /\* Create file. *\/ */
/*       if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,  */
/* 			      H5P_DEFAULT)) < 0) ERR; */
/*       if ((grpid = H5Gcreate(fileid, "grp1", 0)) < 0) ERR; */
      
/*       /\* Create VLEN type. *\/ */
/*       if ((vlen_typeid =  H5Tvlen_create(H5T_NATIVE_FLOAT)) < 0) ERR; */

/*       /\* Create a compound type that holds the vlen type. *\/ */
/*       if ((compound_typeid = H5Tcreate(H5T_COMPOUND,  */
/* 				       sizeof(struct sea_sounding))) < 0) ERR; */
/*       if (H5Tinsert(compound_typeid, "sounding_no", HOFFSET(struct sea_sounding, sounding_no),  */
/* 		    H5T_NATIVE_INT) < 0) ERR; */
/*       if (H5Tinsert(compound_typeid, "temp_vl", HOFFSET(struct sea_sounding, temp_vl),  */
/* 		    vlen_typeid) < 0) ERR; */
/*       if (H5Tcommit(grpid, "sea_sounding_type", compound_typeid) < 0) ERR; */

/*       if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR; */
/*       if ((datasetid = H5Dcreate(grpid, "sea_sounding_dataset", compound_typeid,  */
/* 				 spaceid, H5P_DEFAULT)) < 0) ERR; */
/*       if (H5Dwrite(datasetid, compound_typeid, H5S_ALL, H5S_ALL, H5P_DEFAULT,  */
/* 		   data) < 0) ERR; */

/*       if (H5Dclose(datasetid) < 0) ERR; */
/*       if (H5Tclose(compound_typeid) < 0) ERR; */
/*       if (H5Tclose(vlen_typeid) < 0) ERR; */
/*       if (H5Sclose(spaceid) < 0) ERR; */
/*       if (H5Gclose(grpid) < 0) ERR; */
/*       if (H5Fclose(fileid) < 0) ERR; */
/*    } */

/*    SUMMARIZE_ERR; */

   FINAL_RESULTS;
}