Salome HOME
DCQ : Merge with Ecole_ete_a6.
[modules/kernel.git] / src / HDFPersist / HDFconvert.cc
1 //  SALOME HDFPersist : implementation of HDF persitent ( save/ restore )
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : HDFconvert.cc
25 //  Module : SALOME
26
27 using namespace std;
28 #include "HDFconvert.hxx"
29
30 int HDFConvert::FromAscii(const string& file, const HDFcontainerObject & hdf_container, const string& nomdataset)
31 {
32   
33   HDFdataset   * hdf_dataset;
34   char         * buffer;
35   int          fd;
36   struct stat  status;
37   size_t       length;
38   hdf_size     length_long;
39   
40   // Ouverture du fichier source
41   if ( (fd = open(file.c_str(),O_RDONLY)) <0) { 
42     perror("HDFConvert::FromAscii");
43     return -1;
44   };
45   
46   // Lit l'état du fichier
47   if ( fstat(fd,&status) < 0) {
48     perror("HDFConvert::FromAscii");
49     return -1;
50   };
51   
52   length = status.st_size; //Calcul la taille du fichier en octets
53   length_long = length;
54   
55 #ifdef _POSIX_MAPPED_FILES
56   
57   // Map le fichier en mémoire
58   if ( (buffer = (char *)  mmap(0,length,PROT_READ,MAP_SHARED,fd,0)) == MAP_FAILED ) {
59     perror("HDFConvert::FromAscii");
60     return -1;
61   };
62   
63 #else
64
65   // Sort de la compilation
66 #error Necessite l''utilisation de la primitive mmap
67   
68 #endif
69   
70   // Creation du Dataset utilisateur 
71   hdf_dataset = new HDFdataset::HDFdataset( (char *) nomdataset.c_str(),            /*discard const */
72                                             (HDFcontainerObject*) &hdf_container,   /*discard const, pas de constructeur par référence */
73                                             HDF_STRING,
74                                             &length_long,1);
75   // Cree le Dataset sur le disk
76   hdf_dataset->CreateOnDisk();
77   
78   // Effectue la copie
79   hdf_dataset->WriteOnDisk(buffer);
80   
81   // Ferme le fichier hdf
82   hdf_dataset->CloseOnDisk();
83   
84   // Memory Clean
85   delete hdf_dataset;
86   
87 #ifdef _POSIX_MAPPED_FILES
88   
89   // Desalloue le mapping
90   if (munmap(buffer,length) < 0 ) {
91     perror("HDFConvert::FromAscii");
92     return -1;
93   };
94 #endif
95   
96   // Ferme le fichier ASCII
97   if (close(fd) <0) {
98     perror("HDFConvert::FromAscii");
99     return -1;
100   };
101     
102 };