1 // File : SALOMEDS_Tool.cxx
2 // Created : Mon Oct 21 16:24:34 2002
3 // Author : Sergey RUIN
7 // Copyright : Open CASCADE
9 #include "SALOMEDS_Tool.hxx"
11 #include "utilities.h"
13 #include <TCollection_AsciiString.hxx>
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>
29 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
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()
39 //Find a temporary directory to store a file
41 TCollection_AsciiString aTmpDir;
43 char *Tmp_dir = getenv("SALOME_TMP_DIR");
45 aTmpDir = TCollection_AsciiString(Tmp_dir);
47 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
49 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
54 aTmpDir = TCollection_AsciiString("C:\\");
56 aTmpDir = TCollection_AsciiString("/tmp/");
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");
65 MESSAGE("#### RND " << aRND);
67 aTmpDir += aSubDir; //Get RND sub directory
70 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
72 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
75 OSD_Path aPath(aTmpDir);
76 OSD_Directory aDir(aPath);
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);
84 MESSAGE("#### TMP" << aTmpDir.ToCString());
86 OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
87 aDir.Build(aProtection);
89 return aTmpDir.ToCString();
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)
100 TCollection_AsciiString aDirName(const_cast<char*>(theDirectory));
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;
111 OSD_Protection aProtection = anOSDFile.Protection();
112 aProtection.SetUser(OSD_RW);
113 anOSDFile.SetProtection(aProtection);
119 OSD_Path aPath(aDirName);
120 OSD_Directory aDir(aPath);
121 OSD_FileIterator anIterator(aPath, '*');
123 if(aDir.Exists() && !anIterator.More()) aDir.Remove();
128 //============================================================================
129 // function : PutFilesToStream
130 // purpose : converts the files from a list 'theFiles' to the stream
131 //============================================================================
133 SALOMEDS_Tool::PutFilesToStream(const char* theFromDirectory,
134 const SALOMEDS::ListOfFileNames& theFiles,
135 const int theNamesOnly)
137 int i, aLength = theFiles.length();
140 return (new SALOMEDS::TMPFile);
142 TCollection_AsciiString aTmpDir(const_cast<char*>(theFromDirectory)); //Get a temporary directory for saved a file
144 long aBufferSize = 0;
148 int* aFileNameSize= new int[aLength];
149 long* aFileSize= new long[aLength];
151 //Determine the required size of the buffer
153 for(i=0; i<aLength; i++) {
155 //Check if the file exists
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;
163 ifstream aFile(aFullPath.ToCString(), ios::binary);
165 ifstream aFile(aFullPath.ToCString());
167 aFile.seekg(0, ios::end);
168 aFileSize[i] = aFile.tellg();
169 aBufferSize += aFileSize[i]; //Add a space to store the file
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
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];
182 return (new SALOMEDS::TMPFile);
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)));
192 for(i=0; i<aLength; i++) {
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;
200 aFile = new ifstream(aFullPath.ToCString(), ios::binary);
202 aFile = new ifstream(aFullPath.ToCString());
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)));
211 //Copy the file name to the buffer
212 memcpy((aBuffer + aCurrentPos), theFiles[i], aFileNameSize[i]);
213 aCurrentPos += aFileNameSize[i];
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)));
222 aFile->seekg(0, ios::beg);
223 aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]);
226 aCurrentPos += aFileSize[i];
230 delete[] aFileNameSize;
234 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
236 return (new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1));
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)
248 if(theStream.length() == 0) return NULL;
249 TCollection_AsciiString aTmpDir(const_cast<char*>(theToDirectory)); //Get a temporary directory for saving a file
251 unsigned char *aBuffer = (unsigned char*)theStream.NP_data();
253 if(aBuffer == NULL) return NULL;
255 long aFileSize, aCurrentPos = 4;
256 int i, aFileNameSize, aNbFiles = 0;
258 //Copy the number of files in the stream
259 memcpy(&aNbFiles, aBuffer, sizeof(int));
261 SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
262 aFiles->length(aNbFiles);
264 for(i=0; i<aNbFiles; i++) {
266 //Put a length of the file name to aFileNameSize
267 memcpy(&aFileNameSize, (aBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int)));
270 char *aFileName = new char[aFileNameSize];
271 //Put a file name to aFileName
272 memcpy(aFileName, (aBuffer + aCurrentPos), aFileNameSize);
273 aCurrentPos += aFileNameSize;
275 //Put a length of the file to aFileSize
277 memcpy(&aFileSize, (aBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
280 TCollection_AsciiString aFullPath = aTmpDir + aFileName;
281 ofstream aFile(aFullPath.ToCString());
282 aFile.write((char *)(aBuffer+aCurrentPos), aFileSize);
284 aCurrentPos += aFileSize;
286 aFiles[i] = CORBA::string_dup(aFileName);
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();
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();
316 //=======================================================================
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 )
324 SALOMEDS::GenericAttribute_var anAttr;
325 if ( !theObj->_is_nil() && theObj->FindAttribute( anAttr, "AttributeFlags" ) )
327 SALOMEDS::AttributeFlags_var aFlags = SALOMEDS::AttributeFlags::_narrow( anAttr );
328 return aFlags->Get( theFlag );
334 //=======================================================================
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 )
343 SALOMEDS::SObject_var anObj = theStudy->FindObjectID( theEntry );
345 if ( !anObj->_is_nil() )
347 SALOMEDS::GenericAttribute_var aGAttr;
348 if ( anObj->FindAttribute( aGAttr, "AttributeFlags" ) )
350 SALOMEDS::AttributeFlags_var anAttr = SALOMEDS::AttributeFlags::_narrow( aGAttr );
351 anAttr->Set( theFlag, theValue );
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 );
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 )
375 if ( theObj->_is_nil() )
377 SALOMEDS::SComponentIterator_var anIter = theStudy->NewComponentIterator();
378 for ( ; anIter->More(); anIter->Next() )
380 SALOMEDS::SObject_var anObj = SALOMEDS::SObject::_narrow( anIter->Value() );
381 if ( !anObj->_is_nil() )
383 theList.push_back( anObj );
384 GetAllChildren( theStudy, anObj, theList );
390 SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theObj );
391 for ( ; anIter->More(); anIter->Next() )
393 SALOMEDS::SObject_var anObj = anIter->Value();
394 SALOMEDS::SObject_var aRef;
395 if ( !anObj->ReferencedObject( aRef ) )
397 theList.push_back( anObj );
398 GetAllChildren( theStudy, anObj, theList );