Salome HOME
[EDF30062] : Forward of current directory mecanism
[modules/kernel.git] / src / HDFPersist / HDFascii.cc
index 410446bb7c909b3f27d0547a343a0675cc871fd8..7a0aa19e9f981e05f4b9dd8a7afa5962455861f0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -28,6 +28,8 @@
 //
 #include "HDFOI.hxx"
 
+#include "Basics_Utils.hxx"
+
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
@@ -86,14 +88,18 @@ 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);
+  fd = _wopen(aPath, O_RDONLY);
+  if (fd == -1) 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;
 
   return res;
@@ -170,7 +176,7 @@ char* HDFascii::ConvertFromHDFToASCII(const char* thePath,
       return NULL;
   }
 
-  int length = strlen(aPath.c_str());
+  size_t length = strlen(aPath.c_str());
   char *new_str = new char[ 1+length ];
   strcpy(new_str , aPath.c_str()) ;
 
@@ -250,10 +256,10 @@ void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident)
   delete [] name;
 
   hdf_dataset->GetDim(dim);
-  fprintf(fp, " %i\n", ndim);
+  fprintf(fp, " %li\n", ndim);
 
   for(int i = 0;i < ndim;i++) {
-    fprintf(fp, " %i", dim[i]);
+    fprintf(fp, " %lu", (unsigned long)dim[i]);
   }
 
   fprintf(fp, "\n");
@@ -273,7 +279,7 @@ void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident)
     array->GetDim(arr_dim);
 
     for( int i = 0;i < arr_ndim; i++ ) {
-      fprintf(fp, " %i", arr_dim[i]);
+      fprintf(fp, " %lu", (unsigned long)arr_dim[i]);
     }
         
     //And write the data array
@@ -303,17 +309,17 @@ void SaveDatasetInASCIIfile(HDFdataset *hdf_dataset, FILE* fp, int ident)
 // function : SaveAttributeInASCIIfile
 // purpose  : 
 //============================================================================
-void SaveAttributeInASCIIfile(HDFattribute *hdf_attribute, FILE* fp, int ident)
+void SaveAttributeInASCIIfile(HDFattribute *hdf_attribute, FILE* fp, int /*ident*/)
 {
   hdf_attribute->OpenOnDisk();
 
   hdf_type type = hdf_attribute->GetType();
 
   char* name = makeName(hdf_attribute->GetName());
-  int size = hdf_attribute->GetSize();
+  size_t size = hdf_attribute->GetSize();
 
   fprintf(fp, "%s\n", ATTRIBUTE_ID);
-  fprintf(fp, "%s %i %i\n", name, type, size);
+  fprintf(fp, "%s %i %lu\n", name, type, size);
 
   delete [] name;
 
@@ -423,7 +429,7 @@ char* HDFascii::ConvertFromASCIIToHDF(const char* thePath,
       return NULL;
   }
 
-  int length = strlen(aTmpDir.c_str());
+  size_t length = strlen(aTmpDir.c_str());
   char *new_str = new char[ 1+length ];
   strcpy(new_str , aTmpDir.c_str()) ;
 
@@ -498,8 +504,8 @@ bool CreateGroupFromASCII(HDFcontainerObject *father, FILE *fp)
 bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp)
 {
   char name[HDF_NAME_MAX_LEN+1];
-  hdf_type type;
-  hdf_byte_order order;
+  int type;
+  int order;
   int nbDim, nbAttr;
   long i, size;
 
@@ -529,7 +535,7 @@ bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp)
   HDFarray* anArray = 0;
   if( type == HDF_ARRAY ){
     //Get array information
-    hdf_type arr_data_type;
+    int arr_data_type;
     int arr_ndim;
     fscanf(fp, "%c", &tmp);
     fscanf(fp, " %i\n", &arr_data_type ); //Get array data type
@@ -541,14 +547,14 @@ bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp)
       fscanf(fp, " %i", &tdim);
       arr_dim[i] = tdim;
     }
-    anArray = new HDFarray(0, arr_data_type, arr_ndim, arr_dim);
+    anArray = new HDFarray(0, (hdf_type)arr_data_type, arr_ndim, arr_dim);
     anArray->CreateOnDisk();
 
     type = arr_data_type;
     delete [] arr_dim;
   }
 
-  HDFdataset* hdf_dataset = new HDFdataset(new_name, father, anArray ? HDF_ARRAY : type, sizeArray, nbDim, order);
+  HDFdataset* hdf_dataset = new HDFdataset(new_name, father, anArray ? HDF_ARRAY : (hdf_type)type, sizeArray, nbDim, (hdf_byte_order)order);
   
   if(anArray)
     hdf_dataset->SetArrayId(anArray->GetId());
@@ -587,7 +593,7 @@ bool CreateDatasetFromASCII(HDFcontainerObject *father, FILE *fp)
   } else if(type == HDF_CHAR) {
     hdf_char* val = new hdf_char[size];
     for(i=0; i<size; i++) {
-      fscanf(fp, " %i", &(val[i]));
+      fscanf(fp, " %c", &(val[i]));
     }
     hdf_dataset->WriteOnDisk(val);
     delete [] val;
@@ -636,11 +642,11 @@ bool CreateAttributeFromASCII(HDFinternalObject *father, FILE* fp)
 {
   char name[HDF_NAME_MAX_LEN+1];
 
-  hdf_type type;
+  int type;
   int size;
   fscanf(fp, "%s %i %i\n", name, &type, &size);
   char* new_name = restoreName(name);
-  HDFattribute* hdf_attribute = new HDFattribute(new_name, father, type, size);
+  HDFattribute* hdf_attribute = new HDFattribute(new_name, father, (hdf_type)type, size);
 
   hdf_attribute->CreateOnDisk();
 
@@ -691,9 +697,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 {
@@ -723,9 +737,16 @@ std::string GetTmpDir()
   }
 
 #ifdef WIN32
+#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(aTmpDir.c_str(), NULL);
-  CreateDirectory(aDir.c_str(), NULL);
+  CreateDirectory(aTmpDirToCreate.c_str(), NULL);
+  CreateDirectory(aDirToCreate.c_str(), NULL);
 #else
   mkdir(aDir.c_str(), 0x1ff); 
 #endif
@@ -736,7 +757,7 @@ std::string GetTmpDir()
 char* makeName(char* name)
 {
   std::string aName(name), aNewName;
-  int i, length = aName.size();
+  size_t i, length = aName.size();
   char replace = (char)19;
 
   for(i=0; i<length; i++) {
@@ -753,7 +774,7 @@ char* makeName(char* name)
 char* restoreName(char* name)
 {
   std::string aName(name), aNewName;
-  int i, length = aName.size();
+  size_t i, length = aName.size();
   char replace = (char)19;
 
   for(i=0; i<length; i++) {
@@ -770,7 +791,7 @@ char* restoreName(char* name)
 void write_float64(FILE* fp, hdf_float64* value)
 {
   unsigned char* array = (unsigned char*)value;
-  for(int i = 0; i < sizeof(hdf_float64); i++) {
+  for(int i = 0; i < (int)sizeof(hdf_float64); i++) {
     unsigned tmp = (unsigned short)array[i];
     fprintf(fp, " %2x", tmp);
   }
@@ -779,7 +800,7 @@ void write_float64(FILE* fp, hdf_float64* value)
 void read_float64(FILE* fp, hdf_float64* value)
 {
   unsigned char* array = (unsigned char*)value;
-  for(int i = 0; i < sizeof(hdf_float64); i++) {
+  for(int i = 0; i < (int)sizeof(hdf_float64); i++) {
     unsigned tmp;
     fscanf(fp, " %x", &tmp); 
     array[i] = (unsigned char)tmp;
@@ -789,7 +810,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;
@@ -804,9 +830,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
 }