Salome HOME
Copyrights update
[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/
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 //  Copyright : Open CASCADE
27
28 #include "SALOMEDSImpl_Tool.hxx"
29
30 #include <stdio.h>
31 #include <iostream> 
32 #include <fstream>
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>
41
42 #ifndef WNT
43 #include <sys/time.h>
44 #else
45 #include <time.h>
46 #endif
47 #include <stdlib.h>
48
49 using namespace std;
50
51
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()
57 {
58   //Find a temporary directory to store a file
59
60   TCollection_AsciiString aTmpDir;
61
62   char *Tmp_dir = getenv("SALOME_TMP_DIR");
63   if(Tmp_dir != NULL) {
64     aTmpDir = TCollection_AsciiString(Tmp_dir);
65 #ifdef WIN32
66     if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
67 #else
68     if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
69 #endif      
70   }
71   else {
72 #ifdef WIN32
73     aTmpDir = TCollection_AsciiString("C:\\");
74 #else
75     aTmpDir = TCollection_AsciiString("/tmp/");
76 #endif
77   }
78
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");
83
84   aTmpDir += aSubDir; //Get RND sub directory
85
86 #ifdef WIN32
87   if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
88 #else
89   if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
90 #endif
91
92   OSD_Path aPath(aTmpDir);
93   OSD_Directory aDir(aPath);
94
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);
99   }
100
101   OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
102   aDir.Build(aProtection);
103
104   return aTmpDir;
105 }
106
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)
114 {
115   TCollection_AsciiString aDirName = theDirectory;
116
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;
124
125     OSD_Protection aProtection = anOSDFile.Protection();
126     aProtection.SetUser(OSD_RW);
127     anOSDFile.SetProtection(aProtection);
128
129     anOSDFile.Remove();
130   }
131
132   if(IsDirDeleted) {
133     OSD_Path aPath(aDirName);
134     OSD_Directory aDir(aPath);
135     OSD_FileIterator anIterator(aPath, '*');
136
137     if(aDir.Exists() && !anIterator.More()) aDir.Remove();
138   }
139
140 }
141
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());
150   return aNameString;
151 }
152
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('|','/');
162   return aDirString;
163 }
164
165
166
167
168
169
170
171
172
173
174