File: anfile.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 (139 lines) | stat: -rw-r--r-- 5,105 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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 "tproto.h"
#define TESTFILE "tdfan.hdf"

#define ISFIRST     (int)1
#define NOTFIRST    (int)0
#define MAXLEN_LAB  50
#define MAXLEN_DESC 1000

static int checkannlen(int32 ret, const char *oldstr, const char *type, int32 testflag);

static int checkann(const char *oldstr, const char *newstr, int32 ret, const char *type, int32 testflag);

void
test_anfile(void)
{
    char  lab1[MAXLEN_LAB], lab2[MAXLEN_LAB], desc1[MAXLEN_DESC], desc2[MAXLEN_DESC], tempstr[MAXLEN_DESC];
    int32 testflag = SUCCEED;
    int32 file_id, ret;

    /* set up file labels and descriptions */

    strcpy(lab1, "File label #1: aaa");
    strcpy(lab2, "File label #2: bbbbbb");
    strcpy(desc1, "File Descr #1: 1  2  3  4  5  6  7  8  9 10 11 12 13\n");
    strcat(desc1, "              14 15 16 17 18 19 20 **END FILE DESCR**\n");
    strcpy(desc2, "File Descr #2: A B C D E F G H I J K L\n");
    strcat(desc2, "              M N O **END FILE DESCR**\n");

    /********  Write file labels and descriptions *********/

    file_id = Hopen(TESTFILE, DFACC_CREATE, 0);
    if (file_id == FAIL)
        printf("\nUnable to open file %s for writing.\n\n", TESTFILE);

    MESSAGE(5, puts("Writing file labels."););
    ret = DFANaddfid(file_id, lab1);
    RESULT("DFANaddfid");

    ret = DFANaddfid(file_id, lab2);
    RESULT("DFANaddfid");

    MESSAGE(5, puts("Writing file descriptions."););
    ret = DFANaddfds(file_id, desc1, (int32)strlen(desc1));
    RESULT("DFANaddfds");

    ret = DFANaddfds(file_id, desc2, (int32)strlen(desc2));
    RESULT("DFANaddfds");

    if (FAIL == Hclose(file_id))
        printf("\nUnable to close file %s after writing.\n\n", TESTFILE);

    /********  Read file labels *********/

    file_id = Hopen(TESTFILE, DFACC_READ, 0);
    if (file_id == FAIL)
        printf("\n\nUnable to open file %s for reading.\n\n", TESTFILE);

    MESSAGE(5, puts("Reading length of first file label, followed by label."););
    ret = DFANgetfidlen(file_id, ISFIRST);
    RESULT("DFANgetfidlen");
    checkannlen(ret, lab1, "label", testflag);

    ret = DFANgetfid(file_id, tempstr, MAXLEN_LAB, ISFIRST);
    RESULT("DFANgetfid");
    checkann(lab1, tempstr, ret, "label", testflag);

    MESSAGE(5, puts("Reading length of second file label, followed by label."););
    ret = DFANgetfidlen(file_id, NOTFIRST);
    RESULT("DFANgetfidlen");
    checkannlen(ret, lab2, "label", testflag);

    ret = DFANgetfid(file_id, tempstr, MAXLEN_LAB, NOTFIRST);
    RESULT("DFANgetfid");
    checkann(lab2, tempstr, ret, "label", testflag);

    /********  Read file descriptions *********/

    MESSAGE(5, puts("Reading length of first file descr, followed by descr."););
    ret = DFANgetfdslen(file_id, ISFIRST);
    RESULT("DFANgetfdslen");
    checkannlen(ret, desc1, "description", testflag);

    ret = DFANgetfds(file_id, tempstr, MAXLEN_DESC, ISFIRST);
    RESULT("DFANgetfds");
    checkann(desc1, tempstr, ret, "description", testflag);

    MESSAGE(5, puts("Reading length of second file descr, followed by descr."););
    ret = DFANgetfdslen(file_id, NOTFIRST);
    RESULT("DFANgetfdslen");
    checkannlen(ret, desc2, "description", testflag);

    ret = DFANgetfds(file_id, tempstr, MAXLEN_DESC, NOTFIRST);
    RESULT("DFANgetfds");
    checkann(desc2, tempstr, ret, "description", testflag);

    if (FAIL == Hclose(file_id))
        printf("\n\nUnable to close file %s after reading.\n\n", TESTFILE);
}

static int
checkannlen(int32 ret, const char *oldstr, const char *type, int32 testflag)
{
    (void)testflag;

    if ((ret >= 0) && (ret != (int32)strlen(oldstr))) {
        printf("Length of %s is INCORRECT\n", type);
        printf("It is:  %d\n", (int)ret);
        printf("It should be: %d\n", (int)strlen(oldstr));
        return FAIL;
    }
    return SUCCEED;
}

static int
checkann(const char *oldstr, const char *newstr, int32 ret, const char *type, int32 testflag)
{
    (void)testflag;

    if ((ret >= 0) && (0 != strcmp(oldstr, newstr))) {
        printf("%s is INCORRECT.\n", type);
        printf("It is:  %s\n", newstr);
        printf("It should be: %s\n", oldstr);
        return FAIL;
    }
    return SUCCEED;
}