Пример #1
0
        public void H5TcreateTest1()
        {
            hid_t dtype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);

            Assert.IsTrue(dtype >= 0);
            Assert.IsTrue(H5T.is_variable_str(dtype) > 0);
            Assert.IsTrue(H5T.close(dtype) >= 0);
        }
Пример #2
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);
            }
        }
Пример #3
0
        public static string ReadUnicodeString(hid_t groupId, string name)
        {
            var datasetId = H5D.open(groupId, name);
            var typeId    = H5D.get_type(datasetId);

            if (H5T.is_variable_str(typeId) > 0)
            {
                var   spaceId = H5D.get_space(datasetId);
                hid_t count   = H5S.get_simple_extent_npoints(spaceId);

                IntPtr[] rdata = new IntPtr[count];

                GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);
                H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL,
                         H5P.DEFAULT, hnd.AddrOfPinnedObject());

                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 stringPart = Encoding.UTF8.GetString(buffer);

                    attrStrings.Add(stringPart);

                    H5.free_memory(rdata[i]);
                }

                hnd.Free();
                H5S.close(spaceId);
                H5D.close(datasetId);

                return(attrStrings[0]);
            }

            // Must be a non-variable length string.
            int    size = H5T.get_size(typeId).ToInt32();
            IntPtr iPtr = Marshal.AllocHGlobal(size);

            int result = H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL,
                                  H5P.DEFAULT, iPtr);

            if (result < 0)
            {
                throw new IOException("Failed to read dataset");
            }

            var strDest = new byte[size];

            Marshal.Copy(iPtr, strDest, 0, size);
            Marshal.FreeHGlobal(iPtr);

            H5D.close(datasetId);

            return(Encoding.UTF8.GetString(strDest).TrimEnd((Char)0));
        }
        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'));
        }
        public static bool WriteFixedStringAttribute(hid_t attribute, string value, bool utf8)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var space = H5A.get_space(attribute);

            if (space < 0)
            {
                throw new Exception("Failed to get data space");
            }

            if (H5S.get_simple_extent_type(space) != H5S.class_t.SCALAR)
            {
                H5S.close(space);
                throw new Exception("Not a scalar data space");
            }

            H5S.close(space);

            var type = H5A.get_type(attribute);

            if (type < 0)
            {
                throw new Exception("Failed to get type");
            }

            var typeClass = H5T.get_class(type);

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

            var isVariableString = H5T.is_variable_str(type);

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

            if (isVariableString > 0)
            {
                throw new Exception("Not a fixed string attribute");
            }

            // TODO: Do we really need to create a new memory type here?
            var bytes = value.ToBytes(utf8);

            var memType = CreateFixedStringType(bytes, utf8);

            // TODO: type or memType here?
            H5A.write(attribute, memType, new PinnedObject(bytes));

            H5T.close(memType);

            return(true);
        }
Пример #6
0
        public void H5TinsertTest1()
        {
            // a fixed-length string type
            hid_t fls = H5T.create(H5T.class_t.STRING, new IntPtr(16));

            Assert.IsTrue(fls >= 0);
            Assert.IsTrue(H5T.is_variable_str(fls) == 0);

            // a variable-length string type
            hid_t vls = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);

            Assert.IsTrue(vls >= 0);
            Assert.IsTrue(H5T.is_variable_str(vls) > 0);

            // a key-value compound
            IntPtr size = new IntPtr(16 + IntPtr.Size);
            hid_t  kvt  = H5T.create(H5T.class_t.COMPOUND, size);

            Assert.IsTrue(H5T.insert(kvt, "key", IntPtr.Zero, fls) >= 0);
            Assert.IsTrue(H5T.insert(kvt, "value", new IntPtr(16), vls) >= 0);
            Assert.IsTrue(H5T.close(vls) >= 0);
            Assert.IsTrue(H5T.close(fls) >= 0);

            // create a key-value dataset (3 elements)

            hid_t fsp = H5S.create_simple(1, new hsize_t[] { 3 }, null);

            Assert.IsTrue(fsp >= 0);

            hid_t dset = H5D.create(m_v2_class_file, "KeyVal", kvt, fsp);

            Assert.IsTrue(dset >= 0);
            Assert.IsTrue(H5S.close(fsp) >= 0);

            // write a 3 elements

            string[] keys = new string[] {
                "Key0123456789ABC", "Key0123456789DEF", "Key0123456789GHI"
            };

            IntPtr[] values = new IntPtr[3];
            values[0] = Marshal.StringToHGlobalAnsi("I am a managed String!");
            values[1] = Marshal.StringToHGlobalAnsi("I am also a managed String!");
            values[2] = Marshal.StringToHGlobalAnsi("I am another managed String!");

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            for (int i = 0; i < 3; ++i)
            {
                writer.Write(Encoding.ASCII.GetBytes(keys[i]));
                if (IntPtr.Size == 8)
                {
                    writer.Write(values[i].ToInt64());
                }
                else
                {
                    writer.Write(values[i].ToInt32());
                }
            }

            byte[]   wdata = ms.ToArray();
            GCHandle hnd   = GCHandle.Alloc(wdata, GCHandleType.Pinned);

            Assert.IsTrue(H5D.write(dset, kvt, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                    hnd.AddrOfPinnedObject()) >= 0);

            hnd.Free();

            // now read it back

            byte[] rdata = new byte[3 * size.ToInt32()];
            hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);

            Assert.IsTrue(H5D.read(dset, kvt, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                   hnd.AddrOfPinnedObject()) >= 0);

            hnd.Free();

            // check it out

            MemoryStream ms1    = new MemoryStream(rdata);
            BinaryReader reader = new BinaryReader(ms1);

            for (int i = 0; i < 3; ++i)
            {
                string k = Encoding.ASCII.GetString(reader.ReadBytes(16));
                Assert.IsTrue(k == keys[i]);
                IntPtr ptr = IntPtr.Zero;
                if (IntPtr.Size == 8)
                {
                    ptr = new IntPtr(reader.ReadInt64());
                }
                else
                {
                    ptr = new IntPtr(reader.ReadInt32());
                }
                string v = Marshal.PtrToStringAnsi(ptr);
                Assert.IsTrue(v == Marshal.PtrToStringAnsi(values[i]));
                Marshal.FreeHGlobal(ptr);
                Marshal.FreeHGlobal(values[i]);
            }

            Assert.IsTrue(H5D.close(dset) >= 0);
            Assert.IsTrue(H5T.close(kvt) >= 0);
        }