From f3a6a57182afa68a50c397c39c58746547702847 Mon Sep 17 00:00:00 2001 From: rnv Date: Thu, 21 Feb 2019 11:36:33 +0300 Subject: [PATCH] Run SALOME with UNICODE path --- src/Basics/Basics_DirUtils.cxx | 52 ++- src/Basics/Basics_Utils.cxx | 364 ++++++++++-------- src/Basics/Basics_Utils.hxx | 7 + src/Container/Container_i.cxx | 7 +- src/DF/CMakeLists.txt | 3 +- src/DF/testDF.cxx | 28 +- src/HDFPersist/CMakeLists.txt | 3 +- src/HDFPersist/HDFascii.cc | 46 ++- src/HDFPersist/HDFconvert.cc | 8 +- .../SALOME_ModuleCatalog_impl.cxx | 19 +- .../SALOMEDS_AttributeStudyProperties.cxx | 2 +- src/SALOMEDS/SALOMEDS_Study.cxx | 2 +- src/SALOMEDS/SALOMEDS_Study_i.cxx | 3 +- .../SALOMEDSClient_ClientFactory.cxx | 4 + src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx | 34 +- src/SALOMEDSImpl/SALOMEDSImpl_Tool.cxx | 87 +---- src/SALOMELocalTrace/LocalTraceBufferPool.cxx | 4 +- src/TOOLSDS/SALOMEDS_Tool.cxx | 49 ++- src/Utils/Utils_ExceptHandlers.cxx | 3 +- src/Utils/Utils_Identity.cxx | 24 +- 20 files changed, 450 insertions(+), 299 deletions(-) diff --git a/src/Basics/Basics_DirUtils.cxx b/src/Basics/Basics_DirUtils.cxx index 4dac7d029..afcadf2ed 100644 --- a/src/Basics/Basics_DirUtils.cxx +++ b/src/Basics/Basics_DirUtils.cxx @@ -22,6 +22,7 @@ // Module : SALOME // #include "Basics_DirUtils.hxx" +#include "Basics_Utils.hxx" #include #include #include @@ -72,8 +73,14 @@ namespace Kernel_Utils std::string GetTmpDirByEnv( const std::string& tmp_path_env ) { +#if defined WIN32 && defined UNICODE + std::wstring w_tmp_path_env = utf8_decode_s( tmp_path_env ); + wchar_t* val = _wgetenv( w_tmp_path_env.c_str() ); + std::string dir = val ? utf8_encode_s(val) : ""; +#else char* val = getenv( tmp_path_env.c_str() ); - std::string dir = val ? val : ""; + std::string dir = val ? val : ""; +#endif return GetTmpDirByPath( dir ); } @@ -83,6 +90,15 @@ namespace Kernel_Utils if ( aTmpDir == "" || !IsExists( aTmpDir )) { #ifdef WIN32 +#ifdef UNICODE + wchar_t *wTmp_dir = _wgetenv( L"TEMP" ); + if ( wTmp_dir == NULL ) { + wTmp_dir = _wgetenv(L"TMP"); + if (wTmp_dir == NULL) + wTmp_dir = L"C:\\"; + } + aTmpDir = utf8_encode_s( wTmp_dir ); +#else char *Tmp_dir = getenv("TEMP"); if ( Tmp_dir == NULL ) { @@ -94,6 +110,7 @@ namespace Kernel_Utils } else aTmpDir = Tmp_dir; +#endif #else aTmpDir = "/tmp/"; #endif @@ -121,12 +138,16 @@ namespace Kernel_Utils if ( aDir.back() != _separator_ ) aDir += _separator_; -#ifdef WIN32 - CreateDirectory( aDir.c_str(), NULL ); +#ifdef WIN32 +#ifdef UNICODE + std::wstring aDirToCreate = utf8_decode_s(aDir); +#else + std::string aDirToCreate = aDir; +#endif + CreateDirectory( aDirToCreate.c_str(), NULL ); #else mkdir( aDir.c_str(), 0x1ff ); #endif - return aDir; } @@ -179,7 +200,11 @@ namespace Kernel_Utils //============================================================================ bool IsExists(const std::string& thePath) { +#if defined WIN32 && defined UNICODE + int status = _waccess( utf8_decode_s( thePath).c_str(), F_OK ); +#else int status = access ( thePath.c_str() , F_OK ); +#endif if (status != 0) return false; return true; } @@ -188,10 +213,15 @@ namespace Kernel_Utils // function : IsWritable // purpose : Returns True(False) if the path is (not) writable //============================================================================ - bool IsWritable(const std::string& thePath) + bool IsWritable(const std::string& thePath) { -#ifdef WIN32 - if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) { +#ifdef WIN32 +#ifdef UNICODE + std::wstring aPathToCheck = utf8_decode_s( thePath ); +#else + aPathToCheck = thePath; +#endif + if ( GetFileAttributes ( aPathToCheck.c_str() ) == 0xFFFFFFFF ) { if ( GetLastError () == FILE_ATTRIBUTE_READONLY ) { return false; } @@ -271,7 +301,13 @@ namespace Kernel_Utils #ifdef WIN32 WIN32_FIND_DATA aFileData; - HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData ); +#ifdef UNICODE + std::wstring aPathToCheck = utf8_decode_s(thePath); +#else + aPathToCheck = thePath; +#endif + + HANDLE hFile = FindFirstFile( aPathToCheck.c_str(), &aFileData ); if ( hFile == INVALID_HANDLE_VALUE ) { //empty dir diff --git a/src/Basics/Basics_Utils.cxx b/src/Basics/Basics_Utils.cxx index df5d1a49c..cef825efd 100644 --- a/src/Basics/Basics_Utils.cxx +++ b/src/Basics/Basics_Utils.cxx @@ -1,162 +1,162 @@ -// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE -// -// 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, 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 -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -// - -// File : Basics_Utils.cxx -// Author : Alexander A. BORODIN -// Module : SALOME -// - -#include "Basics_Utils.hxx" -#include -#include - -#ifndef WIN32 -#include -#include -#include -#endif - - -namespace Kernel_Utils -{ - // threadsafe - std::string GetHostname() - { - int ls = 100, r = 1; - char *s; - - while (ls < 10000 && r) - { - ls *= 2; - s = new char[ls]; - r = gethostname(s, ls-1);//threadsafe see man 7 pthread - switch (r) - { - case 0: - break; - default: -#ifdef EINVAL - case EINVAL: -#endif -#ifdef ENAMETOOLONG - case ENAMETOOLONG: -#endif -#ifdef WIN32 - case WSAEFAULT: -#endif - delete [] s; - continue; - } - - } - - if (r != 0) - { - s = new char[50]; - strcpy(s, "localhost"); - } - - // remove all after '.' - char *aDot = (strchr(s,'.')); - if (aDot) aDot[0] = '\0'; - - std::string p = s; - delete [] s; - return p; - } - - Localizer::Localizer() - { - myCurLocale = setlocale(LC_NUMERIC, 0); - setlocale(LC_NUMERIC, "C"); - } - - Localizer::~Localizer() - { - setlocale(LC_NUMERIC, myCurLocale.c_str()); - } - - std::string GetGUID( GUIDtype type ) - { - std::string guid; - - switch ( type ) { - case DefUserID: - guid = "FFFFFFFF-D9CD-11d6-945D-1050DA506788"; break; - case ObjectdID: - guid = "C08F3C95-F112-4023-8776-78F1427D0B6D"; break; - } - - return guid; - } - - const wchar_t* decode(const char* encoded) - { - setlocale(LC_ALL, ""); - size_t length = strlen(encoded) + sizeof(char); - wchar_t* decoded = new wchar_t[length]; - memset( decoded, '\0', length); - mbstowcs(decoded, encoded, length); - return decoded; - } - - const wchar_t* decode_s(std::string encoded) - { - return decode(encoded.c_str()); - } - - const char* encode(const wchar_t* decoded) - { - setlocale(LC_ALL, ""); - size_t length = std::wcslen(decoded) + sizeof(wchar_t); - char* encoded = new char[length]; - memset( encoded, '\0', length); - wcstombs(encoded, decoded, length); - return encoded; - } - - std::string encode_s(const wchar_t* decoded) - { - return std::string(encode(decoded)); - } - -#ifndef WIN32 - void print_traceback() - { - void *array[50]; - size_t size; - char **strings; - size_t i; - - size = backtrace (array, 40); - strings = backtrace_symbols (array, size); - - for (i = 0; i < size; i++) - { - std::cerr << strings[i] << std::endl; - } - - free (strings); - } -#else - #if (_MSC_VER >= 1400) // Visual Studio 2005 - #include +// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE +// +// 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, 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 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +// File : Basics_Utils.cxx +// Author : Alexander A. BORODIN +// Module : SALOME +// + +#include "Basics_Utils.hxx" +#include +#include + +#ifndef WIN32 +#include +#include +#include +#endif + + +namespace Kernel_Utils +{ + // threadsafe + std::string GetHostname() + { + int ls = 100, r = 1; + char *s; + + while (ls < 10000 && r) + { + ls *= 2; + s = new char[ls]; + r = gethostname(s, ls-1);//threadsafe see man 7 pthread + switch (r) + { + case 0: + break; + default: +#ifdef EINVAL + case EINVAL: +#endif +#ifdef ENAMETOOLONG + case ENAMETOOLONG: +#endif +#ifdef WIN32 + case WSAEFAULT: +#endif + delete [] s; + continue; + } + + } + + if (r != 0) + { + s = new char[50]; + strcpy(s, "localhost"); + } + + // remove all after '.' + char *aDot = (strchr(s,'.')); + if (aDot) aDot[0] = '\0'; + + std::string p = s; + delete [] s; + return p; + } + + Localizer::Localizer() + { + myCurLocale = setlocale(LC_NUMERIC, 0); + setlocale(LC_NUMERIC, "C"); + } + + Localizer::~Localizer() + { + setlocale(LC_NUMERIC, myCurLocale.c_str()); + } + + std::string GetGUID( GUIDtype type ) + { + std::string guid; + + switch ( type ) { + case DefUserID: + guid = "FFFFFFFF-D9CD-11d6-945D-1050DA506788"; break; + case ObjectdID: + guid = "C08F3C95-F112-4023-8776-78F1427D0B6D"; break; + } + + return guid; + } + + const wchar_t* decode(const char* encoded) + { + setlocale(LC_ALL, ""); + size_t length = strlen(encoded) + sizeof(char); + wchar_t* decoded = new wchar_t[length]; + memset( decoded, '\0', length); + mbstowcs(decoded, encoded, length); + return decoded; + } + + const wchar_t* decode_s(std::string encoded) + { + return decode(encoded.c_str()); + } + + const char* encode(const wchar_t* decoded) + { + setlocale(LC_ALL, ""); + size_t length = std::wcslen(decoded) + sizeof(wchar_t); + char* encoded = new char[length]; + memset( encoded, '\0', length); + wcstombs(encoded, decoded, length); + return encoded; + } + + std::string encode_s(const wchar_t* decoded) + { + return std::string(encode(decoded)); + } + +#ifndef WIN32 + void print_traceback() + { + void *array[50]; + size_t size; + char **strings; + size_t i; + + size = backtrace (array, 40); + strings = backtrace_symbols (array, size); + + for (i = 0; i < size; i++) + { + std::cerr << strings[i] << std::endl; + } + + free (strings); + } +#else + #if (_MSC_VER >= 1400) // Visual Studio 2005 + #include int setenv(const char *name, const char *value, int rewrite) { std::stringstream sstr; @@ -164,8 +164,44 @@ namespace Kernel_Utils if(rewrite || std::string(getenv(name)).length() == 0) return _putenv(sstr.str().c_str()); else return -1; - } - #endif -#endif - -} + } + #endif +#endif + +#if defined(WIN32) + // Convert a wide Unicode string to an UTF8 string + char* utf8_encode(const wchar_t* encoded) + { + if (encoded == NULL) return NULL; + int size_needed = WideCharToMultiByte(CP_UTF8, 0, encoded, std::wcslen(encoded), NULL, 0, NULL, NULL); + char* strTo = new char[ size_needed + 1 ]; + WideCharToMultiByte(CP_UTF8, 0, encoded, std::wcslen(encoded), strTo, size_needed, NULL, NULL); + strTo[size_needed] = '\0'; + return strTo; + } + // Convert an UTF8 string to a wide Unicode String + wchar_t* utf8_decode(const char* decoded) + { + if (decoded == NULL) return NULL; + int size_needed = MultiByteToWideChar(CP_UTF8, 0, decoded, strlen(decoded), NULL, 0); + wchar_t* wstrTo = new wchar_t[ size_needed + 1 ]; + MultiByteToWideChar(CP_UTF8, 0, decoded, strlen(decoded), wstrTo, size_needed); + wstrTo[size_needed] = '\0'; + return wstrTo; + } + // Convert a wide Unicode std::wstring to an UTF8 string + std::string utf8_encode_s(const std::wstring& encoded) { + char* anEncoded = utf8_encode(encoded.c_str()); + std::string res = std::string(anEncoded); + delete [] anEncoded; + return res; + } + // Convert an UTF8 std::string to a wide Unicode std::wstring + std::wstring utf8_decode_s(const std::string& decoded) { + wchar_t* aDecoded = utf8_decode(decoded.c_str()); + std::wstring res = std::wstring(aDecoded); + delete [] aDecoded; + return res; + } +#endif +} diff --git a/src/Basics/Basics_Utils.hxx b/src/Basics/Basics_Utils.hxx index f4a90c69e..2cf8e5b6a 100644 --- a/src/Basics/Basics_Utils.hxx +++ b/src/Basics/Basics_Utils.hxx @@ -61,6 +61,13 @@ namespace Kernel_Utils BASICS_EXPORT const char* encode(const wchar_t* decoded); BASICS_EXPORT std::string encode_s(const wchar_t* decoded); +#ifdef WIN32 + BASICS_EXPORT char* utf8_encode(const wchar_t* encoded); + BASICS_EXPORT wchar_t* utf8_decode(const char* decoded); + BASICS_EXPORT std::string utf8_encode_s(const std::wstring& encoded); + BASICS_EXPORT std::wstring utf8_decode_s(const std::string& decoded); +#endif + //! Get predefined GUID BASICS_EXPORT std::string GetGUID( GUIDtype ); #ifndef WIN32 diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index d34b36ba8..7f820b2bd 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -554,7 +554,12 @@ Engines_Container_i::load_component_CppImplementation(const char* componentName, } #else HINSTANCE handle; - handle = LoadLibrary( impl_name.c_str() ); +#ifdef UNICODE + std::wstring libToLoad = Kernel_Utils::utf8_decode_s( impl_name ); +#else + std::string libToLoad = impl_name; +#endif + handle = LoadLibrary(libToLoad.c_str() ); if ( !handle ) { reason="ImplementationNotFound"; diff --git a/src/DF/CMakeLists.txt b/src/DF/CMakeLists.txt index a3a2117c3..590ab6ee2 100755 --- a/src/DF/CMakeLists.txt +++ b/src/DF/CMakeLists.txt @@ -19,6 +19,7 @@ INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../Basics ) ADD_DEFINITIONS(${BOOST_DEFINITIONS}) @@ -35,7 +36,7 @@ ADD_LIBRARY(DF ${DF_SOURCES}) INSTALL(TARGETS DF EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_LIBS}) ADD_EXECUTABLE(testDF testDF.cxx) -TARGET_LINK_LIBRARIES(testDF DF) +TARGET_LINK_LIBRARIES(testDF DF SALOMEBasics) INSTALL(TARGETS testDF DESTINATION ${SALOME_INSTALL_BINS}) FILE(GLOB COMMON_HEADERS_HXX "${CMAKE_CURRENT_SOURCE_DIR}/*.hxx") diff --git a/src/DF/testDF.cxx b/src/DF/testDF.cxx index 9475183ed..9da7d9c2b 100644 --- a/src/DF/testDF.cxx +++ b/src/DF/testDF.cxx @@ -34,6 +34,9 @@ #include "DF_Container.hxx" #include "DF_ChildIterator.hxx" +#include "Basics_Utils.hxx" +#include "Basics_DirUtils.hxx" + #ifndef WIN32 #include #include @@ -86,12 +89,21 @@ void GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, in std::string GetUserName() { #ifdef WIN32 - char* pBuff = new char[UNLEN + 1]; +#ifdef UNICODE + wchar_t* pBuff = new wchar_t[UNLEN + 1]; +#else + char* pBuff = new char[UNLEN + 1]; +#endif DWORD dwSize = UNLEN + 1; std::string retVal; GetUserName ( pBuff, &dwSize ); - std::string theTmpUserName(pBuff,(int)dwSize -1 ); +#ifdef UNICODE + std::wstring theTmpUserName(pBuff, (int)dwSize - 1); + retVal = Kernel_Utils::utf8_encode_s(theTmpUserName); +#else + std::string theTmpUserName(pBuff, (int)dwSize - 1); retVal = theTmpUserName; +#endif delete [] pBuff; return retVal; #else @@ -142,17 +154,7 @@ std::string GetDirFromPath(const std::string& thePath) { bool Exists(const std::string thePath) { -#ifdef WIN32 - if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) { - if ( GetLastError () != ERROR_FILE_NOT_FOUND ) { - return false; - } - } -#else - int status = access ( thePath.c_str() , F_OK ); - if (status != 0) return false; -#endif - return true; + return Kernel_Utils::IsExists( thePath ); } diff --git a/src/HDFPersist/CMakeLists.txt b/src/HDFPersist/CMakeLists.txt index 58f9f9224..0b6d7ae21 100755 --- a/src/HDFPersist/CMakeLists.txt +++ b/src/HDFPersist/CMakeLists.txt @@ -20,6 +20,7 @@ INCLUDE_DIRECTORIES( ${HDF5_INCLUDE_DIRS} ${MPI_INCLUDE_DIRS} + ${CMAKE_CURRENT_SOURCE_DIR}/../Basics ) FILE(GLOB SalomeHDFPersist_SOURCES_C "${CMAKE_CURRENT_SOURCE_DIR}/HDF*.c") @@ -28,7 +29,7 @@ SET(SalomeHDFPersist_SOURCES ${SalomeHDFPersist_SOURCES_C} ${SalomeHDFPersist_SO ADD_DEFINITIONS(${HDF5_DEFINITIONS}) ADD_LIBRARY(SalomeHDFPersist ${SalomeHDFPersist_SOURCES}) -TARGET_LINK_LIBRARIES(SalomeHDFPersist ${HDF5_LIBRARIES} ${MPI_LIBRARIES} ${PLATFORM_LIBS}) +TARGET_LINK_LIBRARIES(SalomeHDFPersist ${HDF5_LIBRARIES} ${MPI_LIBRARIES} ${PLATFORM_LIBS} SALOMEBasics) INSTALL(TARGETS SalomeHDFPersist EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_LIBS}) FILE(GLOB COMMON_HEADERS_HXX "${CMAKE_CURRENT_SOURCE_DIR}/*.hxx") diff --git a/src/HDFPersist/HDFascii.cc b/src/HDFPersist/HDFascii.cc index 410446bb7..e1c4f04a1 100644 --- a/src/HDFPersist/HDFascii.cc +++ b/src/HDFPersist/HDFascii.cc @@ -28,6 +28,8 @@ // #include "HDFOI.hxx" +#include "Basics_Utils.hxx" + #include #include #include @@ -86,14 +88,17 @@ 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; return res; @@ -691,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 { @@ -723,9 +736,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 @@ -789,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; @@ -804,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 } diff --git a/src/HDFPersist/HDFconvert.cc b/src/HDFPersist/HDFconvert.cc index 8b39b3377..bb7803814 100644 --- a/src/HDFPersist/HDFconvert.cc +++ b/src/HDFPersist/HDFconvert.cc @@ -68,9 +68,13 @@ int HDFConvert::FromAscii(const std::string& file, const HDFcontainerObject & hd #elif defined WIN32 #define SHMEMSIZE 4096 - +#ifdef UNICODE + std::wstring empty = L""; +#else + std::string empty = ""; +#endif HANDLE hMapObject = CreateFileMapping( - INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, SHMEMSIZE, ""); + INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, SHMEMSIZE, empty.c_str() ); if (hMapObject != NULL) { // Get a pointer to the file-mapped shared memory. buffer = ( char* ) MapViewOfFile( diff --git a/src/ModuleCatalog/SALOME_ModuleCatalog_impl.cxx b/src/ModuleCatalog/SALOME_ModuleCatalog_impl.cxx index 6fd8da5f6..f37303fcf 100644 --- a/src/ModuleCatalog/SALOME_ModuleCatalog_impl.cxx +++ b/src/ModuleCatalog/SALOME_ModuleCatalog_impl.cxx @@ -34,8 +34,19 @@ #include #include "utilities.h" +#include + #ifdef WIN32 # include +# include +#include +#include +#include +#include +#include +#include +#include +#include #else # include #endif @@ -826,9 +837,13 @@ SALOME_ModuleCatalogImpl::Private::_parse_xml_file(const char* file, ParserComponents _moduleList; SALOME_ModuleCatalog_Handler* handler = new SALOME_ModuleCatalog_Handler(_pathList,_moduleList,typeMap,typeList); - +#if defined(WIN32) + const wchar_t* w_file = Kernel_Utils::utf8_decode(file); + FILE* aFile = _wfopen(w_file, L"r"); +#else FILE* aFile = fopen(file, "r"); - +#endif + if (aFile != NULL) { xmlDocPtr aDoc = xmlReadFile(file, NULL, 0); diff --git a/src/SALOMEDS/SALOMEDS_AttributeStudyProperties.cxx b/src/SALOMEDS/SALOMEDS_AttributeStudyProperties.cxx index 0bacc26b7..2bcb37dc2 100644 --- a/src/SALOMEDS/SALOMEDS_AttributeStudyProperties.cxx +++ b/src/SALOMEDS/SALOMEDS_AttributeStudyProperties.cxx @@ -63,7 +63,7 @@ std::string SALOMEDS_AttributeStudyProperties::GetUserName() #ifndef WIN32 else aName = ((SALOMEDS::AttributeStudyProperties_var)SALOMEDS::AttributeStudyProperties::_narrow(_corba_impl))->GetUserName(); #else - else aName = ((SALOMEDS::AttributeStudyProperties_var)SALOMEDS::AttributeStudyProperties::_narrow(_corba_impl))->GetUserNameA(); + else aName = ((SALOMEDS::AttributeStudyProperties_var)SALOMEDS::AttributeStudyProperties::_narrow(_corba_impl))->GetUserNameW(); #endif return aName; } diff --git a/src/SALOMEDS/SALOMEDS_Study.cxx b/src/SALOMEDS/SALOMEDS_Study.cxx index 337fe6c51..760762311 100644 --- a/src/SALOMEDS/SALOMEDS_Study.cxx +++ b/src/SALOMEDS/SALOMEDS_Study.cxx @@ -130,7 +130,7 @@ bool SALOMEDS_Study::Open(const std::string& theStudyUrl) { if(CORBA::is_nil(_corba_impl)) return false; - std::wstring wtheStudyUrl = std::wstring(theStudyUrl.begin(), theStudyUrl.end()); + std::wstring wtheStudyUrl = Kernel_Utils::decode_s( theStudyUrl ); if (!_corba_impl->Open( (wchar_t*)wtheStudyUrl.c_str() ) ) return false; diff --git a/src/SALOMEDS/SALOMEDS_Study_i.cxx b/src/SALOMEDS/SALOMEDS_Study_i.cxx index 748aa8e1f..c243132ab 100644 --- a/src/SALOMEDS/SALOMEDS_Study_i.cxx +++ b/src/SALOMEDS/SALOMEDS_Study_i.cxx @@ -401,9 +401,8 @@ bool SALOMEDS_Study_i::Open(const wchar_t* aWUrl) Unexpect aCatch(SalomeException); MESSAGE("Begin of SALOMEDS_Study_i::Open"); - std::string aUrl = Kernel_Utils::encode_s(aWUrl); - bool res = _impl->Open(std::string(aUrl)); + bool res = _impl->Open( aUrl ); // update desktop title with new study name //NameChanged(); diff --git a/src/SALOMEDSClient/SALOMEDSClient_ClientFactory.cxx b/src/SALOMEDSClient/SALOMEDSClient_ClientFactory.cxx index 2d0a83644..a2d151a0d 100644 --- a/src/SALOMEDSClient/SALOMEDSClient_ClientFactory.cxx +++ b/src/SALOMEDSClient/SALOMEDSClient_ClientFactory.cxx @@ -25,7 +25,11 @@ #ifdef WIN32 #include static HMODULE _libHandle = 0; +#ifdef UNICODE +#define SALOMEDS_LIB_NAME L"SalomeDS.dll" +#else #define SALOMEDS_LIB_NAME "SalomeDS.dll" +#endif #else #include static void* _libHandle = NULL; diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx index 973f7ce6b..b873c891b 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx @@ -777,10 +777,20 @@ bool SALOMEDSImpl_Study::Impl_SaveAs(const std::string& aStudyUrl, #else aCmd ="ls -1 \"" + aStudyTmpDir +"\" > " + aTmpFile; #endif +#if defined(WIN32) && defined(UNICODE) + std::wstring awCmd = Kernel_Utils::utf8_decode_s(aCmd); + _wsystem( awCmd.c_str() ); +#else system(aCmd.c_str()); +#endif // Iterate and move files in the temporary directory +#if defined(WIN32) && defined(UNICODE) + std::wstring awTmpFile = Kernel_Utils::utf8_decode_s(aTmpFile); + FILE* fp = _wfopen(awTmpFile.c_str(), L"rb"); +#else FILE* fp = fopen(aTmpFile.c_str(), "rb"); +#endif if (!fp) { URL( anOldName ); // VSR: restore previous url if operation is failed return false; @@ -796,7 +806,12 @@ bool SALOMEDSImpl_Study::Impl_SaveAs(const std::string& aStudyUrl, #else aCmd = "mv -f \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl)+"\""; #endif +#if defined(WIN32) && defined(UNICODE) + std::wstring awCmd = Kernel_Utils::utf8_decode_s(aCmd); + errors = _wsystem(awCmd.c_str()); +#else errors = system(aCmd.c_str()); +#endif } delete []buffer; @@ -804,14 +819,27 @@ bool SALOMEDSImpl_Study::Impl_SaveAs(const std::string& aStudyUrl, // Perform cleanup #ifdef WIN32 - DeleteFileA(aTmpFile.c_str()); +#ifdef UNICODE + std::wstring aTmpFileToDelete = Kernel_Utils::utf8_decode_s(aTmpFile); + +#else + std::string aTmpFileToDelete = aTmpFile; +#endif + DeleteFile(aTmpFileToDelete.c_str()); #else unlink(aTmpFile.c_str()); #endif #ifdef WIN32 - RemoveDirectoryA(aTmpFileDir.c_str()); - RemoveDirectoryA(aStudyTmpDir.c_str()); +#ifdef UNICODE + std::wstring aTmpFileDirToDelete = Kernel_Utils::utf8_decode_s( aTmpFileDir ); + std::wstring aStudyTmpDirToDelete = Kernel_Utils::utf8_decode_s( aStudyTmpDir ); +#else + std::string aTmpFileDirToDelete = aTmpFileDir; + std::string aStudyTmpDirToDelete = aStudyTmpDir; +#endif + RemoveDirectory( aTmpFileDirToDelete.c_str() ); + RemoveDirectory( aStudyTmpDirToDelete.c_str() ); #else rmdir(aTmpFileDir.c_str()); rmdir(aStudyTmpDir.c_str()); diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Tool.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_Tool.cxx index 1dca26e6e..3274fb5ed 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_Tool.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_Tool.cxx @@ -34,6 +34,9 @@ #include #include +#include "Basics_DirUtils.hxx" +#include "Basics_Utils.hxx" + #include "SALOMEDSImpl_Tool.hxx" #ifndef WIN32 @@ -52,82 +55,16 @@ bool SALOMEDS_Exists(const std::string thePath) { -#ifdef WIN32 - if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) { - if ( GetLastError () == ERROR_FILE_NOT_FOUND ) { - return false; - } - } -#else - int status = access ( thePath.c_str() , F_OK ); - if (status != 0) return false; -#endif - return true; + return Kernel_Utils::IsExists( thePath ); } - - - //============================================================================ // function : GetTempDir // purpose : Return a temp directory to store created files like "/tmp/sub_dir/" //============================================================================ std::string SALOMEDSImpl_Tool::GetTmpDir() { - //Find a temporary directory to store a file - - std::string aTmpDir; - - char *Tmp_dir = getenv("SALOME_TMP_DIR"); - if ( Tmp_dir != NULL && SALOMEDS_Exists( Tmp_dir )) - { - aTmpDir = Tmp_dir; -#ifdef WIN32 - if ( aTmpDir.back() != '\\') aTmpDir += '\\'; -#else - if ( aTmpDir.back() != '/') aTmpDir += '/'; -#endif - } - else - { -#ifdef WIN32 - aTmpDir = "C:\\"; -#else - aTmpDir = "/tmp/"; -#endif - } - - srand( (unsigned int)time( NULL )); - int aRND = 999 + (int) (100000.0*rand() / (RAND_MAX+1.0)); //Get a random number to present a name of a sub directory - char buffer[127]; - sprintf( buffer, "%d", aRND ); - std::string aSubDir( buffer ); - if ( aSubDir.size() <= 1 ) aSubDir = "123409876"; - - aTmpDir += aSubDir; //Get RND sub directory - - std::string aDir = aTmpDir; - - for ( aRND = 0; SALOMEDS_Exists( aDir ); aRND++ ) - { - sprintf(buffer, "%d", aRND); - aDir = aTmpDir + buffer; //Build a unique directory name - } - -#ifdef WIN32 - if ( aDir.back() != '\\') aDir += '\\'; -#else - if ( aDir.back() != '/' ) aDir += '/'; -#endif - - -#ifdef WIN32 - CreateDirectory( aDir.c_str(), NULL ); -#else - mkdir( aDir.c_str(), 0x1ff ); -#endif - - return aDir; + return Kernel_Utils::GetTmpDirByEnv("SALOME_TMP_DIR"); } //============================================================================ @@ -147,7 +84,12 @@ void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory, if(!SALOMEDS_Exists(aFile)) continue; #ifdef WIN32 - DeleteFile(aFile.c_str()); +#if defined(UNICODE) + std::wstring aFileToDelete = Kernel_Utils::utf8_decode_s(aFile); +#else + std::string aFileToDelete = aFile; +#endif + DeleteFile( aFileToDelete.c_str() ); #else unlink(aFile.c_str()); #endif @@ -156,7 +98,12 @@ void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory, if(IsDirDeleted) { if(SALOMEDS_Exists(aDirName)) { #ifdef WIN32 - RemoveDirectory(aDirName.c_str()); +#if defined(UNICODE) + std::wstring aDirToDelete = Kernel_Utils::utf8_decode_s(aDirName); +#else + std::string aDirToDelete = aDirName; +#endif + RemoveDirectory(aDirToDelete.c_str()); #else rmdir(aDirName.c_str()); #endif diff --git a/src/SALOMELocalTrace/LocalTraceBufferPool.cxx b/src/SALOMELocalTrace/LocalTraceBufferPool.cxx index 6a5a962ac..54a5b0bf8 100644 --- a/src/SALOMELocalTrace/LocalTraceBufferPool.cxx +++ b/src/SALOMELocalTrace/LocalTraceBufferPool.cxx @@ -121,8 +121,8 @@ LocalTraceBufferPool* LocalTraceBufferPool::instance() handle = dlopen( impl_name.c_str() , RTLD_LAZY | RTLD_GLOBAL ) ; #else HINSTANCE handle; - std::string impl_name = std::string ("lib") + traceKind + std::string(".dll"); - handle = LoadLibrary( impl_name.c_str() ); + std::string impl_name = std::string ("lib") + traceKind + std::string(".dll"); + handle = LoadLibraryA( impl_name.c_str() ); #endif if ( handle ) { diff --git a/src/TOOLSDS/SALOMEDS_Tool.cxx b/src/TOOLSDS/SALOMEDS_Tool.cxx index 6fffff1fe..5dfa7a9a8 100644 --- a/src/TOOLSDS/SALOMEDS_Tool.cxx +++ b/src/TOOLSDS/SALOMEDS_Tool.cxx @@ -30,6 +30,7 @@ #include "utilities.h" #include "Basics_DirUtils.hxx" +#include "Basics_Utils.hxx" #ifndef WIN32 #include @@ -50,17 +51,7 @@ bool Exists(const std::string thePath) { -#ifdef WIN32 - if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) { - if ( GetLastError () == ERROR_FILE_NOT_FOUND ) { - return false; - } - } -#else - int status = access ( thePath.c_str() , F_OK ); - if (status != 0) return false; -#endif - return true; + return Kernel_Utils::IsExists(thePath); } @@ -143,7 +134,12 @@ void SALOMEDS_Tool::RemoveTemporaryFiles(const std::string& theDirectory, if(!Exists(aFile)) continue; #ifdef WIN32 - DeleteFile(aFile.c_str()); +#if defined(UNICODE) + std::wstring aFileToDelete = Kernel_Utils::utf8_decode_s(aFile); +#else + std::string aFileToDelete = aFile; +#endif + DeleteFile(aFileToDelete.c_str()); #else unlink(aFile.c_str()); #endif @@ -152,7 +148,12 @@ void SALOMEDS_Tool::RemoveTemporaryFiles(const std::string& theDirectory, if(IsDirDeleted) { if(Exists(aDirName)) { #ifdef WIN32 - RemoveDirectory(aDirName.c_str()); +#if defined(UNICODE) + std::wstring aDirToDelete = Kernel_Utils::utf8_decode_s(aDirName); +#else + std::string aDirToDelete = aDirName; +#endif + RemoveDirectory(aDirToDelete.c_str()); #else rmdir(aDirName.c_str()); #endif @@ -197,7 +198,11 @@ namespace std::string aFullPath = aTmpDir + theFiles[i]; if(!Exists(aFullPath)) continue; #ifdef WIN32 +#ifdef UNICODE + std::ifstream aFile(Kernel_Utils::utf8_decode_s(aFullPath).c_str(), std::ios::binary); +#else std::ifstream aFile(aFullPath.c_str(), std::ios::binary); +#endif #else std::ifstream aFile(aFullPath.c_str()); #endif @@ -214,7 +219,8 @@ namespace if ( aNbFiles == 0 ) return (new SALOMEDS::TMPFile); aBufferSize += 4; //4 bytes for a number of the files that will be written to the stream; - unsigned char* aBuffer = new unsigned char[aBufferSize]; + unsigned char* aBuffer = new unsigned char[aBufferSize]; + if(aBuffer == NULL) return (new SALOMEDS::TMPFile); @@ -227,12 +233,16 @@ namespace aCurrentPos = 4; for(i=0; iseekg(0, std::ios::beg); - aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]); + aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]); aFile->close(); delete(aFile); aCurrentPos += aFileSize[i]; @@ -307,7 +317,6 @@ SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream, //Get a temporary directory for saving a file std::string aTmpDir = theToDirectory; - unsigned char *aBuffer = (unsigned char*)theStream.NP_data(); if(aBuffer == NULL) @@ -346,11 +355,15 @@ SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream, std::string aFullPath = aTmpDir + aFileName; #ifdef WIN32 +#ifdef UNICODE + std::ofstream aFile(Kernel_Utils::utf8_decode_s(aFullPath).c_str(), std::ios::binary); +#else std::ofstream aFile(aFullPath.c_str(), std::ios::binary); +#endif #else std::ofstream aFile(aFullPath.c_str()); #endif - aFile.write((char *)(aBuffer+aCurrentPos), aFileSize); + aFile.write((char *)(aBuffer + aCurrentPos), aFileSize); aFile.close(); aCurrentPos += aFileSize; } diff --git a/src/Utils/Utils_ExceptHandlers.cxx b/src/Utils/Utils_ExceptHandlers.cxx index 34d29e13c..c7ad3510a 100644 --- a/src/Utils/Utils_ExceptHandlers.cxx +++ b/src/Utils/Utils_ExceptHandlers.cxx @@ -48,7 +48,8 @@ void printBacktrace(std::stringstream& txt) { typedef USHORT(WINAPI *CaptureStackBackTraceType)(__in ULONG, __in ULONG, __out PVOID*, __out_opt PULONG); - CaptureStackBackTraceType func = (CaptureStackBackTraceType)(GetProcAddress(LoadLibrary("kernel32.dll"), "RtlCaptureStackBackTrace")); + + CaptureStackBackTraceType func = (CaptureStackBackTraceType)(GetProcAddress(LoadLibraryA("kernel32.dll"), "RtlCaptureStackBackTrace")); if (func == NULL) return; diff --git a/src/Utils/Utils_Identity.cxx b/src/Utils/Utils_Identity.cxx index f6cc23699..16922edde 100644 --- a/src/Utils/Utils_Identity.cxx +++ b/src/Utils/Utils_Identity.cxx @@ -89,15 +89,26 @@ const char* const get_pwname( void ) #include #include +#include "Basics_Utils.hxx" + const char* duplicate( const char *const str ) ; const char* get_uname( void ) { - static std::string hostName(4096, 0); +#ifdef UNICODE + std::wstring hostName(4096, 0); +#else + static std::string hostName(4096, 0); +#endif static DWORD nSize = hostName.length(); static int res = ::GetComputerNameEx(ComputerNameDnsFullyQualified, &hostName[0], &nSize); ASSERT( res ); +#ifdef UNICODE + static std::string aRes = Kernel_Utils::utf8_encode_s(hostName); + return aRes.c_str(); +#else return hostName.c_str(); +#endif } const char* get_adip( void ) @@ -127,11 +138,20 @@ const char* get_adip( void ) const char* const get_pwname( void ) { - static std::string retVal(4096, 0); +#ifdef UNICODE + std::wstring retVal(4096, 0); +#else + static std::string retVal(4096, 0); +#endif static DWORD dwSize = retVal.length() + 1; static int res = GetUserName( &retVal[0], &dwSize ); ASSERT( res ); +#ifdef UNICODE + static std::string aRes = Kernel_Utils::utf8_encode_s(retVal); + return aRes.c_str(); +#else return retVal.c_str(); +#endif } PSID getuid() { -- 2.39.2