Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/kernel.git] / src / HDFPersist / HDFdataset.cc
1 //  SALOME HDFPersist : implementation of HDF persitent ( save/ restore )
2 //
3 //  Copyright (C) 2003  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. 
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 //
24 //  File   : HDFdataset.cc
25 //  Module : SALOME
26
27 extern "C"
28 {
29 #include "hdfi.h"
30 #include <string.h>
31 }
32 #include "HDFdataset.hxx"
33 #include "HDFcontainerObject.hxx"
34 #include "HDFexception.hxx"
35
36 #include <iostream>
37 using namespace std;
38
39 herr_t dataset_attr(hid_t loc_id, const char *attr_name, void *operator_data)
40 {
41   *(char**)operator_data = new char[strlen(attr_name)+1];
42   strcpy(*(char**)operator_data, attr_name);
43   return 1;
44 }
45
46 HDFdataset::HDFdataset(char *name, HDFcontainerObject *father,hdf_type type, 
47                        hdf_size dim[], int dimsize, hdf_byte_order order)
48   : HDFinternalObject(name)
49 {
50   int i;
51
52   _father = father;
53   _fid = _father->GetId();
54   _father->AddSon(this);
55   _type = type;
56   _ndim = dimsize;
57   _dim = new hdf_size[dimsize];
58   _byte_order = order;
59   _size = 1;
60   _attribute = NULL;
61   for (i=0;i<dimsize;i++)
62     {
63       _dim[i] = dim[i];
64       _size = _size * _dim[i];
65     }
66 }
67
68
69 HDFdataset::HDFdataset(char *name,HDFcontainerObject *father)
70   : HDFinternalObject(name)
71 {
72   _father = father;
73   _fid = _father->GetId();
74   _father->AddSon(this);
75   _type = HDF_NONE;
76   _ndim = -1;
77   _dim = 0;
78   _byte_order = H5T_ORDER_ERROR;
79   _size = -1;
80   _attribute = NULL;
81 }
82
83 HDFdataset::~HDFdataset()
84 {
85   delete [] _dim;
86 }
87
88 void HDFdataset::CreateOnDisk()
89 {
90   if ((_id = HDFdatasetCreate(_fid,_name,_type,_dim,_ndim,_byte_order)) < 0)
91     throw HDFexception("Can't create dataset");
92 }
93
94 void HDFdataset::OpenOnDisk()
95 {
96   if ((_id = HDFdatasetOpen(_fid,_name)) < 0)
97     throw HDFexception("Can't open dataset");
98 }
99
100 void HDFdataset::CloseOnDisk()
101 {
102   hdf_err ret;
103
104   if ((ret = HDFdatasetClose(_id)) < 0)
105     throw HDFexception("Can't close dataset");
106   _id = -1;
107 }
108
109 void HDFdataset::WriteOnDisk(void *values)
110 {
111   hdf_err ret;
112
113   if ((ret = HDFdatasetWrite(_id,values)) < 0)
114     throw HDFexception("Can't write dataset");
115  
116 }
117
118 void HDFdataset::ReadFromDisk(void *values)
119 {
120   hdf_err ret;
121
122   if ((ret = HDFdatasetRead(_id,values)) < 0)
123       throw HDFexception("Can't read dataset");
124 }
125
126 HDFcontainerObject *HDFdataset::GetFather()
127 {
128   return _father;
129 }
130
131 hdf_type HDFdataset::GetType()
132 {
133   if (_type == HDF_NONE)
134     if ((_type = HDFdatasetGetType(_id)) == HDF_NONE)
135       throw HDFexception("Can't determine the type of data in the dataset");
136   
137   return _type;
138 }
139
140 int HDFdataset::nDim()
141 {
142   if (_ndim == -1)
143     if ((_ndim = HDFdatasetGetnDim(_id)) < 0)
144       throw HDFexception("Can't determine the dataset dimensions number");
145
146   return _ndim;
147 }
148
149 void HDFdataset::GetDim(hdf_size dim[])
150 {
151   int i;
152   int ndim;
153   hdf_err ret;
154
155   if (_dim == 0)
156     {
157       if (_ndim == -1)
158         ndim = HDFdatasetGetnDim(_id);
159       else
160         ndim = _ndim;
161       _dim = new hdf_size[ndim];
162       if ((ret = HDFdatasetGetDim(_id,_dim)) < 0)
163         throw HDFexception("Can't determine the size dimensions of the dataset ");
164     }
165
166   for (i=0;i<_ndim;i++)
167     dim[i] = _dim[i];
168 }
169
170 int HDFdataset::GetSize()
171 {
172   int size_type;
173
174   if (_size == -1)
175     {
176       if ((_size = HDFdatasetGetSize(_id)) < 0)
177         throw HDFexception("Can't determine the size of the dataset");
178       
179       if (_type == HDF_NONE)
180         if ((_type = HDFdatasetGetType(_id)) == HDF_NONE)
181           throw HDFexception("Can't determine the size of the dataset");
182       
183       switch (_type)
184         {
185         case HDF_INT32 : 
186           size_type = 4;
187           break;
188           
189         case HDF_INT64 :
190         case HDF_FLOAT64 :
191           size_type = 8;
192           break;
193           
194         default :
195           size_type = 1;
196         }
197       _size = _size / size_type;
198     }
199
200   return _size;
201 }
202
203 hdf_byte_order HDFdataset::GetOrder()
204 {
205   if (_byte_order < 0 )
206     if ((_byte_order = HDFdatasetGetOrder( _id )) < 0)
207       throw HDFexception("Can't determine the byte order of the dataset");
208   return _byte_order;
209 }
210
211 hdf_object_type HDFdataset::GetObjectType()
212 {
213   return HDF_DATASET;
214 }
215
216
217 int HDFdataset::nAttributes()
218 {
219   int nbAttrs = H5Aget_num_attrs(_id);
220   if(nbAttrs <= 0) nbAttrs = 0;
221   return nbAttrs; 
222 }
223
224
225 char* HDFdataset::GetAttributeName(unsigned idx)
226 {
227   int nbAttrs = nAttributes();
228   if(nbAttrs == 0) return NULL;
229   H5Aiterate(_id, &idx, dataset_attr, &_attribute);
230   return _attribute;
231 }
232