/*
   h5cmpenum.c - ENUM in a compound datatype
 */

#include "hdf5.h"

#define FILE          "cenum.h5"
#define DATASETNAME   "enum"
#define LENGTH        10
#define RANK          1

#define CPTR(VAR,CONST) ((VAR)=(CONST),&(VAR))

typedef enum {
    E1_RED,
    E1_GREEN,
    E1_BLUE,
    E1_WHITE,
    E1_BLACK
} c_e1;

typedef struct c1_t {
  c_e1  color;
} c1_t;

c1_t c1[LENGTH];

int
main(void)
{

    c_e1   val;
    hid_t   file, dataset, space, type, c1_tid;
    herr_t  status;
    hsize_t dim[]= {LENGTH};
    int  i;


    for (i=0; i<5; i++)
      c1[i].color=i;
    for (i=5; i<LENGTH; i++)
      c1[i].color=i-5;

    /*
     * Create the data space.
     */
    space = H5Screate_simple (RANK, dim, NULL);
    printf ("H5Screate_simple: %i\n", space);

    /*
     * Create the file.
     */
    file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    printf ("H5Fcreate: %i\n", file);

    type = H5Tcreate(H5T_ENUM, sizeof(c_e1));
    printf ("H5Tcreate: %i\n", type);

    status = H5Tenum_insert(type, "RED",   CPTR(val, E1_RED  ));
    printf ("H5Tenum_insert (red): %i\n", status);
    status = H5Tenum_insert(type, "GREEN", CPTR(val, E1_GREEN));
    printf ("H5Tenum_insert (green): %i\n", status);
    status = H5Tenum_insert(type, "BLUE",  CPTR(val, E1_BLUE ));
    printf ("H5Tenum_insert (blue): %i\n", status);
    status = H5Tenum_insert(type, "WHITE", CPTR(val, E1_WHITE));
    printf ("H5Tenum_insert (white): %i\n", status);
    status = H5Tenum_insert(type, "BLACK", CPTR(val, E1_BLACK));
    printf ("H5Tenum_insert (black): %i\n", status);
    /*
     * Create the memory data type. 
     */
    c1_tid = H5Tcreate (H5T_COMPOUND, sizeof(type));
    printf ("H5Tcreate: %i\n", c1_tid);

    status = H5Tinsert(c1_tid, "color_name", HOFFSET(c1_t, color), type);
    printf ("H5Tinsert: %i\n", status);

    /* 
     * Create the dataset.
     */
    dataset = H5Dcreate(file, DATASETNAME, c1_tid, space, H5P_DEFAULT);
    printf ("H5Dcreate: %i\n", dataset);

    /*
     * Wtite data to the dataset; 
     */
    status = H5Dwrite(dataset, c1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, c1);
    printf ("H5Dwrite: %i\n", status);

    /*
     * Release resources
     */
    status = H5Tclose(type);
    printf ("H5Tclose (type): %i\n", status);
    status = H5Tclose(c1_tid);
    printf ("H5Tclose (c1_tid): %i\n", status);
    status = H5Sclose(space);
    printf ("H5Sclose (space): %i\n", status);
    status = H5Dclose(dataset);
    printf ("H5Dclose (dataset): %i\n", status);
    status = H5Fclose(file);
    printf ("H5Fclose (file): %i\n", status);
 
}
