Salome HOME
0034c4242094eb5dc995cde27673cea6df800ba5
[modules/kernel.git] / src / HDFPersist / test9.cxx
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   : test9.cxx
25 //  Module : SALOME
26
27 #include <iostream.h>
28 #include "HDFOI.hxx"
29 #include <stdlib.h>
30 using namespace std;
31
32
33 int main()
34 {
35   HDFfile      *hdf_file;
36   HDFgroup     *hdf_group;
37   HDFdataset   *hdf_dataset1;
38   int          fd;
39   size_t       size;
40   char *       buffer;
41   off_t        pos;
42
43   system("rm ascii_hdf.txt");
44
45   try  {
46    
47     // a new HDFfile object
48     hdf_file = new HDFfile("file_ascii.hdf");
49     MESSAGE( ">> A HDFfile object is created" );
50       
51     hdf_file->OpenOnDisk(HDF_RDONLY);
52     MESSAGE( ">> The HDF file is opened on Disk with HDF_RDONLY access mode" )
53   
54     hdf_group = new HDFgroup("ASCII GROUP",hdf_file); 
55     MESSAGE(">> A new HDF group object ASCII GROUP" << " is created in memory")
56
57     hdf_group->OpenOnDisk();
58     MESSAGE( ">> The group ASCII GROUP is opened on disk" );
59       
60     // The dataset object 'ASCII DATASET'
61     hdf_dataset1 = new HDFdataset("ASCII DATASET",hdf_group);
62     MESSAGE( ">> The dataset object 'ASCII DATASET' is created in memory " );
63
64     hdf_dataset1->OpenOnDisk();
65     MESSAGE( ">> The dataset object 'ASCII DATASET' is opened on disk " );
66       
67     size = (size_t) hdf_dataset1->GetSize();
68  
69     // Open the target file
70       
71     if ( (fd = open("ascii_hdf.txt",O_RDWR|O_CREAT,00666)) <0) { 
72       perror("open : test9");
73       return -1;
74     };
75
76   //    if ( (pos = lseek(fd,(off_t) size-3,SEEK_SET)) <0) {
77 //        perror("lseek : test9");
78 //        return -1;
79 //      };
80
81 //      if ( write(fd,"END",3) <0) { 
82 //        perror("write : test9");
83 //        return -1;
84 //      };
85
86 #ifdef _POSIX_MAPPED_FILES
87     // Map le fichier en mémoire
88     //   if ((buffer = (char *)  mmap(0,size,PROT_WRITE,MAP_SHARED,fd,0)) == MAP_FAILED ) {
89     // perror("mmap : test9");
90     //return -1;
91     //};
92     buffer = new char[size];
93 #else
94     // Sort de la compilation
95 #error Necessite l''utilisation de la primitive mmap      
96 #endif
97       
98     // Transfert les informations dans le fichier destination
99     hdf_dataset1->ReadFromDisk(buffer);
100
101     if ( write(fd,buffer,size) <0) { 
102       perror("write : test9");
103       return -1;
104     };
105           
106     // Desalloue le mapping
107 //      if (munmap(buffer,size) < 0 ) {
108 //        perror("munmap : test9");
109 //        return -1;
110 //      };
111       
112     // Close the target file
113     if (close(fd) <0) {
114       perror("close : test9");
115       return -1;
116     }
117       
118     hdf_dataset1->CloseOnDisk();
119     MESSAGE( ">> The dataset is closed on disk " );
120  
121     hdf_group->CloseOnDisk();
122     MESSAGE( ">> The group is closed on file" );
123       
124     hdf_file->CloseOnDisk();
125     MESSAGE( ">> The HDF file is closed on Disk" )
126         
127     // Memory clean
128     delete hdf_dataset1;
129     delete hdf_group;
130     delete hdf_file;
131     delete buffer;
132     MESSAGE( ">> MEMORY CLEAN : all HDF objects have been deleted" );
133  
134   }
135   catch (HDFexception) {
136     return -1;
137     MESSAGE( "!!! HDFexception !!! " )
138   }  
139   
140   return 0;
141 }
142