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