Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Tool.cxx
1 //  File      : SALOMEDSImpl_Tool.cxx
2 //  Created   : Mon Oct 21 16:24:34 2002
3 //  Author    : Sergey RUIN
4
5 //  Project   : SALOME
6 //  Module    : SALOMEDSImpl
7 //  Copyright : Open CASCADE
8
9 #include "SALOMEDSImpl_Tool.hxx"
10
11 #include <stdio.h>
12 #include <iostream> 
13 #include <fstream>
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>
22
23 #ifndef WNT
24 #include <sys/time.h>
25 #else
26 #include <time.h>
27 #endif
28 #include <stdlib.h>
29
30 using namespace std;
31
32
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()
38 {
39   //Find a temporary directory to store a file
40
41   TCollection_AsciiString aTmpDir;
42
43   char *Tmp_dir = getenv("SALOME_TMP_DIR");
44   if(Tmp_dir != NULL) {
45     aTmpDir = TCollection_AsciiString(Tmp_dir);
46 #ifdef WIN32
47     if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
48 #else
49     if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
50 #endif      
51   }
52   else {
53 #ifdef WIN32
54     aTmpDir = TCollection_AsciiString("C:\\");
55 #else
56     aTmpDir = TCollection_AsciiString("/tmp/");
57 #endif
58   }
59
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");
64
65   aTmpDir += aSubDir; //Get RND sub directory
66
67 #ifdef WIN32
68   if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
69 #else
70   if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
71 #endif
72
73   OSD_Path aPath(aTmpDir);
74   OSD_Directory aDir(aPath);
75
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);
80   }
81
82   OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
83   aDir.Build(aProtection);
84
85   return aTmpDir;
86 }
87
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)
95 {
96   TCollection_AsciiString aDirName = theDirectory;
97
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;
105
106     OSD_Protection aProtection = anOSDFile.Protection();
107     aProtection.SetUser(OSD_RW);
108     anOSDFile.SetProtection(aProtection);
109
110     anOSDFile.Remove();
111   }
112
113   if(IsDirDeleted) {
114     OSD_Path aPath(aDirName);
115     OSD_Directory aDir(aPath);
116     OSD_FileIterator anIterator(aPath, '*');
117
118     if(aDir.Exists() && !anIterator.More()) aDir.Remove();
119   }
120
121 }
122
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());
131   return aNameString;
132 }
133
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('|','/');
143   return aDirString;
144 }
145
146
147
148
149
150
151
152
153
154
155