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