From: rnv Date: Thu, 21 Feb 2019 08:36:33 +0000 (+0300) Subject: Run SALOME with UNICODE path on Windows platform. X-Git-Tag: V9_3_0b1~2^2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=f91bb8bf5cb60da9732f42320b208236a198311f;p=modules%2Fkernel.git Run SALOME with UNICODE path on Windows platform. --- diff --git a/src/Basics/Basics_DirUtils.cxx b/src/Basics/Basics_DirUtils.cxx index aa147cf4c..daa4b8f55 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 + std::string 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 + std::string 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 056725147..cd18da5fa 100644 --- a/src/Basics/Basics_Utils.cxx +++ b/src/Basics/Basics_Utils.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE +// 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 @@ -40,13 +40,13 @@ namespace Kernel_Utils { 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) + switch (r) { case 0: break; @@ -58,29 +58,29 @@ namespace Kernel_Utils case ENAMETOOLONG: #endif #ifdef WIN32 - case WSAEFAULT: + 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); @@ -168,4 +168,40 @@ namespace Kernel_Utils #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 d06b30baf..ddb351d3b 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 0c8a0a48b..213a778cf 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 2e2208c17..f309b8ab5 100644 --- 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 b387227f9..67363f52b 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 028802aa2..8f3c2bcbc 100644 --- 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 d95f788dc..b595d3c1d 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 a96f12a36..99fc091ce 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 d84227142..8f8c20d2e 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 cc9396c5b..3f758950c 100644 --- a/src/SALOMEDS/SALOMEDS_AttributeStudyProperties.cxx +++ b/src/SALOMEDS/SALOMEDS_AttributeStudyProperties.cxx @@ -62,8 +62,12 @@ std::string SALOMEDS_AttributeStudyProperties::GetUserName() } #ifndef WIN32 else aName = ((SALOMEDS::AttributeStudyProperties_var)SALOMEDS::AttributeStudyProperties::_narrow(_corba_impl))->GetUserName(); +#else +#ifdef UNICODE + else aName = ((SALOMEDS::AttributeStudyProperties_var)SALOMEDS::AttributeStudyProperties::_narrow(_corba_impl))->GetUserNameW(); #else else aName = ((SALOMEDS::AttributeStudyProperties_var)SALOMEDS::AttributeStudyProperties::_narrow(_corba_impl))->GetUserNameA(); +#endif #endif return aName; } diff --git a/src/SALOMEDS/SALOMEDS_Study.cxx b/src/SALOMEDS/SALOMEDS_Study.cxx index 1905d7d25..2a14971d7 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 93ba73d5e..6573f4e0d 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 b6ee7985d..ad3954ed8 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 3e63782ae..62a2ea30d 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()); @@ -2091,7 +2119,12 @@ bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath, //Create a file that will contain a main Study script std::fstream fp; +#if defined(WIN32) && defined(UNICODE) + std::wstring aConverterFN = Kernel_Utils::utf8_decode_s(aFileName); + fp.open(aConverterFN.c_str(), std::ios::out); +#else fp.open(aFileName.c_str(), std::ios::out); +#endif #ifdef WIN32 bool isOpened = fp.is_open(); @@ -2209,7 +2242,12 @@ bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath, aFileName += aScriptName+ std::string(".py"); aSeqOfFileNames.push_back(aFileName); +#if defined(WIN32) && defined(UNICODE) + std::wstring aConverterFN2 = Kernel_Utils::utf8_decode_s(aFileName); + fp2.open(aConverterFN2.c_str(), std::ios::out); +#else fp2.open(aFileName.c_str(), std::ios::out); +#endif #ifdef WIN32 isOpened = fp2.is_open(); diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Tool.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_Tool.cxx index bf95cf018..cbf835c95 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 46acee2c1..d4f66776f 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 95906c30e..d83e243b2 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 5a1be7cbe..59886a18e 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 88094f18a..853b27e79 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() {