Back to index

salome-kernel  6.5.0
HDFfile.cc
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME HDFPersist : implementation of HDF persitent ( save/ restore )
00024 //  File   : HDFfile.cc
00025 //  Module : SALOME
00026 //
00027 #include "hdfi.h"
00028 #ifndef WIN32
00029 #include <unistd.h>
00030 #else
00031 #include <io.h>
00032 #define F_OK 0
00033 #define access _access
00034 #endif
00035 #include <string.h>
00036 #include <iostream>
00037 #include "HDFfile.hxx"
00038 #include "HDFexception.hxx"
00039 
00040 herr_t file_attr(hid_t loc_id, const char *attr_name, void *operator_data)
00041 {
00042    *(char**)operator_data = new char[strlen(attr_name)+1];
00043    strcpy(*(char**)operator_data, attr_name);
00044    return 1;
00045 }
00046 
00047 HDFfile::HDFfile(char *name)
00048   : HDFcontainerObject(name) 
00049 {
00050   _attribute = NULL;
00051 }
00052 
00053 void HDFfile::CreateOnDisk()
00054 {
00055   if ((_id = HDFfileCreate(_name)) < 0) 
00056     throw HDFexception("Can't create HDF file");
00057 }
00058 
00059 void HDFfile::OpenOnDisk(hdf_access_mode access_mode)
00060 {
00061   _access_mode = access_mode;
00062 
00063   switch (_access_mode)
00064     {
00065     case HDF_RDWR :
00066       if (access(_name,F_OK))
00067        {
00068          if ((_id = HDFfileCreate(_name)) < 0) 
00069            throw HDFexception("Can't open HDF file");
00070        }
00071       else
00072        if ((_id = HDFfileOpen(_name,_access_mode)) < 0)
00073          throw HDFexception("Can't open HDF file");
00074       break;
00075       
00076     case HDF_RDONLY :
00077       if ((_id = HDFfileOpen(_name,_access_mode)) < 0)
00078        throw HDFexception("Can't open HDF file");
00079       break;
00080       
00081     default :
00082       throw HDFexception("Can't open HDF file : bad acces option");
00083     }
00084 }
00085 
00086 void HDFfile::CloseOnDisk()
00087 {
00088   hdf_err ret;
00089 
00090   if ((ret = HDFfileClose(_id)) < 0) 
00091     throw HDFexception("Can't close HDF file");
00092   _id = -1;
00093 }
00094   
00095 
00096 hdf_access_mode HDFfile::GetAccessMode()
00097 {
00098   return _access_mode;
00099 }
00100 
00101 hdf_object_type HDFfile::GetObjectType()
00102 {
00103   return HDF_FILE;
00104 }
00105 
00106 int HDFfile::nInternalObjects()
00107 {
00108   int n;
00109   hdf_err ret;   
00110 
00111   if ((ret = HDFnObjects(_id,"/",&n)) < 0)
00112     throw HDFexception("Can't determine the number of internal objects");
00113 
00114   return  n;
00115 }
00116 
00117 void HDFfile::InternalObjectIndentify(int rank, char *object_name)
00118 {
00119   hdf_err ret;
00120 
00121   if ((ret = HDFobjectIdentify(_id,"/",rank,object_name)) < 0)
00122     throw HDFexception("Can't identify an internal object");
00123 }
00124 
00125 int HDFfile::ExistInternalObject(const char *object_name)
00126 {
00127   int n,i;
00128   int ret = 0;
00129   char name[HDF_NAME_MAX_LEN+1];
00130 
00131   n = this->nInternalObjects(); 
00132   for (i=0;i<n;i++) 
00133     {
00134       this->InternalObjectIndentify(i,name);
00135       if (!strcmp(name,object_name))
00136        {
00137          ret = 1;
00138          break;
00139        }
00140     }  
00141   return ret;
00142 }
00143 
00144 hdf_object_type HDFfile::InternalObjectType(char *object_name)
00145 {
00146   hdf_object_type type;
00147   hdf_err ret;
00148   
00149   if ((ret = HDFobjectType(_id,object_name,&type)) < 0)
00150     throw HDFexception("Can't determine internal object type");
00151 
00152   return type;
00153 }
00154 
00155 int HDFfile::nAttributes()
00156 {
00157   int nbAttrs = H5Aget_num_attrs(_id);
00158   if(nbAttrs <= 0) nbAttrs = 0;
00159   return nbAttrs; 
00160 }
00161 
00162 char* HDFfile::GetAttributeName(unsigned idx)
00163 {
00164   int nbAttrs = nAttributes();
00165   if(nbAttrs == 0) return NULL;
00166   H5Aiterate(_id, &idx, file_attr, &_attribute);
00167   return _attribute;
00168 }