File: tutils.c

package info (click to toggle)
libhdf4 4.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,384 kB
  • sloc: ansic: 128,700; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 794; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (357 lines) | stat: -rw-r--r-- 11,741 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
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdlib.h>
#include <string.h>

#include "mfhdf.h"
#include "hdftest.h"
#include "srcdir_str.h"

/* Buffer to construct path in and return pointer to */
static char srcdir_path[1024];

/* Buffer to construct file in and return pointer to */
static char srcdir_testpath[1024];

/*-------------------------------------------------------------------------
 * Function:    get_srcdir_filename
 *
 * Purpose:     Append the test file name to the srcdir path and return the whole string
 *
 * Return:      The string or NULL (errors or not enough space)
 *
 *-------------------------------------------------------------------------
 */
const char *
get_srcdir_filename(const char *filename)
{
    const char *srcdir = get_srcdir();

    /* Check for error */
    if (NULL == srcdir)
        return NULL;

    /* Build path to test file */
    if ((strlen(srcdir) + strlen(filename) + 1) < sizeof(srcdir_testpath)) {
        snprintf(srcdir_testpath, sizeof(srcdir_testpath), "%s/%s", srcdir, filename);
        return srcdir_testpath;
    }

    /* If not enough space, just return NULL */
    return NULL;
} /* end get_srcdir_filename() */

/*-------------------------------------------------------------------------
 * Function:    get_srcdir
 *
 * Purpose:     Just return the srcdir path
 *
 * Return:      The string
 *
 *-------------------------------------------------------------------------
 */
const char *
get_srcdir(void)
{
    const char *srcdir = getenv("srcdir");

    /* Check for using the srcdir from configure time */
    if (NULL == srcdir)
        srcdir = config_srcdir;

    /* Build path to all test files */
    if ((strlen(srcdir) + 2) < sizeof(srcdir_path)) {
        snprintf(srcdir_path, sizeof(srcdir_path), "%s/", srcdir);
        return (srcdir_path);
    }
    else
        return NULL;
} /* end get_srcdir() */

/********************************************************************
   Name: make_sourcepath() - Generates the source path
   Description:
        Generate the path of srcdir if it exists, otherwise, assume
        it is the current directory.
   Return value:
        Returns SUCCEED if the source path is generated successfully,
        or FAIL, otherwise.
*********************************************************************/
intn
make_sourcepath(char *src_path, unsigned int size)
{
    char *srcdir  = getenv("srcdir");
    char *tempdir = NULL;

    tempdir = (char *)malloc(size * sizeof(char));
    CHECK_ALLOC(tempdir, "tempdir", "make_sourcepath");
    memset(tempdir, 0, size);

    /* Generate the source path */
    if (srcdir && ((strlen(srcdir)) + 1) < size) {
        strcpy(tempdir, srcdir);
        strcat(tempdir, "/");
        strcat(tempdir, "\0");
    }

    /* No srcdir option */
    if (srcdir == NULL)
        strcpy(tempdir, "./");

    /* Verify that source path is not NULL */
    if (tempdir == NULL || tempdir[0] == '\0')
        return FAIL;

    strcpy(src_path, tempdir);
    free(tempdir);
    return SUCCEED;
}

/********************************************************************
   Name: make_SDS() - Creates and writes a 3-D unlimited SDS.
   Description:
    Calls SDcreate, SDwritedata, and SDendaccess to create a 3-D
    unlimited SDS, then close it.
    (Note: should be modified for more different ranks.)
   Return value:
        Returns the size of the data that had been written successfully,
        of FAIL.

*********************************************************************/
int32
make_SDS(int32 sd_id, char *sds_name, int32 type, int32 rank, int32 *dim_sizes, int32 unlim_dim,
         void *written_data)
{
    int32  sds_id;
    int32 *start, *edges;
    int32  sds_size = 0, count = 0;
    intn   status, ii;
    intn   num_errs = 0; /* number of errors in compression test so far */

    start = (int32 *)malloc(sizeof(int32) * rank);
    CHECK_ALLOC(start, "start", "make_SDS");
    edges = (int32 *)malloc(sizeof(int32) * rank);
    CHECK_ALLOC(edges, "edges", "make_SDS");

    /* Create the array with the name defined in SDS_NAME */
    sds_id = SDcreate(sd_id, sds_name, type, rank, dim_sizes);
    CHECK(sds_id, FAIL, "SDcreate");

    /* Set the parameters start and edges to write */
    for (ii = 0; ii < rank; ii++) {
        start[ii] = 0;
        edges[ii] = dim_sizes[ii];
    }

    /* Give real size to the unlimited dimension */
    if (dim_sizes[0] == SD_UNLIMITED)
        edges[0] = unlim_dim;

    /* Write the data */
    status = SDwritedata(sds_id, start, NULL, edges, written_data);
    CHECK(status, FAIL, "SDwritedata");

    /* Calculate data set's size to verify later */
    for (ii = 0; ii < rank; ii++) {
        if (ii == 0)
            count = edges[0];
        else
            count = count * edges[ii];
    }
    sds_size = count * DFKNTsize(type);

    /* Terminate access to the data set */
    status = SDendaccess(sds_id);
    CHECK(status, FAIL, "SDendaccess");

    free(edges);
    free(start);

    /* Return the size of data being written if no error */
    if (num_errs == 0)
        return (sds_size);
    else
        return FAIL;
} /* make_SDS */

/********************************************************************
   Name: make_Ext3D_SDS() - Creates and writes a 3D SDS with external data.
   Description:
    Calls SDcreate, SDsetexternalfile, SDwritedata, and SDendaccess
    to create a 3-D SDS with external data storage, then close it.
    (Note: should be modified for different ranks.)
   Return value:
        Returns the size of the data that had been written successfully.
   Return value:
        None.

*********************************************************************/
int32
make_Ext3D_SDS(int32 sd_id, char *sds_name, int32 type, int32 rank, int32 *dim_sizes, void *written_data,
               int32 offset, char *ext_file_name)
{
    int32  sds_id;
    int32 *start, *edges;
    int32  sds_size = 0, count;
    intn   status   = 0, ii;
    intn   num_errs = 0; /* number of errors in compression test so far */

    start = (int32 *)malloc(sizeof(int32) * rank);
    CHECK_ALLOC(start, "start", "make_Ext3D_SDS");
    edges = (int32 *)malloc(sizeof(int32) * rank);
    CHECK_ALLOC(edges, "edges", "make_Ext3D_SDS");

    /* Set the parameters start and edges to write */
    for (ii = 0; ii < rank; ii++) {
        start[ii] = 0;
        edges[ii] = dim_sizes[ii];
    }

    /* Create the array with the name defined in SDS_NAME. */
    sds_id = SDcreate(sd_id, sds_name, type, rank, dim_sizes);
    CHECK(sds_id, FAIL, "SDcreate");

    status = SDsetexternalfile(sds_id, ext_file_name, offset);
    CHECK(status, FAIL, "SDsetexternalfile");

    /* Write the data */
    status = SDwritedata(sds_id, start, NULL, edges, written_data);
    CHECK(status, FAIL, "SDwritedata");

    /* Calculate data set's size to verify later */
    count = 1;
    for (ii = 0; ii < rank; ii++) {
        count = count * dim_sizes[ii];
    }
    sds_size = count * DFKNTsize(type);

    /* Terminate access to the data set */
    status = SDendaccess(sds_id);
    CHECK(status, FAIL, "SDendaccess");

    free(edges);
    free(start);

    /* Return the size of data being written if no error */
    if (num_errs == 0)
        return (sds_size);
    else
        return FAIL;
} /* make_Ext3D_SDS */

/********************************************************************
   Name: get_SDSbyName() - Find and open an SDS by name.
   Description:
    Calls SDnametoindex and SDselect to open a data set by its name.
   Return value:
        Returns the SDS' identifier if successful, or FAIL, otherwise.

*********************************************************************/
int32
get_SDSbyName(int32 sd_id, const char *sds_name)
{
    int32 sds_id, sds_index;
    intn  num_errs = 0; /* number of errors in compression test so far */

    sds_index = SDnametoindex(sd_id, sds_name);
    CHECK(sds_index, FAIL, "SDnametoindex");

    /* Select the data set */
    sds_id = SDselect(sd_id, sds_index);
    CHECK(sds_id, FAIL, "SDselect");

    /* Return the data set id or FAIL */
    return (sds_id);

} /* get_SDSbyName */

/********************************************************************
   Name: append_Data2SDS() - Selects a data set by name then write data to it.
   Description:
    Uses the helper function get_SDSbyName to find and open the
    data set, then calls SDwritedata to append data, and SDendaccess
    to close it.
   Return value:
        Returns the size of the data that had been written successfully,
        or FAIL.

*********************************************************************/
int32
append_Data2SDS(int32 sd_id, char *sds_name, int32 *start, int32 *edges, void *ap_data)
{
    int32 sds_id;
    int32 sds_size, ntype;
    int32 comp_size = 0, uncomp_size = 0;
    char  name[80];
    intn  status   = 0;
    intn  num_errs = 0; /* number of errors in compression test so far */

    /* Find and select the data set */
    sds_id = get_SDSbyName(sd_id, sds_name);
    CHECK(sds_id, FAIL, "get_SDSbyName");

    /* Get the current size of this dataset */
    status = SDgetinfo(sds_id, name, NULL, NULL, &ntype, NULL);
    CHECK(status, FAIL, "SDgetinfo");

    status = SDgetdatasize(sds_id, &comp_size, &uncomp_size);
    CHECK(status, FAIL, "SDgetdatasize");

    /* Append data to it */
    status = SDwritedata(sds_id, start, NULL, edges, (void *)ap_data);
    CHECK(status, FAIL, "SDwritedata");

    /* Calculate data set's size to verify later */
    sds_size = uncomp_size + edges[0] * edges[1] * edges[2] * DFKNTsize(ntype);

    /* Terminate access to the data set and file */
    status = SDendaccess(sds_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Return the size of data being written if no error */
    if (num_errs == 0)
        return (sds_size);
    else
        return FAIL;

} /* append_Data2SDS */

/********************************************************************
   Name: verify_datasize() - Checks data size
   Description:
    Calls SDgetdatasize then verifies the data size against the
    given data_size and reports if they are different.
   Return value:
        SUCCEED or FAIL

*********************************************************************/
intn
verify_datasize(int32 sds_id, int32 data_size, char *sds_name)
{
    int32 comp_size = 0, uncomp_size = 0;
    char  msg[80];
    intn  status   = 0;
    intn  num_errs = 0; /* number of errors in compression test so far */

    /* Get the size of data set's data */
    status = SDgetdatasize(sds_id, &comp_size, &uncomp_size);
    CHECK(status, FAIL, "SDgetdatasize");
    sprintf(msg, "%s on data set %s\n", "SDgetdatasize", sds_name);
    VERIFY(data_size, uncomp_size, msg);

    if (num_errs == 0)
        return SUCCEED;
    else
        return FAIL;
} /* verify_datasize */