Salome HOME
Merge branch 'V9_9_BR'
[modules/kernel.git] / src / HDFPersist / HDFgroup.cc
1 // Copyright (C) 2007-2022  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   : HDFgroup.cc
25 //  Module : SALOME
26 //
27 #include "hdfi.h"
28 #include <string.h>
29 #include "HDFgroup.hxx"
30 #include "HDFexception.hxx"
31
32 herr_t group_attr(hid_t /*loc_id*/, const char *attr_name, void *operator_data)
33 {
34   *(char**)operator_data = new char[strlen(attr_name)+1];
35   strcpy(*(char**)operator_data, attr_name);
36   return 1;
37 }
38
39 HDFgroup::HDFgroup(const char *name, HDFcontainerObject *father)
40   : HDFcontainerObject(name)
41 {
42   _father = father;
43   _fid = _father->GetId();
44   _father->AddSon(this);
45   _mid = -1;
46   _attribute = NULL;
47 }
48
49 void HDFgroup::CreateOnDisk()
50 {
51   if ((_id = HDFgroupCreate(_fid,_name)) < 0) 
52     throw HDFexception("Can't create group");
53 }
54   
55 void HDFgroup::OpenOnDisk()
56 {
57   if ((_id = HDFgroupOpen(_fid,_name)) < 0)
58     throw HDFexception("Can't open group");
59 }
60   
61 void HDFgroup::CloseOnDisk()
62 {
63   hdf_err ret;
64
65   if ((ret = HDFgroupClose(_id)) < 0)
66     throw HDFexception("Can't close group");
67   _id = -1;
68 }
69
70 HDFcontainerObject *HDFgroup::GetFather()
71 {
72   return _father;
73 }
74
75 hdf_object_type HDFgroup::GetObjectType()
76 {
77   return HDF_GROUP;
78 }
79
80 int HDFgroup::nInternalObjects()
81 {
82   int n = 0;
83   hdf_err ret;   
84   
85   if ((ret = HDFnObjects(_fid,_name,&n)) < 0)
86     throw HDFexception("Can't determine the number of internal objects");
87
88   return  n;  
89 }
90
91 void HDFgroup::InternalObjectIndentify(int rank, char *object_name)
92 {
93   hdf_err ret;
94
95   if ((ret = HDFobjectIdentify(_fid,_name,rank,object_name)) < 0)
96     throw HDFexception("Can't identify an internal object");
97 }
98
99 int HDFgroup::ExistInternalObject(const char *object_name)
100 {
101   int n,i;
102   int ret = 0;
103   char name[HDF_NAME_MAX_LEN+1];
104
105   n = this->nInternalObjects(); 
106   for (i=0;i<n;i++) 
107   {
108     this->InternalObjectIndentify(i,name);
109     if (!strcmp(name,object_name))
110     {
111       ret = 1;
112       break;
113     }
114   }  
115   return ret;
116 }
117
118 void HDFgroup::GetAllObjects(std::vector< std::string > & object_names )
119 {
120   int n,i;
121   char name[HDF_NAME_MAX_LEN+1];
122
123   n = this->nInternalObjects();
124   object_names.reserve( object_names.size() + n );
125   for (i=0;i<n;i++)
126   {
127     this->InternalObjectIndentify(i,name);
128     object_names.push_back( name );
129   }
130 }
131
132 hdf_object_type HDFgroup::InternalObjectType(char *object_name)
133 {
134   hdf_object_type type;
135   hdf_err ret;
136   
137   if ((ret = HDFobjectType(_id,object_name,&type)) < 0)
138     throw HDFexception("Can't determine internal object type");
139
140   return type;
141 }
142
143 void HDFgroup::FileMount(char *file,hdf_access_mode mode)
144 {
145   hdf_err ret;
146
147   if (_mid != -1)
148     throw HDFexception("Can't mount the file");    
149
150   if ((_mid = HDFfileOpen(file,mode)) < 0)
151     throw HDFexception("Can't mount the file");
152
153   if ((ret = HDFfileMount(_fid,_name,_mid)) < 0)
154     throw HDFexception("Can't mount the file");
155 }
156
157 void HDFgroup::FileUnMount()
158 {
159   hdf_err ret;
160
161   if ((ret = HDFfileUmount(_fid,_name)) < 0)
162     throw HDFexception("Can't unmount the file");
163
164   if ((ret = HDFfileClose(_mid)) < 0)
165     throw HDFexception("Can't unmount the file");
166
167   _mid = -1;
168 }
169
170 int HDFgroup::nAttributes()
171 {
172   int nbAttrs = H5Aget_num_attrs(_id);
173   if(nbAttrs <= 0) nbAttrs = 0;
174   return nbAttrs; 
175 }
176
177 char* HDFgroup::GetAttributeName(unsigned idx)
178 {
179   int nbAttrs = nAttributes();
180   if(nbAttrs == 0) return NULL;
181   H5Aiterate(_id, &idx, group_attr, &_attribute);
182   return _attribute;
183 }
184
185
186