summaryrefslogtreecommitdiffstats
path: root/doxygen/examples/H5L_examples.c
blob: 63f54fe0db0738a963a14dc51f311d9123c253b7 (plain)
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
/* -*- c-file-style: "stroustrup" -*- */

#include "hdf5.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

//! <!-- [iter_cb] -->
herr_t
iter_cb(hid_t group, const char *name, const H5L_info_t *info, void *op_data)
{
    printf("Link \"%s\" is a", name);
    switch (info->type) {
        case H5L_TYPE_HARD:
            printf(" hard link.\n");
            break;
        case H5L_TYPE_SOFT:
            printf(" soft link.\n");
            break;
        case H5L_TYPE_EXTERNAL:
            printf("n external link.\n");
            break;
        default:
            printf(" UFO link.\n");
            break;
    }

    return 0;
}
//! <!-- [iter_cb] -->

int
main(void)
{
    int ret_val = EXIT_SUCCESS;

    //! <!-- [create] -->
    {
        __label__ fail_link, fail_prop, fail_lcpl, fail_create;

        hid_t file, lcpl;

        if ((file = H5Fcreate("l1.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_create;
        }

        // make link creation easier by auto-creating intermediate
        // groups and UTF-8 encoding of link names
        if ((lcpl = H5Pcreate(H5P_LINK_CREATE)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_lcpl;
        }
        if (H5Pset_create_intermediate_group(lcpl, 1) < 0 || H5Pset_char_encoding(lcpl, H5T_CSET_UTF8) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_prop;
        }

        // create a loop by hard linking the root group
        if (H5Lcreate_hard(file, ".", file, "√", lcpl, H5P_DEFAULT) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_link;
        }
        // create a soft link to nowhere
        if (H5Lcreate_soft("e1 62 80 87 04 09 43 ba 02 d3", file, "/path/to/nowhere", lcpl, H5P_DEFAULT) <
            0) {
            ret_val = EXIT_FAILURE;
            goto fail_link;
        }
        // create an external link to nowhere in a non-existent file
        if (H5Lcreate_external("non-existent-file.h5", "???", file, "external", lcpl, H5P_DEFAULT) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_link;
        }

fail_link:
fail_prop:
        H5Pclose(lcpl);
fail_lcpl:
        H5Fclose(file);
fail_create:;
    }
    //! <!-- [create] -->

    //! <!-- [read] -->
    {
        __label__ fail_iterate, fail_read;
        hid_t   file;
        hsize_t idx = 0;

        if ((file = H5Fopen("l1.h5", H5F_ACC_RDONLY, H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_read;
        }

        if (H5Literate(file, H5_INDEX_NAME, H5_ITER_NATIVE, &idx, iter_cb, NULL) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_iterate;
        }

fail_iterate:
        H5Fclose(file);
fail_read:;
    }
    //! <!-- [read] -->

    //! <!-- [update] -->
    {
        __label__ fail_move, fail_update;
        hid_t file;

        if ((file = H5Fopen("l1.h5", H5F_ACC_RDWR, H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_update;
        }

        // move the "√" link to the group at "/path/to"
        // the cycle remains!
        if (H5Lmove(file, "√", file, "path/to/√", H5P_DEFAULT, H5P_DEFAULT) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_move;
        }

fail_move:
        H5Fclose(file);
fail_update:;
    }
    //! <!-- [update] -->

    //! <!-- [delete] -->
    {
        __label__ fail_delete, fail_file;
        hid_t file;

        if ((file = H5Fopen("l1.h5", H5F_ACC_RDWR, H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_file;
        }

        // delete the "external" link
        if (H5Ldelete(file, "external", H5P_DEFAULT) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_delete;
        }

fail_delete:
        H5Fclose(file);
fail_file:;
    }
    //! <!-- [delete] -->

    assert(ret_val == EXIT_SUCCESS);

    return ret_val;
}