#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif
#include <string>

#ifndef H5_NO_NAMESPACE
#ifndef H5_NO_STD
    using std::cerr;
    using std::cout;
    using std::endl;
#endif  // H5_NO_STD
#endif

#include "testhdf5.h"   // C test header file
#include "H5Cpp.h"      // C++ API header file

#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif

#define SPACE1_DIM1             4
#define SPACE1_RANK             1
#define DSET_VLSTR_NAME         "vlstr_type"

static int
test_vlstr_dtype()
{
    const char *wdata[SPACE1_DIM1]= {
        "Four score and seven years ago our forefathers brought forth on this continent a new nation,",
        "conceived in liberty and dedicated to the proposition that all men are created equal.",
        "Now we are engaged in a great civil war,",
        "testing whether that nation or any nation so conceived and so dedicated can long endure."
        };   /* Information to write */
    char *rdata[SPACE1_DIM1];   /* Information read in */
    hid_t		native_type;       /* Datatype ID */
    hsize_t		dims1[] = {SPACE1_DIM1};

    H5File file("varlen.h5", H5F_ACC_TRUNC);

    /* Create dataspace for datasets */
    DataSpace sid1(SPACE1_RANK, dims1);

    /* Create a datatype to refer to */
    StrType tid1(0, H5T_VARIABLE);

    if(H5T_STRING!=H5Tget_class(tid1.getId()) || !H5Tis_variable_str(tid1.getId()))
        cerr << "this is not a variable length string type!!!" << endl;

    /* Create a dataset */
    DataSet dataset = file.createDataSet(DSET_VLSTR_NAME, tid1, sid1);

    /* Write dataset to disk */
    dataset.write((void*)wdata, tid1);

    /* Close Dataset */
    dataset.close();

    /* Open a dataset */
    dataset = file.openDataSet(DSET_VLSTR_NAME);

    /* Get datatype for dataset */
    DataType dtype = dataset.getDataType();

    /* Construct native type */
    if((native_type=H5Tget_native_type(dtype.getId(), H5T_DIR_DEFAULT))<0)
        cerr << "H5Tget_native_type  failed!!! " << endl;

    /* Check if the data type is equal */
    if(!H5Tequal(native_type, tid1.getId()))
        cerr << "native type is not tid1!!!" << endl;

    /* Read dataset from disk */
    dataset.read((void*)rdata, dtype);

    /* Validate and print data read in */
    cout << "data read:" << endl;
    for(unsigned i=0; i<SPACE1_DIM1; i++) {
        if(strlen(wdata[i])!=strlen(rdata[i])) {
	    cerr << "    VL data length don't match!, strlen(wdata[" << i 
		 << "])=" << strlen(wdata[i]) << ", strlen(rdata[" << i 
		 << "])=" << strlen(rdata[i]) << endl;

            return -1;
        } /* end if */

        if( strcmp(wdata[i],rdata[i]) != 0 ) {
	    cerr << "    VL data values don't match!, wdata[" << i 
		 << "]=" << wdata[i] << ", rdata[" << i << "]=" 
		 << rdata[i] << endl;
            return -1;
        } /* end if */
	else
	    cout << rdata[i];
    } /* end for */
    cout << endl;

    /* Free memory for rdata */
    for(unsigned i=0; i<SPACE1_DIM1; i++) {
        HDfree(rdata[i]);
    }
    return 0;
} /* end test_vlstr_dtype() */

int main()
{
    int num_errs = test_vlstr_dtype();
    cerr << "num_errs = " << num_errs << endl;
    return 0;
}
