Back to index

salome-kernel  6.5.0
test9.cxx
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   : test9.cxx
00025 //  Module : SALOME
00026 //
00027 #include <iostream.h>
00028 #include "HDFOI.hxx"
00029 #include <stdlib.h>
00030 
00031 
00032 int main()
00033 {
00034   HDFfile      *hdf_file;
00035   HDFgroup     *hdf_group;
00036   HDFdataset   *hdf_dataset1;
00037   int          fd;
00038   size_t       size;
00039   char *       buffer;
00040   off_t        pos;
00041 
00042   system("rm ascii_hdf.txt");
00043 
00044   try  {
00045    
00046     // a new HDFfile object
00047     hdf_file = new HDFfile("file_ascii.hdf");
00048     MESSAGE( ">> A HDFfile object is created" );
00049       
00050     hdf_file->OpenOnDisk(HDF_RDONLY);
00051     MESSAGE( ">> The HDF file is opened on Disk with HDF_RDONLY access mode" )
00052   
00053     hdf_group = new HDFgroup("ASCII GROUP",hdf_file); 
00054     MESSAGE(">> A new HDF group object ASCII GROUP" << " is created in memory")
00055 
00056     hdf_group->OpenOnDisk();
00057     MESSAGE( ">> The group ASCII GROUP is opened on disk" );
00058       
00059     // The dataset object 'ASCII DATASET'
00060     hdf_dataset1 = new HDFdataset("ASCII DATASET",hdf_group);
00061     MESSAGE( ">> The dataset object 'ASCII DATASET' is created in memory " );
00062 
00063     hdf_dataset1->OpenOnDisk();
00064     MESSAGE( ">> The dataset object 'ASCII DATASET' is opened on disk " );
00065       
00066     size = (size_t) hdf_dataset1->GetSize();
00067  
00068     // Open the target file
00069       
00070     if ( (fd = open("ascii_hdf.txt",O_RDWR|O_CREAT,00666)) <0) { 
00071       perror("open : test9");
00072       return -1;
00073     };
00074 
00075   //    if ( (pos = lseek(fd,(off_t) size-3,SEEK_SET)) <0) {
00076 //        perror("lseek : test9");
00077 //        return -1;
00078 //      };
00079 
00080 //      if ( write(fd,"END",3) <0) { 
00081 //        perror("write : test9");
00082 //        return -1;
00083 //      };
00084 
00085 #ifdef _POSIX_MAPPED_FILES
00086     // Map le fichier en mémoire
00087     //   if ((buffer = (char *)  mmap(0,size,PROT_WRITE,MAP_SHARED,fd,0)) == MAP_FAILED ) {
00088     // perror("mmap : test9");
00089     //return -1;
00090     //};
00091     buffer = new char[size];
00092 #else
00093     // Sort de la compilation
00094 #error Necessite l''utilisation de la primitive mmap      
00095 #endif
00096       
00097     // Transfert les informations dans le fichier destination
00098     hdf_dataset1->ReadFromDisk(buffer);
00099 
00100     if ( write(fd,buffer,size) <0) { 
00101       perror("write : test9");
00102       return -1;
00103     };
00104           
00105     // Desalloue le mapping
00106 //      if (munmap(buffer,size) < 0 ) {
00107 //        perror("munmap : test9");
00108 //        return -1;
00109 //      };
00110       
00111     // Close the target file
00112     if (close(fd) <0) {
00113       perror("close : test9");
00114       return -1;
00115     }
00116       
00117     hdf_dataset1->CloseOnDisk();
00118     MESSAGE( ">> The dataset is closed on disk " );
00119  
00120     hdf_group->CloseOnDisk();
00121     MESSAGE( ">> The group is closed on file" );
00122       
00123     hdf_file->CloseOnDisk();
00124     MESSAGE( ">> The HDF file is closed on Disk" )
00125         
00126     // Memory clean
00127     delete hdf_dataset1;
00128     delete hdf_group;
00129     delete hdf_file;
00130     delete buffer;
00131     MESSAGE( ">> MEMORY CLEAN : all HDF objects have been deleted" );
00132  
00133   }
00134   catch (HDFexception) {
00135     return -1;
00136     MESSAGE( "!!! HDFexception !!! " )
00137   }  
00138   
00139   return 0;
00140 }
00141