Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[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 #include "HDFconvert.hxx"
28 using namespace std;
29
30 #ifdef WNT
31 #include <io.h>
32 #include <windows.h>
33 #endif
34
35 int HDFConvert::FromAscii(const string& file, const HDFcontainerObject & hdf_container, const string& nomdataset)
36 {
37   
38   HDFdataset   * hdf_dataset;
39   char         * buffer;
40   int          fd;
41   struct stat  status;
42   size_t       length;
43   hdf_size     length_long;
44   
45   // Ouverture du fichier source
46   if ( (fd = open(file.c_str(),O_RDONLY)) <0) { 
47     perror("HDFConvert::FromAscii");
48     return -1;
49   };
50   
51   // Lit l'\89tat du fichier
52   if ( fstat(fd,&status) < 0) {
53     perror("HDFConvert::FromAscii");
54     return -1;
55   };
56   
57   length = status.st_size; //Calcul la taille du fichier en octets
58   length_long = length;
59   
60 #ifdef _POSIX_MAPPED_FILES
61   
62   // Map le fichier en m\89moire
63   if ( (buffer = (char *)  mmap(0,length,PROT_READ,MAP_SHARED,fd,0)) == MAP_FAILED ) {
64     perror("HDFConvert::FromAscii");
65     return -1;
66   };
67 #elif defined WNT
68
69 #define SHMEMSIZE 4096
70
71   HANDLE hMapObject = CreateFileMapping( 
72            INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, SHMEMSIZE, "");
73   if (hMapObject != NULL) {
74   // Get a pointer to the file-mapped shared memory.
75   buffer = ( char* ) MapViewOfFile( 
76     hMapObject, FILE_MAP_WRITE, 0, 0, 0 );
77   if( buffer == NULL )
78     CloseHandle(hMapObject);
79   };
80
81 #else
82
83   // Sort de la compilation
84 #error Necessite l''utilisation de la primitive mmap
85   
86 #endif
87   
88   // Creation du Dataset utilisateur 
89   hdf_dataset = new HDFdataset( (char *) nomdataset.c_str(),            /*discard const */
90                                             (HDFcontainerObject*) &hdf_container,   /*discard const, pas de constructeur par r\89f\89rence */
91                                             HDF_STRING,
92                                             &length_long,1);
93   // Cree le Dataset sur le disk
94   hdf_dataset->CreateOnDisk();
95   
96   // Effectue la copie
97   hdf_dataset->WriteOnDisk(buffer);
98   
99   // Ferme le fichier hdf
100   hdf_dataset->CloseOnDisk();
101   
102   // Memory Clean
103   delete hdf_dataset;
104   
105 #ifdef _POSIX_MAPPED_FILES
106   
107   // Desalloue le mapping
108   if (munmap(buffer,length) < 0 ) {
109     perror("HDFConvert::FromAscii");
110     return -1;
111   };
112 #endif
113   
114   // Ferme le fichier ASCII
115   if (close(fd) <0) {
116     perror("HDFConvert::FromAscii");
117     return -1;
118   };
119     
120
121   return length;    
122 };