1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDS_Tool.cxx
21 // Created : Mon Oct 21 16:24:34 2002
22 // Author : Sergey RUIN
27 #include "SALOMEDS_Tool.hxx"
29 #include "utilities.h"
31 #include <TCollection_AsciiString.hxx>
33 #include <OSD_Path.hxx>
34 #include <OSD_File.hxx>
35 #include <OSD_Directory.hxx>
36 #include <OSD_Process.hxx>
37 #include <OSD_Directory.hxx>
38 #include <OSD_Protection.hxx>
39 #include <OSD_SingleProtection.hxx>
40 #include <OSD_FileIterator.hxx>
51 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
55 //============================================================================
56 // function : GetTempDir
57 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
58 //============================================================================
59 std::string SALOMEDS_Tool::GetTmpDir()
61 //Find a temporary directory to store a file
63 TCollection_AsciiString aTmpDir;
65 char *Tmp_dir = getenv("SALOME_TMP_DIR");
67 aTmpDir = TCollection_AsciiString(Tmp_dir);
69 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
71 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
76 aTmpDir = TCollection_AsciiString("C:\\");
78 aTmpDir = TCollection_AsciiString("/tmp/");
82 srand((unsigned int)time(NULL));
83 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
84 TCollection_AsciiString aSubDir(aRND);
85 if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
87 aTmpDir += aSubDir; //Get RND sub directory
90 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
92 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
95 OSD_Path aPath(aTmpDir);
96 OSD_Directory aDir(aPath);
98 for(aRND = 0; aDir.Exists(); aRND++) {
99 aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name
100 aPath = OSD_Path(aTmpDir);
101 aDir = OSD_Directory(aPath);
104 MESSAGE("#### TMP" << aTmpDir.ToCString());
106 OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
107 aDir.Build(aProtection);
109 return aTmpDir.ToCString();
112 //============================================================================
113 // function : RemoveTemporaryFiles
114 // purpose : Removes files listed in theFileList
115 //============================================================================
116 void SALOMEDS_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
117 const SALOMEDS::ListOfFileNames& theFiles,
118 const bool IsDirDeleted)
120 TCollection_AsciiString aDirName(const_cast<char*>(theDirectory.c_str()));
122 int i, aLength = theFiles.length();
123 for(i=0; i<aLength; i++) {
124 TCollection_AsciiString aFile(aDirName);
125 // aFile += (char*)theFiles[i];
126 aFile += (char*)theFiles[i].in();
127 OSD_Path anOSDPath(aFile);
128 OSD_File anOSDFile(anOSDPath);
129 if(!anOSDFile.Exists()) continue;
131 OSD_Protection aProtection = anOSDFile.Protection();
132 aProtection.SetUser(OSD_RW);
133 anOSDFile.SetProtection(aProtection);
139 OSD_Path aPath(aDirName);
140 OSD_Directory aDir(aPath);
141 OSD_FileIterator anIterator(aPath, '*');
143 if(aDir.Exists() && !anIterator.More()) aDir.Remove();
148 //============================================================================
149 // function : PutFilesToStream
150 // purpose : converts the files from a list 'theFiles' to the stream
151 //============================================================================
155 PutFilesToStream(const std::string& theFromDirectory,
156 const SALOMEDS::ListOfFileNames& theFiles,
157 const SALOMEDS::ListOfFileNames& theFileNames,
158 const int theNamesOnly)
160 int i, aLength = theFiles.length();
162 return (new SALOMEDS::TMPFile);
164 //Get a temporary directory for saved a file
165 TCollection_AsciiString aTmpDir(const_cast<char*>(theFromDirectory.c_str()));
167 long aBufferSize = 0;
171 int* aFileNameSize= new int[aLength];
172 long* aFileSize= new long[aLength];
174 //Determine the required size of the buffer
176 for(i=0; i<aLength; i++) {
178 //Check if the file exists
180 if (!theNamesOnly) { // mpv 15.01.2003: if only file names must be stroed, then size of files is zero
181 TCollection_AsciiString aFullPath = aTmpDir + const_cast<char*>(theFiles[i].in());
182 OSD_Path anOSDPath(aFullPath);
183 OSD_File anOSDFile(anOSDPath);
184 if(!anOSDFile.Exists()) continue;
186 ifstream aFile(aFullPath.ToCString(), ios::binary);
188 ifstream aFile(aFullPath.ToCString());
190 aFile.seekg(0, ios::end);
191 aFileSize[i] = aFile.tellg();
192 aBufferSize += aFileSize[i]; //Add a space to store the file
194 aFileNameSize[i] = strlen(theFileNames[i])+1;
195 aBufferSize += aFileNameSize[i]; //Add a space to store the file name
196 aBufferSize += (theNamesOnly)?4:12; //Add 4 bytes: a length of the file name,
197 // 8 bytes: length of the file itself
201 aBufferSize += 4; //4 bytes for a number of the files that will be written to the stream;
202 unsigned char* aBuffer = new unsigned char[aBufferSize];
204 return (new SALOMEDS::TMPFile);
206 //Initialize 4 bytes of the buffer by 0
207 memset(aBuffer, 0, 4);
208 //Copy the number of files that will be written to the stream
209 memcpy(aBuffer, &aNbFiles, ((sizeof(int) > 4) ? 4 : sizeof(int)));
214 for(i=0; i<aLength; i++) {
216 if (!theNamesOnly) { // mpv 15.01.2003: we don't open any file if theNamesOnly = true
217 TCollection_AsciiString aFullPath = aTmpDir + const_cast<char*>(theFiles[i].in());
218 OSD_Path anOSDPath(aFullPath);
219 OSD_File anOSDFile(anOSDPath);
220 if(!anOSDFile.Exists()) continue;
222 aFile = new ifstream(aFullPath.ToCString(), ios::binary);
224 aFile = new ifstream(aFullPath.ToCString());
227 //Initialize 4 bytes of the buffer by 0
228 memset((aBuffer + aCurrentPos), 0, 4);
229 //Copy the length of the file name to the buffer
230 memcpy((aBuffer + aCurrentPos), (aFileNameSize + i), ((sizeof(int) > 4) ? 4 : sizeof(int)));
233 //Copy the file name to the buffer
234 memcpy((aBuffer + aCurrentPos), theFileNames[i], aFileNameSize[i]);
235 aCurrentPos += aFileNameSize[i];
237 if (!theNamesOnly) { // mpv 15.01.2003: we don't copy file content to the buffer if !theNamesOnly
238 //Initialize 8 bytes of the buffer by 0
239 memset((aBuffer + aCurrentPos), 0, 8);
240 //Copy the length of the file to the buffer
241 memcpy((aBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));
244 aFile->seekg(0, ios::beg);
245 aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]);
248 aCurrentPos += aFileSize[i];
252 delete[] aFileNameSize;
256 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
258 return (new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1));
265 SALOMEDS_Tool::PutFilesToStream(const std::string& theFromDirectory,
266 const SALOMEDS::ListOfFileNames& theFiles,
267 const int theNamesOnly)
269 SALOMEDS::ListOfFileNames aFileNames(theFiles);
270 return ::PutFilesToStream(theFromDirectory,theFiles,aFileNames,theNamesOnly);
275 SALOMEDS_Tool::PutFilesToStream(const SALOMEDS::ListOfFileNames& theFiles,
276 const SALOMEDS::ListOfFileNames& theFileNames)
278 return ::PutFilesToStream("",theFiles,theFileNames,0);
281 //============================================================================
282 // function : PutStreamToFile
283 // purpose : converts the stream "theStream" to the files
284 //============================================================================
285 SALOMEDS::ListOfFileNames_var
286 SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream,
287 const std::string& theToDirectory,
288 const int theNamesOnly)
290 SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
292 if(theStream.length() == 0)
295 //Get a temporary directory for saving a file
296 TCollection_AsciiString aTmpDir(const_cast<char*>(theToDirectory.c_str()));
298 unsigned char *aBuffer = (unsigned char*)theStream.NP_data();
303 long aFileSize, aCurrentPos = 4;
304 int i, aFileNameSize, aNbFiles = 0;
306 //Copy the number of files in the stream
307 memcpy(&aNbFiles, aBuffer, sizeof(int));
309 aFiles->length(aNbFiles);
311 for(i=0; i<aNbFiles; i++) {
313 //Put a length of the file name to aFileNameSize
314 memcpy(&aFileNameSize, (aBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int)));
317 char *aFileName = new char[aFileNameSize];
318 //Put a file name to aFileName
319 memcpy(aFileName, (aBuffer + aCurrentPos), aFileNameSize);
320 aCurrentPos += aFileNameSize;
322 //Put a length of the file to aFileSize
324 memcpy(&aFileSize, (aBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
327 TCollection_AsciiString aFullPath = aTmpDir + aFileName;
328 ofstream aFile(aFullPath.ToCString());
329 aFile.write((char *)(aBuffer+aCurrentPos), aFileSize);
331 aCurrentPos += aFileSize;
333 aFiles[i] = CORBA::string_dup(aFileName);
340 //============================================================================
341 // function : GetNameFromPath
342 // purpose : Returns the name by the path
343 //============================================================================
344 std::string SALOMEDS_Tool::GetNameFromPath(const std::string& thePath) {
347 OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast<char*>(thePath.c_str())));
348 TCollection_AsciiString aNameString(aPath.Name());
349 return aNameString.ToCString();
352 //============================================================================
353 // function : GetDirFromPath
354 // purpose : Returns the dir by the path
355 //============================================================================
356 std::string SALOMEDS_Tool::GetDirFromPath(const std::string& thePath) {
359 OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast<char*>(thePath.c_str())));
360 TCollection_AsciiString aDirString(aPath.Trek());
361 aDirString.ChangeAll('|','/');
362 return aDirString.ToCString();
365 //=======================================================================
367 // Purpose : Retrieve specified flaf from "AttributeFlags" attribute
368 //=======================================================================
369 bool SALOMEDS_Tool::GetFlag( const int theFlag,
370 SALOMEDS::Study_var theStudy,
371 SALOMEDS::SObject_var theObj )
373 SALOMEDS::GenericAttribute_var anAttr;
374 if ( !theObj->_is_nil() && theObj->FindAttribute( anAttr, "AttributeFlags" ) )
376 SALOMEDS::AttributeFlags_var aFlags = SALOMEDS::AttributeFlags::_narrow( anAttr );
377 return aFlags->Get( theFlag );
383 //=======================================================================
385 // Purpose : Set/Unset specified flaf from "AttributeFlags" attribute
386 //=======================================================================
387 bool SALOMEDS_Tool::SetFlag( const int theFlag,
388 SALOMEDS::Study_var theStudy,
389 const std::string& theEntry,
390 const bool theValue )
392 SALOMEDS::SObject_var anObj = theStudy->FindObjectID(theEntry.c_str());
394 if ( !anObj->_is_nil() )
396 SALOMEDS::GenericAttribute_var aGAttr;
397 if ( anObj->FindAttribute( aGAttr, "AttributeFlags" ) )
399 SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow( aGAttr );
400 anAttr->Set( theFlag, theValue );
404 SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
405 SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow(
406 aBuilder->FindOrCreateAttribute( anObj, "AttributeFlags" ) );
407 anAttr->Set( theFlag, theValue );
415 //=======================================================================
416 // name : getAllChildren
417 // Purpose : Get all children of object.
418 // If theObj is null all objects of study are returned
419 //=======================================================================
420 void SALOMEDS_Tool::GetAllChildren( SALOMEDS::Study_var theStudy,
421 SALOMEDS::SObject_var theObj,
422 std::list<SALOMEDS::SObject_var>& theList )
424 if ( theObj->_is_nil() )
426 SALOMEDS::SComponentIterator_var anIter = theStudy->NewComponentIterator();
427 for ( ; anIter->More(); anIter->Next() )
429 SALOMEDS::SObject_var anObj = SALOMEDS::SObject::_narrow( anIter->Value() );
430 if ( !anObj->_is_nil() )
432 theList.push_back( anObj );
433 GetAllChildren( theStudy, anObj, theList );
439 SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theObj );
440 for ( ; anIter->More(); anIter->Next() )
442 SALOMEDS::SObject_var anObj = anIter->Value();
443 SALOMEDS::SObject_var aRef;
444 if ( !anObj->ReferencedObject( aRef ) )
446 theList.push_back( anObj );
447 GetAllChildren( theStudy, anObj, theList );