Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Tool.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File      : SALOMEDSImpl_Tool.cxx
21 //  Created   : Mon Oct 21 16:24:34 2002
22 //  Author    : Sergey RUIN
23
24 //  Project   : SALOME
25 //  Module    : SALOMEDSImpl
26
27 #include "SALOMEDSImpl_Tool.hxx"
28
29 #include <stdio.h>
30 #include <iostream> 
31 #include <fstream>
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>
40
41 #ifndef WNT
42 #include <sys/time.h>
43 #else
44 #include <time.h>
45 #endif
46 #include <stdlib.h>
47
48 using namespace std;
49
50
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()
56 {
57   //Find a temporary directory to store a file
58
59   TCollection_AsciiString aTmpDir;
60
61   char *Tmp_dir = getenv("SALOME_TMP_DIR");
62   if(Tmp_dir != NULL) {
63     aTmpDir = TCollection_AsciiString(Tmp_dir);
64 #ifdef WIN32
65     if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
66 #else
67     if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
68 #endif      
69   }
70   else {
71 #ifdef WIN32
72     aTmpDir = TCollection_AsciiString("C:\\");
73 #else
74     aTmpDir = TCollection_AsciiString("/tmp/");
75 #endif
76   }
77
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");
82
83   aTmpDir += aSubDir; //Get RND sub directory
84
85 #ifdef WIN32
86   if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
87 #else
88   if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
89 #endif
90
91   OSD_Path aPath(aTmpDir);
92   OSD_Directory aDir(aPath);
93
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);
98   }
99
100   OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
101   aDir.Build(aProtection);
102
103   return aTmpDir;
104 }
105
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)
113 {
114   TCollection_AsciiString aDirName = theDirectory;
115
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;
123
124     OSD_Protection aProtection = anOSDFile.Protection();
125     aProtection.SetUser(OSD_RW);
126     anOSDFile.SetProtection(aProtection);
127
128     anOSDFile.Remove();
129   }
130
131   if(IsDirDeleted) {
132     OSD_Path aPath(aDirName);
133     OSD_Directory aDir(aPath);
134     OSD_FileIterator anIterator(aPath, '*');
135
136     if(aDir.Exists() && !anIterator.More()) aDir.Remove();
137   }
138
139 }
140
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());
149   return aNameString;
150 }
151
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(aPath.Trek());
160   aDirString.ChangeAll('|','/');
161   return aDirString;
162 }
163
164
165
166
167
168
169
170
171
172
173