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