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 : SALOMEDSImpl_Tool.cxx
21 // Created : Mon Oct 21 16:24:34 2002
22 // Author : Sergey RUIN
25 // Module : SALOMEDSImpl
26 // Copyright : Open CASCADE
28 #include "SALOMEDSImpl_Tool.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>
52 //============================================================================
53 // function : GetTempDir
54 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
55 //============================================================================
56 TCollection_AsciiString SALOMEDSImpl_Tool::GetTmpDir()
58 //Find a temporary directory to store a file
60 TCollection_AsciiString aTmpDir;
62 char *Tmp_dir = getenv("SALOME_TMP_DIR");
64 aTmpDir = TCollection_AsciiString(Tmp_dir);
66 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
68 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
73 aTmpDir = TCollection_AsciiString("C:\\");
75 aTmpDir = TCollection_AsciiString("/tmp/");
79 srand((unsigned int)time(NULL));
80 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
81 TCollection_AsciiString aSubDir(aRND);
82 if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
84 aTmpDir += aSubDir; //Get RND sub directory
87 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
89 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
92 OSD_Path aPath(aTmpDir);
93 OSD_Directory aDir(aPath);
95 for(aRND = 0; aDir.Exists(); aRND++) {
96 aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name
97 aPath = OSD_Path(aTmpDir);
98 aDir = OSD_Directory(aPath);
101 OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
102 aDir.Build(aProtection);
107 //============================================================================
108 // function : RemoveTemporaryFiles
109 // purpose : Removes files listed in theFileList
110 //============================================================================
111 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const TCollection_AsciiString& theDirectory,
112 const Handle(TColStd_HSequenceOfAsciiString)& theFiles,
113 const bool IsDirDeleted)
115 TCollection_AsciiString aDirName = theDirectory;
117 int i, aLength = theFiles->Length();
118 for(i=1; i<=aLength; i++) {
119 TCollection_AsciiString aFile(aDirName);
120 aFile += theFiles->Value(i);
121 OSD_Path anOSDPath(aFile);
122 OSD_File anOSDFile(anOSDPath);
123 if(!anOSDFile.Exists()) continue;
125 OSD_Protection aProtection = anOSDFile.Protection();
126 aProtection.SetUser(OSD_RW);
127 anOSDFile.SetProtection(aProtection);
133 OSD_Path aPath(aDirName);
134 OSD_Directory aDir(aPath);
135 OSD_FileIterator anIterator(aPath, '*');
137 if(aDir.Exists() && !anIterator.More()) aDir.Remove();
142 //============================================================================
143 // function : GetNameFromPath
144 // purpose : Returns the name by the path
145 //============================================================================
146 TCollection_AsciiString SALOMEDSImpl_Tool::GetNameFromPath(const TCollection_AsciiString& thePath) {
147 if (thePath.IsEmpty()) return "";
148 OSD_Path aPath = OSD_Path(thePath);
149 TCollection_AsciiString aNameString(aPath.Name());
153 //============================================================================
154 // function : GetDirFromPath
155 // purpose : Returns the dir by the path
156 //============================================================================
157 TCollection_AsciiString SALOMEDSImpl_Tool::GetDirFromPath(const TCollection_AsciiString& thePath) {
158 if (thePath.IsEmpty()) return "";
159 OSD_Path aPath = OSD_Path(thePath);
160 TCollection_AsciiString aDirString(aPath.Trek());
161 aDirString.ChangeAll('|','/');