Salome HOME
Merge branch 'omu/multijob'
[modules/kernel.git] / src / HDFPersist / HDFascii.cc
index c8fd3acfdb82ca52f7ab7246f8b8430656c7a4c0..4b86a48a288712b9b47ab5ad4d6a3eff12b9c9df 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -28,6 +28,8 @@
 //
 #include "HDFOI.hxx"
 
+#include "Basics_Utils.hxx"
+
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
@@ -86,15 +88,20 @@ void WriteSimpleData( FILE* fp, HDFdataset *hdf_dataset, hdf_type type, long siz
 //============================================================================
 bool HDFascii::isASCII(const char* thePath) {
   int fd;
+#if defined(WIN32) && defined(UNICODE)
+  const wchar_t * aPath = Kernel_Utils::utf8_decode(thePath);
+  if (!(fd = _wopen(aPath, O_RDONLY))) return false;
+#else
   if(!(fd = open(thePath, O_RDONLY))) return false;
+#endif
   char* aBuffer = new char[9];
   aBuffer[8] = (char)0;
   read(fd, aBuffer, 8); 
   close(fd);
+  bool res = (strcmp(aBuffer, ASCIIHDF_ID) == 0);
+  delete [] aBuffer;
 
-  if(strcmp(aBuffer, ASCIIHDF_ID) == 0) return true;
-
-  return false;
+  return res;
 }
 
 //############################## HDF => ASCII ################################
@@ -689,9 +696,17 @@ std::string GetTmpDir()
 {
  //Find a temporary directory to store a file
   std::string aTmpDir;
+#if defined(UNICODE)
+  wchar_t *Tmp_dir = _wgetenv(L"SALOME_TMP_DIR");
+#else
   char *Tmp_dir = getenv("SALOME_TMP_DIR");
+#endif
   if(Tmp_dir != NULL) {
+#if defined(UNICODE)
+       aTmpDir = Kernel_Utils::utf8_encode_s(Tmp_dir);
+#else
     aTmpDir = std::string(Tmp_dir);
+#endif
     if(aTmpDir[aTmpDir.size()-1] != dir_separator) aTmpDir+=dir_separator;
   }
   else {
@@ -721,9 +736,16 @@ std::string GetTmpDir()
   }
 
 #ifdef WIN32
-  //fuction CreateDirectory create only final directory, but not intermediate
-  CreateDirectory(aTmpDir.c_str(), NULL);
-  CreateDirectory(aDir.c_str(), NULL);
+#if defined(UNICODE)
+  std::wstring aTmpDirToCreate = Kernel_Utils::utf8_decode_s(aTmpDir);
+  std::wstring aDirToCreate = Kernel_Utils::utf8_decode_s(aDir);
+#else
+  std::string aTmpDirToCreate = aTmpDir;
+  std::string aDirToCreate = aDir;
+#endif
+  //function CreateDirectory create only final directory, but not intermediate
+  CreateDirectory(aTmpDirToCreate.c_str(), NULL);
+  CreateDirectory(aDirToCreate.c_str(), NULL);
 #else
   mkdir(aDir.c_str(), 0x1ff); 
 #endif
@@ -787,7 +809,12 @@ void read_float64(FILE* fp, hdf_float64* value)
 bool Exists(const std::string thePath) 
 {
 #ifdef WIN32 
- if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
+#if defined(UNICODE)
+       std::wstring aPathToCheck = Kernel_Utils::utf8_decode_s( thePath );
+#else
+       std::string aPathToCheck = thePath;
+#endif
+ if (  GetFileAttributes ( aPathToCheck.c_str()  ) == 0xFFFFFFFF  ) {
     DWORD errorId = GetLastError ();
     if ( errorId == ERROR_FILE_NOT_FOUND || errorId == ERROR_PATH_NOT_FOUND )
       return false;
@@ -802,9 +829,16 @@ bool Exists(const std::string thePath)
 void Move(const std::string& fName, const std::string& fNameDst)
 { 
 #ifdef WIN32
-  MoveFileEx (fName.c_str(), fNameDst.c_str(),MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
+#if defined(UNICODE)
+       std::wstring fNameToMove = Kernel_Utils::utf8_decode_s( fName );
+       std::wstring fNameDestination = Kernel_Utils::utf8_decode_s( fNameDst );
+#else
+       std::string fNameToMove = fName;
+       std::string fNameDestination = fNameDst;
+#endif
+  MoveFileEx ( fNameToMove.c_str(), fNameDestination.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED );
 #else
-  rename(fName.c_str(), fNameDst.c_str());
+  rename( fName.c_str(), fNameDst.c_str() );
 #endif
 }