summaryrefslogtreecommitdiffstats
path: root/doxygen/examples/H5Z_examples.c
blob: 28a1ea2e1b4f3ee01df44ef569f14a6dd277df83 (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
/* -*- c-file-style: "stroustrup" -*- */

#include "hdf5.h"

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

//! <!-- [filter] -->
size_t
filter(unsigned int flags, size_t cd_nelmts, const unsigned int cd_values[], size_t nbytes, size_t *buf_size,
       void **buf)
{
    buf_size = 0;

    if (flags & H5Z_FLAG_REVERSE) {
        // read data, e.g., decompress data
        // ...
    }
    else {
        // write data, e.g., compress data
        // ...
    }

    return nbytes;
}
//! <!-- [filter] -->

int
main(void)
{
    int ret_val = EXIT_SUCCESS;

    //! <!-- [create] -->
    {
        __label__ fail_register;
        H5Z_class_t cls;
        cls.version         = H5Z_CLASS_T_VERS;
        cls.id              = 256;
        cls.encoder_present = 1;
        cls.decoder_present = 1;
        cls.name            = "Identity filter";
        cls.can_apply       = NULL;
        cls.set_local       = NULL;
        cls.filter          = &filter;

        // register the filter
        if (H5Zregister(&cls) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_register;
        }

        // do something with filter
        // ...

        // unregister the filter if no longer required
        // ...

fail_register:;
    }
    //! <!-- [create] -->

    //! <!-- [read] -->
    {
        __label__ fail_avail;

        H5Z_filter_t flt   = H5Z_FILTER_DEFLATE;
        unsigned     flags = 0;

        // check if the deflate filter is available
        if (H5Zfilter_avail(flt) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_avail;
        }
        // retrieve the deflate filter info
        if (H5Zget_filter_info(flt, &flags) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_avail;
        }

        // check if the deflate encoder or decoder is enabled
        if (H5Z_FILTER_CONFIG_ENCODE_ENABLED & flags)
            printf("Deflate encoder enabled.\n");
        if (H5Z_FILTER_CONFIG_DECODE_ENABLED & flags)
            printf("Deflate decoder enabled.\n");

fail_avail:;
    }
    //! <!-- [read] -->

    //! <!-- [update] -->
    {
        // N/A
    } //! <!-- [update] -->

    //! <!-- [delete] -->
    {
        // unregister the identity filter
        if (H5Zunregister(256) < 0) {
            ret_val = EXIT_FAILURE;
        }
    }
    //! <!-- [delete] -->

    assert(ret_val == EXIT_SUCCESS);

    return ret_val;
}