Back to index

salome-kernel  6.5.0
HDFconvert.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   : HDFconvert.cc
00025 //  Module : SALOME
00026 //
00027 #include "HDFconvert.hxx"
00028 
00029 #ifdef WIN32
00030 #include <io.h>
00031 #include <windows.h>
00032 #define open _open
00033 #define close _close
00034 #endif
00035 
00036 int HDFConvert::FromAscii(const std::string& file, const HDFcontainerObject & hdf_container, const std::string& nomdataset)
00037 {
00038   
00039   HDFdataset   * hdf_dataset;
00040   char         * buffer;
00041   int          fd;
00042   struct stat  status;
00043   size_t       length;
00044   hdf_size     length_long;
00045   
00046   // Ouverture du fichier source
00047   if ( (fd = open(file.c_str(),O_RDONLY)) <0) { 
00048     perror("HDFConvert::FromAscii");
00049     return -1;
00050   };
00051   
00052   // Lit l'‰tat du fichier
00053   if ( fstat(fd,&status) < 0) {
00054     perror("HDFConvert::FromAscii");
00055     return -1;
00056   };
00057   
00058   length = status.st_size; //Calcul la taille du fichier en octets
00059   length_long = length;
00060   
00061 #ifdef _POSIX_MAPPED_FILES
00062   
00063   // Map le fichier en m‰moire
00064   if ( (buffer = (char *)  mmap(0,length,PROT_READ,MAP_SHARED,fd,0)) == MAP_FAILED ) {
00065     perror("HDFConvert::FromAscii");
00066     return -1;
00067   };
00068 #elif defined WIN32
00069 
00070 #define SHMEMSIZE 4096
00071 
00072   HANDLE hMapObject = CreateFileMapping( 
00073            INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, SHMEMSIZE, "");
00074   if (hMapObject != NULL) {
00075   // Get a pointer to the file-mapped shared memory.
00076   buffer = ( char* ) MapViewOfFile( 
00077     hMapObject, FILE_MAP_WRITE, 0, 0, 0 );
00078   if( buffer == NULL )
00079     CloseHandle(hMapObject);
00080   };
00081 
00082 #else
00083 
00084   // Sort de la compilation
00085 #error Necessite l''utilisation de la primitive mmap
00086   
00087 #endif
00088   
00089   // Creation du Dataset utilisateur 
00090   hdf_dataset = new HDFdataset( (char *) nomdataset.c_str(),            /*discard const */
00091                                             (HDFcontainerObject*) &hdf_container,   /*discard const, pas de constructeur par r‰f‰rence */
00092                                             HDF_STRING,
00093                                             &length_long,1);
00094   // Cree le Dataset sur le disk
00095   hdf_dataset->CreateOnDisk();
00096   
00097   // Effectue la copie
00098   hdf_dataset->WriteOnDisk(buffer);
00099   
00100   // Ferme le fichier hdf
00101   hdf_dataset->CloseOnDisk();
00102   
00103   // Memory Clean
00104   delete hdf_dataset;
00105   
00106 #ifdef _POSIX_MAPPED_FILES
00107   
00108   // Desalloue le mapping
00109   if (munmap(buffer,length) < 0 ) {
00110     perror("HDFConvert::FromAscii");
00111     return -1;
00112   };
00113 #endif
00114   
00115   // Ferme le fichier ASCII
00116   if (close(fd) <0) {
00117     perror("HDFConvert::FromAscii");
00118     return -1;
00119   };
00120     
00121 
00122   return length;    
00123 };