diff options
author | Thomas White <taw@physics.org> | 2011-10-13 14:16:14 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2012-02-22 15:27:39 +0100 |
commit | 42d10caabdb9fa5f1fb3dcb8223236458ed003af (patch) | |
tree | 03dd4e6fadfc3717e5b5092debeb62db64347399 /src/hdf5-file.c | |
parent | d8a4f7509d0b5937bc56441e77e9343c84a3b38b (diff) |
indexamajig: Add --copy-hdf5-field option for copying something to the HDF5 file
Diffstat (limited to 'src/hdf5-file.c')
-rw-r--r-- | src/hdf5-file.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/hdf5-file.c b/src/hdf5-file.c index b43297ec..6215cc5c 100644 --- a/src/hdf5-file.c +++ b/src/hdf5-file.c @@ -18,6 +18,7 @@ #include <stdio.h> #include <stdint.h> #include <hdf5.h> +#include <assert.h> #include "image.h" #include "hdf5-file.h" @@ -538,6 +539,74 @@ double get_value(struct hdfile *f, const char *name) } +struct copy_hdf5_field +{ + char **fields; + int n_fields; + int max_fields; +}; + + +struct copy_hdf5_field *new_copy_hdf5_field_list() +{ + struct copy_hdf5_field *n; + + n = calloc(1, sizeof(struct copy_hdf5_field)); + if ( n == NULL ) return NULL; + + n->max_fields = 32; + n->fields = malloc(n->max_fields*sizeof(char *)); + if ( n->fields == NULL ) { + free(n); + return NULL; + } + + return n; +} + + +void add_copy_hdf5_field(struct copy_hdf5_field *copyme, + const char *name) +{ + assert(copyme->n_fields < copyme->max_fields); /* FIXME */ + + copyme->fields[copyme->n_fields] = strdup(name); + if ( copyme->fields[copyme->n_fields] == NULL ) { + ERROR("Failed to add field for copying '%s'\n", name); + return; + } + + copyme->n_fields++; +} + + +void copy_hdf5_fields(struct hdfile *f, const struct copy_hdf5_field *copyme, + FILE *fh) +{ + int i; + + if ( copyme == NULL ) return; + + for ( i=0; i<copyme->n_fields; i++ ) { + + char *val; + char *field; + + field = copyme->fields[i]; + val = hdfile_get_string_value(f, field); + + if ( field[0] == '/' ) { + fprintf(fh, "hdf5%s = %s\n", field, val); + } else { + fprintf(fh, "hdf5/%s = %s\n", field, val); + } + + free(val); + + } +} + + char *hdfile_get_string_value(struct hdfile *f, const char *name) { hid_t dh; |