Salome HOME
Porting KERNEL on new XML reader.
[modules/kernel.git] / src / HDFPersist / HDFfile.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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : HDFfile.cc
25 //  Module : SALOME
26
27 extern "C"
28 {
29 #include "hdfi.h"
30 #ifndef WNT
31 #include <unistd.h>
32 #else
33 #include <io.h>
34 #define F_OK 0
35 #endif
36 #include <string.h>
37 }
38 #include <iostream>
39 #include "HDFfile.hxx"
40 #include "HDFexception.hxx"
41 using namespace std;
42
43 herr_t file_attr(hid_t loc_id, const char *attr_name, void *operator_data)
44 {
45    *(char**)operator_data = new char[strlen(attr_name)+1];
46    strcpy(*(char**)operator_data, attr_name);
47    return 1;
48 }
49
50 HDFfile::HDFfile(char *name)
51   : HDFcontainerObject(name) 
52 {
53   _attribute = NULL;
54 }
55
56 void HDFfile::CreateOnDisk()
57 {
58   if ((_id = HDFfileCreate(_name)) < 0) 
59     throw HDFexception("Can't create HDF file");
60 }
61
62 void HDFfile::OpenOnDisk(hdf_access_mode access_mode)
63 {
64   _access_mode = access_mode;
65
66   switch (_access_mode)
67     {
68     case HDF_RDWR :
69       if (access(_name,F_OK))
70         {
71           if ((_id = HDFfileCreate(_name)) < 0) 
72             throw HDFexception("Can't open HDF file");
73         }
74       else
75         if ((_id = HDFfileOpen(_name,_access_mode)) < 0)
76           throw HDFexception("Can't open HDF file");
77       break;
78       
79     case HDF_RDONLY :
80       if ((_id = HDFfileOpen(_name,_access_mode)) < 0)
81         throw HDFexception("Can't open HDF file");
82       break;
83       
84     default :
85       throw HDFexception("Can't open HDF file : bad acces option");
86     }
87 }
88
89 void HDFfile::CloseOnDisk()
90 {
91   hdf_err ret;
92
93   if ((ret = HDFfileClose(_id)) < 0) 
94     throw HDFexception("Can't close HDF file");
95   _id = -1;
96 }
97   
98
99 hdf_access_mode HDFfile::GetAccessMode()
100 {
101   return _access_mode;
102 }
103
104 hdf_object_type HDFfile::GetObjectType()
105 {
106   return HDF_FILE;
107 }
108
109 int HDFfile::nInternalObjects()
110 {
111   int n;
112   hdf_err ret;   
113
114   if ((ret = HDFnObjects(_id,"/",&n)) < 0)
115     throw HDFexception("Can't determine the number of internal objects");
116
117   return  n;
118 }
119
120 void HDFfile::InternalObjectIndentify(int rank, char *object_name)
121 {
122   hdf_err ret;
123
124   if ((ret = HDFobjectIdentify(_id,"/",rank,object_name)) < 0)
125     throw HDFexception("Can't identify an internal object");
126 }
127
128 int HDFfile::ExistInternalObject(char *object_name)
129 {
130   int n,i;
131   int ret = 0;
132   char name[HDF_NAME_MAX_LEN+1];
133
134   n = this->nInternalObjects(); 
135   for (i=0;i<n;i++) 
136     {
137       this->InternalObjectIndentify(i,name);
138       if (!strcmp(name,object_name))
139         {
140           ret = 1;
141           break;
142         }
143     }  
144   return ret;
145 }
146
147 hdf_object_type HDFfile::InternalObjectType(char *object_name)
148 {
149   hdf_object_type type;
150   hdf_err ret;
151   
152   if ((ret = HDFobjectType(_id,object_name,&type)) < 0)
153     throw HDFexception("Can't determine internal object type");
154
155   return type;
156 }
157
158 int HDFfile::nAttributes()
159 {
160   int nbAttrs = H5Aget_num_attrs(_id);
161   if(nbAttrs <= 0) nbAttrs = 0;
162   return nbAttrs; 
163 }
164
165 char* HDFfile::GetAttributeName(unsigned idx)
166 {
167   int nbAttrs = nAttributes();
168   if(nbAttrs == 0) return NULL;
169   H5Aiterate(_id, &idx, file_attr, &_attribute);
170   return _attribute;
171 }