Beispiel #1
0
        public static bool WriteStringAttribute(hid_t hid, string key, string value, bool utf8 = true, bool variable = true)
        {
            if (H5A.exists(hid, key) == 0)
            {
                // Attribute doesn't exist.
                return(variable ? CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8)
                    : CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8));
            }
            else
            {
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    return(false);
                }

                var type = H5A.get_type(attribute);
                if (type < 0)
                {
                    H5A.close(attribute);
                    return(false);
                }

                var typeClass = H5T.get_class(type);
                if (typeClass == H5T.class_t.NO_CLASS)
                {
                    H5T.close(type);
                    H5T.close(attribute);
                    return(false);
                }

                if (typeClass != H5T.class_t.STRING)
                {
                    H5T.close(type);
                    H5T.close(attribute);
                    throw new ArgumentException("H5T.get_class(type) != H5T.class_t.STRING");
                }

                utf8 = H5T.get_cset(type) == H5T.cset_t.UTF8;

                bool ok;
                if (H5T.is_variable_str(type) > 0)
                {
                    ok = CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8);
                }
                else
                {
                    ok = CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8);
                }

                H5T.close(type);
                H5T.close(attribute);

                return(ok);
            }
        }
        public static string ReadStringAttribute(hid_t hid, string key)
        {
            var attribute = H5A.open(hid, key);

            if (attribute < 0)
            {
                throw new ArgumentException(string.Format("Attribute {0} not found.", key));
            }

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                H5A.close(attribute);
                throw new Exception("H5A.get_type failed.");
            }

            var typeClass = H5T.get_class(type);

            if (typeClass != H5T.class_t.STRING)
            {
                H5T.close(type);
                throw new Exception("Not a string attribute");
            }

            var utf8  = H5T.get_cset(type) == H5T.cset_t.UTF8;
            var ascii = H5T.get_cset(type) == H5T.cset_t.ASCII;

            if (!utf8 && !ascii)
            {
                H5T.close(type);
                H5A.close(attribute);
                throw new Exception("Neither ASCII nor UTF8.");
            }

            var isVariableString = H5T.is_variable_str(type);

            if (isVariableString < 0)
            {
                H5T.close(type);
                H5A.close(attribute);
                throw new Exception("H5T.is_variable_str failed");
            }

            if (isVariableString > 0)
            {
                var space = H5A.get_space(attribute);
                if (space < 0)
                {
                    H5T.close(type);
                    H5A.close(attribute);
                    throw new Exception("H5A.get_space failed.");
                }

                hid_t count = H5S.get_simple_extent_npoints(space);
                var   rdata = new IntPtr[count];

                H5A.read(attribute, type, new PinnedObject(rdata));

                var attrStrings = new List <string>();
                for (int i = 0; i < rdata.Length; ++i)
                {
                    int attrLength = 0;
                    while (Marshal.ReadByte(rdata[i], attrLength) != 0)
                    {
                        ++attrLength;
                    }

                    byte[] buffer = new byte[attrLength];
                    Marshal.Copy(rdata[i], buffer, 0, buffer.Length);

                    string part = utf8 ? Encoding.UTF8.GetString(buffer)
                        : Encoding.ASCII.GetString(buffer);

                    attrStrings.Add(part);

                    H5.free_memory(rdata[i]);
                }

                H5S.close(space);
                H5T.close(type);
                H5A.close(attribute);

                return(attrStrings[0]);
            }

            // Must be a non-variable length string.
            var size            = H5T.get_size(type).ToInt32();
            var unmanagedBuffer = new UnmanagedBuffer(size);

            int result = H5A.read(attribute, type, unmanagedBuffer);

            if (result < 0)
            {
                H5T.close(type);
                H5D.close(attribute);
                throw new IOException("Failed to read attribute.");
            }

            var bytes = new byte[size];

            unmanagedBuffer.CopyTo(bytes, 0, bytes.Length);

            H5T.close(type);
            H5A.close(attribute);

            var value = utf8 ? Encoding.UTF8.GetString(bytes)
                : Encoding.ASCII.GetString(bytes);

            return(value.TrimEnd('\0'));
        }