Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : HDFdataset.cc
25 //  Module : SALOME
26
27 extern "C"
28 {
29 #include "hdfi.h"
30 }
31 #include <string>
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)
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   _size = 1;
59   _attribute = NULL;
60   for (i=0;i<dimsize;i++)
61     {
62       _dim[i] = dim[i];
63       _size = _size * _dim[i];
64     }
65 }
66
67
68 HDFdataset::HDFdataset(char *name,HDFcontainerObject *father)
69   : HDFinternalObject(name)
70 {
71   _father = father;
72   _fid = _father->GetId();
73   _father->AddSon(this);
74   _type = HDF_NONE;
75   _ndim = -1;
76   _dim = 0;
77   _size = -1;
78   _attribute = NULL;
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)) < 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 int HDFdataset::GetSize()
169 {
170   int size_type;
171
172   if (_size == -1)
173     {
174       if ((_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_object_type HDFdataset::GetObjectType()
202 {
203   return HDF_DATASET;
204 }
205
206
207 int HDFdataset::nAttributes()
208 {
209   int nbAttrs = H5Aget_num_attrs(_id);
210   if(nbAttrs <= 0) nbAttrs = 0;
211   return nbAttrs; 
212 }
213
214
215 char* HDFdataset::GetAttributeName(unsigned idx)
216 {
217   int nbAttrs = nAttributes();
218   if(nbAttrs == 0) return NULL;
219   H5Aiterate(_id, &idx, dataset_attr, &_attribute);
220   return _attribute;
221 }
222