1 // File : SALOMEDSImpl_Tool.cxx
2 // Created : Mon Oct 21 16:24:34 2002
3 // Author : Sergey RUIN
6 // Module : SALOMEDSImpl
7 // Copyright : Open CASCADE
9 #include "SALOMEDSImpl_Tool.hxx"
14 #include <OSD_Path.hxx>
15 #include <OSD_File.hxx>
16 #include <OSD_Directory.hxx>
17 #include <OSD_Process.hxx>
18 #include <OSD_Directory.hxx>
19 #include <OSD_Protection.hxx>
20 #include <OSD_SingleProtection.hxx>
21 #include <OSD_FileIterator.hxx>
33 //============================================================================
34 // function : GetTempDir
35 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
36 //============================================================================
37 TCollection_AsciiString SALOMEDSImpl_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 aTmpDir += aSubDir; //Get RND sub directory
68 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
70 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
73 OSD_Path aPath(aTmpDir);
74 OSD_Directory aDir(aPath);
76 for(aRND = 0; aDir.Exists(); aRND++) {
77 aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name
78 aPath = OSD_Path(aTmpDir);
79 aDir = OSD_Directory(aPath);
82 OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
83 aDir.Build(aProtection);
88 //============================================================================
89 // function : RemoveTemporaryFiles
90 // purpose : Removes files listed in theFileList
91 //============================================================================
92 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const TCollection_AsciiString& theDirectory,
93 const Handle(TColStd_HSequenceOfAsciiString)& theFiles,
94 const bool IsDirDeleted)
96 TCollection_AsciiString aDirName = theDirectory;
98 int i, aLength = theFiles->Length();
99 for(i=1; i<=aLength; i++) {
100 TCollection_AsciiString aFile(aDirName);
101 aFile += theFiles->Value(i);
102 OSD_Path anOSDPath(aFile);
103 OSD_File anOSDFile(anOSDPath);
104 if(!anOSDFile.Exists()) continue;
106 OSD_Protection aProtection = anOSDFile.Protection();
107 aProtection.SetUser(OSD_RW);
108 anOSDFile.SetProtection(aProtection);
114 OSD_Path aPath(aDirName);
115 OSD_Directory aDir(aPath);
116 OSD_FileIterator anIterator(aPath, '*');
118 if(aDir.Exists() && !anIterator.More()) aDir.Remove();
123 //============================================================================
124 // function : GetNameFromPath
125 // purpose : Returns the name by the path
126 //============================================================================
127 TCollection_AsciiString SALOMEDSImpl_Tool::GetNameFromPath(const TCollection_AsciiString& thePath) {
128 if (thePath.IsEmpty()) return "";
129 OSD_Path aPath = OSD_Path(thePath);
130 TCollection_AsciiString aNameString(aPath.Name());
134 //============================================================================
135 // function : GetDirFromPath
136 // purpose : Returns the dir by the path
137 //============================================================================
138 TCollection_AsciiString SALOMEDSImpl_Tool::GetDirFromPath(const TCollection_AsciiString& thePath) {
139 if (thePath.IsEmpty()) return "";
140 OSD_Path aPath = OSD_Path(thePath);
141 TCollection_AsciiString aDirString(aPath.Trek());
142 aDirString.ChangeAll('|','/');