Example #1
0
/*-------------------------------------------------------------------------
* subroutine for test_text_dtype(): test_variables().
*-------------------------------------------------------------------------
*/
static int test_variables(void)
{
    hid_t   dtype;
    H5T_class_t type_class;
    char*   dt_str;
    size_t  str_len;

    TESTING3("        text for variable types");

    if((dtype = H5LTtext_to_dtype("H5T_VLEN { H5T_NATIVE_CHAR }\n", H5LT_DDL))<0)
        goto out;

    if((type_class = H5Tget_class(dtype))<0)
        goto out;
    if(type_class != H5T_VLEN)
        goto out;

    if(H5Tis_variable_str(dtype))
        goto out;

    if(H5Tclose(dtype)<0)
        goto out;

    if((dtype = H5LTtext_to_dtype("H5T_VLEN { H5T_VLEN { H5T_STD_I32BE } }", H5LT_DDL))<0)
        goto out;

    if(H5Tis_variable_str(dtype))
        goto out;

    if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0)
        goto out;
    dt_str = (char*)calloc(str_len, sizeof(char));
    if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0)
        goto out;
    if(strcmp(dt_str, "H5T_VLEN {\n      H5T_VLEN {\n         H5T_STD_I32BE\n      }\n   }")) {
        printf("dt=\n%s\n", dt_str);
        goto out;
    }
    free(dt_str);

    if(H5Tclose(dtype)<0)
        goto out;

    PASSED();
    return 0;

out:
    H5_FAILED();
    return -1;
}
Example #2
0
 std::string toString() const
 {
     if( H5Tis_variable_str(this->type) )
         return "VLString";
     else
         return "String";
 }
Example #3
0
void write(const hid_attribute_adaptor& attrib, const std::string& value)
{
	hid_t attr_type_id = H5CPP_ERR_ON_NEG(H5Aget_type(attrib.id()));
	if(H5Tget_class(attr_type_id) != H5T_STRING)
		H5CPP_THROW("Attempted to set string value on non-string attribute '"
			<< object::get_name(attrib.id()) << '\'');

	const char* cstr = value.c_str();

	if(H5Tis_variable_str(attr_type_id))
		H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, &cstr));
	else
	{
		std::size_t attriblen = H5Aget_storage_size(attrib.id());
		if(attriblen > value.length())
		{
			// avoid reading past end of value into unalloc'd mem
			std::vector<char> buf(attriblen,'\0');
			std::copy(cstr, cstr+value.length(), &buf[0]);
			H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, &buf[0]));
		}
		else
			H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, cstr));
	}
}
Example #4
0
        size_t getSize() const
        {
            size_t myElements = H5Tget_size(this->type);

            /* for variable length string the size is first known after reading
             * the actual data or attribute, so we forward HDF5's behaviour */
            if( H5Tis_variable_str(this->type) )
               return myElements; /* == sizeof(char*) see H5Tget_size description */
            else
               return sizeof(char) * (myElements - 1); /* just as strlen() */
        }
bool hdf5_datatype::is_variable_length_string() const
{
    htri_t status = H5Tis_variable_str(get_id());
    if(status < 0) {
        boost::serialization::throw_exception(
            hdf5_archive_exception(
                hdf5_archive_exception::hdf5_archive_datatype_access_error
            )
        );
    }
    else if(status == 0) {
        return false;
    }
    return true;
}
Example #6
0
        static CollectionType* genType(hid_t datatype_id)
        {
            H5T_class_t h5_class = H5Tget_class(datatype_id);

            if(h5_class == H5T_STRING)
            {
                if( H5Tis_variable_str(datatype_id) )
                {
                    return new ColTypeString;
                } else
                {
                    size_t size = H5Tget_size(datatype_id);
                    return new ColTypeString(size);
                }
            } else
            {
                return NULL;
            }
        }
Example #7
0
int H5mdfile::H5_write_value(int argc, char **argv, Tcl_Interp *interp)
{
	/* Read value from Tcl and write it to dataset array */
	// Get array index
	int index=0;
	if(dataset_rank>=1) index+=atoi(argv[4+dataset_rank-0]);
	if(dataset_rank>=2) index+=atoi(argv[4+dataset_rank-1])*dims[dataset_rank-1];else goto label_index;
	if(dataset_rank>=3) index+=atoi(argv[4+dataset_rank-2])*dims[dataset_rank-1]*dims[dataset_rank-2];else goto label_index;
	if(dataset_rank>=4) index+=atoi(argv[4+dataset_rank-3])*dims[dataset_rank-1]*dims[dataset_rank-2]*dims[dataset_rank-3];else goto label_index;
	if(dataset_rank>=5) index+=atoi(argv[4+dataset_rank-4])*dims[dataset_rank-1]*dims[dataset_rank-2]*dims[dataset_rank-3]*dims[dataset_rank-4];else goto label_index;
	if(dataset_rank>=6) index+=atoi(argv[4+dataset_rank-5])*dims[dataset_rank-1]*dims[dataset_rank-2]*dims[dataset_rank-3]*dims[dataset_rank-4]*dims[dataset_rank-5];else goto label_index;
	if(dataset_rank>=7) index+=atoi(argv[4+dataset_rank-6])*dims[dataset_rank-1]*dims[dataset_rank-2]*dims[dataset_rank-3]*dims[dataset_rank-4]*dims[dataset_rank-5]*dims[dataset_rank-6];else goto label_index;
	if(dataset_rank>=8) index+=atoi(argv[4+dataset_rank-7])*dims[dataset_rank-1]*dims[dataset_rank-2]*dims[dataset_rank-3]*dims[dataset_rank-4]*dims[dataset_rank-5]*dims[dataset_rank-6]*dims[dataset_rank-7];else goto label_index;
	label_index:
	// Write single value from Tcl to dataset array
	if(H5Tequal(dataset_type_id, H5T_NATIVE_FLOAT))
	{
		dset_data_float = static_cast<float*>(dset_data);
		dset_data_float[index]=(float)atof(argv[3]);
	}
	if(H5Tequal(dataset_type_id, H5T_NATIVE_DOUBLE))
	{
		dset_data_double = static_cast<double*>(dset_data);
		dset_data_double[index]=atof(argv[3]);
	}
	if(H5Tequal(dataset_type_id, H5T_NATIVE_INT))
	{
		dset_data_int = static_cast<int*>(dset_data);
		dset_data_int[index]=atoi(argv[3]);
	}
	
	if(H5Tis_variable_str(dataset_type_id)!=0)
	{
//		dset_data_string = static_cast<h5string*>(dset_data);
//        dset_data_chars=static_cast<char *>(dset_data);
        dset_data_chars=(char**) dset_data;
		dset_data_chars[index]=strdup(argv[3]);
	}
	return TCL_OK;
}
H5CompoundData::H5CompoundData(H5Object & _parent, const hsize_t _totalSize, const hsize_t _dataSize, const hsize_t _ndims, const hsize_t * _dims, char * _data, hid_t compoundType, const hsize_t _stride, const size_t _offset, const bool _dataOwner) : H5BasicData<char>(_parent, _totalSize, _dataSize, _ndims, _dims, _data, _stride, _offset, _dataOwner), type(compoundType), cumprod(H5Object::getCumProd(_ndims, dims))
{
    nfields = (unsigned int)H5Tget_nmembers(compoundType);
    infos = new std::map<std::string, FieldInfo *>();
    fieldinfos = new FieldInfo *[nfields];
    for (unsigned int i = 0; i < nfields; i++)
    {
        hid_t mtype = H5Tget_member_type(compoundType, i);
        hsize_t size = H5Tget_size(mtype);
        char * mname = H5Tget_member_name(compoundType, i);
        size_t offs = H5Tget_member_offset(compoundType, i);
        FieldInfo * info = 0;
        if (H5Tget_class(type) == H5T_STRING && !H5Tis_variable_str(type))
        {
            // We have a C-string so it is null terminated
            size++;
        }

        info = new FieldInfo(mtype, size, offs, std::string(mname));
        (*infos)[std::string(mname)] = info;
        fieldinfos[i] = info;
        free(mname);
    }
}
Example #9
0
bool DataType::isVariableString() const {
    HTri res = H5Tis_variable_str(hid);
    res.check("DataType::isVariableString(): H5Tis_variable_str failed");
    return res.result();
}
Example #10
0
std::string fast5_get_string_attribute(fast5_file& fh, const std::string& group_name, const std::string& attribute_name)
{
    hid_t group, attribute, attribute_type, native_type;
    std::string out;

    // according to http://hdf-forum.184993.n3.nabble.com/check-if-dataset-exists-td194725.html
    // we should use H5Lexists to check for the existence of a group/dataset using an arbitrary path
    // HDF5 1.8 returns 0 on the root group, so we explicitly check for it
    int ret = group_name == "/" ? 1 : H5Lexists(fh.hdf5_file, group_name.c_str(), H5P_DEFAULT);
    if(ret <= 0) {
        return "";
    }

    // Open the group containing the attribute we want
    group = H5Gopen(fh.hdf5_file, group_name.c_str(), H5P_DEFAULT);
    if(group < 0) {
#ifdef DEBUG_FAST5_IO
        fprintf(stderr, "could not open group %s\n", group_name.c_str());
#endif
        goto close_group;
    }

    // Ensure attribute exists
    ret = H5Aexists(group, attribute_name.c_str());
    if(ret <= 0) {
        goto close_group;
    }

    // Open the attribute
    attribute = H5Aopen(group, attribute_name.c_str(), H5P_DEFAULT);
    if(attribute < 0) {
#ifdef DEBUG_FAST5_IO
        fprintf(stderr, "could not open attribute: %s\n", attribute_name.c_str());
#endif
        goto close_attr;
    }

    // Get data type and check it is a fixed-length string
    attribute_type = H5Aget_type(attribute);
    if(attribute_type < 0) {
#ifdef DEBUG_FAST5_IO
        fprintf(stderr, "failed to get attribute type %s\n", attribute_name.c_str());
#endif
        goto close_type;
    }

    if(H5Tget_class(attribute_type) != H5T_STRING) {
#ifdef DEBUG_FAST5_IO
        fprintf(stderr, "attribute %s is not a string\n", attribute_name.c_str());
#endif
        goto close_type;
    }

    native_type = H5Tget_native_type(attribute_type, H5T_DIR_ASCEND);
    if(native_type < 0) {
#ifdef DEBUG_FAST5_IO
        fprintf(stderr, "failed to get native type for %s\n", attribute_name.c_str());
#endif
        goto close_native_type;
    }

    if(H5Tis_variable_str(attribute_type) > 0) {
        // variable length string
        char* buffer;
        ret = H5Aread(attribute, native_type, &buffer);
        if(ret < 0) {
            fprintf(stderr, "error reading attribute %s\n", attribute_name.c_str());
            exit(EXIT_FAILURE);
        }
        out = buffer;
        free(buffer);
        buffer = NULL;

    } else {
        // fixed length string
        size_t storage_size;
        char* buffer;

        // Get the storage size and allocate
        storage_size = H5Aget_storage_size(attribute);
        buffer = (char*)calloc(storage_size + 1, sizeof(char));

        // finally read the attribute
        ret = H5Aread(attribute, attribute_type, buffer);
        if(ret >= 0) {
            out = buffer;
        }

        // clean up
        free(buffer);
    }

close_native_type:
    H5Tclose(native_type);    
close_type:
    H5Tclose(attribute_type);
close_attr:
    H5Aclose(attribute);
close_group:
    H5Gclose(group);

    return out;
}
Example #11
0
/*-------------------------------------------------------------------------
 * Function:    h5tools_str_sprint
 *
 * Purpose: Renders the value pointed to by VP of type TYPE into variable
 *      length string STR.
 *
 * Return:  A pointer to memory containing the result or NULL on error.
 *
 * Programmer:  Robb Matzke
 *              Thursday, July 23, 1998
 *
 * Modifications:
 *      Robb Matzke, 1999-04-26
 *      Made this function safe from overflow problems by allowing it
 *      to reallocate the output string.
 *
 *      Robb Matzke, 1999-06-04
 *      Added support for object references. The new `container'
 *      argument is the dataset where the reference came from.
 *
 *      Robb Matzke, 1999-06-07
 *      Added support for printing raw data. If info->raw is non-zero
 *      then data is printed in hexadecimal format.
 *
 *  Robb Matzke, 2003-01-10
 *  Binary output format is dd:dd:... instead of 0xdddd... so it
 *  doesn't look like a hexadecimal integer, and thus users will
 *  be less likely to complain that HDF5 didn't properly byte
 *  swap their data during type conversion.
 *
 *  Robb Matzke, LLNL, 2003-06-05
 *  If TYPE is a variable length string then the pointer to
 *  the value to pring (VP) is a pointer to a `char*'.
 *
 *  PVN, 28 March 2006
 *  added H5T_NATIVE_LDOUBLE case
 *-------------------------------------------------------------------------
 */
char *
h5tools_str_sprint(h5tools_str_t *str, const h5tool_format_t *info, hid_t container,
                   hid_t type, void *vp, h5tools_context_t *ctx)
{
    size_t         n, offset, size=0, nelmts, start;
    char          *name;
    unsigned char *ucp_vp = (unsigned char *)vp;
    char          *cp_vp = (char *)vp;
    hid_t          memb, obj, region;
    unsigned       nmembs;
    static char    fmt_llong[8], fmt_ullong[8];
    H5T_str_t      pad;

    /*
     * some tempvars to store the value before we append it to the string to
     * get rid of the memory alignment problem
     */
    unsigned long long tempullong;
    long long          templlong;
    unsigned long      tempulong;
    long               templong;
    unsigned int       tempuint;
    int                tempint;

    /* Build default formats for long long types */
    if (!fmt_llong[0]) {
        sprintf(fmt_llong, "%%%sd", H5_PRINTF_LL_WIDTH);
        sprintf(fmt_ullong, "%%%su", H5_PRINTF_LL_WIDTH);
    }

    /* Append value depending on data type */
    start = h5tools_str_len(str);

    if (info->raw) {
        size_t i;

        n = H5Tget_size(type);
        if (1 == n) {
            h5tools_str_append(str, OPT(info->fmt_raw, "0x%02x"), ucp_vp[0]);
        }
        else {
            for (i = 0; i < n; i++) {
                if (i)
                    h5tools_str_append(str, ":");
                h5tools_str_append(str, OPT(info->fmt_raw, "%02x"), ucp_vp[i]);
            }
        }
    }
    else if (H5Tequal(type, H5T_NATIVE_FLOAT)) {
        float tempfloat;

        HDmemcpy(&tempfloat, vp, sizeof(float));
        h5tools_str_append(str, OPT(info->fmt_float, "%g"), tempfloat);
    }
    else if (H5Tequal(type, H5T_NATIVE_DOUBLE)) {
        double tempdouble;

        HDmemcpy(&tempdouble, vp, sizeof(double));
        h5tools_str_append(str, OPT(info->fmt_double, "%g"), tempdouble);
#if H5_SIZEOF_LONG_DOUBLE !=0
    }
    else if (H5Tequal(type, H5T_NATIVE_LDOUBLE)) {
        long double templdouble;

        HDmemcpy(&templdouble, vp, sizeof(long double));
        h5tools_str_append(str, "%Lf", templdouble);
#endif
    }
    else if (info->ascii && (H5Tequal(type, H5T_NATIVE_SCHAR) ||
                             H5Tequal(type, H5T_NATIVE_UCHAR))) {
        h5tools_print_char(str, info, (char) (*ucp_vp));
    }
    else if (H5T_STRING == H5Tget_class(type)) {
        unsigned int i;
        char quote = '\0';
        char *s;

        quote = '\0';
        if (H5Tis_variable_str(type)) {
            /* cp_vp is the pointer into the struct where a `char*' is stored. So we have
             * to dereference the pointer to get the `char*' to pass to HDstrlen(). */
            s = *(char**) cp_vp;
            if (s != NULL)
                size = HDstrlen(s);
        }
        else {
            s = cp_vp;
            size = H5Tget_size(type);
        }
        pad = H5Tget_strpad(type);

        /* Check for NULL pointer for string */
        if (s == NULL) {
            h5tools_str_append(str, "NULL");
        }
        else {
            for (i = 0; i < size && (s[i] || pad != H5T_STR_NULLTERM); i++) {
                int j = 1;

                /*
                 * Count how many times the next character repeats. If the
                 * threshold is zero then that means it can repeat any number
                 * of times.
                 */
                if (info->str_repeat > 0)
                    while (i + j < size && s[i] == s[i + j])
                        j++;

                /*
                 * Print the opening quote.  If the repeat count is high enough to
                 * warrant printing the number of repeats instead of enumerating
                 * the characters, then make sure the character to be repeated is
                 * in it's own quote.
                 */
                if (info->str_repeat > 0 && j > info->str_repeat) {
                    if (quote)
                        h5tools_str_append(str, "%c", quote);

                    quote = '\'';
                    h5tools_str_append(str, "%s%c", i ? " " : "", quote);
                }
                else if (!quote) {
                    quote = '"';
                    h5tools_str_append(str, "%s%c", i ? " " : "", quote);
                }

                /* Print the character */
                h5tools_print_char(str, info, s[i]);

                /* Print the repeat count */
                if (info->str_repeat && j > info->str_repeat) {
#ifdef REPEAT_VERBOSE
                    h5tools_str_append(str, "%c repeats %d times", quote, j - 1);
#else
                    h5tools_str_append(str, "%c*%d", quote, j - 1);
#endif  /* REPEAT_VERBOSE */
                    quote = '\0';
                    i += j - 1;
                }

            }

            if (quote)
                h5tools_str_append(str, "%c", quote);

            if (i == 0)
                /*empty string*/
                h5tools_str_append(str, "\"\"");
        } /* end else */
    }
    else if (H5Tequal(type, H5T_NATIVE_INT)) {
        HDmemcpy(&tempint, vp, sizeof(int));
#ifdef H5_HAVE_H5DUMP_PACKED_BITS
        if(packed_bits_num)
            tempint = (tempint >> packed_data_offset) & packed_data_mask;
#endif
        h5tools_str_append(str, OPT(info->fmt_int, "%d"), tempint);
    }
    else if (H5Tequal(type, H5T_NATIVE_UINT)) {
Example #12
0
/****************************************************************
**
**  test_vlstring_type(): Test VL string type.
**      Tests if VL string is treated as string.
**
****************************************************************/
static void test_vlstring_type(void)
{
    hid_t               fid;           /* HDF5 File IDs                */
    hid_t               tid_vlstr;
    H5T_cset_t          cset;
    H5T_str_t           pad;
    htri_t              vl_str;         /* Whether string is VL */
    herr_t              ret;

    /* Output message about test being performed */
    MESSAGE(5, ("Testing VL String type\n"));

    /* Open file */
    fid = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT);
    CHECK(fid, FAIL, "H5Fopen");

    /* Create a datatype to refer to */
    tid_vlstr = H5Tcopy(H5T_C_S1);
    CHECK(tid_vlstr, FAIL, "H5Tcopy");

    /* Change padding and verify it */
    ret = H5Tset_strpad(tid_vlstr, H5T_STR_NULLPAD);
    CHECK(ret, FAIL, "H5Tset_strpad");
    pad = H5Tget_strpad(tid_vlstr);
    VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad");

    /* Convert to variable-length string */
    ret = H5Tset_size(tid_vlstr, H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    /* Check if datatype is VL string */
    ret = H5Tget_class(tid_vlstr);
    VERIFY(ret, H5T_STRING, "H5Tget_class");
    ret = H5Tis_variable_str(tid_vlstr);
    VERIFY(ret, TRUE, "H5Tis_variable_str");

    /* Verify that the class detects as a string */
    vl_str = H5Tdetect_class(tid_vlstr, H5T_STRING);
    CHECK(vl_str, FAIL, "H5Tdetect_class");
    VERIFY(vl_str, TRUE, "H5Tdetect_class");

    /* Check default character set and padding */
    cset = H5Tget_cset(tid_vlstr);
    VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset");
    pad = H5Tget_strpad(tid_vlstr);
    VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad");

    /* Commit variable-length string datatype to storage */
    ret = H5Tcommit2(fid, VLSTR_TYPE, tid_vlstr, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(ret, FAIL, "H5Tcommit2");

    /* Close datatype */
    ret = H5Tclose(tid_vlstr);
    CHECK(ret, FAIL, "H5Tclose");

    tid_vlstr = H5Topen2(fid, VLSTR_TYPE, H5P_DEFAULT);
    CHECK(tid_vlstr, FAIL, "H5Topen2");

    ret = H5Tclose(tid_vlstr);
    CHECK(ret, FAIL, "H5Tclose");

    ret = H5Fclose(fid);
    CHECK(ret, FAIL, "H5Fclose");


    fid = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT);
    CHECK(fid, FAIL, "H5Fopen");

    /* Open the variable-length string datatype just created */
    tid_vlstr = H5Topen2(fid, VLSTR_TYPE, H5P_DEFAULT);
    CHECK(tid_vlstr, FAIL, "H5Topen2");

    /* Verify character set and padding */
    cset = H5Tget_cset(tid_vlstr);
    VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset");
    pad = H5Tget_strpad(tid_vlstr);
    VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad");

    /* Close datatype and file */
    ret = H5Tclose(tid_vlstr);
    CHECK(ret, FAIL, "H5Tclose");
    ret = H5Fclose(fid);
    CHECK(ret, FAIL, "H5Fclose");

} /* end test_vlstring_type() */
Example #13
0
static herr_t HDF5AttrIterate( hid_t hH5ObjID,
                               const char *pszAttrName,
                               // TODO(schwehr): void * -> HDF5Dataset *
                               void *pDS )
{
    char **papszTokens = nullptr;
    CPLString osKey;
    HDF5Dataset *const poDS = static_cast<HDF5Dataset *>(pDS);

    // Convert "/" into "_" for the path component
    const char *pszPath = poDS->poH5CurrentObject->pszUnderscorePath;
    if(pszPath != nullptr && strlen(pszPath) > 0)
    {
        papszTokens = CSLTokenizeString2(pszPath, "/", CSLT_HONOURSTRINGS);

        for( hsize_t i = 0; papszTokens != nullptr && papszTokens[i] != nullptr; ++i )
        {
            if( i != 0)
                osKey += '_';
            osKey += papszTokens[i];
        }
        CSLDestroy(papszTokens);
    }

    // Convert whitespaces into "_" for the attribute name component
    papszTokens = CSLTokenizeString2(
        pszAttrName, " ", CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES);
    for( hsize_t i = 0; papszTokens != nullptr && papszTokens[i] != nullptr; ++i )
    {
        if(!osKey.empty())
            osKey += '_';
        osKey += papszTokens[i];
    }
    CSLDestroy(papszTokens);

    const hid_t hAttrID = H5Aopen_name(hH5ObjID, pszAttrName);
    const hid_t hAttrTypeID = H5Aget_type(hAttrID);
    const hid_t hAttrNativeType =
        H5Tget_native_type(hAttrTypeID, H5T_DIR_DEFAULT);
    const hid_t hAttrSpace = H5Aget_space(hAttrID);

    if( H5Tget_class(hAttrNativeType) == H5T_VLEN )
        return 0;

    hsize_t nSize[64] = {};
    const unsigned int nAttrDims =
        H5Sget_simple_extent_dims(hAttrSpace, nSize, nullptr);

    unsigned int nAttrElmts = 1;
    for( hsize_t i = 0; i < nAttrDims; i++ )
    {
        nAttrElmts *= static_cast<int>(nSize[i]);
    }

    char *szData = nullptr;
    hsize_t nAttrSize = 0;
    char *szValue = nullptr;

    if( H5Tget_class(hAttrNativeType) == H5T_STRING )
    {
        if ( H5Tis_variable_str(hAttrNativeType) )
        {
            char **papszStrings =
                static_cast<char **>(CPLMalloc(nAttrElmts * sizeof(char *)));

            // Read the values.
            H5Aread(hAttrID, hAttrNativeType, papszStrings);

            // Concatenate all values as one string separated by a space.
            CPLString osVal = papszStrings[0];
            for( hsize_t i = 1; i < nAttrElmts; i++ )
            {
                osVal += " ";
                osVal += papszStrings[i];
            }

            szValue = static_cast<char *>(CPLMalloc(osVal.length() + 1));
            strcpy(szValue, osVal.c_str());

            H5Dvlen_reclaim(hAttrNativeType, hAttrSpace, H5P_DEFAULT,
                            papszStrings);
            CPLFree(papszStrings);
        }
        else
        {
            nAttrSize = H5Aget_storage_size(hAttrID);
            szValue = static_cast<char *>(CPLMalloc((size_t)(nAttrSize + 1)));
            H5Aread(hAttrID, hAttrNativeType, szValue);
            szValue[nAttrSize] = '\0';
        }
    }
    else
    {
        const size_t nDataLen = 8192;
        void *buf = nullptr;

        if( nAttrElmts > 0 )
        {
            buf = CPLMalloc(nAttrElmts * H5Tget_size(hAttrNativeType));
            szData = static_cast<char *>(CPLMalloc(nDataLen));
            szValue = static_cast<char *>(CPLMalloc(MAX_METADATA_LEN));
            szData[0] = '\0';
            szValue[0] = '\0';
            H5Aread(hAttrID, hAttrNativeType, buf);
        }
        if( H5Tequal(H5T_NATIVE_CHAR, hAttrNativeType ) ||
            H5Tequal(H5T_NATIVE_SCHAR, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ )
            {
                snprintf(szData, nDataLen, "%c ", static_cast<char *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_UCHAR, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ )
            {
                snprintf(szData, nDataLen, "%c", static_cast<char *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_SHORT, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ )
            {
                snprintf(szData, nDataLen, "%d ", static_cast<short *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_USHORT, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ )
            {
              snprintf(szData, nDataLen, "%ud ",
                       static_cast<unsigned short *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_INT, hAttrNativeType) )
        {
            for( hsize_t i=0; i < nAttrElmts; i++ )
            {
                snprintf(szData, nDataLen, "%d ", static_cast<int *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_UINT, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ )
            {
                snprintf(szData, nDataLen, "%ud ",
                         static_cast<unsigned int *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_LONG, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ )
            {
                snprintf(szData, nDataLen, "%ld ", static_cast<long *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_ULONG, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ ) {
                snprintf(szData, nDataLen, "%lu ",
                         static_cast<unsigned long *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_FLOAT, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ )
            {
                CPLsnprintf(szData, nDataLen, "%.8g ",
                            static_cast<float *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        else if( H5Tequal(H5T_NATIVE_DOUBLE, hAttrNativeType) )
        {
            for( hsize_t i = 0; i < nAttrElmts; i++ )
            {
                CPLsnprintf(szData, nDataLen, "%.15g ",
                            static_cast<double *>(buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                    MAX_METADATA_LEN )
                    CPLError(CE_Warning, CPLE_OutOfMemory,
                             "Header data too long. Truncated");
            }
        }
        CPLFree(buf);
    }
    H5Sclose(hAttrSpace);
    H5Tclose(hAttrNativeType);
    H5Tclose(hAttrTypeID);
    H5Aclose(hAttrID);
    poDS->papszMetadata = CSLSetNameValue(poDS->papszMetadata, osKey, szValue);

    CPLFree(szData);
    CPLFree(szValue);

    return 0;
}
Example #14
0
herr_t HDF5AttrIterate( hid_t hH5ObjID,
                        const char *pszAttrName,
                        void *pDS )
{
    hid_t           hAttrID;
    hid_t           hAttrTypeID;
    hid_t           hAttrNativeType;
    hid_t           hAttrSpace;

    char           *szData = NULL;
    hsize_t        nSize[64];
    unsigned int   nAttrElmts;
    hsize_t        nAttrSize;
    hsize_t        i;
    void           *buf = NULL;
    unsigned int   nAttrDims;

    char          **papszTokens;

    HDF5Dataset    *poDS;
    CPLString       osKey;
    char           *szValue = NULL;

    poDS = (HDF5Dataset *) pDS;

    // Convert "/" into "_" for the path component
    const char* pszPath = poDS->poH5CurrentObject->pszUnderscorePath;
    if(pszPath != NULL && strlen(pszPath) > 0)
    {
        papszTokens = CSLTokenizeString2( pszPath, "/", CSLT_HONOURSTRINGS );

        for( i = 0; papszTokens != NULL && papszTokens[i] != NULL; ++i )
        {
            if( i != 0)
                osKey += '_';
            osKey += papszTokens[i];
        }
        CSLDestroy( papszTokens );
    }

    // Convert whitespaces into "_" for the attribute name component
    papszTokens = CSLTokenizeString2( pszAttrName, " ",
                            CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES );
    for( i = 0; papszTokens != NULL && papszTokens[i] != NULL; ++i )
    {
        if(!osKey.empty())
            osKey += '_';
        osKey += papszTokens[i];
    }
    CSLDestroy( papszTokens );

    hAttrID          = H5Aopen_name( hH5ObjID, pszAttrName );
    hAttrTypeID      = H5Aget_type( hAttrID );
    hAttrNativeType  = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT );
    hAttrSpace       = H5Aget_space( hAttrID );
    nAttrDims        = H5Sget_simple_extent_dims( hAttrSpace, nSize, NULL );

    nAttrElmts = 1;
    for( i=0; i < nAttrDims; i++ ) {
        nAttrElmts *= (int) nSize[i];
    }

    if( H5Tget_class( hAttrNativeType ) == H5T_STRING )
    {
        if ( H5Tis_variable_str(hAttrNativeType) )
        {
            char** papszStrings;
            papszStrings = (char**) CPLMalloc( nAttrElmts * sizeof(char*) );

            // Read the values
            H5Aread( hAttrID, hAttrNativeType, papszStrings );

            // Concatenate all values as one string (separated by a space)
            CPLString osVal = papszStrings[0];
            for( i=1; i < nAttrElmts; i++ ) {
                osVal += " ";
                osVal += papszStrings[i];
            }

            szValue = (char*) CPLMalloc(osVal.length() + 1);
            strcpy( szValue, osVal.c_str() );

            H5Dvlen_reclaim( hAttrNativeType, hAttrSpace, H5P_DEFAULT,
                             papszStrings );
            CPLFree( papszStrings );
        }
        else
        {
            nAttrSize = H5Aget_storage_size( hAttrID );
            szValue = (char*) CPLMalloc((size_t) (nAttrSize+1));
            H5Aread( hAttrID, hAttrNativeType, szValue );
            szValue[nAttrSize] = '\0';
        }
    }
    else {
        if( nAttrElmts > 0 ) {
            buf = (void *) CPLMalloc( nAttrElmts*
                          H5Tget_size( hAttrNativeType ));
            szData = (char*) CPLMalloc( 8192 );
            szValue = (char*) CPLMalloc( MAX_METADATA_LEN );
            szData[0] = '\0';
            szValue[0] ='\0';
            H5Aread( hAttrID, hAttrNativeType, buf );
        }
        if( H5Tequal( H5T_NATIVE_CHAR, hAttrNativeType ) 
            || H5Tequal( H5T_NATIVE_SCHAR,  hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                sprintf( szData, "%c ", ((char *) buf)[i]);
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_UCHAR,  hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                sprintf( szData, "%c", ((char *) buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_SHORT,  hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                sprintf( szData, "%d ", ((short *) buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_USHORT, hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                sprintf( szData, "%ud ", ((unsigned short *) buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_INT,    hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                sprintf( szData, "%d ", ((int *) buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_UINT,   hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                sprintf( szData, "%ud ", ((unsigned int *) buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_LONG,   hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                sprintf( szData, "%ld ", ((long *)buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_ULONG,  hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                sprintf( szData, "%ld ", ((unsigned long *)buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_FLOAT,  hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                CPLsprintf( szData, "%.8g ",  ((float *)buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) {
            for( i=0; i < nAttrElmts; i++ ) {
                CPLsprintf( szData, "%.15g ",  ((double *)buf)[i] );
                if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >=
                                                            MAX_METADATA_LEN )
                    CPLError( CE_Warning, CPLE_OutOfMemory,
                              "Header data too long. Truncated\n");
            }
        }
        CPLFree( buf );

    }
    H5Sclose(hAttrSpace);
    H5Tclose(hAttrNativeType);
    H5Tclose(hAttrTypeID);
    H5Aclose( hAttrID );
    poDS->papszMetadata = CSLSetNameValue( poDS->papszMetadata, osKey, szValue);

    CPLFree( szData );
    CPLFree( szValue );

    return 0;
}
Example #15
0
  MatlabObject::MatlabObject (const HDF5::Object& o) {
    HDF5::Object object = o;

    bool hasEmptyAttribute;

    for (;;) {
      isOctaveNewFormat_ = object.getType () == H5I_GROUP && object.existsAttribute ("OCTAVE_NEW_FORMAT");
      if (!isOctaveNewFormat_) {
        hasEmptyAttribute = object.existsAttribute ("MATLAB_empty");
        break;
      }

      HDF5::Group grp = (HDF5::Group) object;
      HDF5::DataSet typeDs = (HDF5::DataSet) grp.open ("type", setEFilePrefix ());

      ASSERT (typeDs.getSpace ().getSimpleExtentType () == H5S_SCALAR);
      HDF5::DataSpace s = HDF5::DataSpace::create (H5S_SCALAR);

      HDF5::StringType ty = (HDF5::StringType) typeDs.getDataType ();
      ASSERT (!(Exception::check ("H5Tis_variable_str", H5Tis_variable_str (ty.handle ())) != 0));

      /*
        const char* str = NULL;
        HDF5::DataType t = getH5Type<const char*> ();
        typeDs.read (&str, t, s, s);
        std::string str2 (str);
        typeDs.vlenReclaim (&str, t, s);
      */

      size_t size = H5Tget_size (ty.handle ());
      HDF5::Exception::check ("H5Tget_size", size ? 0 : -1);
      std::vector<char> str (size);
      typeDs.read (str.data (), ty, s, s);
      std::string str2 (str.begin (), str.end ());

      size_t pos = str2.find ('\0');
      if (pos != std::string::npos)
        str2 = str2.substr (0, pos);

      if (str2 != "cell") { 
        hasEmptyAttribute = grp.existsAttribute ("OCTAVE_EMPTY_MATRIX");
        object = grp.open ("value", setEFilePrefix ());
        octaveType_ = str2;
        break;
      } else {
        HDF5::DataSet dimsDs = (HDF5::DataSet) grp.open ("value/dims", setEFilePrefix ());
        HDF5::DataSpace dimsSp = dimsDs.getSpace ();
        ASSERT (dimsSp.getSimpleExtentType () == H5S_SIMPLE);
        ASSERT (dimsSp.getSimpleExtentNdims () == 1);
        hsize_t dim;
        dimsSp.getSimpleExtentDims (&dim);
        ASSERT (dim == 2);
        
        int64_t dims[2];
        dimsDs.read (dims, HDF5::getH5Type<int64_t> (), dimsSp);
        ASSERT (dims[0] == 1);
        ASSERT (dims[1] == 1);

        object = grp.open ("value/_0", setEFilePrefix ());
      }
    }

    H5I_type_t type = object.getType ();

    if (type == H5I_DATASET) {
      isStruct_ = false;
      dataSet_ = (HDF5::DataSet) object;
      if (hasEmptyAttribute) {
        isEmpty_ = true;
        isNullDataSpace_ = false;

        HDF5::DataSpace dataSpace = dataSet_.getSpace ();
        ASSERT (dataSpace.getSimpleExtentType () == H5S_SIMPLE);
        ASSERT (dataSpace.getSimpleExtentNdims () == 1);
        hsize_t ndims;
        dataSpace.getSimpleExtentDims (&ndims);
        ASSERT (ndims > 0);

        std::vector<uint64_t> values (Core::checked_cast<size_t> (ndims));
        dataSet_.read (values.data (), getH5Type<uint64_t> (), dataSpace);

        bool foundZero = false;
        for (size_t i = 0; i < ndims; i++)
          if (!values[i])
            foundZero = true;
        ASSERT (foundZero);

        size_.resize (Core::checked_cast<size_t> (ndims));
        for (size_t i = 0; i < ndims; i++) {
          ASSERT (values[i] <= std::numeric_limits<size_t>::max ());
          size_[i] = Core::checked_cast<size_t> (values[i]);
        }
      } else {
        dataSpace_ = dataSet_.getSpace ();
        H5S_class_t extentType = dataSpace_.getSimpleExtentType ();
        //ASSERT (extentType == H5S_SIMPLE || extentType == H5S_NULL);
        ASSERT (extentType == H5S_SIMPLE || extentType == H5S_NULL || extentType == H5S_SCALAR);
        if (extentType == H5S_SIMPLE) {
          isNullDataSpace_ = false;
          size_t dim = dataSpace_.getSimpleExtentNdims ();
          size_.resize (dim);
          std::vector<hsize_t> dims (dim);
          dataSpace_.getSimpleExtentDims (dims.data ());
          isEmpty_ = false;
          for (size_t i = 0; i < dim; i++) {
            ASSERT (dims[i] >= std::numeric_limits<size_t>::min () && dims[i] <= std::numeric_limits<size_t>::max ());
            size_[dim - 1 - i] = Core::checked_cast<size_t> (dims[i]);
            if (!dims[i])
              isEmpty_ = true;
          }
          if (isEmpty_) {
            dataSpace_ = HDF5::DataSpace ();
          }
        } else if (extentType == H5S_SCALAR) {
          isNullDataSpace_ = false;
          isEmpty_ = false;
          //size_.resize (0);
          size_.resize (1);
          size_[0] = 1;
        } else { // H5S_NULL
          isEmpty_ = true;
          isNullDataSpace_ = true;
          size_.resize (1);
          size_[0] = 0;
        }
      }
      if (isEmpty_)
        dataSet_ = HDF5::DataSet ();
    } else if (type == H5I_GROUP) {
      isStruct_ = true;
      group_ = (HDF5::Group) object;
    } else {
      ABORT_MSG ("Unknown object type");
    }
  }
Example #16
0
hsize_t diff_attr(hid_t loc1_id,
                  hid_t loc2_id,
                  const char *path1,
                  const char *path2,
                  diff_opt_t *options)
{
    hid_t      attr1_id=-1;     /* attr ID */
    hid_t      attr2_id=-1;     /* attr ID */
    hid_t      space1_id=-1;    /* space ID */
    hid_t      space2_id=-1;    /* space ID */
    hid_t      ftype1_id=-1;    /* file data type ID */
    hid_t      ftype2_id=-1;    /* file data type ID */
    int	       vstrtype1=0;     /* ftype1 is a variable string */
    int	       vstrtype2=0;     /* ftype2 is a variable string */
    hid_t      mtype1_id=-1;    /* memory data type ID */
    hid_t      mtype2_id=-1;    /* memory data type ID */
    size_t     msize1;          /* memory size of memory type */
    size_t     msize2;          /* memory size of memory type */
    void       *buf1=NULL;      /* data buffer */
    void       *buf2=NULL;      /* data buffer */
    int	       buf1hasdata=0;	/* buffer has data */
    int	       buf2hasdata=0;	/* buffer has data */
    hsize_t    nelmts1;         /* number of elements in dataset */
    int        rank1;           /* rank of dataset */
    int        rank2;           /* rank of dataset */
    hsize_t    dims1[H5S_MAX_RANK];/* dimensions of dataset */
    hsize_t    dims2[H5S_MAX_RANK];/* dimensions of dataset */
    char       *name1;
    char       *name2;
    char       np1[512];
    char       np2[512];
    unsigned   u;                  /* Local index variable */
    hsize_t    nfound = 0;
    hsize_t    nfound_total = 0;
    int       j;

    table_attrs_t * match_list_attrs = NULL;
    if( build_match_list_attrs(loc1_id, loc2_id, &match_list_attrs, options) < 0)
        goto error;

    /* if detect any unique extra attr */
    if(match_list_attrs->nattrs_only1 || match_list_attrs->nattrs_only2)
    {
        /* exit will be 1 */
        options->contents = 0;
    }

    for(u = 0; u < (unsigned)match_list_attrs->nattrs; u++)
    {
        if( (match_list_attrs->attrs[u].exist[0]) && (match_list_attrs->attrs[u].exist[1]) )
        {
        name1 = name2 = match_list_attrs->attrs[u].name;

       /*--------------
        * attribute 1 */
        if((attr1_id = H5Aopen(loc1_id, name1, H5P_DEFAULT)) < 0)
            goto error;

       /*--------------
        * attribute 2 */
        if((attr2_id = H5Aopen(loc2_id, name2, H5P_DEFAULT)) < 0)
            goto error;

        /* get the datatypes  */
        if((ftype1_id = H5Aget_type(attr1_id)) < 0)
            goto error;
	vstrtype1 = H5Tis_variable_str(ftype1_id);
        if((ftype2_id = H5Aget_type(attr2_id)) < 0)
            goto error;
	vstrtype2 = H5Tis_variable_str(ftype2_id);
	/* no compare if either one but not both are variable string type */
	if (vstrtype1 != vstrtype2){
	    if ((options->m_verbose||options->m_list_not_cmp))
		parallel_print("Not comparable: one of attribute <%s/%s> or <%s/%s> is of variable length type\n",
		    path1, name1, path2, name2);
	    options->not_cmp = 1;
	    return 0;
	}

        if((mtype1_id = h5tools_get_native_type(ftype1_id))<0)
            goto error;
        if((mtype2_id = h5tools_get_native_type(ftype2_id))<0)
            goto error;
        if((msize1 = H5Tget_size(mtype1_id))==0)
            goto error;
        if((msize2 = H5Tget_size(mtype2_id))==0)
            goto error;

        /* get the dataspace   */
        if((space1_id = H5Aget_space(attr1_id)) < 0)
            goto error;
        if((space2_id = H5Aget_space(attr2_id)) < 0)
            goto error;

        /* get dimensions  */
        if((rank1 = H5Sget_simple_extent_dims(space1_id, dims1, NULL)) < 0)
            goto error;
        if((rank2 = H5Sget_simple_extent_dims(space2_id, dims2, NULL)) < 0)
            goto error;


       /*----------------------------------------------------------------------
        * check for comparable TYPE and SPACE
        *----------------------------------------------------------------------
        */

        /* pass dims1 and dims2 for maxdims as well since attribute's maxdims
         * are always same */
        if( diff_can_type(ftype1_id, ftype2_id, rank1, rank2, dims1, dims2,
                          dims1, dims2, name1, name2, options, 0) != 1 )
        {
            if(H5Tclose(ftype1_id) < 0)
                goto error;
            if(H5Tclose(ftype2_id) < 0)
                goto error;
            if(H5Sclose(space1_id) < 0)
                goto error;
            if(H5Sclose(space2_id) < 0)
                goto error;
            if(H5Aclose(attr1_id) < 0)
                goto error;
            if(H5Aclose(attr2_id) < 0)
                goto error;
            if(H5Tclose(mtype1_id) < 0)
                goto error;
            if(H5Tclose(mtype2_id) < 0)
                goto error;

            continue;
        }

       /*-----------------------------------------------------------------
        * "upgrade" the smaller memory size
        *------------------------------------------------------------------
        */
        if (FAIL == match_up_memsize (ftype1_id, ftype2_id,
                                      &mtype1_id, &mtype2_id, 
                                      &msize1, &msize2))
            goto error;

        /*---------------------------------------------------------------------
        * read
        *----------------------------------------------------------------------
        */
        nelmts1 = 1;
        for(j = 0; j < rank1; j++)
            nelmts1 *= dims1[j];

        buf1 = (void *)HDmalloc((size_t)(nelmts1 * msize1));
        buf2 = (void *)HDmalloc((size_t)(nelmts1 * msize2));
        if(buf1 == NULL || buf2 == NULL){
            parallel_print( "cannot read into memory\n" );
            goto error;
        }
        if(H5Aread(attr1_id,mtype1_id,buf1) < 0){
	    parallel_print("Failed reading attribute1 %s/%s\n", path1, name1);
	    goto error;
	}else
	    buf1hasdata = 1;
        if(H5Aread(attr2_id,mtype2_id,buf2) < 0){
	    parallel_print("Failed reading attribute2 %s/%s\n", path2, name2);
	    goto error;
	}else
	    buf2hasdata = 1;

        /* format output string */
        HDsnprintf(np1, sizeof(np1), "%s of <%s>", name1, path1);
        HDsnprintf(np2, sizeof(np1), "%s of <%s>", name2, path2);

        /*---------------------------------------------------------------------
        * array compare
        *----------------------------------------------------------------------
        */

        /* always print name */
        /* verbose (-v) and report (-r) mode */
        if(options->m_verbose || options->m_report) {
            do_print_attrname("attribute", np1, np2);

            nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1,
                options, np1, np2, mtype1_id, attr1_id, attr2_id);
            print_found(nfound);
        }
        /* quiet mode (-q), just count differences */
        else if(options->m_quiet) {
            nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1,
                options, np1, np2, mtype1_id, attr1_id, attr2_id);
        }
        /* the rest (-c, none, ...) */
        else {
            nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1,
                options, np1, np2, mtype1_id, attr1_id, attr2_id);

                /* print info if compatible and difference found */
                if(nfound) {
                    do_print_attrname("attribute", np1, np2);
                    print_found(nfound);
                } /* end if */
        } /* end else */


       /*----------------------------------------------------------------------
        * close
        *----------------------------------------------------------------------
        */

        /* Free buf1 and buf2, check both VLEN-data VLEN-string to reclaim any 
         * VLEN memory first */
        if(TRUE == h5tools_detect_vlen(mtype1_id))
            H5Dvlen_reclaim(mtype1_id, space1_id, H5P_DEFAULT, buf1);
        HDfree(buf1);
        buf1 = NULL;

        if(TRUE == h5tools_detect_vlen(mtype2_id))
            H5Dvlen_reclaim(mtype2_id, space2_id, H5P_DEFAULT, buf2);
        HDfree(buf2);
        buf2 = NULL;

        if(H5Tclose(ftype1_id) < 0)
            goto error;
        if(H5Tclose(ftype2_id) < 0)
            goto error;
        if(H5Sclose(space1_id) < 0)
            goto error;
        if(H5Sclose(space2_id) < 0)
            goto error;
        if(H5Aclose(attr1_id) < 0)
            goto error;
        if(H5Aclose(attr2_id) < 0)
            goto error;
        if(H5Tclose(mtype1_id) < 0)
            goto error;
        if(H5Tclose(mtype2_id) < 0)
            goto error;

        nfound_total += nfound;
        }
    } /* u */

    table_attrs_free(match_list_attrs);

    return nfound_total;

error:
    H5E_BEGIN_TRY {
        if(buf1) {
            if(buf1hasdata && TRUE == h5tools_detect_vlen(mtype1_id))
                H5Dvlen_reclaim(mtype1_id, space1_id, H5P_DEFAULT, buf1);
            HDfree(buf1);
        } /* end if */
        if(buf2) {
            if(buf2hasdata && TRUE == h5tools_detect_vlen(mtype2_id))
                H5Dvlen_reclaim(mtype2_id, space2_id, H5P_DEFAULT, buf2);
            HDfree(buf2);
        } /* end if */

        table_attrs_free(match_list_attrs);

        H5Tclose(ftype1_id);
        H5Tclose(ftype2_id);
        H5Tclose(mtype1_id);
        H5Tclose(mtype2_id);
        H5Sclose(space1_id);
        H5Sclose(space2_id);
        H5Aclose(attr1_id);
        H5Aclose(attr2_id);
    } H5E_END_TRY;

    options->err_stat = 1;
    return nfound_total;
}
Example #17
0
int hdf5read(char *name, struct descriptor_xd *xd)
{
  hid_t obj,type;
  H5G_stat_t statbuf;
  int item_type;
  int idx = 0;
  int status = FindItem(name, &obj, &item_type);
  if (status & 1)
  {
    if (item_type == H5G_DATASET) {
      int size;
      char dtype;
      int htype = 42;
      int is_signed;
      hsize_t ds_dims[64];
      hid_t space = H5Dget_space(obj);
      int n_ds_dims = H5Sget_simple_extent_dims(space,ds_dims,0);
      size_t precision;
      H5Sclose(space);
      type = H5Dget_type(obj);
      switch (H5Tget_class(type))
	{
	case H5T_COMPOUND:
	  {
            printf("Compound data is not supported, skipping\n");
            break;
          }
	case H5T_INTEGER:
	  precision = H5Tget_precision(type);
	  is_signed = (H5Tget_sign(type) != H5T_SGN_NONE);
          size = precision/8;
	  switch (precision)
            {
	    case 8:
              dtype = is_signed ? DTYPE_B : DTYPE_BU;
              htype = is_signed ? H5T_NATIVE_CHAR : H5T_NATIVE_UCHAR;
	      break;
	    case 16: 
              dtype = is_signed ? DTYPE_W : DTYPE_WU;
              htype = is_signed ? H5T_NATIVE_SHORT : H5T_NATIVE_USHORT;
	      break;
	    case 32: 
              dtype = is_signed ? DTYPE_L : DTYPE_LU;
              htype = is_signed ? H5T_NATIVE_INT : H5T_NATIVE_UINT;
	      break;
	    case 64: 
              dtype = is_signed ? DTYPE_Q : DTYPE_QU;
              htype = is_signed ? H5T_NATIVE_LLONG : H5T_NATIVE_ULLONG;
	      break;
	    default: 
	      dtype = 0;
	      break;
            }
	  PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,0,xd);
	  break;
	case H5T_FLOAT:
	  precision = H5Tget_precision(type);
          size = precision/8;
	  switch (precision)
	    {
	    case 32: 
              dtype = DTYPE_NATIVE_FLOAT;
              htype = H5T_NATIVE_FLOAT;
	      break;
	    case 64: 
              dtype = DTYPE_NATIVE_DOUBLE;
              htype = H5T_NATIVE_DOUBLE;
              break;
	    default:
	      dtype = 0;
	      break;
            }
	  PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,0,xd);
	  break;
	case H5T_TIME:
	  printf("dataset is time ---- UNSUPPORTED\n"); break;
	case H5T_STRING:
	  {
	    int slen = H5Tget_size(type);
	    hid_t st_id;
	    if (slen < 0) {
	      printf("Badly formed string attribute\n");
	      return;
	    }
#if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1
	    if(H5Tis_variable_str(type)) {                    
	      st_id = H5Tcopy (H5T_C_S1);
	      H5Tset_size(st_id, H5T_VARIABLE);
	    } else {
#endif
	      st_id = H5Tcopy (type);
	      H5Tset_cset(st_id, H5T_CSET_ASCII);
#if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1
	    } 
#endif	  
            if (H5Tget_size(st_id) > slen) {
	      slen = H5Tget_size(st_id);
	    }
	      H5Tset_size (st_id, slen);
	      PutData(obj, DTYPE_T, st_id, slen, n_ds_dims, ds_dims, 0, xd); 
	  }		
/*        printf("dataset is string\n"); */
/* 	  dtype = DTYPE_T; */
/* 	  htype = H5T_STRING; */
/* 	  PutData(obj, dtype, htype, 0, 0, 0, 1, xd); */
	  break;
	case H5T_BITFIELD:
	  printf("dataset is bitfield ---- UNSUPPORTED\n"); break;
	case H5T_OPAQUE:
	  printf("dataset is opaque ---- UNSUPPORTED\n"); break;
        case H5T_ARRAY:
	  printf("dataset is array ---- UNSUPPORTED\n"); break;
        case H5T_VLEN:
	  printf("dataset is vlen ---- UNSUPPORTED\n"); break;
        }
      H5Tclose(type);
    }
    else {
      int size;
      char dtype;
      int htype = 42;
      int is_signed;
      hsize_t ds_dims[64];
      hid_t space = H5Aget_space(obj);
      int n_ds_dims = H5Sget_simple_extent_dims(space,ds_dims,0);
      size_t precision;
      H5Sclose(space);
      type = H5Aget_type(obj);
      switch (H5Tget_class(type))
	{
	case H5T_COMPOUND:
	{
	  printf("Compound data is not supported, skipping\n");
	  break;
	}
	case H5T_INTEGER:
	  precision = H5Tget_precision(type);
	  is_signed = (H5Tget_sign(type) != H5T_SGN_NONE);
	  size = precision/8;
	  switch (precision)
	    {
	    case 8:
	      dtype = is_signed ? DTYPE_B : DTYPE_BU;
	      htype = is_signed ? H5T_NATIVE_CHAR : H5T_NATIVE_UCHAR;
	      break;
	    case 16: 
	      dtype = is_signed ? DTYPE_W : DTYPE_WU;
	      htype = is_signed ? H5T_NATIVE_SHORT : H5T_NATIVE_USHORT;
	      break;
	    case 32: 
	      dtype = is_signed ? DTYPE_L : DTYPE_LU;
	      htype = is_signed ? H5T_NATIVE_INT : H5T_NATIVE_UINT;
	      break;
	    case 64: 
	      dtype = is_signed ? DTYPE_Q : DTYPE_QU;
	      htype = is_signed ? H5T_NATIVE_LLONG : H5T_NATIVE_ULLONG;
	      break;
	    default: 
	      dtype = 0;
	      break;
	    }
	  PutData(obj, dtype, htype, size, n_ds_dims, ds_dims, 1, xd);
	  break;
	case H5T_FLOAT:
	  precision = H5Tget_precision(type);
	  size = precision/8;
	  switch (precision)
	    {
	    case 32: 
	      dtype = DTYPE_NATIVE_FLOAT;
	      htype = H5T_NATIVE_FLOAT;
	      break;
	    case 64: 
	      dtype = DTYPE_NATIVE_DOUBLE;
	      htype = H5T_NATIVE_DOUBLE;
	      break;
	    default:
	      dtype = 0;
	      break;
	    }
	  PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,1, xd);
	  break;
	case H5T_TIME:
	  printf("dataset is time ---- UNSUPPORTED\n"); break;
	case H5T_STRING:
	  {
	    int slen = H5Tget_size(type);
	    hid_t st_id;
	    if (slen < 0) {
	      printf("Badly formed string attribute\n");
	      return;
	    }
#if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1
	    if(H5Tis_variable_str(type)) {                    
	      st_id = H5Tcopy (H5T_C_S1);
	      H5Tset_size(st_id, H5T_VARIABLE);
	    } else {
#endif
	      st_id = H5Tcopy (type);
	      H5Tset_cset(st_id, H5T_CSET_ASCII);
#if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1
	    } 
#endif	  
            if (H5Tget_size(st_id) > slen) {
	      slen = H5Tget_size(st_id);
	    }
	      H5Tset_size (st_id, slen);
	      PutData(obj, DTYPE_T, st_id, slen, n_ds_dims, ds_dims, 1, xd); 
	  }		
/* 	  dtype = DTYPE_T; */
/* 	  htype = H5T_STRING; */
/* 	  PutData(obj, dtype, htype, 0, 0, 0, 1, xd); */
	  break;
	case H5T_BITFIELD:
	  printf("dataset is bitfield ---- UNSUPPORTED\n"); break;
	case H5T_OPAQUE:
	  printf("dataset is opaque ---- UNSUPPORTED\n"); break;
	case H5T_ARRAY:
	  printf("dataset is array ---- UNSUPPORTED\n"); break;
	case H5T_VLEN:
	  printf("dataset is vlen ---- UNSUPPORTED\n"); break;
	}
      H5Tclose(type);
    }
  }
  return status;
}
Example #18
0
/*-------------------------------------------------------------------------
* subroutine for test_text_dtype(): test_strings().
*-------------------------------------------------------------------------
*/
static int test_strings(void)
{
    hid_t   dtype;
    size_t  str_size;
    H5T_str_t   str_pad;
    H5T_cset_t  str_cset;
    H5T_class_t type_class;
    char*   dt_str;
    size_t  str_len;

    TESTING3("        text for string types");

    if((dtype = H5LTtext_to_dtype("H5T_STRING { STRSIZE 13; STRPAD H5T_STR_NULLTERM; CSET H5T_CSET_ASCII; CTYPE H5T_C_S1; }", H5LT_DDL))<0)
        goto out;

    if((type_class = H5Tget_class(dtype))<0)
        goto out;
    if(type_class != H5T_STRING)
        goto out;

    str_size = H5Tget_size(dtype);
    if(str_size != 13)
        goto out;

    str_pad = H5Tget_strpad(dtype);
    if(str_pad != H5T_STR_NULLTERM)
        goto out;

    str_cset = H5Tget_cset(dtype);
    if(str_cset != H5T_CSET_ASCII)
        goto out;

    if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0)
        goto out;
    dt_str = (char*)calloc(str_len, sizeof(char));
    if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0)
        goto out;
    if(strcmp(dt_str, "H5T_STRING {\n      STRSIZE 13;\n      STRPAD H5T_STR_NULLTERM;\n      CSET H5T_CSET_ASCII;\n      CTYPE H5T_C_S1;\n   }")) {
        printf("dt=\n%s\n", dt_str);
        goto out;
    }
    free(dt_str);

    if(H5Tclose(dtype)<0)
        goto out;

    if((dtype = H5LTtext_to_dtype("H5T_STRING { STRSIZE H5T_VARIABLE; STRPAD H5T_STR_NULLPAD; CSET H5T_CSET_ASCII; CTYPE H5T_C_S1; }", H5LT_DDL))<0)
        goto out;

    if(!H5Tis_variable_str(dtype))
        goto out;

    str_pad = H5Tget_strpad(dtype);
    if(str_pad != H5T_STR_NULLPAD)
        goto out;

    str_cset = H5Tget_cset(dtype);
    if(str_cset != H5T_CSET_ASCII)
        goto out;

    if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0)
        goto out;
    dt_str = (char*)calloc(str_len, sizeof(char));
    if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0)
        goto out;
    if(strcmp(dt_str, "H5T_STRING {\n      STRSIZE H5T_VARIABLE;\n      STRPAD H5T_STR_NULLPAD;\n      CSET H5T_CSET_ASCII;\n      CTYPE H5T_C_S1;\n   }")) {
        printf("dt=\n%s\n", dt_str);
        goto out;
    }
    free(dt_str);

    if(H5Tclose(dtype)<0)
        goto out;

    PASSED();
    return 0;

out:
    H5_FAILED();
    return -1;
}