File: hrepack_an.c

package info (click to toggle)
libhdf4 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 29,892 kB
  • sloc: ansic: 128,688; sh: 14,969; fortran: 12,444; java: 5,864; xml: 1,305; makefile: 900; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (241 lines) | stat: -rw-r--r-- 7,916 bytes parent folder | download | duplicates (3)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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 "hdf.h"
#include "mfhdf.h"

#include "hrepack_an.h"

/*-------------------------------------------------------------------------
 * Function: copy_vg_an
 *
 * Purpose: copy Vgroup ANs
 *
 * Return: ok, 1, -1 not ok
 *
 *-------------------------------------------------------------------------
 */

int
copy_vg_an(int32 infile_id, int32 outfile_id, int32 vgroup_id, int32 vgroup_id_out, char *path,
           options_t *options)
{
    int32 ref_in, tag_in, ref_out, tag_out;

    if (options->trip == 0) {
        return 1;
    }

    if ((ref_in = VQueryref(vgroup_id)) == FAIL) {
        printf("Failed to get ref for <%s>\n", path);
        return -1;
    }
    if ((tag_in = VQuerytag(vgroup_id)) == FAIL) {
        printf("Failed to get tag for <%s>\n", path);
        return -1;
    }
    if ((ref_out = VQueryref(vgroup_id_out)) == FAIL) {
        printf("Failed to get ref for <%s>\n", path);
        return -1;
    }
    if ((tag_out = VQuerytag(vgroup_id_out)) == FAIL) {
        printf("Failed to get tag for <%s>\n", path);
        return -1;
    }

    if (copy_an(infile_id, outfile_id, ref_in, tag_in, ref_out, tag_out, path, options) < 0)
        return FAIL;

    return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function: copy_vs_an
 *
 * Purpose: copy Vdata ANs
 *
 * Return: ok, 1, -1 not ok
 *
 *-------------------------------------------------------------------------
 */

int
copy_vs_an(int32 infile_id, int32 outfile_id, int32 vdata_id, int32 vdata_id_out, char *path,
           options_t *options)
{
    int32 ref_in, tag_in, ref_out, tag_out;

    if (options->trip == 0) {
        return 1;
    }

    if ((ref_in = VSQueryref(vdata_id)) == FAIL) {
        printf("Failed to get ref for <%s>\n", path);
        return -1;
    }
    if ((tag_in = VSQuerytag(vdata_id)) == FAIL) {
        printf("Failed to get tag for <%s>\n", path);
        return -1;
    }
    if ((ref_out = VSQueryref(vdata_id_out)) == FAIL) {
        printf("Failed to get ref for <%s>\n", path);
        return -1;
    }
    if ((tag_out = VSQuerytag(vdata_id_out)) == FAIL) {
        printf("Failed to get tag for <%s>\n", path);
        return -1;
    }

    if (copy_an(infile_id, outfile_id, ref_in, tag_in, ref_out, tag_out, path, options) < 0)
        return FAIL;

    return 1;
}

/*-------------------------------------------------------------------------
 * Function: copy_an_data
 *
 * Purpose: copy DATA ANs
 *
 * Return: ok, 1, -1 not ok
 *
 *-------------------------------------------------------------------------
 */

int
copy_an_data(int32 infile_id, int32 outfile_id, int32 ref_in, int32 tag_in, int32 ref_out, int32 tag_out,
             ann_type type, char *path, options_t *options)
{
    int32 an_id,    /* AN interface identifier */
        an_out,     /* AN interface identifier */
        ann_id,     /* an annotation identifier */
        ann_out,    /* an annotation identifier */
        ann_length, /* length of the text in an annotation */
        n_anno;
    int   i;   /* position of an annotation */
    char *buf; /* buffer to hold the read annotation */
    int   is_label = (type == AN_DATA_LABEL) ? 1 : 0;
    int   ret      = 0;

    if (options->trip == 0) {
        return 1;
    }

    /* Initialize the AN interface  */
    an_id  = ANstart(infile_id);
    an_out = ANstart(outfile_id);

    /* Get the number of ANs in this object  */
    if ((n_anno = ANnumann(an_id, type, (uint16)tag_in, (uint16)ref_in)) == FAIL) {
        printf("Failed to get annotations for <%s>\n", path);
        return -1;
    }

    for (i = 0; i < n_anno; i++) {
        /*-------------------------------------------------------------------------
         * read
         *-------------------------------------------------------------------------
         */
        if ((ann_id = ANselect(an_id, i, type)) == FAIL) {
            printf("Failed to select AN %d of <%s>\n", i, path);
            continue;
        }
        if ((ann_length = ANannlen(ann_id)) == FAIL) {
            printf("Failed to get AN %d length of <%s>\n", i, path);
            continue;
        }

        /*
         * Read the data label.  Note that the size of the buffer,
         * i.e., the third parameter, is 1 character more than the length of
         * the data label; that is for the null character.  It is not the case
         * when a description is retrieved because the description does not
         * necessarily end with a null character.
         *
         */
        if (is_label)
            ann_length++;

        if ((buf = (char *)malloc((ann_length) * sizeof(int8))) == NULL) {
            printf("Failed to get memory for AN %d of <%s>\n", i, path);
            continue;
        }
        if (ANreadann(ann_id, buf, ann_length) == FAIL) {
            printf("Failed to read AN %d of <%s>\n", i, path);
            free(buf);
            continue;
        }
        if (ANendaccess(ann_id) == FAIL) {
            printf("Failed to end AN %d of <%s>\n", i, path);
            free(buf);
            continue;
        }
        /*-------------------------------------------------------------------------
         * write
         *-------------------------------------------------------------------------
         */
        /* Create the data label for the vgroup identified by its tag and ref number */
        if ((ann_out = ANcreate(an_out, (uint16)tag_out, (uint16)ref_out, type)) == FAIL) {
            printf("Failed to create AN %d of <%s>\n", i, path);
            continue;
        }
        /* Write the annotation  */
        if (ANwriteann(ann_out, buf, ann_length) == FAIL) {
            printf("Failed to write AN %d of <%s>\n", i, path);
        }
        if (ANendaccess(ann_out) == FAIL) {
            printf("Failed to end AN %d of <%s>\n", i, path);
            free(buf);
            continue;
        }
        free(buf);
    }

    /* Terminate access to the AN interface */
    if (ANend(an_id) == FAIL) {
        printf("Failed close AN for <%s>\n", path);
        ret = -1;
    }
    if (ANend(an_out) == FAIL) {
        printf("Failed close AN for <%s>\n", path);
        ret = -1;
    }
    return ret;
}

/*-------------------------------------------------------------------------
 * Function: copy_an
 *
 * Purpose: copy DATA ANs (AN_DATA_LABEL and AN_DATA_DESC)
 *
 * Return: ok, 1, -1 not ok
 *
 *-------------------------------------------------------------------------
 */

int
copy_an(int32 infile_id, int32 outfile_id, int32 ref_in, int32 tag_in, int32 ref_out, int32 tag_out,
        char *path, options_t *options)
{

    if (copy_an_data(infile_id, outfile_id, ref_in, tag_in, ref_out, tag_out, AN_DATA_LABEL, path, options) <
        0)
        return FAIL;
    if (copy_an_data(infile_id, outfile_id, ref_in, tag_in, ref_out, tag_out, AN_DATA_DESC, path, options) <
        0)
        return FAIL;

    return SUCCEED;
}