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 : SALOMEDSImpl_Tool.cxx
21 // Created : Mon Oct 21 16:24:34 2002
22 // Author : Sergey RUIN
25 // Module : SALOMEDSImpl
27 #include "SALOMEDSImpl_Tool.hxx"
32 #include <OSD_Path.hxx>
33 #include <OSD_File.hxx>
34 #include <OSD_Directory.hxx>
35 #include <OSD_Process.hxx>
36 #include <OSD_Directory.hxx>
37 #include <OSD_Protection.hxx>
38 #include <OSD_SingleProtection.hxx>
39 #include <OSD_FileIterator.hxx>
51 //============================================================================
52 // function : GetTempDir
53 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
54 //============================================================================
55 TCollection_AsciiString SALOMEDSImpl_Tool::GetTmpDir()
57 //Find a temporary directory to store a file
59 TCollection_AsciiString aTmpDir;
61 char *Tmp_dir = getenv("SALOME_TMP_DIR");
63 aTmpDir = TCollection_AsciiString(Tmp_dir);
65 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
67 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
72 aTmpDir = TCollection_AsciiString("C:\\");
74 aTmpDir = TCollection_AsciiString("/tmp/");
78 srand((unsigned int)time(NULL));
79 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
80 TCollection_AsciiString aSubDir(aRND);
81 if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
83 aTmpDir += aSubDir; //Get RND sub directory
86 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
88 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
91 OSD_Path aPath(aTmpDir);
92 OSD_Directory aDir(aPath);
94 for(aRND = 0; aDir.Exists(); aRND++) {
95 aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name
96 aPath = OSD_Path(aTmpDir);
97 aDir = OSD_Directory(aPath);
100 OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
101 aDir.Build(aProtection);
106 //============================================================================
107 // function : RemoveTemporaryFiles
108 // purpose : Removes files listed in theFileList
109 //============================================================================
110 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const TCollection_AsciiString& theDirectory,
111 const Handle(TColStd_HSequenceOfAsciiString)& theFiles,
112 const bool IsDirDeleted)
114 TCollection_AsciiString aDirName = theDirectory;
116 int i, aLength = theFiles->Length();
117 for(i=1; i<=aLength; i++) {
118 TCollection_AsciiString aFile(aDirName);
119 aFile += theFiles->Value(i);
120 OSD_Path anOSDPath(aFile);
121 OSD_File anOSDFile(anOSDPath);
122 if(!anOSDFile.Exists()) continue;
124 OSD_Protection aProtection = anOSDFile.Protection();
125 aProtection.SetUser(OSD_RW);
126 anOSDFile.SetProtection(aProtection);
132 OSD_Path aPath(aDirName);
133 OSD_Directory aDir(aPath);
134 OSD_FileIterator anIterator(aPath, '*');
136 if(aDir.Exists() && !anIterator.More()) aDir.Remove();
141 //============================================================================
142 // function : GetNameFromPath
143 // purpose : Returns the name by the path
144 //============================================================================
145 TCollection_AsciiString SALOMEDSImpl_Tool::GetNameFromPath(const TCollection_AsciiString& thePath) {
146 if (thePath.IsEmpty()) return "";
147 OSD_Path aPath = OSD_Path(thePath);
148 TCollection_AsciiString aNameString(aPath.Name());
152 //============================================================================
153 // function : GetDirFromPath
154 // purpose : Returns the dir by the path
155 //============================================================================
156 TCollection_AsciiString SALOMEDSImpl_Tool::GetDirFromPath(const TCollection_AsciiString& thePath) {
157 if (thePath.IsEmpty()) return "";
158 OSD_Path aPath = OSD_Path(thePath);
159 TCollection_AsciiString aDirString;
160 /*Unix platform don't use <disk> assignment in <path> string
161 but we must to add it for WNT case*/
162 TCollection_AsciiString disk = aPath.Disk();
163 if(disk.Length()) aDirString.AssignCat(disk);
164 aDirString.AssignCat(aPath.Trek());
165 aDirString.ChangeAll('|','/');