Salome HOME
Initialisation de la base KERNEL avec la version operationnelle de KERNEL_SRC issue...
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Tool.cxx
1 using namespace std;
2 //  File      : SALOMEDS_Tool.cxx
3 //  Created   : Mon Oct 21 16:24:34 2002
4 //  Author    : Sergey RUIN
5
6 //  Project   : SALOME
7 //  Module    : SALOMEDS
8 //  Copyright : Open CASCADE
9
10 #include "SALOMEDS_Tool.hxx"
11
12 #include "utilities.h"
13
14 #include <TCollection_AsciiString.hxx> 
15 #include <stdio.h>
16 #include <iostream.h> 
17 #include <fstream.h>
18 #include <OSD_Path.hxx>
19 #include <OSD_File.hxx>
20 #include <OSD_Directory.hxx>
21 #include <OSD_Process.hxx>
22 #include <OSD_Directory.hxx>
23 #include <OSD_Protection.hxx>
24 #include <OSD_SingleProtection.hxx>
25 #include <OSD_FileIterator.hxx>
26
27 #include <sys/time.h>
28 #include <stdlib.h>
29
30 //============================================================================
31 // function : GetTempDir
32 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
33 //============================================================================ 
34 char* SALOMEDS_Tool::GetTmpDir()
35 {
36   //Find a temporary directory to store a file
37
38   TCollection_AsciiString aTmpDir;
39
40   char *Tmp_dir = getenv("SALOME_TMP_DIR");
41   if(Tmp_dir != NULL) {
42     aTmpDir = TCollection_AsciiString(Tmp_dir);
43 #ifdef WIN32
44     if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
45 #else
46     if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
47 #endif      
48   }
49   else {
50 #ifdef WIN32
51     aTmpDir = TCollection_AsciiString("C:\\");
52 #else
53     aTmpDir = TCollection_AsciiString("/tmp/");
54 #endif
55   }
56
57   srand((unsigned int)time(NULL));
58   int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
59   TCollection_AsciiString aSubDir(aRND);
60   if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
61
62   MESSAGE("#### RND "  << aRND);
63
64   aTmpDir += aSubDir; //Get RND sub directory
65
66 #ifdef WIN32
67   if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
68 #else
69   if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
70 #endif
71
72   OSD_Path aPath(aTmpDir);
73   OSD_Directory aDir(aPath);
74
75   for(aRND = 0; aDir.Exists(); aRND++) {
76     aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND));  //Build a unique directory name
77     aPath = OSD_Path(aTmpDir);
78     aDir = OSD_Directory(aPath);
79   }
80
81   MESSAGE("#### TMP" << aTmpDir.ToCString());
82
83   OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
84   aDir.Build(aProtection);
85
86   return CORBA::string_dup(aTmpDir.ToCString());
87 }
88
89 //============================================================================
90 // function : RemoveTemporaryFiles
91 // purpose  : Removes files listed in theFileList
92 //============================================================================
93 void SALOMEDS_Tool::RemoveTemporaryFiles(const char* theDirectory, 
94                                          const SALOMEDS::ListOfFileNames& theFiles,
95                                          const bool IsDirDeleted)
96 {
97   TCollection_AsciiString aDirName(const_cast<char*>(theDirectory));
98
99   int i, aLength = theFiles.length();
100   for(i=0; i<aLength; i++) {
101     TCollection_AsciiString aFile(aDirName);
102 //     aFile += (char*)theFiles[i];
103     aFile += (char*)theFiles[i].in();
104     OSD_Path anOSDPath(aFile);
105     OSD_File anOSDFile(anOSDPath);
106     if(!anOSDFile.Exists()) continue;
107
108     anOSDFile.Remove();
109   }
110
111   if(IsDirDeleted) {
112     OSD_Path aPath(aDirName);
113     OSD_Directory aDir(aPath);
114     OSD_FileIterator anIterator(aPath, '*');
115
116     if(aDir.Exists() && !anIterator.More()) aDir.Remove();
117   }
118
119 }
120
121 //============================================================================
122 // function : PutFilesToStream
123 // purpose  : converts the files from a list 'theFiles' to the stream
124 //============================================================================
125 SALOMEDS::TMPFile* 
126 SALOMEDS_Tool::PutFilesToStream(const char* theFromDirectory,
127                                 const SALOMEDS::ListOfFileNames& theFiles,
128                                 const int theNamesOnly)
129 {
130   int i, aLength = theFiles.length();
131   if(aLength == 0) return NULL;
132
133   TCollection_AsciiString aTmpDir(const_cast<char*>(theFromDirectory)); //Get a temporary directory for saved a file
134
135   long aBufferSize = 0;
136   long aCurrentPos;
137
138   int aNbFiles = 0;
139   int* aFileNameSize= new int[aLength];
140   long* aFileSize= new long[aLength];
141
142   //Determine the required size of the buffer
143
144   for(i=0; i<aLength; i++) {
145
146     //Check if the file exists
147     
148     if (!theNamesOnly) { // mpv 15.01.2003: if only file names must be stroed, then size of files is zero
149       TCollection_AsciiString aFullPath = aTmpDir + strdup(theFiles[i]);   
150       OSD_Path anOSDPath(aFullPath);
151       OSD_File anOSDFile(anOSDPath);
152       if(!anOSDFile.Exists()) continue;
153 #ifdef WNT
154       ifstream aFile(aFullPath.ToCString(), ios::binary);
155 #else
156       ifstream aFile(aFullPath.ToCString());
157 #endif
158       aFile.seekg(0, ios::end);
159       aFileSize[i] = aFile.tellg();
160       aBufferSize += aFileSize[i];              //Add a space to store the file
161     }
162     aFileNameSize[i] = strlen(theFiles[i])+1;
163     aBufferSize += aFileNameSize[i];          //Add a space to store the file name
164     aBufferSize += (theNamesOnly)?4:12;       //Add 4 bytes: a length of the file name,
165                                               //    8 bytes: length of the file itself
166     aNbFiles++;
167   } 
168
169   aBufferSize += 4;      //4 bytes for a number of the files that will be written to the stream;
170   unsigned char* aBuffer = new unsigned char[aBufferSize];  
171   if(aBuffer == NULL) return NULL; 
172
173   //Initialize 4 bytes of the buffer by 0
174   memset(aBuffer, 0, 4); 
175   //Copy the number of files that will be written to the stream
176   memcpy(aBuffer, &aNbFiles, ((sizeof(int) > 4) ? 4 : sizeof(int))); 
177
178
179   aCurrentPos = 4;
180
181   for(i=0; i<aLength; i++) {
182     ifstream *aFile;
183     if (!theNamesOnly) { // mpv 15.01.2003: we don't open any file if theNamesOnly = true
184       TCollection_AsciiString aFullPath = aTmpDir + strdup(theFiles[i]);
185       OSD_Path anOSDPath(aFullPath);
186       OSD_File anOSDFile(anOSDPath);
187       if(!anOSDFile.Exists()) continue;
188 #ifdef WNT
189       aFile = new ifstream(aFullPath.ToCString(), ios::binary);
190 #else
191       aFile = new ifstream(aFullPath.ToCString());
192 #endif  
193     }
194     //Initialize 4 bytes of the buffer by 0
195     memset((aBuffer + aCurrentPos), 0, 4); 
196     //Copy the length of the file name to the buffer
197     memcpy((aBuffer + aCurrentPos), (aFileNameSize + i), ((sizeof(int) > 4) ? 4 : sizeof(int))); 
198     aCurrentPos += 4;
199
200     //Copy the file name to the buffer
201     memcpy((aBuffer + aCurrentPos), theFiles[i], aFileNameSize[i]);
202     aCurrentPos += aFileNameSize[i];
203     
204     if (!theNamesOnly) { // mpv 15.01.2003: we don't copy file content to the buffer if !theNamesOnly
205       //Initialize 8 bytes of the buffer by 0
206       memset((aBuffer + aCurrentPos), 0, 8); 
207       //Copy the length of the file to the buffer
208       memcpy((aBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));
209       aCurrentPos += 8;
210       
211       aFile->seekg(0, ios::beg);
212       aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]);
213       aFile->close();
214       delete(aFile);
215       aCurrentPos += aFileSize[i];
216     }
217   }
218
219   delete[] aFileNameSize;
220   delete[] aFileSize;
221   
222   
223   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
224   
225   return (new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1));
226 }
227
228 //============================================================================
229 // function : PutStreamToFile
230 // purpose  : converts the stream "theStream" to the files
231 //============================================================================
232 SALOMEDS::ListOfFileNames* 
233 SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream,
234                                 const char* theToDirectory,
235                                 const int theNamesOnly)
236 {
237   if(theStream.length() == 0) return NULL;
238   TCollection_AsciiString aTmpDir(const_cast<char*>(theToDirectory)); //Get a temporary directory for saving a file
239
240   unsigned char *aBuffer = (unsigned char*)theStream.NP_data();
241
242
243   long aBufferSize = theStream.length();
244   long aFileSize, aCurrentPos = 4;
245   int i, aFileNameSize, aNbFiles = 0;
246
247   //Copy the number of files in the stream
248   memcpy(&aNbFiles, aBuffer, sizeof(int)); 
249
250   SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
251   aFiles->length(aNbFiles);
252
253   for(i=0; i<aNbFiles; i++) {
254
255     //Put a length of the file name to aFileNameSize
256     memcpy(&aFileNameSize, (aBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int))); 
257     aCurrentPos += 4;
258
259     char *aFileName = new char[aFileNameSize];
260     //Put a file name to aFileName
261     memcpy(aFileName, (aBuffer + aCurrentPos), aFileNameSize); 
262     aCurrentPos += aFileNameSize;
263  
264     //Put a length of the file to aFileSize
265     if (!theNamesOnly) {
266       memcpy(&aFileSize, (aBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
267       aCurrentPos += 8;    
268       
269       TCollection_AsciiString aFullPath = aTmpDir + aFileName;
270       ofstream aFile(aFullPath.ToCString());
271       aFile.write((char *)(aBuffer+aCurrentPos), aFileSize); 
272       aFile.close();  
273       aCurrentPos += aFileSize;
274     }
275     aFiles[i] = CORBA::string_dup(aFileName);
276     delete[] aFileName;
277   }
278
279   return aFiles._retn();
280 }
281
282 //============================================================================
283 // function : GetNameFromPath
284 // purpose  : Returns the name by the path
285 //============================================================================
286 char* SALOMEDS_Tool::GetNameFromPath(const char* thePath) {
287   if (thePath == NULL) return strdup("");
288   OSD_Path aPath = OSD_Path(TCollection_AsciiString(strdup(thePath)));
289   TCollection_AsciiString aNameString(aPath.Name());
290   return aNameString.ToCString();
291 }
292
293 //============================================================================
294 // function : GetDirFromPath
295 // purpose  : Returns the dir by the path
296 //============================================================================
297 char* SALOMEDS_Tool::GetDirFromPath(const char* thePath) {
298   if (thePath == NULL) return strdup("");
299   OSD_Path aPath = OSD_Path(TCollection_AsciiString(strdup(thePath)));
300   TCollection_AsciiString aDirString(aPath.Trek());
301   aDirString.ChangeAll('|','/');
302   return aDirString.ToCString();
303 }