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