2 // File : SALOMEDS_Tool.cxx
3 // Created : Mon Oct 21 16:24:34 2002
4 // Author : Sergey RUIN
8 // Copyright : Open CASCADE
10 #include "SALOMEDS_Tool.hxx"
12 #include "utilities.h"
14 #include <TCollection_AsciiString.hxx>
18 #include <OSD_Path.hxx>
19 #include <OSD_File.hxx>
20 #include <OSD_Directory.hxx>
21 #include <OSD_Process.hxx>
22 #include <OSD_Directory.hxx>
23 #include <OSD_Protection.hxx>
24 #include <OSD_SingleProtection.hxx>
25 #include <OSD_FileIterator.hxx>
30 //============================================================================
31 // function : GetTempDir
32 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
33 //============================================================================
34 char* SALOMEDS_Tool::GetTmpDir()
36 //Find a temporary directory to store a file
38 TCollection_AsciiString aTmpDir;
40 char *Tmp_dir = getenv("SALOME_TMP_DIR");
42 aTmpDir = TCollection_AsciiString(Tmp_dir);
44 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
46 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
51 aTmpDir = TCollection_AsciiString("C:\\");
53 aTmpDir = TCollection_AsciiString("/tmp/");
57 srand((unsigned int)time(NULL));
58 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
59 TCollection_AsciiString aSubDir(aRND);
60 if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
62 MESSAGE("#### RND " << aRND);
64 aTmpDir += aSubDir; //Get RND sub directory
67 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
69 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
72 OSD_Path aPath(aTmpDir);
73 OSD_Directory aDir(aPath);
75 for(aRND = 0; aDir.Exists(); aRND++) {
76 aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name
77 aPath = OSD_Path(aTmpDir);
78 aDir = OSD_Directory(aPath);
81 MESSAGE("#### TMP" << aTmpDir.ToCString());
83 OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
84 aDir.Build(aProtection);
86 return CORBA::string_dup(aTmpDir.ToCString());
89 //============================================================================
90 // function : RemoveTemporaryFiles
91 // purpose : Removes files listed in theFileList
92 //============================================================================
93 void SALOMEDS_Tool::RemoveTemporaryFiles(const char* theDirectory,
94 const SALOMEDS::ListOfFileNames& theFiles,
95 const bool IsDirDeleted)
97 TCollection_AsciiString aDirName(const_cast<char*>(theDirectory));
99 int i, aLength = theFiles.length();
100 for(i=0; i<aLength; i++) {
101 TCollection_AsciiString aFile(aDirName);
102 // aFile += (char*)theFiles[i];
103 aFile += (char*)theFiles[i].in();
104 OSD_Path anOSDPath(aFile);
105 OSD_File anOSDFile(anOSDPath);
106 if(!anOSDFile.Exists()) continue;
112 OSD_Path aPath(aDirName);
113 OSD_Directory aDir(aPath);
114 OSD_FileIterator anIterator(aPath, '*');
116 if(aDir.Exists() && !anIterator.More()) aDir.Remove();
121 //============================================================================
122 // function : PutFilesToStream
123 // purpose : converts the files from a list 'theFiles' to the stream
124 //============================================================================
126 SALOMEDS_Tool::PutFilesToStream(const char* theFromDirectory,
127 const SALOMEDS::ListOfFileNames& theFiles,
128 const int theNamesOnly)
130 int i, aLength = theFiles.length();
131 if(aLength == 0) return NULL;
133 TCollection_AsciiString aTmpDir(const_cast<char*>(theFromDirectory)); //Get a temporary directory for saved a file
135 long aBufferSize = 0;
139 int* aFileNameSize= new int[aLength];
140 long* aFileSize= new long[aLength];
142 //Determine the required size of the buffer
144 for(i=0; i<aLength; i++) {
146 //Check if the file exists
148 if (!theNamesOnly) { // mpv 15.01.2003: if only file names must be stroed, then size of files is zero
149 TCollection_AsciiString aFullPath = aTmpDir + strdup(theFiles[i]);
150 OSD_Path anOSDPath(aFullPath);
151 OSD_File anOSDFile(anOSDPath);
152 if(!anOSDFile.Exists()) continue;
154 ifstream aFile(aFullPath.ToCString(), ios::binary);
156 ifstream aFile(aFullPath.ToCString());
158 aFile.seekg(0, ios::end);
159 aFileSize[i] = aFile.tellg();
160 aBufferSize += aFileSize[i]; //Add a space to store the file
162 aFileNameSize[i] = strlen(theFiles[i])+1;
163 aBufferSize += aFileNameSize[i]; //Add a space to store the file name
164 aBufferSize += (theNamesOnly)?4:12; //Add 4 bytes: a length of the file name,
165 // 8 bytes: length of the file itself
169 aBufferSize += 4; //4 bytes for a number of the files that will be written to the stream;
170 unsigned char* aBuffer = new unsigned char[aBufferSize];
171 if(aBuffer == NULL) return NULL;
173 //Initialize 4 bytes of the buffer by 0
174 memset(aBuffer, 0, 4);
175 //Copy the number of files that will be written to the stream
176 memcpy(aBuffer, &aNbFiles, ((sizeof(int) > 4) ? 4 : sizeof(int)));
181 for(i=0; i<aLength; i++) {
183 if (!theNamesOnly) { // mpv 15.01.2003: we don't open any file if theNamesOnly = true
184 TCollection_AsciiString aFullPath = aTmpDir + strdup(theFiles[i]);
185 OSD_Path anOSDPath(aFullPath);
186 OSD_File anOSDFile(anOSDPath);
187 if(!anOSDFile.Exists()) continue;
189 aFile = new ifstream(aFullPath.ToCString(), ios::binary);
191 aFile = new ifstream(aFullPath.ToCString());
194 //Initialize 4 bytes of the buffer by 0
195 memset((aBuffer + aCurrentPos), 0, 4);
196 //Copy the length of the file name to the buffer
197 memcpy((aBuffer + aCurrentPos), (aFileNameSize + i), ((sizeof(int) > 4) ? 4 : sizeof(int)));
200 //Copy the file name to the buffer
201 memcpy((aBuffer + aCurrentPos), theFiles[i], aFileNameSize[i]);
202 aCurrentPos += aFileNameSize[i];
204 if (!theNamesOnly) { // mpv 15.01.2003: we don't copy file content to the buffer if !theNamesOnly
205 //Initialize 8 bytes of the buffer by 0
206 memset((aBuffer + aCurrentPos), 0, 8);
207 //Copy the length of the file to the buffer
208 memcpy((aBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));
211 aFile->seekg(0, ios::beg);
212 aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]);
215 aCurrentPos += aFileSize[i];
219 delete[] aFileNameSize;
223 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
225 return (new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1));
228 //============================================================================
229 // function : PutStreamToFile
230 // purpose : converts the stream "theStream" to the files
231 //============================================================================
232 SALOMEDS::ListOfFileNames*
233 SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream,
234 const char* theToDirectory,
235 const int theNamesOnly)
237 if(theStream.length() == 0) return NULL;
238 TCollection_AsciiString aTmpDir(const_cast<char*>(theToDirectory)); //Get a temporary directory for saving a file
240 unsigned char *aBuffer = (unsigned char*)theStream.NP_data();
243 long aBufferSize = theStream.length();
244 long aFileSize, aCurrentPos = 4;
245 int i, aFileNameSize, aNbFiles = 0;
247 //Copy the number of files in the stream
248 memcpy(&aNbFiles, aBuffer, sizeof(int));
250 SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
251 aFiles->length(aNbFiles);
253 for(i=0; i<aNbFiles; i++) {
255 //Put a length of the file name to aFileNameSize
256 memcpy(&aFileNameSize, (aBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int)));
259 char *aFileName = new char[aFileNameSize];
260 //Put a file name to aFileName
261 memcpy(aFileName, (aBuffer + aCurrentPos), aFileNameSize);
262 aCurrentPos += aFileNameSize;
264 //Put a length of the file to aFileSize
266 memcpy(&aFileSize, (aBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
269 TCollection_AsciiString aFullPath = aTmpDir + aFileName;
270 ofstream aFile(aFullPath.ToCString());
271 aFile.write((char *)(aBuffer+aCurrentPos), aFileSize);
273 aCurrentPos += aFileSize;
275 aFiles[i] = CORBA::string_dup(aFileName);
279 return aFiles._retn();
282 //============================================================================
283 // function : GetNameFromPath
284 // purpose : Returns the name by the path
285 //============================================================================
286 char* SALOMEDS_Tool::GetNameFromPath(const char* thePath) {
287 if (thePath == NULL) return strdup("");
288 OSD_Path aPath = OSD_Path(TCollection_AsciiString(strdup(thePath)));
289 TCollection_AsciiString aNameString(aPath.Name());
290 return aNameString.ToCString();
293 //============================================================================
294 // function : GetDirFromPath
295 // purpose : Returns the dir by the path
296 //============================================================================
297 char* SALOMEDS_Tool::GetDirFromPath(const char* thePath) {
298 if (thePath == NULL) return strdup("");
299 OSD_Path aPath = OSD_Path(TCollection_AsciiString(strdup(thePath)));
300 TCollection_AsciiString aDirString(aPath.Trek());
301 aDirString.ChangeAll('|','/');
302 return aDirString.ToCString();