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