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 <SALOMEconfig.h>
52 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
56 //============================================================================
57 // function : GetTempDir
58 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
59 //============================================================================
60 std::string SALOMEDS_Tool::GetTmpDir()
62 //Find a temporary directory to store a file
64 TCollection_AsciiString aTmpDir;
66 char *Tmp_dir = getenv("SALOME_TMP_DIR");
68 aTmpDir = TCollection_AsciiString(Tmp_dir);
70 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
72 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
77 aTmpDir = TCollection_AsciiString("C:\\");
79 aTmpDir = TCollection_AsciiString("/tmp/");
83 srand((unsigned int)time(NULL));
84 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
85 TCollection_AsciiString aSubDir(aRND);
86 if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
88 aTmpDir += aSubDir; //Get RND sub directory
91 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
93 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
96 OSD_Path aPath(aTmpDir);
97 OSD_Directory aDir(aPath);
99 for(aRND = 0; aDir.Exists(); aRND++) {
100 aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name
101 aPath = OSD_Path(aTmpDir);
102 aDir = OSD_Directory(aPath);
105 MESSAGE("#### TMP" << aTmpDir.ToCString());
107 //OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
108 OSD_Protection aProtection(OSD_RWXD, OSD_RWX, OSD_RX, OSD_RX );
109 aDir.Build(aProtection);
111 return aTmpDir.ToCString();
114 //============================================================================
115 // function : RemoveTemporaryFiles
116 // purpose : Removes files listed in theFileList
117 //============================================================================
118 void SALOMEDS_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
119 const SALOMEDS::ListOfFileNames& theFiles,
120 const bool IsDirDeleted)
122 TCollection_AsciiString aDirName(const_cast<char*>(theDirectory.c_str()));
124 int i, aLength = theFiles.length();
125 for(i=0; i<aLength; i++) {
126 TCollection_AsciiString aFile(aDirName);
127 // aFile += (char*)theFiles[i];
128 aFile += (char*)theFiles[i].in();
129 OSD_Path anOSDPath(aFile);
130 OSD_File anOSDFile(anOSDPath);
131 if(!anOSDFile.Exists()) continue;
133 OSD_Protection aProtection = anOSDFile.Protection();
134 aProtection.SetUser(OSD_RWD);
135 //aProtection.SetSystem(OSD_RW);
136 anOSDFile.SetProtection(aProtection);
142 OSD_Path aPath(aDirName);
143 OSD_Directory aDir(aPath);
144 OSD_FileIterator* anIterator = new OSD_FileIterator(aPath, '*');
146 if(aDir.Exists() && !anIterator->More())
155 //============================================================================
156 // function : PutFilesToStream
157 // purpose : converts the files from a list 'theFiles' to the stream
158 //============================================================================
162 PutFilesToStream(const std::string& theFromDirectory,
163 const SALOMEDS::ListOfFileNames& theFiles,
164 const SALOMEDS::ListOfFileNames& theFileNames,
165 const int theNamesOnly)
167 int i, aLength = theFiles.length();
169 return (new SALOMEDS::TMPFile);
171 //Get a temporary directory for saved a file
172 TCollection_AsciiString aTmpDir(const_cast<char*>(theFromDirectory.c_str()));
174 long aBufferSize = 0;
178 int* aFileNameSize= new int[aLength];
179 long* aFileSize= new long[aLength];
181 //Determine the required size of the buffer
183 for(i=0; i<aLength; i++) {
185 //Check if the file exists
187 if (!theNamesOnly) { // mpv 15.01.2003: if only file names must be stroed, then size of files is zero
188 TCollection_AsciiString aFullPath = aTmpDir + const_cast<char*>(theFiles[i].in());
189 OSD_Path anOSDPath(aFullPath);
190 OSD_File anOSDFile(anOSDPath);
191 if(!anOSDFile.Exists()) continue;
193 ifstream aFile(aFullPath.ToCString(), ios::binary);
195 ifstream aFile(aFullPath.ToCString());
197 aFile.seekg(0, ios::end);
198 aFileSize[i] = aFile.tellg();
199 aBufferSize += aFileSize[i]; //Add a space to store the file
201 aFileNameSize[i] = strlen(theFileNames[i])+1;
202 aBufferSize += aFileNameSize[i]; //Add a space to store the file name
203 aBufferSize += (theNamesOnly)?4:12; //Add 4 bytes: a length of the file name,
204 // 8 bytes: length of the file itself
208 aBufferSize += 4; //4 bytes for a number of the files that will be written to the stream;
209 unsigned char* aBuffer = new unsigned char[aBufferSize];
211 return (new SALOMEDS::TMPFile);
213 //Initialize 4 bytes of the buffer by 0
214 memset(aBuffer, 0, 4);
215 //Copy the number of files that will be written to the stream
216 memcpy(aBuffer, &aNbFiles, ((sizeof(int) > 4) ? 4 : sizeof(int)));
221 for(i=0; i<aLength; i++) {
223 if (!theNamesOnly) { // mpv 15.01.2003: we don't open any file if theNamesOnly = true
224 TCollection_AsciiString aFullPath = aTmpDir + const_cast<char*>(theFiles[i].in());
225 OSD_Path anOSDPath(aFullPath);
226 OSD_File anOSDFile(anOSDPath);
227 if(!anOSDFile.Exists()) continue;
229 aFile = new ifstream(aFullPath.ToCString(), ios::binary);
231 aFile = new ifstream(aFullPath.ToCString());
234 //Initialize 4 bytes of the buffer by 0
235 memset((aBuffer + aCurrentPos), 0, 4);
236 //Copy the length of the file name to the buffer
237 memcpy((aBuffer + aCurrentPos), (aFileNameSize + i), ((sizeof(int) > 4) ? 4 : sizeof(int)));
240 //Copy the file name to the buffer
241 memcpy((aBuffer + aCurrentPos), theFileNames[i], aFileNameSize[i]);
242 aCurrentPos += aFileNameSize[i];
244 if (!theNamesOnly) { // mpv 15.01.2003: we don't copy file content to the buffer if !theNamesOnly
245 //Initialize 8 bytes of the buffer by 0
246 memset((aBuffer + aCurrentPos), 0, 8);
247 //Copy the length of the file to the buffer
248 memcpy((aBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));
251 aFile->seekg(0, ios::beg);
252 aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]);
255 aCurrentPos += aFileSize[i];
259 delete[] aFileNameSize;
263 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
265 return (new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1));
272 SALOMEDS_Tool::PutFilesToStream(const std::string& theFromDirectory,
273 const SALOMEDS::ListOfFileNames& theFiles,
274 const int theNamesOnly)
276 SALOMEDS::ListOfFileNames aFileNames(theFiles);
277 return ::PutFilesToStream(theFromDirectory,theFiles,aFileNames,theNamesOnly);
282 SALOMEDS_Tool::PutFilesToStream(const SALOMEDS::ListOfFileNames& theFiles,
283 const SALOMEDS::ListOfFileNames& theFileNames)
285 return ::PutFilesToStream("",theFiles,theFileNames,0);
288 //============================================================================
289 // function : PutStreamToFile
290 // purpose : converts the stream "theStream" to the files
291 //============================================================================
292 SALOMEDS::ListOfFileNames_var
293 SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream,
294 const std::string& theToDirectory,
295 const int theNamesOnly)
297 SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
299 if(theStream.length() == 0)
302 //Get a temporary directory for saving a file
303 TCollection_AsciiString aTmpDir(const_cast<char*>(theToDirectory.c_str()));
305 unsigned char *aBuffer = (unsigned char*)theStream.NP_data();
310 long aFileSize, aCurrentPos = 4;
311 int i, aFileNameSize, aNbFiles = 0;
313 //Copy the number of files in the stream
314 memcpy(&aNbFiles, aBuffer, sizeof(int));
316 aFiles->length(aNbFiles);
318 for(i=0; i<aNbFiles; i++) {
320 //Put a length of the file name to aFileNameSize
321 memcpy(&aFileNameSize, (aBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int)));
324 char *aFileName = new char[aFileNameSize];
325 //Put a file name to aFileName
326 memcpy(aFileName, (aBuffer + aCurrentPos), aFileNameSize);
327 aCurrentPos += aFileNameSize;
329 //Put a length of the file to aFileSize
331 memcpy(&aFileSize, (aBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
334 TCollection_AsciiString aFullPath = aTmpDir + aFileName;
336 ofstream aFile(aFullPath.ToCString(), ios::binary);
338 ofstream aFile(aFullPath.ToCString());
340 aFile.write((char *)(aBuffer+aCurrentPos), aFileSize);
342 aCurrentPos += aFileSize;
344 aFiles[i] = CORBA::string_dup(aFileName);
351 //============================================================================
352 // function : GetNameFromPath
353 // purpose : Returns the name by the path
354 //============================================================================
355 std::string SALOMEDS_Tool::GetNameFromPath(const std::string& thePath) {
358 OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast<char*>(thePath.c_str())));
359 TCollection_AsciiString aNameString(aPath.Name());
360 return aNameString.ToCString();
363 //============================================================================
364 // function : GetDirFromPath
365 // purpose : Returns the dir by the path
366 //============================================================================
367 std::string SALOMEDS_Tool::GetDirFromPath(const std::string& thePath) {
370 OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast<char*>(thePath.c_str())));
371 TCollection_AsciiString aDirString(aPath.Trek());
372 aDirString.ChangeAll('|','/');
373 return aDirString.ToCString();
376 //=======================================================================
378 // Purpose : Retrieve specified flaf from "AttributeFlags" attribute
379 //=======================================================================
380 bool SALOMEDS_Tool::GetFlag( const int theFlag,
381 SALOMEDS::Study_var theStudy,
382 SALOMEDS::SObject_var theObj )
384 SALOMEDS::GenericAttribute_var anAttr;
385 if ( !theObj->_is_nil() && theObj->FindAttribute( anAttr, "AttributeFlags" ) )
387 SALOMEDS::AttributeFlags_var aFlags = SALOMEDS::AttributeFlags::_narrow( anAttr );
388 return aFlags->Get( theFlag );
394 //=======================================================================
396 // Purpose : Set/Unset specified flaf from "AttributeFlags" attribute
397 //=======================================================================
398 bool SALOMEDS_Tool::SetFlag( const int theFlag,
399 SALOMEDS::Study_var theStudy,
400 const std::string& theEntry,
401 const bool theValue )
403 SALOMEDS::SObject_var anObj = theStudy->FindObjectID(theEntry.c_str());
405 if ( !anObj->_is_nil() )
407 SALOMEDS::GenericAttribute_var aGAttr;
408 if ( anObj->FindAttribute( aGAttr, "AttributeFlags" ) )
410 SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow( aGAttr );
411 anAttr->Set( theFlag, theValue );
415 SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
416 SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow(
417 aBuilder->FindOrCreateAttribute( anObj, "AttributeFlags" ) );
418 anAttr->Set( theFlag, theValue );
426 //=======================================================================
427 // name : getAllChildren
428 // Purpose : Get all children of object.
429 // If theObj is null all objects of study are returned
430 //=======================================================================
431 void SALOMEDS_Tool::GetAllChildren( SALOMEDS::Study_var theStudy,
432 SALOMEDS::SObject_var theObj,
433 std::list<SALOMEDS::SObject_var>& theList )
435 if ( theObj->_is_nil() )
437 SALOMEDS::SComponentIterator_var anIter = theStudy->NewComponentIterator();
438 for ( ; anIter->More(); anIter->Next() )
440 SALOMEDS::SObject_var anObj = SALOMEDS::SObject::_narrow( anIter->Value() );
441 if ( !anObj->_is_nil() )
443 theList.push_back( anObj );
444 GetAllChildren( theStudy, anObj, theList );
450 SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theObj );
451 for ( ; anIter->More(); anIter->Next() )
453 SALOMEDS::SObject_var anObj = anIter->Value();
454 SALOMEDS::SObject_var aRef;
455 if ( !anObj->ReferencedObject( aRef ) )
457 theList.push_back( anObj );
458 GetAllChildren( theStudy, anObj, theList );