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/
20 // File : SALOMEDS_Tool.cxx
21 // Created : Mon Oct 21 16:24:34 2002
22 // Author : Sergey RUIN
26 // Copyright : Open CASCADE
28 #include "SALOMEDS_Tool.hxx"
30 #include "utilities.h"
32 #include <TCollection_AsciiString.hxx>
34 #include <OSD_Path.hxx>
35 #include <OSD_File.hxx>
36 #include <OSD_Directory.hxx>
37 #include <OSD_Process.hxx>
38 #include <OSD_Directory.hxx>
39 #include <OSD_Protection.hxx>
40 #include <OSD_SingleProtection.hxx>
41 #include <OSD_FileIterator.hxx>
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 aDir.Build(aProtection);
110 return aTmpDir.ToCString();
113 //============================================================================
114 // function : RemoveTemporaryFiles
115 // purpose : Removes files listed in theFileList
116 //============================================================================
117 void SALOMEDS_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
118 const SALOMEDS::ListOfFileNames& theFiles,
119 const bool IsDirDeleted)
121 TCollection_AsciiString aDirName(const_cast<char*>(theDirectory.c_str()));
123 int i, aLength = theFiles.length();
124 for(i=0; i<aLength; i++) {
125 TCollection_AsciiString aFile(aDirName);
126 // aFile += (char*)theFiles[i];
127 aFile += (char*)theFiles[i].in();
128 OSD_Path anOSDPath(aFile);
129 OSD_File anOSDFile(anOSDPath);
130 if(!anOSDFile.Exists()) continue;
132 OSD_Protection aProtection = anOSDFile.Protection();
133 aProtection.SetUser(OSD_RW);
134 anOSDFile.SetProtection(aProtection);
140 OSD_Path aPath(aDirName);
141 OSD_Directory aDir(aPath);
142 OSD_FileIterator anIterator(aPath, '*');
144 if(aDir.Exists() && !anIterator.More()) aDir.Remove();
149 //============================================================================
150 // function : PutFilesToStream
151 // purpose : converts the files from a list 'theFiles' to the stream
152 //============================================================================
154 SALOMEDS_Tool::PutFilesToStream(const std::string& theFromDirectory,
155 const SALOMEDS::ListOfFileNames& theFiles,
156 const int theNamesOnly)
158 int i, aLength = theFiles.length();
161 return (new SALOMEDS::TMPFile);
163 //Get a temporary directory for saved a file
164 TCollection_AsciiString aTmpDir(const_cast<char*>(theFromDirectory.c_str()));
166 long aBufferSize = 0;
170 int* aFileNameSize= new int[aLength];
171 long* aFileSize= new long[aLength];
173 //Determine the required size of the buffer
175 for(i=0; i<aLength; i++) {
177 //Check if the file exists
179 if (!theNamesOnly) { // mpv 15.01.2003: if only file names must be stroed, then size of files is zero
180 TCollection_AsciiString aFullPath = aTmpDir + CORBA::string_dup(theFiles[i]);
181 OSD_Path anOSDPath(aFullPath);
182 OSD_File anOSDFile(anOSDPath);
183 if(!anOSDFile.Exists()) continue;
185 ifstream aFile(aFullPath.ToCString(), ios::binary);
187 ifstream aFile(aFullPath.ToCString());
189 aFile.seekg(0, ios::end);
190 aFileSize[i] = aFile.tellg();
191 aBufferSize += aFileSize[i]; //Add a space to store the file
193 aFileNameSize[i] = strlen(theFiles[i])+1;
194 aBufferSize += aFileNameSize[i]; //Add a space to store the file name
195 aBufferSize += (theNamesOnly)?4:12; //Add 4 bytes: a length of the file name,
196 // 8 bytes: length of the file itself
200 aBufferSize += 4; //4 bytes for a number of the files that will be written to the stream;
201 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 + CORBA::string_dup(theFiles[i]);
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), theFiles[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));
261 //============================================================================
262 // function : PutStreamToFile
263 // purpose : converts the stream "theStream" to the files
264 //============================================================================
265 SALOMEDS::ListOfFileNames_var
266 SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream,
267 const std::string& theToDirectory,
268 const int theNamesOnly)
270 if(theStream.length() == 0)
273 //Get a temporary directory for saving a file
274 TCollection_AsciiString aTmpDir(const_cast<char*>(theToDirectory.c_str()));
276 unsigned char *aBuffer = (unsigned char*)theStream.NP_data();
278 if(aBuffer == NULL) return NULL;
280 long aFileSize, aCurrentPos = 4;
281 int i, aFileNameSize, aNbFiles = 0;
283 //Copy the number of files in the stream
284 memcpy(&aNbFiles, aBuffer, sizeof(int));
286 SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
287 aFiles->length(aNbFiles);
289 for(i=0; i<aNbFiles; i++) {
291 //Put a length of the file name to aFileNameSize
292 memcpy(&aFileNameSize, (aBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int)));
295 char *aFileName = new char[aFileNameSize];
296 //Put a file name to aFileName
297 memcpy(aFileName, (aBuffer + aCurrentPos), aFileNameSize);
298 aCurrentPos += aFileNameSize;
300 //Put a length of the file to aFileSize
302 memcpy(&aFileSize, (aBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
305 TCollection_AsciiString aFullPath = aTmpDir + aFileName;
306 ofstream aFile(aFullPath.ToCString());
307 aFile.write((char *)(aBuffer+aCurrentPos), aFileSize);
309 aCurrentPos += aFileSize;
311 aFiles[i] = CORBA::string_dup(aFileName);
318 //============================================================================
319 // function : GetNameFromPath
320 // purpose : Returns the name by the path
321 //============================================================================
322 std::string SALOMEDS_Tool::GetNameFromPath(const std::string& thePath) {
325 OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast<char*>(thePath.c_str())));
326 TCollection_AsciiString aNameString(aPath.Name());
327 return aNameString.ToCString();
330 //============================================================================
331 // function : GetDirFromPath
332 // purpose : Returns the dir by the path
333 //============================================================================
334 std::string SALOMEDS_Tool::GetDirFromPath(const std::string& thePath) {
337 OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast<char*>(thePath.c_str())));
338 TCollection_AsciiString aDirString(aPath.Trek());
339 aDirString.ChangeAll('|','/');
340 return aDirString.ToCString();
343 //=======================================================================
345 // Purpose : Retrieve specified flaf from "AttributeFlags" attribute
346 //=======================================================================
347 bool SALOMEDS_Tool::GetFlag( const int theFlag,
348 SALOMEDS::Study_var theStudy,
349 SALOMEDS::SObject_var theObj )
351 SALOMEDS::GenericAttribute_var anAttr;
352 if ( !theObj->_is_nil() && theObj->FindAttribute( anAttr, "AttributeFlags" ) )
354 SALOMEDS::AttributeFlags_var aFlags = SALOMEDS::AttributeFlags::_narrow( anAttr );
355 return aFlags->Get( theFlag );
361 //=======================================================================
363 // Purpose : Set/Unset specified flaf from "AttributeFlags" attribute
364 //=======================================================================
365 bool SALOMEDS_Tool::SetFlag( const int theFlag,
366 SALOMEDS::Study_var theStudy,
367 const std::string& theEntry,
368 const bool theValue )
370 SALOMEDS::SObject_var anObj = theStudy->FindObjectID(theEntry.c_str());
372 if ( !anObj->_is_nil() )
374 SALOMEDS::GenericAttribute_var aGAttr;
375 if ( anObj->FindAttribute( aGAttr, "AttributeFlags" ) )
377 SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow( aGAttr );
378 anAttr->Set( theFlag, theValue );
382 SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
383 SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow(
384 aBuilder->FindOrCreateAttribute( anObj, "AttributeFlags" ) );
385 anAttr->Set( theFlag, theValue );
393 //=======================================================================
394 // name : getAllChildren
395 // Purpose : Get all children of object.
396 // If theObj is null all objects of study are returned
397 //=======================================================================
398 void SALOMEDS_Tool::GetAllChildren( SALOMEDS::Study_var theStudy,
399 SALOMEDS::SObject_var theObj,
400 std::list<SALOMEDS::SObject_var>& theList )
402 if ( theObj->_is_nil() )
404 SALOMEDS::SComponentIterator_var anIter = theStudy->NewComponentIterator();
405 for ( ; anIter->More(); anIter->Next() )
407 SALOMEDS::SObject_var anObj = SALOMEDS::SObject::_narrow( anIter->Value() );
408 if ( !anObj->_is_nil() )
410 theList.push_back( anObj );
411 GetAllChildren( theStudy, anObj, theList );
417 SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theObj );
418 for ( ; anIter->More(); anIter->Next() )
420 SALOMEDS::SObject_var anObj = anIter->Value();
421 SALOMEDS::SObject_var aRef;
422 if ( !anObj->ReferencedObject( aRef ) )
424 theList.push_back( anObj );
425 GetAllChildren( theStudy, anObj, theList );