]> SALOME platform Git repositories - modules/kernel.git/blob - src/HDFPersist/HDFdataset.cc
Salome HOME
0021196: [CEA 456] Integration and merge modification for debian packages
[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 #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 }
63
64
65 HDFdataset::HDFdataset(const char *name,HDFcontainerObject *father)
66   : HDFinternalObject(name)
67 {
68   _father = father;
69   _fid = _father->GetId();
70   _father->AddSon(this);
71   _type = HDF_NONE;
72   _ndim = -1;
73   _dim = 0;
74   _byte_order = H5T_ORDER_ERROR;
75   _size = -1;
76   _attribute = NULL;
77 }
78
79 HDFdataset::~HDFdataset()
80 {
81   delete [] _dim;
82 }
83
84 void HDFdataset::CreateOnDisk()
85 {
86   if ((_id = HDFdatasetCreate(_fid,_name,_type,_dim,_ndim,_byte_order)) < 0)
87     throw HDFexception("Can't create dataset");
88 }
89
90 void HDFdataset::OpenOnDisk()
91 {
92   if ((_id = HDFdatasetOpen(_fid,_name)) < 0)
93     throw HDFexception("Can't open dataset");
94 }
95
96 void HDFdataset::CloseOnDisk()
97 {
98   hdf_err ret;
99
100   if ((ret = HDFdatasetClose(_id)) < 0)
101     throw HDFexception("Can't close dataset");
102   _id = -1;
103 }
104
105 void HDFdataset::WriteOnDisk(void *values)
106 {
107   hdf_err ret;
108
109   if ((ret = HDFdatasetWrite(_id,values)) < 0)
110     throw HDFexception("Can't write dataset");
111  
112 }
113
114 void HDFdataset::ReadFromDisk(void *values)
115 {
116   hdf_err ret;
117
118   if ((ret = HDFdatasetRead(_id,values)) < 0)
119       throw HDFexception("Can't read dataset");
120 }
121
122 HDFcontainerObject *HDFdataset::GetFather()
123 {
124   return _father;
125 }
126
127 hdf_type HDFdataset::GetType()
128 {
129   if (_type == HDF_NONE)
130     if ((_type = HDFdatasetGetType(_id)) == HDF_NONE)
131       throw HDFexception("Can't determine the type of data in the dataset");
132   
133   return _type;
134 }
135
136 int HDFdataset::nDim()
137 {
138   if (_ndim == -1)
139     if ((_ndim = HDFdatasetGetnDim(_id)) < 0)
140       throw HDFexception("Can't determine the dataset dimensions number");
141
142   return _ndim;
143 }
144
145 void HDFdataset::GetDim(hdf_size dim[])
146 {
147   int i;
148   int ndim;
149   hdf_err ret;
150
151   if (_dim == 0)
152     {
153       if (_ndim == -1)
154         ndim = HDFdatasetGetnDim(_id);
155       else
156         ndim = _ndim;
157       _dim = new hdf_size[ndim];
158       if ((ret = HDFdatasetGetDim(_id,_dim)) < 0)
159         throw HDFexception("Can't determine the size dimensions of the dataset ");
160     }
161
162   for (i=0;i<_ndim;i++)
163     dim[i] = _dim[i];
164 }
165
166 hdf_size HDFdataset::GetSize()
167 {
168   int size_type;
169
170   if (_size == -1)
171     {
172       if ((_size = HDFdatasetGetSize(_id)) < 0)
173         throw HDFexception("Can't determine the size of the dataset");
174       
175       if (_type == HDF_NONE)
176         if ((_type = HDFdatasetGetType(_id)) == HDF_NONE)
177           throw HDFexception("Can't determine the size of the dataset");
178       
179       switch (_type)
180         {
181         case HDF_INT32 : 
182           size_type = 4;
183           break;
184           
185         case HDF_INT64 :
186         case HDF_FLOAT64 :
187           size_type = 8;
188           break;
189           
190         default :
191           size_type = 1;
192         }
193       _size = _size / size_type;
194     }
195
196   return _size;
197 }
198
199 hdf_byte_order HDFdataset::GetOrder()
200 {
201   if (_byte_order < 0 )
202     if ((_byte_order = HDFdatasetGetOrder( _id )) < 0)
203       throw HDFexception("Can't determine the byte order of the dataset");
204   return _byte_order;
205 }
206
207 hdf_object_type HDFdataset::GetObjectType()
208 {
209   return HDF_DATASET;
210 }
211
212
213 int HDFdataset::nAttributes()
214 {
215   int nbAttrs = H5Aget_num_attrs(_id);
216   if(nbAttrs <= 0) nbAttrs = 0;
217   return nbAttrs; 
218 }
219
220
221 char* HDFdataset::GetAttributeName(unsigned idx)
222 {
223   int nbAttrs = nAttributes();
224   if(nbAttrs == 0) return NULL;
225   H5Aiterate(_id, &idx, dataset_attr, &_attribute);
226   return _attribute;
227 }
228