]> SALOME platform Git repositories - modules/kernel.git/blob - src/TOOLSDS/SALOMEDS_Tool.cxx
Salome HOME
81d646b6069826aa1d8cdc2ec0c08ec34cc6fc8b
[modules/kernel.git] / src / TOOLSDS / SALOMEDS_Tool.cxx
1 //  File      : SALOMEDS_Tool.cxx
2 //  Created   : Mon Oct 21 16:24:34 2002
3 //  Author    : Sergey RUIN
4
5 //  Project   : SALOME
6 //  Module    : SALOMEDS
7 //  Copyright : Open CASCADE
8
9 #include "SALOMEDS_Tool.hxx"
10
11 #include "utilities.h"
12
13 #include <TCollection_AsciiString.hxx> 
14 #include <stdio.h>
15 #include <iostream.h> 
16 #include <fstream.h>
17 #include <OSD_Path.hxx>
18 #include <OSD_File.hxx>
19 #include <OSD_Directory.hxx>
20 #include <OSD_Process.hxx>
21 #include <OSD_Directory.hxx>
22 #include <OSD_Protection.hxx>
23 #include <OSD_SingleProtection.hxx>
24 #include <OSD_FileIterator.hxx>
25
26 #include <sys/time.h>
27 #include <stdlib.h>
28
29 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
30
31 using namespace std;
32
33 //============================================================================
34 // function : GetTempDir
35 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
36 //============================================================================ 
37 std::string SALOMEDS_Tool::GetTmpDir()
38 {
39   //Find a temporary directory to store a file
40
41   TCollection_AsciiString aTmpDir;
42
43   char *Tmp_dir = getenv("SALOME_TMP_DIR");
44   if(Tmp_dir != NULL) {
45     aTmpDir = TCollection_AsciiString(Tmp_dir);
46 #ifdef WIN32
47     if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
48 #else
49     if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
50 #endif      
51   }
52   else {
53 #ifdef WIN32
54     aTmpDir = TCollection_AsciiString("C:\\");
55 #else
56     aTmpDir = TCollection_AsciiString("/tmp/");
57 #endif
58   }
59
60   srand((unsigned int)time(NULL));
61   int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
62   TCollection_AsciiString aSubDir(aRND);
63   if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
64
65   MESSAGE("#### RND "  << aRND);
66
67   aTmpDir += aSubDir; //Get RND sub directory
68
69 #ifdef WIN32
70   if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
71 #else
72   if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
73 #endif
74
75   OSD_Path aPath(aTmpDir);
76   OSD_Directory aDir(aPath);
77
78   for(aRND = 0; aDir.Exists(); aRND++) {
79     aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND));  //Build a unique directory name
80     aPath = OSD_Path(aTmpDir);
81     aDir = OSD_Directory(aPath);
82   }
83
84   MESSAGE("#### TMP" << aTmpDir.ToCString());
85
86   OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
87   aDir.Build(aProtection);
88
89   return aTmpDir.ToCString();
90 }
91
92 //============================================================================
93 // function : RemoveTemporaryFiles
94 // purpose  : Removes files listed in theFileList
95 //============================================================================
96 void SALOMEDS_Tool::RemoveTemporaryFiles(const char* theDirectory, 
97                                          const SALOMEDS::ListOfFileNames& theFiles,
98                                          const bool IsDirDeleted)
99 {
100   TCollection_AsciiString aDirName(const_cast<char*>(theDirectory));
101
102   int i, aLength = theFiles.length();
103   for(i=0; i<aLength; i++) {
104     TCollection_AsciiString aFile(aDirName);
105 //     aFile += (char*)theFiles[i];
106     aFile += (char*)theFiles[i].in();
107     OSD_Path anOSDPath(aFile);
108     OSD_File anOSDFile(anOSDPath);
109     if(!anOSDFile.Exists()) continue;
110
111     OSD_Protection aProtection = anOSDFile.Protection();
112     aProtection.SetUser(OSD_RW);
113     anOSDFile.SetProtection(aProtection);
114
115     anOSDFile.Remove();
116   }
117
118   if(IsDirDeleted) {
119     OSD_Path aPath(aDirName);
120     OSD_Directory aDir(aPath);
121     OSD_FileIterator anIterator(aPath, '*');
122
123     if(aDir.Exists() && !anIterator.More()) aDir.Remove();
124   }
125
126 }
127
128 //============================================================================
129 // function : PutFilesToStream
130 // purpose  : converts the files from a list 'theFiles' to the stream
131 //============================================================================
132 SALOMEDS::TMPFile* 
133 SALOMEDS_Tool::PutFilesToStream(const char* theFromDirectory,
134                                 const SALOMEDS::ListOfFileNames& theFiles,
135                                 const int theNamesOnly)
136 {
137   int i, aLength = theFiles.length();
138   if(aLength == 0)
139 //    return NULL;
140     return (new SALOMEDS::TMPFile);
141
142   TCollection_AsciiString aTmpDir(const_cast<char*>(theFromDirectory)); //Get a temporary directory for saved a file
143
144   long aBufferSize = 0;
145   long aCurrentPos;
146
147   int aNbFiles = 0;
148   int* aFileNameSize= new int[aLength];
149   long* aFileSize= new long[aLength];
150
151   //Determine the required size of the buffer
152
153   for(i=0; i<aLength; i++) {
154
155     //Check if the file exists
156     
157     if (!theNamesOnly) { // mpv 15.01.2003: if only file names must be stroed, then size of files is zero
158       TCollection_AsciiString aFullPath = aTmpDir + CORBA::string_dup(theFiles[i]);   
159       OSD_Path anOSDPath(aFullPath);
160       OSD_File anOSDFile(anOSDPath);
161       if(!anOSDFile.Exists()) continue;
162 #ifdef WNT
163       ifstream aFile(aFullPath.ToCString(), ios::binary);
164 #else
165       ifstream aFile(aFullPath.ToCString());
166 #endif
167       aFile.seekg(0, ios::end);
168       aFileSize[i] = aFile.tellg();
169       aBufferSize += aFileSize[i];              //Add a space to store the file
170     }
171     aFileNameSize[i] = strlen(theFiles[i])+1;
172     aBufferSize += aFileNameSize[i];          //Add a space to store the file name
173     aBufferSize += (theNamesOnly)?4:12;       //Add 4 bytes: a length of the file name,
174                                               //    8 bytes: length of the file itself
175     aNbFiles++;
176   } 
177
178   aBufferSize += 4;      //4 bytes for a number of the files that will be written to the stream;
179   unsigned char* aBuffer = new unsigned char[aBufferSize];  
180   if(aBuffer == NULL)
181 //    return NULL; 
182     return (new SALOMEDS::TMPFile);
183
184   //Initialize 4 bytes of the buffer by 0
185   memset(aBuffer, 0, 4); 
186   //Copy the number of files that will be written to the stream
187   memcpy(aBuffer, &aNbFiles, ((sizeof(int) > 4) ? 4 : sizeof(int))); 
188
189
190   aCurrentPos = 4;
191
192   for(i=0; i<aLength; i++) {
193     ifstream *aFile;
194     if (!theNamesOnly) { // mpv 15.01.2003: we don't open any file if theNamesOnly = true
195       TCollection_AsciiString aFullPath = aTmpDir + CORBA::string_dup(theFiles[i]);
196       OSD_Path anOSDPath(aFullPath);
197       OSD_File anOSDFile(anOSDPath);
198       if(!anOSDFile.Exists()) continue;
199 #ifdef WNT
200       aFile = new ifstream(aFullPath.ToCString(), ios::binary);
201 #else
202       aFile = new ifstream(aFullPath.ToCString());
203 #endif  
204     }
205     //Initialize 4 bytes of the buffer by 0
206     memset((aBuffer + aCurrentPos), 0, 4); 
207     //Copy the length of the file name to the buffer
208     memcpy((aBuffer + aCurrentPos), (aFileNameSize + i), ((sizeof(int) > 4) ? 4 : sizeof(int))); 
209     aCurrentPos += 4;
210
211     //Copy the file name to the buffer
212     memcpy((aBuffer + aCurrentPos), theFiles[i], aFileNameSize[i]);
213     aCurrentPos += aFileNameSize[i];
214     
215     if (!theNamesOnly) { // mpv 15.01.2003: we don't copy file content to the buffer if !theNamesOnly
216       //Initialize 8 bytes of the buffer by 0
217       memset((aBuffer + aCurrentPos), 0, 8); 
218       //Copy the length of the file to the buffer
219       memcpy((aBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));
220       aCurrentPos += 8;
221       
222       aFile->seekg(0, ios::beg);
223       aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]);
224       aFile->close();
225       delete(aFile);
226       aCurrentPos += aFileSize[i];
227     }
228   }
229
230   delete[] aFileNameSize;
231   delete[] aFileSize;
232   
233   
234   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
235   
236   return (new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1));
237 }
238
239 //============================================================================
240 // function : PutStreamToFile
241 // purpose  : converts the stream "theStream" to the files
242 //============================================================================
243 SALOMEDS::ListOfFileNames_var 
244 SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream,
245                                 const char* theToDirectory,
246                                 const int theNamesOnly)
247 {
248   if(theStream.length() == 0) return NULL;
249   TCollection_AsciiString aTmpDir(const_cast<char*>(theToDirectory)); //Get a temporary directory for saving a file
250
251   unsigned char *aBuffer = (unsigned char*)theStream.NP_data();
252
253   if(aBuffer == NULL) return NULL;
254
255   long aFileSize, aCurrentPos = 4;
256   int i, aFileNameSize, aNbFiles = 0;
257
258   //Copy the number of files in the stream
259   memcpy(&aNbFiles, aBuffer, sizeof(int)); 
260
261   SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
262   aFiles->length(aNbFiles);
263
264   for(i=0; i<aNbFiles; i++) {
265
266     //Put a length of the file name to aFileNameSize
267     memcpy(&aFileNameSize, (aBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int))); 
268     aCurrentPos += 4;
269
270     char *aFileName = new char[aFileNameSize];
271     //Put a file name to aFileName
272     memcpy(aFileName, (aBuffer + aCurrentPos), aFileNameSize); 
273     aCurrentPos += aFileNameSize;
274  
275     //Put a length of the file to aFileSize
276     if (!theNamesOnly) {
277       memcpy(&aFileSize, (aBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
278       aCurrentPos += 8;    
279       
280       TCollection_AsciiString aFullPath = aTmpDir + aFileName;
281       ofstream aFile(aFullPath.ToCString());
282       aFile.write((char *)(aBuffer+aCurrentPos), aFileSize); 
283       aFile.close();  
284       aCurrentPos += aFileSize;
285     }
286     aFiles[i] = CORBA::string_dup(aFileName);
287     delete[] aFileName;
288   }
289
290   return aFiles;
291 }
292
293 //============================================================================
294 // function : GetNameFromPath
295 // purpose  : Returns the name by the path
296 //============================================================================
297 std::string SALOMEDS_Tool::GetNameFromPath(const char* thePath) {
298   if (thePath == NULL) return string("");
299   OSD_Path aPath = OSD_Path(TCollection_AsciiString((char*)thePath));
300   TCollection_AsciiString aNameString(aPath.Name());
301   return aNameString.ToCString();
302 }
303
304 //============================================================================
305 // function : GetDirFromPath
306 // purpose  : Returns the dir by the path
307 //============================================================================
308 std::string SALOMEDS_Tool::GetDirFromPath(const char* thePath) {
309   if (thePath == NULL) return string("");
310   OSD_Path aPath = OSD_Path(TCollection_AsciiString((char*)thePath));
311   TCollection_AsciiString aDirString(aPath.Trek());
312   aDirString.ChangeAll('|','/');
313   return aDirString.ToCString();
314 }
315
316 //=======================================================================
317 // name    : GetFlag
318 // Purpose : Retrieve specified flaf from "AttributeFlags" attribute
319 //=======================================================================
320 bool SALOMEDS_Tool::GetFlag( const int             theFlag,
321                              SALOMEDS::Study_var   theStudy,
322                              SALOMEDS::SObject_var theObj )
323 {
324   SALOMEDS::GenericAttribute_var anAttr;
325   if ( !theObj->_is_nil() && theObj->FindAttribute( anAttr, "AttributeFlags" ) )
326   {
327     SALOMEDS::AttributeFlags_var aFlags = SALOMEDS::AttributeFlags::_narrow( anAttr );
328     return aFlags->Get( theFlag );
329   }
330
331   return false;
332 }
333
334 //=======================================================================
335 // name    : SetFlag
336 // Purpose : Set/Unset specified flaf from "AttributeFlags" attribute
337 //=======================================================================
338 bool SALOMEDS_Tool::SetFlag( const int           theFlag,
339                              SALOMEDS::Study_var theStudy,
340                              const char*         theEntry,
341                              const bool          theValue )
342 {
343   SALOMEDS::SObject_var anObj = theStudy->FindObjectID( theEntry );
344
345   if ( !anObj->_is_nil() )
346   {
347     SALOMEDS::GenericAttribute_var aGAttr;
348     if ( anObj->FindAttribute( aGAttr, "AttributeFlags" ) )
349     {
350       SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow( aGAttr );
351       anAttr->Set( theFlag, theValue );
352     }
353     else if ( theValue )
354     {
355       SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
356       SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow(
357         aBuilder->FindOrCreateAttribute( anObj, "AttributeFlags" ) );
358       anAttr->Set( theFlag, theValue );
359     }
360     return true;
361   }
362
363   return false;
364 }
365
366 //=======================================================================
367 // name    : getAllChildren
368 // Purpose : Get all children of object.
369 //           If theObj is null all objects of study are returned
370 //=======================================================================
371 void SALOMEDS_Tool::GetAllChildren( SALOMEDS::Study_var               theStudy,
372                                     SALOMEDS::SObject_var             theObj,
373                                     std::list<SALOMEDS::SObject_var>& theList )
374 {
375   if ( theObj->_is_nil() )
376   {
377     SALOMEDS::SComponentIterator_var anIter = theStudy->NewComponentIterator();
378     for ( ; anIter->More(); anIter->Next() )
379     {
380       SALOMEDS::SObject_var anObj = SALOMEDS::SObject::_narrow( anIter->Value() );
381       if ( !anObj->_is_nil() )
382       {
383         theList.push_back( anObj );
384         GetAllChildren( theStudy, anObj, theList );
385       }
386     }
387   }
388   else
389   {
390     SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theObj );
391     for ( ; anIter->More(); anIter->Next() )
392     {
393       SALOMEDS::SObject_var anObj = anIter->Value();
394       SALOMEDS::SObject_var aRef;
395       if ( !anObj->ReferencedObject( aRef ) )
396       {
397         theList.push_back( anObj );
398         GetAllChildren( theStudy, anObj, theList );
399       }
400     }
401   }
402 }
403
404
405
406
407
408
409
410
411
412
413