Salome HOME
14b526e79b4d8c1e2a9b6bc8e01d5f7a44a6509a
[modules/kernel.git] / src / HDFPersist / HDFdataset.cc
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME HDFPersist : implementation of HDF persitent ( save/ restore )
24 //  File   : HDFdataset.cc
25 //  Module : SALOME
26 //
27 #include "hdfi.h"
28 #include <string.h>
29 #include "HDFdataset.hxx"
30 #include "HDFcontainerObject.hxx"
31 #include "HDFexception.hxx"
32
33 #include <iostream>
34
35 herr_t dataset_attr(hid_t /*loc_id*/, const char *attr_name, void *operator_data)
36 {
37   *(char**)operator_data = new char[strlen(attr_name)+1];
38   strcpy(*(char**)operator_data, attr_name);
39   return 1;
40 }
41
42 HDFdataset::HDFdataset(const char *name, HDFcontainerObject *father,hdf_type type, 
43                        hdf_size dim[], int dimsize, hdf_byte_order order)
44   : HDFinternalObject(name)
45 {
46   int i;
47
48   _father = father;
49   _fid = _father->GetId();
50   _father->AddSon(this);
51   _type = type;
52   _ndim = dimsize;
53   _dim = new hdf_size[dimsize];
54   _byte_order = order;
55   _size = 1;
56   _attribute = NULL;
57   for (i=0;i<dimsize;i++)
58     {
59       _dim[i] = dim[i];
60       _size = _size * _dim[i];
61     }
62   _arrayId = -1;
63 }
64
65
66 HDFdataset::HDFdataset(const char *name,HDFcontainerObject *father)
67   : HDFinternalObject(name)
68 {
69   _father = father;
70   _fid = _father->GetId();
71   _father->AddSon(this);
72   _type = HDF_NONE;
73   _ndim = -1;
74   _dim = 0;
75   _byte_order = H5T_ORDER_ERROR;
76   _size = -1;
77   _attribute = NULL;
78   _arrayId = -1;
79 }
80
81 HDFdataset::~HDFdataset()
82 {
83   delete [] _dim;
84 }
85
86 void HDFdataset::CreateOnDisk()
87 {
88   if ((_id = HDFdatasetCreate(_fid,_name,_type,_dim,_ndim,_byte_order,_arrayId)) < 0)
89     throw HDFexception("Can't create dataset");
90 }
91
92 void HDFdataset::OpenOnDisk()
93 {
94   if ((_id = HDFdatasetOpen(_fid,_name)) < 0)
95     throw HDFexception("Can't open dataset");
96 }
97
98 void HDFdataset::CloseOnDisk()
99 {
100   hdf_err ret;
101
102   if ((ret = HDFdatasetClose(_id)) < 0)
103     throw HDFexception("Can't close dataset");
104   _id = -1;
105 }
106
107 void HDFdataset::WriteOnDisk(void *values)
108 {
109   hdf_err ret;
110
111   if ((ret = HDFdatasetWrite(_id,values)) < 0)
112     throw HDFexception("Can't write dataset");
113  
114 }
115
116 void HDFdataset::ReadFromDisk(void *values)
117 {
118   hdf_err ret;
119
120   if ((ret = HDFdatasetRead(_id,values)) < 0)
121       throw HDFexception("Can't read dataset");
122 }
123
124 HDFcontainerObject *HDFdataset::GetFather()
125 {
126   return _father;
127 }
128
129 hdf_type HDFdataset::GetType()
130 {
131   if (_type == HDF_NONE)
132     if ((_type = HDFdatasetGetType(_id)) == HDF_NONE)
133       throw HDFexception("Can't determine the type of data in the dataset");
134   
135   return _type;
136 }
137
138 int HDFdataset::nDim()
139 {
140   if (_ndim == -1)
141     if ((_ndim = HDFdatasetGetnDim(_id)) < 0)
142       throw HDFexception("Can't determine the dataset dimensions number");
143
144   return _ndim;
145 }
146
147 void HDFdataset::GetDim(hdf_size dim[])
148 {
149   int i;
150   int ndim;
151   hdf_err ret;
152
153   if (_dim == 0)
154     {
155       if (_ndim == -1)
156         ndim = HDFdatasetGetnDim(_id);
157       else
158         ndim = _ndim;
159       _dim = new hdf_size[ndim];
160       if ((ret = HDFdatasetGetDim(_id,_dim)) < 0)
161         throw HDFexception("Can't determine the size dimensions of the dataset ");
162     }
163
164   for (i=0;i<_ndim;i++)
165     dim[i] = _dim[i];
166 }
167
168 hdf_size HDFdataset::GetSize()
169 {
170   int size_type;
171
172   if ((long)_size == -1)
173     {
174       if ((long)(_size = HDFdatasetGetSize(_id)) < 0)
175         throw HDFexception("Can't determine the size of the dataset");
176       
177       if (_type == HDF_NONE)
178         if ((_type = HDFdatasetGetType(_id)) == HDF_NONE)
179           throw HDFexception("Can't determine the size of the dataset");
180       
181       switch (_type)
182         {
183         case HDF_INT32 : 
184           size_type = 4;
185           break;
186           
187         case HDF_INT64 :
188         case HDF_FLOAT64 :
189           size_type = 8;
190           break;
191           
192         default :
193           size_type = 1;
194         }
195       _size = _size / size_type;
196     }
197
198   return _size;
199 }
200
201 hdf_byte_order HDFdataset::GetOrder()
202 {
203   if (_byte_order < 0 )
204     if ((_byte_order = HDFdatasetGetOrder( _id )) < 0)
205       throw HDFexception("Can't determine the byte order of the dataset");
206   return _byte_order;
207 }
208
209 hdf_object_type HDFdataset::GetObjectType()
210 {
211   return HDF_DATASET;
212 }
213
214
215 int HDFdataset::nAttributes()
216 {
217   int nbAttrs = H5Aget_num_attrs(_id);
218   if(nbAttrs <= 0) nbAttrs = 0;
219   return nbAttrs; 
220 }
221
222
223 char* HDFdataset::GetAttributeName(unsigned idx)
224 {
225   int nbAttrs = nAttributes();
226   if(nbAttrs == 0) return NULL;
227   H5Aiterate(_id, &idx, dataset_attr, &_attribute);
228   return _attribute;
229 }
230
231 void HDFdataset::SetArrayId(hdf_idt arrayId) {
232   _arrayId = arrayId;
233 }