Beispiel #1
0
        public static string GetAttributeValue(H5ObjectWithAttributes objectWithAttributes, string name)
        {
            if (objectWithAttributes is null)
            {
                throw new ArgumentNullException(nameof(objectWithAttributes));
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            H5AttributeId h5AttributeId = H5A.open(objectWithAttributes, name);
            H5DataTypeId  h5DataTypeId  = H5A.getType(h5AttributeId);

            if (H5T.isVariableString(h5DataTypeId))
            {
                VariableLengthString[] variableLengthStrings = new VariableLengthString[1];
                H5A.read(h5AttributeId, h5DataTypeId, new H5Array <VariableLengthString> (variableLengthStrings));
                H5T.close(h5DataTypeId);
                H5A.close(h5AttributeId);
                return(variableLengthStrings[0].ToString());
            }
            byte[] bytes = new byte[H5T.getSize(h5DataTypeId)];
            H5A.read(h5AttributeId, h5DataTypeId, new H5Array <byte> (bytes));
            H5T.close(h5DataTypeId);
            H5A.close(h5AttributeId);
            return(Encoding.ASCII.GetString(bytes));
        }
Beispiel #2
0
        public static string AttributeAsString(H5AttributeId _attributeId)
        {
            H5DataTypeId dataTypeId       = H5A.getType(_attributeId);
            bool         isVariableLength = H5T.isVariableString(dataTypeId);

            if (isVariableLength)
            {
                // Variable length string attribute
                // NOTE: This section only works if the array length is 1
                VariableLengthString[] value = new VariableLengthString[1];
                H5A.read <VariableLengthString>(_attributeId, dataTypeId,
                                                new H5Array <VariableLengthString>(value));

                return(value[0].ToString());
            }
            else
            {
                // Make length smaller so null termination character is not read
                int length = (int)H5T.getSize(dataTypeId) - 1;

                // Fixed length string attribute
                byte[] valueBytes = new byte[length];

                H5A.read <byte>(_attributeId, dataTypeId, new H5Array <byte>(valueBytes));
                string value = System.Text.ASCIIEncoding.ASCII.GetString(valueBytes);
                return(value);
            }
        }