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