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