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