public static (int success, long CreatedgroupId) WriteStrings(long groupId, string name, IEnumerable <string> strs) { // create UTF-8 encoded test datasets long datatype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); H5T.set_cset(datatype, Hdf5Utils.GetCharacterSet(Settings.CharacterSetType)); H5T.set_strpad(datatype, Hdf5Utils.GetCharacterPadding(Settings.CharacterPaddingType)); int strSz = strs.Count(); long spaceId = H5S.create_simple(1, new[] { (ulong)strSz }, null); string normalizedName = Hdf5Utils.NormalizedName(name); var datasetId = Hdf5Utils.GetDatasetId(groupId, normalizedName, datatype, spaceId); if (datasetId == -1L) { return(-1, -1L); } GCHandle[] hnds = new GCHandle[strSz]; IntPtr[] wdata = new IntPtr[strSz]; int cntr = 0; foreach (string str in strs) { hnds[cntr] = GCHandle.Alloc( Hdf5Utils.StringToByte(str), GCHandleType.Pinned); wdata[cntr] = hnds[cntr].AddrOfPinnedObject(); cntr++; } var hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned); var result = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); for (int i = 0; i < strSz; ++i) { hnds[i].Free(); } H5D.close(datasetId); H5S.close(spaceId); H5T.close(datatype); return(result, datasetId); }
public static int WriteUnicodeString(long groupId, string name, string str, H5T.str_t strPad = H5T.str_t.SPACEPAD) { byte[] wdata = Hdf5Utils.StringToByte(str); long spaceId = H5S.create(H5S.class_t.SCALAR); long dtype = H5T.create(H5T.class_t.STRING, new IntPtr(wdata.Length)); H5T.set_cset(dtype, Hdf5Utils.GetCharacterSet(Settings.CharacterSetType)); H5T.set_strpad(dtype, strPad); long datasetId = H5D.create(groupId, Hdf5Utils.NormalizedName(name), dtype, spaceId); GCHandle hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned); int result = H5D.write(datasetId, dtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); H5T.close(dtype); H5D.close(datasetId); H5S.close(spaceId); return(result); }