From 05ff5b3bfe29a9e9d3b181ccdc4d745616dc26c7 Mon Sep 17 00:00:00 2001 From: Vladimir Stoiakin Date: Wed, 24 Aug 2022 13:53:25 +0300 Subject: [PATCH] fileutils: migrate to boost::nowide Change-Id: Iceae2a19ce3febce62f7916af8b4242e06a7e1d4 --- CMakeLists.txt | 4 +- README | 10 +- compat/msvc/package.json | 3 +- configure.ac | 2 + contrib/src/nowide/SHA512SUMS | 1 + contrib/src/nowide/package.json | 20 +++ contrib/src/nowide/rules.mak | 21 +++ meson.build | 1 + src/archiver.cpp | 7 +- src/data_transfer.cpp | 24 +-- src/data_transfer.h | 6 +- src/debug_utils.h | 6 +- src/fileutils.cpp | 145 ++++++------------ src/fileutils.h | 13 -- src/im/message_engine.cpp | 14 +- src/jamidht/account_manager.cpp | 1 - src/jamidht/archive_account_manager.cpp | 6 +- src/jamidht/contact_list.cpp | 8 +- src/jamidht/conversation.cpp | 7 +- src/jamidht/conversation_module.cpp | 8 +- src/jamidht/conversationrepository.cpp | 33 ++-- src/jamidht/gitserver.cpp | 3 +- src/jamidht/jamiaccount.cpp | 26 ++-- src/jamidht/namedirectory.cpp | 6 +- src/logger.cpp | 13 +- src/manager.cpp | 25 +-- src/media/audio/pulseaudio/pulselayer.cpp | 1 - src/media/audio/ringbuffer.h | 1 - src/media/audio/sound/audiofile.cpp | 2 +- src/media/libav_utils.cpp | 3 +- src/media/media_encoder.cpp | 4 +- src/media/video/sinkclient.cpp | 3 +- .../video/winvideo/video_device_impl.cpp | 5 +- .../winvideo/video_device_monitor_impl.cpp | 3 +- src/meson.build | 3 +- src/plugin/jamipluginmanager.cpp | 4 +- src/plugin/pluginpreferencesutils.cpp | 20 +-- src/plugin/pluginsutils.cpp | 4 +- src/ring_api.cpp | 4 +- src/sip/sipaccount.cpp | 5 +- src/string_utils.cpp | 33 ---- src/string_utils.h | 5 - 42 files changed, 238 insertions(+), 275 deletions(-) create mode 100644 contrib/src/nowide/SHA512SUMS create mode 100644 contrib/src/nowide/package.json create mode 100644 contrib/src/nowide/rules.mak diff --git a/CMakeLists.txt b/CMakeLists.txt index e8bdef9cd..44d8d2f27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -164,7 +164,8 @@ if(MSVC) "${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/webrtc-audio-processing;" "${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/portaudio/include;" "${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/pjproject/pjmedia/include;" - "${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/libgit2/include" + "${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/libgit2/include;" + "${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/nowide/include;" ) endif() @@ -281,6 +282,7 @@ if(MSVC) ${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/webrtc-audio-processing/build/Release/webrtc-audio-processing.lib ${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/portaudio/build/Release/portaudio_static_x64.lib ${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/libgit2/msvc/Release/git2.lib + ${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/nowide/build/Release/nowide.lib /ignore:4006" ) diff --git a/README b/README index 2b7e512fa..273084c3d 100644 --- a/README +++ b/README @@ -81,11 +81,11 @@ B) With Meson cd ../../ mkdir build -export PATH=$PATH:`pwd`/contrib/`cc -dumpmachine`/bin -meson -Dpkg_config_path=`pwd`/contrib/`cc -dumpmachine`/lib/pkgconfig -Ddefault_library=static -Dinterfaces=dbus build -cd build -ninja -ninja install +path_deps=`pwd`/contrib/`cc -dumpmachine` +export PATH=$path_deps/bin:$PATH +meson build -Dpkg_config_path=$path_deps/lib/pkgconfig -Dcmake_prefix_path=$path_deps +meson compile -C build +meson install -C build Done ! diff --git a/compat/msvc/package.json b/compat/msvc/package.json index d4c4193a2..084588d6f 100644 --- a/compat/msvc/package.json +++ b/compat/msvc/package.json @@ -13,7 +13,8 @@ "yaml-cpp", "libarchive", "webrtc-audio-processing", - "libgit2" + "libgit2", + "nowide" ], "configuration": "ReleaseLib_win32", "project_paths": ["ring-daemon.vcxproj"] diff --git a/configure.ac b/configure.ac index dcdb75c4a..8b503fbea 100644 --- a/configure.ac +++ b/configure.ac @@ -618,6 +618,8 @@ AC_DEFINE_UNQUOTED([HAVE_LIBNATPMP], [Define if you have libnatpmp]) AM_CONDITIONAL([BUILD_NATPMP], test "x$with_natpmp" = "xyes") +DAEMONLDFLAGS+=" -lnowide" + # SHM dnl check for shm AS_IF([test "x$with_dbus" == xyes], diff --git a/contrib/src/nowide/SHA512SUMS b/contrib/src/nowide/SHA512SUMS new file mode 100644 index 000000000..b1fe7ed5d --- /dev/null +++ b/contrib/src/nowide/SHA512SUMS @@ -0,0 +1 @@ +c3748921b85648aa0e89970f2ab24588cbc72d05edd7ddf4f61a607d9ecbddd45e9e6799d2ed83386c43045b9487693e027494a81b11a6a7bdfaa939d1251938 nowide_standalone_v11.2.0.tar.gz diff --git a/contrib/src/nowide/package.json b/contrib/src/nowide/package.json new file mode 100644 index 000000000..298b3950e --- /dev/null +++ b/contrib/src/nowide/package.json @@ -0,0 +1,20 @@ +{ + "name": "nowide", + "version": "11.2.0", + "url": "https://github.com/boostorg/nowide/releases/download/v__VERSION__/nowide_standalone_v__VERSION__.tar.gz", + "deps": [], + "use_cmake" : true, + "defines": [ + "NOWIDE_INSTALL=ON", + "BUILD_TESTING=OFF" + ], + "patches": [], + "win_patches": [], + "project_paths": [], + "with_env" : "", + "custom_scripts": { + "pre_build": [], + "build": [], + "post_build": [] + } +} diff --git a/contrib/src/nowide/rules.mak b/contrib/src/nowide/rules.mak new file mode 100644 index 000000000..f88875fae --- /dev/null +++ b/contrib/src/nowide/rules.mak @@ -0,0 +1,21 @@ +NOWIDE_VERSION := 11.2.0 +NOWIDE_URL := https://github.com/boostorg/nowide/releases/download/v$(NOWIDE_VERSION)/nowide_standalone_v$(NOWIDE_VERSION).tar.gz + +PKGS += nowide + +NOWIDE_CMAKECONF := -DNOWIDE_INSTALL=ON \ + -DBUILD_TESTING=OFF + +$(TARBALLS)/nowide_standalone_v$(NOWIDE_VERSION).tar.gz: + $(call download,$(NOWIDE_URL)) + +.sum-nowide: nowide_standalone_v$(NOWIDE_VERSION).tar.gz + +nowide: nowide_standalone_v$(NOWIDE_VERSION).tar.gz + $(UNPACK) + $(MOVE) + +.nowide: nowide toolchain.cmake .sum-nowide + cd $< && $(HOSTVARS) $(CMAKE) . $(NOWIDE_CMAKECONF) + cd $< && $(MAKE) install + touch $@ diff --git a/meson.build b/meson.build index 65f560514..ca6a0c166 100644 --- a/meson.build +++ b/meson.build @@ -33,6 +33,7 @@ depfmt = dependency('fmt', version: '>= 5.3') depyamlcpp = dependency('yaml-cpp', version: '>= 0.5.1') depjsoncpp = dependency('jsoncpp', version: '>= 1.6.5') depzlib = dependency('zlib') +depnowide = dependency('nowide', method: 'cmake') if get_option('interfaces').contains('dbus') depdbuscpp = dependency('dbus-c++-1') diff --git a/src/archiver.cpp b/src/archiver.cpp index 472bb91de..dba8eea8d 100644 --- a/src/archiver.cpp +++ b/src/archiver.cpp @@ -48,7 +48,8 @@ extern "C" { #endif #include -#include +#include +#include using namespace std::literals; @@ -105,7 +106,7 @@ accountToJsonValue(const std::map& details) || i.first == DRing::Account::ConfProperties::TLS::CERTIFICATE_FILE || i.first == DRing::Account::ConfProperties::TLS::PRIVATE_KEY_FILE) { // replace paths by the files content - std::ifstream ifs = fileutils::ifstream(i.second); + nowide::ifstream ifs(i.second); std::string fileContent((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); root[i.first] = fileContent; @@ -218,7 +219,7 @@ gzFile openGzip(const std::string& path, const char* mode) { #ifdef _WIN32 - return gzopen_w(jami::to_wstring(path).c_str(), mode); + return gzopen_w(nowide::widen(path).c_str(), mode); #else return gzopen(path.c_str(), mode); #endif diff --git a/src/data_transfer.cpp b/src/data_transfer.cpp index 41fa2e747..4b30eb6ba 100644 --- a/src/data_transfer.cpp +++ b/src/data_transfer.cpp @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include #include @@ -367,7 +367,7 @@ private: } mutable std::shared_ptr metaInfo_; - mutable std::ifstream input_; + mutable nowide::ifstream input_; mutable bool headerSent_ {false}; bool peerReady_ {false}; const std::string peerUri_; @@ -385,7 +385,7 @@ SubOutgoingFileTransfer::SubOutgoingFileTransfer(DRing::DataTransferId tid, , peerUri_(peerUri) { info_ = metaInfo_->info(); - fileutils::openStream(input_, info_.path, std::ios::in | std::ios::binary); + input_.open(info_.path, std::ios::in | std::ios::binary); if (!input_) throw std::runtime_error("input file open failed"); metaInfo_->addLinkedTransfer(this); @@ -510,7 +510,7 @@ private: OutgoingFileTransfer() = delete; mutable std::shared_ptr metaInfo_; - mutable std::ifstream input_; + mutable nowide::ifstream input_; mutable std::vector> subtransfer_; }; @@ -519,7 +519,7 @@ OutgoingFileTransfer::OutgoingFileTransfer(DRing::DataTransferId tid, const InternalCompletionCb& cb) : DataTransfer(tid, cb) { - fileutils::openStream(input_, info.path, std::ios::binary); + input_.open(info.path, std::ios::binary); if (!input_) throw std::runtime_error("input file open failed"); @@ -574,7 +574,7 @@ private: DRing::DataTransferId internalId_; - std::ofstream fout_; + nowide::ofstream fout_; std::mutex cbMtx_ {}; std::function onFilenameCb_ {}; }; @@ -614,7 +614,7 @@ IncomingFileTransfer::requestFilename(const std::function& channel, channel_->shutdown(); return; } - fileutils::openStream(stream_, info_.path, std::ios::binary | std::ios::in); + stream_.open(info_.path, std::ios::binary | std::ios::in); if (!stream_ || !stream_.is_open()) { channel_->shutdown(); return; @@ -815,7 +815,7 @@ IncomingFile::IncomingFile(const std::shared_ptr& channel, : FileInfo(channel, fileId, interactionId, info) , sha3Sum_(sha3Sum) { - fileutils::openStream(stream_, info_.path, std::ios::binary | std::ios::out); + stream_.open(info_.path, std::ios::binary | std::ios::out); if (!stream_) return; @@ -925,7 +925,7 @@ public: } void saveWaiting() { - std::ofstream file(waitingPath_, std::ios::trunc | std::ios::binary); + nowide::ofstream file(waitingPath_, std::ios::trunc | std::ios::binary); msgpack::pack(file, waitingIds_); } @@ -1146,7 +1146,7 @@ TransferManager::info(const std::string& fileId, progress = itI->second->info().bytesProgress; return true; } else if (fileutils::isFile(path)) { - std::ifstream transfer(path, std::ios::binary); + nowide::ifstream transfer(path, std::ios::binary); transfer.seekg(0, std::ios::end); progress = transfer.tellg(); if (itW != pimpl_->waitingIds_.end()) { diff --git a/src/data_transfer.h b/src/data_transfer.h index 4965a7c62..cbf6b02a7 100644 --- a/src/data_transfer.h +++ b/src/data_transfer.h @@ -26,7 +26,7 @@ #include #include -#include +#include #include namespace jami { @@ -96,7 +96,7 @@ private: { return std::static_pointer_cast(shared_from_this()); } - std::ofstream stream_; + nowide::ofstream stream_; std::string sha3Sum_ {}; }; @@ -114,7 +114,7 @@ public: void cancel() override; private: - std::ifstream stream_; + nowide::ifstream stream_; size_t start_ {0}; size_t end_ {0}; }; diff --git a/src/debug_utils.h b/src/debug_utils.h index 25238219d..41237c7bd 100644 --- a/src/debug_utils.h +++ b/src/debug_utils.h @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include #include @@ -94,7 +94,7 @@ public: AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP}; - f_ = std::ofstream(filename, std::ios_base::out | std::ios_base::binary); + f_ = nowide::ofstream(filename, std::ios_base::out | std::ios_base::binary); f_.imbue(std::locale::classic()); f_ << "RIFF----WAVEfmt "; if (std::find(v.begin(), v.end(), format_) == v.end()) { @@ -189,7 +189,7 @@ public: } private: - std::ofstream f_; + nowide::ofstream f_; size_t dataChunk_ {0}; size_t factChunk_ {0}; size_t length_ {0}; diff --git a/src/fileutils.cpp b/src/fileutils.cpp index 324e26b2d..9678cec1f 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -43,7 +43,6 @@ #ifdef _WIN32 #include -#include "string_utils.h" #endif #include @@ -75,12 +74,14 @@ #include #include -#include +#include #include #include #include -#include +#include +#include +#include #include #include #include @@ -99,34 +100,22 @@ #include #endif -#ifndef _MSC_VER -#define PROTECTED_GETENV(str) \ - ({ \ - char* envvar_ = getenv((str)); \ - envvar_ ? envvar_ : ""; \ - }) - -#define XDG_DATA_HOME (PROTECTED_GETENV("XDG_DATA_HOME")) -#define XDG_CONFIG_HOME (PROTECTED_GETENV("XDG_CONFIG_HOME")) -#define XDG_CACHE_HOME (PROTECTED_GETENV("XDG_CACHE_HOME")) -#else -const wchar_t* -winGetEnv(const wchar_t* name) +inline const std::string +getProtectedEnvString(const char* name) { - const DWORD buffSize = 65535; - static wchar_t buffer[buffSize]; - if (GetEnvironmentVariable(name, buffer, buffSize)) { - return buffer; - } else { - return L""; - } + char* envvar_ = nowide::getenv(name); + return envvar_ ? envvar_ : ""; } +#define PROTECTED_GETENV(str) getProtectedEnvString(str) -#define PROTECTED_GETENV(str) winGetEnv(str) - -#define JAMI_DATA_HOME PROTECTED_GETENV(L"JAMI_DATA_HOME") -#define JAMI_CONFIG_HOME PROTECTED_GETENV(L"JAMI_CONFIG_HOME") -#define JAMI_CACHE_HOME PROTECTED_GETENV(L"JAMI_CACHE_HOME") +#ifndef _MSC_VER +#define XDG_DATA_HOME PROTECTED_GETENV("XDG_DATA_HOME") +#define XDG_CONFIG_HOME PROTECTED_GETENV("XDG_CONFIG_HOME") +#define XDG_CACHE_HOME PROTECTED_GETENV("XDG_CACHE_HOME") +#else +#define JAMI_DATA_HOME PROTECTED_GETENV("JAMI_DATA_HOME") +#define JAMI_CONFIG_HOME PROTECTED_GETENV("JAMI_CONFIG_HOME") +#define JAMI_CACHE_HOME PROTECTED_GETENV("JAMI_CACHE_HOME") #endif #define PIDFILE ".ring.pid" @@ -218,10 +207,10 @@ isFile(const std::string& path, bool resolveSymlink) #ifdef _WIN32 if (resolveSymlink) { struct _stat64i32 s; - if (_wstat(jami::to_wstring(path).c_str(), &s) == 0) + if (_wstat(nowide::widen(path).c_str(), &s) == 0) return S_ISREG(s.st_mode); } else { - DWORD attr = GetFileAttributes(jami::to_wstring(path).c_str()); + DWORD attr = GetFileAttributesW(nowide::widen(path).c_str()); if ((attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY) && !(attr & FILE_ATTRIBUTE_REPARSE_POINT)) return true; @@ -264,7 +253,7 @@ isSymLink(const std::string& path) if (lstat(path.c_str(), &s) == 0) return S_ISLNK(s.st_mode); #elif !defined(_MSC_VER) - DWORD attr = GetFileAttributes(jami::to_wstring(path).c_str()); + DWORD attr = GetFileAttributesW(nowide::widen(path).c_str()); if (attr & FILE_ATTRIBUTE_REPARSE_POINT) return true; #endif @@ -289,13 +278,13 @@ writeTime(const std::string& path) ext_params.dwSecurityQosFlags = SECURITY_ANONYMOUS; ext_params.lpSecurityAttributes = nullptr; ext_params.hTemplateFile = nullptr; - HANDLE h = CreateFile2(jami::to_wstring(path).c_str(), + HANDLE h = CreateFile2(nowide::widen(path).c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &ext_params); #elif _WIN32 - HANDLE h = CreateFileW(jami::to_wstring(path).c_str(), + HANDLE h = CreateFileW(nowide::widen(path).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, @@ -415,7 +404,7 @@ std::vector loadFile(const std::string& path, const std::string& default_dir) { std::vector buffer; - std::ifstream file = ifstream(getFullPath(default_dir, path), std::ios::binary); + nowide::ifstream file(getFullPath(default_dir, path), std::ios::binary); if (!file) throw std::runtime_error("Can't read file: " + path); file.seekg(0, std::ios::end); @@ -433,7 +422,7 @@ std::string loadTextFile(const std::string& path, const std::string& default_dir) { std::string buffer; - std::ifstream file = ifstream(getFullPath(default_dir, path)); + nowide::ifstream file(getFullPath(default_dir, path)); if (!file) throw std::runtime_error("Can't read file: " + path); file.seekg(0, std::ios::end); @@ -450,7 +439,7 @@ loadTextFile(const std::string& path, const std::string& default_dir) void saveFile(const std::string& path, const uint8_t* data, size_t data_size, mode_t UNUSED mode) { - std::ofstream file = fileutils::ofstream(path, std::ios::trunc | std::ios::binary); + nowide::ofstream file(path, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", path.c_str()); return; @@ -659,9 +648,10 @@ get_cache_dir(const char* pkg) + DIR_SEPARATOR_STR + pkg; #else #ifdef _WIN32 - const std::wstring cache_home(JAMI_CACHE_HOME); + const std::string cache_home(JAMI_CACHE_HOME); + if (not cache_home.empty()) - return jami::to_string(cache_home); + return cache_home; #else const std::string cache_home(XDG_CACHE_HOME); if (not cache_home.empty()) @@ -697,7 +687,7 @@ get_home_dir() #elif defined _WIN32 TCHAR path[MAX_PATH]; if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_PROFILE, nullptr, 0, path))) { - return jami::to_string(path); + return nowide::narrow(path); } return program_dir; #else @@ -734,9 +724,9 @@ get_data_dir(const char* pkg) return get_home_dir() + DIR_SEPARATOR_STR + "Library" + DIR_SEPARATOR_STR + "Application Support" + DIR_SEPARATOR_STR + pkg; #elif defined(_WIN32) - const std::wstring data_home(JAMI_DATA_HOME); + const std::string data_home(JAMI_DATA_HOME); if (not data_home.empty()) - return jami::to_string(data_home) + DIR_SEPARATOR_STR + pkg; + return data_home + DIR_SEPARATOR_STR + pkg; if (!strcmp(pkg, "ring")) { return get_home_dir() + DIR_SEPARATOR_STR + ".local" + DIR_SEPARATOR_STR @@ -794,9 +784,9 @@ get_config_dir(const char* pkg) configdir = fileutils::get_home_dir() + DIR_SEPARATOR_STR + "Library" + DIR_SEPARATOR_STR + "Application Support" + DIR_SEPARATOR_STR + pkg; #elif defined(_WIN32) - const std::wstring xdg_env(JAMI_CONFIG_HOME); + const std::string xdg_env(JAMI_CONFIG_HOME); if (not xdg_env.empty()) { - configdir = jami::to_string(xdg_env) + DIR_SEPARATOR_STR + pkg; + configdir = xdg_env + DIR_SEPARATOR_STR + pkg; } else if (!strcmp(pkg, "ring")) { configdir = fileutils::get_home_dir() + DIR_SEPARATOR_STR + ".config" + DIR_SEPARATOR_STR + pkg; @@ -832,14 +822,14 @@ recursive_mkdir(const std::string& path, mode_t mode) #ifndef _WIN32 if (mkdir(path.data(), mode) != 0) { #else - if (_wmkdir(jami::to_wstring(path.data()).c_str()) != 0) { + if (_wmkdir(nowide::widen(path.data()).c_str()) != 0) { #endif if (errno == ENOENT) { recursive_mkdir(path.substr(0, path.find_last_of(DIR_SEPARATOR_CH)), mode); #ifndef _WIN32 if (mkdir(path.data(), mode) != 0) { #else - if (_wmkdir(jami::to_wstring(path.data()).c_str()) != 0) { + if (_wmkdir(nowide::widen(path.data()).c_str()) != 0) { #endif JAMI_ERR("Could not create directory."); return false; @@ -853,8 +843,13 @@ recursive_mkdir(const std::string& path, mode_t mode) bool eraseFile_win32(const std::string& path, bool dosync) { - HANDLE h - = CreateFileA(path.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + HANDLE h = CreateFileW(nowide::widen(path).c_str(), + GENERIC_WRITE, + 0, + 0, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + 0); if (h == INVALID_HANDLE_VALUE) { JAMI_WARN("Can not open file %s for erasing.", path.c_str()); return false; @@ -980,10 +975,10 @@ remove(const std::string& path, bool erase) #ifdef _WIN32 // use Win32 api since std::remove will not unlink directory in use if (isDirectory(path)) - return !RemoveDirectory(jami::to_wstring(path).c_str()); + return !RemoveDirectoryW(nowide::widen(path).c_str()); #endif - return std::remove(path.c_str()); + return nowide::remove(path.c_str()); } int @@ -1001,53 +996,13 @@ removeAll(const std::string& path, bool erase) return remove(path, erase); } -void -openStream(std::ifstream& file, const std::string& path, std::ios_base::openmode mode) -{ -#ifdef _WIN32 - file.open(jami::to_wstring(path), mode); -#else - file.open(path, mode); -#endif -} - -void -openStream(std::ofstream& file, const std::string& path, std::ios_base::openmode mode) -{ -#ifdef _WIN32 - file.open(jami::to_wstring(path), mode); -#else - file.open(path, mode); -#endif -} - -std::ifstream -ifstream(const std::string& path, std::ios_base::openmode mode) -{ -#ifdef _WIN32 - return std::ifstream(jami::to_wstring(path), mode); -#else - return std::ifstream(path, mode); -#endif -} - -std::ofstream -ofstream(const std::string& path, std::ios_base::openmode mode) -{ -#ifdef _WIN32 - return std::ofstream(jami::to_wstring(path), mode); -#else - return std::ofstream(path, mode); -#endif -} - int64_t size(const std::string& path) { - std::ifstream file; - int64_t size; + nowide::ifstream file; + int64_t size = 0; try { - openStream(file, path, std::ios::binary | std::ios::in); + file.open(path, std::ios::binary | std::ios::in); file.seekg(0, std::ios_base::end); size = file.tellg(); file.close(); @@ -1062,11 +1017,11 @@ sha3File(const std::string& path) sha3_512_ctx ctx; sha3_512_init(&ctx); - std::ifstream file; + nowide::ifstream file; try { if (!fileutils::isFile(path)) return {}; - openStream(file, path, std::ios::binary | std::ios::in); + file.open(path, std::ios::binary | std::ios::in); if (!file) return {}; std::vector buffer(8192, 0); @@ -1113,7 +1068,7 @@ int accessFile(const std::string& file, int mode) { #ifdef _WIN32 - return _waccess(jami::to_wstring(file).c_str(), mode); + return _waccess(nowide::widen(file).c_str(), mode); #else return access(file.c_str(), mode); #endif diff --git a/src/fileutils.h b/src/fileutils.h index 3b798758b..31c88cf28 100644 --- a/src/fileutils.h +++ b/src/fileutils.h @@ -132,19 +132,6 @@ int remove(const std::string& path, bool erase = false); */ int removeAll(const std::string& path, bool erase = false); -/** - * Wrappers for fstream opening that will convert paths to wstring - * on windows - */ -void openStream(std::ifstream& file, - const std::string& path, - std::ios_base::openmode mode = std::ios_base::in); -void openStream(std::ofstream& file, - const std::string& path, - std::ios_base::openmode mode = std::ios_base::out); -std::ifstream ifstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::in); -std::ofstream ofstream(const std::string& path, std::ios_base::openmode mode = std::ios_base::out); - int64_t size(const std::string& path); std::string sha3File(const std::string& path); diff --git a/src/im/message_engine.cpp b/src/im/message_engine.cpp index a7340fe87..a4e7b62ab 100644 --- a/src/im/message_engine.cpp +++ b/src/im/message_engine.cpp @@ -28,7 +28,7 @@ #include #include -#include +#include namespace jami { namespace im { @@ -229,9 +229,9 @@ MessageEngine::load() Json::Value root; { std::lock_guard lock(fileutils::getFileLock(savePath_)); - std::ifstream file; - file.exceptions(std::ifstream::failbit | std::ifstream::badbit); - fileutils::openStream(file, savePath_); + nowide::ifstream file; + file.exceptions(nowide::ifstream::failbit | nowide::ifstream::badbit); + file.open(savePath_); if (file.is_open()) file >> root; } @@ -318,9 +318,9 @@ MessageEngine::save_() const wbuilder["commentStyle"] = "None"; wbuilder["indentation"] = ""; const std::unique_ptr writer(wbuilder.newStreamWriter()); - std::ofstream file; - file.exceptions(std::ifstream::failbit | std::ifstream::badbit); - fileutils::openStream(file, path, std::ios::trunc); + nowide::ofstream file; + file.exceptions(nowide::ifstream::failbit | nowide::ifstream::badbit); + file.open(path, std::ios::trunc); if (file.is_open()) writer->write(root, &file); } catch (const std::exception& e) { diff --git a/src/jamidht/account_manager.cpp b/src/jamidht/account_manager.cpp index 94ed89f90..77fe376ec 100644 --- a/src/jamidht/account_manager.cpp +++ b/src/jamidht/account_manager.cpp @@ -30,7 +30,6 @@ #include #include -#include #include namespace jami { diff --git a/src/jamidht/archive_account_manager.cpp b/src/jamidht/archive_account_manager.cpp index f372bb82e..d8ffa25bb 100644 --- a/src/jamidht/archive_account_manager.cpp +++ b/src/jamidht/archive_account_manager.cpp @@ -29,7 +29,7 @@ #include #include -#include +#include namespace jami { @@ -823,10 +823,10 @@ ArchiveAccountManager::exportArchive(const std::string& destinationPath, const s // Export the file auto sourcePath = fileutils::getFullPath(path_, archivePath_); - std::ifstream src(sourcePath, std::ios::in | std::ios::binary); + nowide::ifstream src(sourcePath, std::ios::in | std::ios::binary); if (!src) return false; - std::ofstream dst(destinationPath, std::ios::out | std::ios::binary); + nowide::ofstream dst(destinationPath, std::ios::out | std::ios::binary); dst << src.rdbuf(); return true; } catch (const std::runtime_error& ex) { diff --git a/src/jamidht/contact_list.cpp b/src/jamidht/contact_list.cpp index 755327a16..f3b5a513b 100644 --- a/src/jamidht/contact_list.cpp +++ b/src/jamidht/contact_list.cpp @@ -27,7 +27,7 @@ #include "account_const.h" -#include +#include #include namespace jami { @@ -235,14 +235,14 @@ ContactList::loadContacts() void ContactList::saveContacts() const { - std::ofstream file(path_ + DIR_SEPARATOR_STR "contacts", std::ios::trunc | std::ios::binary); + nowide::ofstream file(path_ + DIR_SEPARATOR_STR "contacts", std::ios::trunc | std::ios::binary); msgpack::pack(file, contacts_); } void ContactList::saveTrustRequests() const { - std::ofstream file(path_ + DIR_SEPARATOR_STR "incomingTrustRequests", + nowide::ofstream file(path_ + DIR_SEPARATOR_STR "incomingTrustRequests", std::ios::trunc | std::ios::binary); msgpack::pack(file, trustRequests_); } @@ -449,7 +449,7 @@ ContactList::loadKnownDevices() void ContactList::saveKnownDevices() const { - std::ofstream file(path_ + DIR_SEPARATOR_STR "knownDevices", std::ios::trunc | std::ios::binary); + nowide::ofstream file(path_ + DIR_SEPARATOR_STR "knownDevices", std::ios::trunc | std::ios::binary); std::map> devices; for (const auto& id : knownDevices_) diff --git a/src/jamidht/conversation.cpp b/src/jamidht/conversation.cpp index 0f6c193ac..1866aac0b 100644 --- a/src/jamidht/conversation.cpp +++ b/src/jamidht/conversation.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef ENABLE_PLUGIN #include "manager.h" @@ -301,7 +302,7 @@ public: } void saveFetched() { - std::ofstream file(fetchedPath_, std::ios::trunc | std::ios::binary); + nowide::ofstream file(fetchedPath_, std::ios::trunc | std::ios::binary); msgpack::pack(file, fetchedDevices_); } @@ -320,7 +321,7 @@ public: } void saveSending() { - std::ofstream file(sendingPath_, std::ios::trunc | std::ios::binary); + nowide::ofstream file(sendingPath_, std::ios::trunc | std::ios::binary); msgpack::pack(file, sending_); } @@ -340,7 +341,7 @@ public: void saveLastDisplayed() const { - std::ofstream file(lastDisplayedPath_, std::ios::trunc | std::ios::binary); + nowide::ofstream file(lastDisplayedPath_, std::ios::trunc | std::ios::binary); msgpack::pack(file, lastDisplayed_); } diff --git a/src/jamidht/conversation_module.cpp b/src/jamidht/conversation_module.cpp index f06459686..54dc4991b 100644 --- a/src/jamidht/conversation_module.cpp +++ b/src/jamidht/conversation_module.cpp @@ -20,7 +20,7 @@ #include "conversation_module.h" -#include +#include #include @@ -849,8 +849,8 @@ ConversationModule::saveConvRequestsToPath( { std::lock_guard lock( fileutils::getFileLock(path + DIR_SEPARATOR_STR + "convRequests")); - std::ofstream file(path + DIR_SEPARATOR_STR + "convRequests", - std::ios::trunc | std::ios::binary); + nowide::ofstream file(path + DIR_SEPARATOR_STR + "convRequests", + std::ios::trunc | std::ios::binary); msgpack::pack(file, conversationsRequests); } @@ -864,7 +864,7 @@ ConversationModule::saveConvInfos(const std::string& accountId, const ConvInfoMa void ConversationModule::saveConvInfosToPath(const std::string& path, const ConvInfoMap& conversations) { - std::ofstream file(path + DIR_SEPARATOR_STR + "convInfo", std::ios::trunc | std::ios::binary); + nowide::ofstream file(path + DIR_SEPARATOR_STR + "convInfo", std::ios::trunc | std::ios::binary); msgpack::pack(file, conversations); } diff --git a/src/jamidht/conversationrepository.cpp b/src/jamidht/conversationrepository.cpp index 87f2c8f28..767ca35a7 100644 --- a/src/jamidht/conversationrepository.cpp +++ b/src/jamidht/conversationrepository.cpp @@ -29,7 +29,8 @@ using random_device = dht::crypto::random_device; #include -#include +#include +#include #include #include #include @@ -374,7 +375,7 @@ add_initial_files(GitRepository& repo, // /admins std::string adminPath = adminsPath + "/" + parentCert->getId().toString() + ".crt"; - auto file = fileutils::ofstream(adminPath, std::ios::trunc | std::ios::binary); + auto file = nowide::ofstream(adminPath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", adminPath.c_str()); return false; @@ -389,7 +390,7 @@ add_initial_files(GitRepository& repo, // /devices std::string devicePath = devicesPath + "/" + deviceId + ".crt"; - file = fileutils::ofstream(devicePath, std::ios::trunc | std::ios::binary); + file = nowide::ofstream(devicePath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", devicePath.c_str()); return false; @@ -408,7 +409,7 @@ add_initial_files(GitRepository& repo, continue; std::string crlPath = crlsPath + DIR_SEPARATOR_STR + deviceId + DIR_SEPARATOR_STR + dht::toHex(crl->getNumber()) + ".crl"; - file = fileutils::ofstream(crlPath, std::ios::trunc | std::ios::binary); + file = nowide::ofstream(crlPath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", crlPath.c_str()); return false; @@ -429,7 +430,7 @@ add_initial_files(GitRepository& repo, return false; } - auto file = fileutils::ofstream(invitedMemberPath, std::ios::trunc | std::ios::binary); + auto file = nowide::ofstream(invitedMemberPath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", invitedMemberPath.c_str()); return false; @@ -1552,7 +1553,7 @@ ConversationRepository::Impl::validateDevice() JAMI_ERR("Current device's certificate is invalid. A migration is needed"); return false; } - auto file = fileutils::ofstream(devicePath, std::ios::trunc | std::ios::binary); + auto file = nowide::ofstream(devicePath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", devicePath.c_str()); return false; @@ -1574,7 +1575,7 @@ ConversationRepository::Impl::validateDevice() path = memberPath; fullpath += path; if (fileutils::isFile(fullpath)) { - auto file = fileutils::ofstream(fullpath, std::ios::trunc | std::ios::binary); + auto file = nowide::ofstream(fullpath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", path.c_str()); return false; @@ -2417,7 +2418,7 @@ ConversationRepository::createConversation(const std::weak_ptr& acc // Move to wanted directory auto newPath = conversationsPath + "/" + id; - if (std::rename(tmpPath.c_str(), newPath.c_str())) { + if (nowide::rename(tmpPath.c_str(), newPath.c_str())) { JAMI_ERR("Couldn't move %s in %s", tmpPath.c_str(), newPath.c_str()); fileutils::removeAll(tmpPath, true); return {}; @@ -2726,7 +2727,7 @@ ConversationRepository::addMember(const std::string& uri) return {}; } - auto file = fileutils::ofstream(devicePath, std::ios::trunc | std::ios::binary); + auto file = nowide::ofstream(devicePath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", devicePath.c_str()); return {}; @@ -2912,7 +2913,7 @@ ConversationRepository::Impl::addUserDevice() std::string path = fmt::format("devices/{}.crt", account->currentDeviceId()); std::string devicePath = git_repository_workdir(repo.get()) + path; if (!fileutils::isFile(devicePath)) { - auto file = fileutils::ofstream(devicePath, std::ios::trunc | std::ios::binary); + auto file = nowide::ofstream(devicePath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", devicePath.c_str()); return; @@ -3158,7 +3159,7 @@ ConversationRepository::join() JAMI_ERR("Error when creating %s. Abort create conversations", membersPath.c_str()); return {}; } - auto file = fileutils::ofstream(memberFile, std::ios::trunc | std::ios::binary); + auto file = nowide::ofstream(memberFile, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", memberFile.c_str()); return {}; @@ -3321,7 +3322,7 @@ ConversationRepository::voteKick(const std::string& uri, const std::string& type return {}; } auto votePath = fileutils::getFullPath(voteDirectory, adminUri); - auto voteFile = fileutils::ofstream(votePath, std::ios::trunc | std::ios::binary); + auto voteFile = nowide::ofstream(votePath, std::ios::trunc | std::ios::binary); if (!voteFile.is_open()) { JAMI_ERR("Could not write data to %s", votePath.c_str()); return {}; @@ -3361,7 +3362,7 @@ ConversationRepository::voteUnban(const std::string& uri, const std::string& typ return {}; } auto votePath = fileutils::getFullPath(voteDirectory, adminUri); - auto voteFile = fileutils::ofstream(votePath, std::ios::trunc | std::ios::binary); + auto voteFile = nowide::ofstream(votePath, std::ios::trunc | std::ios::binary); if (!voteFile.is_open()) { JAMI_ERR("Could not write data to %s", votePath.c_str()); return {}; @@ -3399,7 +3400,7 @@ ConversationRepository::Impl::resolveBan(const std::string& type, const std::str return false; } - if (std::rename(originFilePath.c_str(), destFilePath.c_str())) { + if (nowide::rename(originFilePath.c_str(), destFilePath.c_str())) { JAMI_ERR("Error when moving %s to %s. Abort resolving vote", originFilePath.c_str(), destFilePath.c_str()); @@ -3450,7 +3451,7 @@ ConversationRepository::Impl::resolveUnban(const std::string& type, const std::s JAMI_ERR("Error when creating %s. Abort resolving vote", destPath.c_str()); return false; } - if (std::rename(originFilePath.c_str(), destFilePath.c_str())) { + if (nowide::rename(originFilePath.c_str(), destFilePath.c_str())) { JAMI_ERR("Error when moving %s to %s. Abort resolving vote", originFilePath.c_str(), destFilePath.c_str()); @@ -3661,7 +3662,7 @@ ConversationRepository::updateInfos(const std::map& pr return {}; std::string repoPath = git_repository_workdir(repo.get()); auto profilePath = repoPath + "profile.vcf"; - auto file = fileutils::ofstream(profilePath, std::ios::trunc | std::ios::binary); + auto file = nowide::ofstream(profilePath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", profilePath.c_str()); return {}; diff --git a/src/jamidht/gitserver.cpp b/src/jamidht/gitserver.cpp index 51e15134a..ad5322085 100644 --- a/src/jamidht/gitserver.cpp +++ b/src/jamidht/gitserver.cpp @@ -26,7 +26,6 @@ #include #include -#include #include #include @@ -492,4 +491,4 @@ GitServer::stop() pimpl_->stop(); } -} // namespace jami \ No newline at end of file +} // namespace jami diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp index 76df7d129..f3eb0e809 100644 --- a/src/jamidht/jamiaccount.cpp +++ b/src/jamidht/jamiaccount.cpp @@ -103,6 +103,8 @@ #include #include #include +#include +#include using namespace std::placeholders; @@ -817,7 +819,7 @@ JamiAccount::saveConfig() const auto accountConfig = getPath() + DIR_SEPARATOR_STR + "config.yml"; std::lock_guard lock(fileutils::getFileLock(accountConfig)); - std::ofstream fout = fileutils::ofstream(accountConfig); + nowide::ofstream fout(accountConfig); fout << accountOut.c_str(); JAMI_DBG("Exported account to %s", accountConfig.c_str()); } catch (const std::exception& e) { @@ -1432,8 +1434,8 @@ JamiAccount::setAccountDetails(const std::map& details proxyServerCached_ = {}; auto proxyCachePath = cachePath_ + DIR_SEPARATOR_STR "dhtproxy"; auto proxyListCachePath = cachePath_ + DIR_SEPARATOR_STR "dhtproxylist"; - std::remove(proxyCachePath.c_str()); - std::remove(proxyListCachePath.c_str()); + nowide::remove(proxyCachePath.c_str()); + nowide::remove(proxyListCachePath.c_str()); } if (not managerUri_.empty() and managerUri_.rfind("http", 0) != 0) { managerUri_ = "https://" + managerUri_; @@ -2577,7 +2579,7 @@ std::set> loadIdList(const std::string& path) { std::set> ids; - std::ifstream file = fileutils::ifstream(path); + nowide::ifstream file(path); if (!file.is_open()) { JAMI_DBG("Could not load %s", path.c_str()); return ids; @@ -2601,7 +2603,7 @@ template> void saveIdList(const std::string& path, const List& ids) { - std::ofstream file = fileutils::ofstream(path, std::ios::trunc | std::ios::binary); + nowide::ofstream file(path, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not save to %s", path.c_str()); return; @@ -2815,7 +2817,7 @@ JamiAccount::getDhtProxyServer(const std::string& serverList) // Cache it! fileutils::check_dir(cachePath_.c_str(), 0700); std::string proxyCachePath = cachePath_ + DIR_SEPARATOR_STR "dhtproxy"; - std::ofstream file = fileutils::ofstream(proxyCachePath); + nowide::ofstream file(proxyCachePath); JAMI_DBG("Cache DHT proxy server: %s", proxyServerCached_.c_str()); if (file.is_open()) file << proxyServerCached_; @@ -3103,7 +3105,7 @@ JamiAccount::sendTrustRequest(const std::string& to, const std::vector& auto requestPath = cachePath_ + DIR_SEPARATOR_STR + "requests"; fileutils::recursive_mkdir(requestPath, 0700); auto cachedFile = requestPath + DIR_SEPARATOR_STR + to; - std::ofstream req = fileutils::ofstream(cachedFile, std::ios::trunc | std::ios::binary); + nowide::ofstream req(cachedFile, std::ios::trunc | std::ios::binary); if (!req.is_open()) { JAMI_ERR("Could not write data to %s", cachedFile.c_str()); return; @@ -3788,7 +3790,7 @@ JamiAccount::cacheTurnServers() fileutils::recursive_mkdir(this_->cachePath_ + DIR_SEPARATOR_STR + "domains", 0700); auto pathV4 = this_->cachePath_ + DIR_SEPARATOR_STR + "domains" + DIR_SEPARATOR_STR + "v4." + server; - if (auto turnV4File = std::ifstream(pathV4)) { + if (auto turnV4File = nowide::ifstream(pathV4)) { std::string content((std::istreambuf_iterator(turnV4File)), std::istreambuf_iterator()); std::lock_guard lk(this_->cachedTurnMutex_); @@ -3796,7 +3798,7 @@ JamiAccount::cacheTurnServers() } auto pathV6 = this_->cachePath_ + DIR_SEPARATOR_STR + "domains" + DIR_SEPARATOR_STR + "v6." + server; - if (auto turnV6File = std::ifstream(pathV6)) { + if (auto turnV6File = nowide::ifstream(pathV6)) { std::string content((std::istreambuf_iterator(turnV6File)), std::istreambuf_iterator()); std::lock_guard lk(this_->cachedTurnMutex_); @@ -3807,7 +3809,7 @@ JamiAccount::cacheTurnServers() { if (turnV4) { // Cache value to avoid a delay when starting up Jami - std::ofstream turnV4File(pathV4); + nowide::ofstream turnV4File(pathV4); turnV4File << turnV4.toString(); } else fileutils::remove(pathV4, true); @@ -3819,7 +3821,7 @@ JamiAccount::cacheTurnServers() { if (turnV6) { // Cache value to avoid a delay when starting up Jami - std::ofstream turnV6File(pathV6); + nowide::ofstream turnV6File(pathV6); turnV6File << turnV6.toString(); } else fileutils::remove(pathV6, true); @@ -4067,7 +4069,7 @@ JamiAccount::sendProfile(const std::string& peerUri, const std::string& deviceId std::lock_guard lock(fileutils::getFileLock(path)); if (fileutils::isFile(path)) return; - fileutils::ofstream(path); + nowide::ofstream file(path); }); } catch (const std::exception& e) { diff --git a/src/jamidht/namedirectory.cpp b/src/jamidht/namedirectory.cpp index 3ca71db87..a05e1800d 100644 --- a/src/jamidht/namedirectory.cpp +++ b/src/jamidht/namedirectory.cpp @@ -44,7 +44,7 @@ #include #include #include -#include +#include namespace jami { @@ -452,7 +452,7 @@ NameDirectory::saveCache() { fileutils::recursive_mkdir(fileutils::get_cache_dir() + DIR_SEPARATOR_STR + CACHE_DIRECTORY); std::lock_guard lock(fileutils::getFileLock(cachePath_)); - std::ofstream file = fileutils::ofstream(cachePath_, std::ios::trunc | std::ios::binary); + nowide::ofstream file(cachePath_, std::ios::trunc | std::ios::binary); { std::lock_guard l(cacheLock_); msgpack::pack(file, nameCache_); @@ -470,7 +470,7 @@ NameDirectory::loadCache() // read file { std::lock_guard lock(fileutils::getFileLock(cachePath_)); - std::ifstream file = fileutils::ifstream(cachePath_); + nowide::ifstream file(cachePath_); if (!file.is_open()) { JAMI_DBG("Could not load %s", cachePath_.c_str()); return; diff --git a/src/logger.cpp b/src/logger.cpp index d26a66a55..ab390d934 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -45,7 +45,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -335,8 +336,10 @@ public: virtual void consume(jami::Logger::Msg& msg) override { - static bool with_color = !(getenv("NO_COLOR") || getenv("NO_COLORS") || getenv("NO_COLOUR") - || getenv("NO_COLOURS")); + static bool with_color = !(nowide::getenv("NO_COLOR") || + nowide::getenv("NO_COLORS") || + nowide::getenv("NO_COLOUR") || + nowide::getenv("NO_COLOURS")); printLogImpl(msg, with_color); } @@ -454,7 +457,7 @@ public: thread_.join(); } - std::ofstream file; + nowide::ofstream file; if (not path.empty()) { file.open(path, std::ofstream::out | std::ofstream::app); enable(true); @@ -502,7 +505,7 @@ private: cv_.notify_one(); } - void do_consume(std::ofstream& file, const std::vector& messages) + void do_consume(nowide::ofstream& file, const std::vector& messages) { for (const auto& msg : messages) { file << msg.header_ << msg.payload_; diff --git a/src/manager.cpp b/src/manager.cpp index 717d5499a..c1441b74d 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -114,9 +114,10 @@ using random_device = dht::crypto::random_device; #include #include -#include +#include #include -#include +#include +#include #include #include #include @@ -144,8 +145,8 @@ bool Manager::isIOSExtension = {false}; static void copy_over(const std::string& srcPath, const std::string& destPath) { - std::ifstream src = fileutils::ifstream(srcPath.c_str()); - std::ofstream dest = fileutils::ofstream(destPath.c_str()); + nowide::ifstream src(srcPath.c_str()); + nowide::ofstream dest(destPath.c_str()); dest << src.rdbuf(); src.close(); dest.close(); @@ -175,7 +176,7 @@ check_rename(const std::string& old_dir, const std::string& new_dir) if (not fileutils::isDirectory(new_dir)) { JAMI_WARN() << "Migrating " << old_dir << " to " << new_dir; - std::rename(old_dir.c_str(), new_dir.c_str()); + nowide::rename(old_dir.c_str(), new_dir.c_str()); } else { for (const auto& file : fileutils::readDirectory(old_dir)) { auto old_dest = fileutils::getFullPath(old_dir, file); @@ -184,7 +185,7 @@ check_rename(const std::string& old_dir, const std::string& new_dir) check_rename(old_dest, new_dest); } else { JAMI_WARN() << "Migrating " << old_dest << " to " << new_dest; - std::rename(old_dest.c_str(), new_dest.c_str()); + nowide::rename(old_dest.c_str(), new_dest.c_str()); } } fileutils::removeAll(old_dir); @@ -206,7 +207,7 @@ static void setDhtLogLevel() { #ifndef RING_UWP - char* envvar = getenv(DHTLOGLEVEL); + char* envvar = nowide::getenv(DHTLOGLEVEL); int level = 0; if (envvar != nullptr) { @@ -236,7 +237,7 @@ static void setSipLogLevel() { #ifndef RING_UWP - char* envvar = getenv(SIPLOGLEVEL); + char* envvar = nowide::getenv(SIPLOGLEVEL); int level = 0; @@ -280,7 +281,7 @@ static void setGnuTlsLogLevel() { #ifndef RING_UWP - char* envvar = getenv("RING_TLS_LOGLEVEL"); + char* envvar = nowide::getenv("RING_TLS_LOGLEVEL"); int level = RING_TLS_LOGLEVEL; if (envvar != nullptr) { @@ -480,7 +481,7 @@ Manager::ManagerPimpl::parseConfiguration() bool result = true; try { - std::ifstream file = fileutils::ifstream(path_); + nowide::ifstream file(path_); YAML::Node parsedFile = YAML::Load(file); file.close(); const int error_count = base_.loadAccountMap(parsedFile); @@ -1748,7 +1749,7 @@ Manager::saveConfig() #endif std::lock_guard lock(fileutils::getFileLock(pimpl_->path_)); - std::ofstream fout = fileutils::ofstream(pimpl_->path_); + nowide::ofstream fout(pimpl_->path_); fout << out.c_str(); } catch (const YAML::Exception& e) { JAMI_ERR("%s", e.what()); @@ -2845,7 +2846,7 @@ Manager::loadAccountMap(const YAML::Node& node) if (fileutils::isFile(configFile)) { try { if (auto a = accountFactory.createAccount(JamiAccount::ACCOUNT_TYPE, dir)) { - std::ifstream file = fileutils::ifstream(configFile); + nowide::ifstream file(configFile); YAML::Node parsedConfig = YAML::Load(file); file.close(); a->unserialize(parsedConfig); diff --git a/src/media/audio/pulseaudio/pulselayer.cpp b/src/media/audio/pulseaudio/pulselayer.cpp index c23f423a5..2caa2dc34 100644 --- a/src/media/audio/pulseaudio/pulselayer.cpp +++ b/src/media/audio/pulseaudio/pulselayer.cpp @@ -36,7 +36,6 @@ #include #include -#include #include #include diff --git a/src/media/audio/ringbuffer.h b/src/media/audio/ringbuffer.h index 6a530dcfa..721aa8166 100644 --- a/src/media/audio/ringbuffer.h +++ b/src/media/audio/ringbuffer.h @@ -34,7 +34,6 @@ #include #include #include -#include namespace jami { diff --git a/src/media/audio/sound/audiofile.cpp b/src/media/audio/sound/audiofile.cpp index 280aecfe5..4ec043d65 100644 --- a/src/media/audio/sound/audiofile.cpp +++ b/src/media/audio/sound/audiofile.cpp @@ -20,7 +20,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include + #include #include #include diff --git a/src/media/libav_utils.cpp b/src/media/libav_utils.cpp index 668f22d63..a549f5d16 100644 --- a/src/media/libav_utils.cpp +++ b/src/media/libav_utils.cpp @@ -29,6 +29,7 @@ #include "video/video_base.h" #include "logger.h" +#include #include #include #include @@ -98,7 +99,7 @@ static void setAvLogLevel() { #ifndef RING_UWP - char* envvar = getenv(AVLOGLEVEL); + char* envvar = nowide::getenv(AVLOGLEVEL); signed level = AV_LOG_WARNING; if (envvar != nullptr) { diff --git a/src/media/media_encoder.cpp b/src/media/media_encoder.cpp index 0e865d2b9..fc70e14c7 100644 --- a/src/media/media_encoder.cpp +++ b/src/media/media_encoder.cpp @@ -41,7 +41,7 @@ extern "C" { } #include -#include +#include #include #include #include @@ -1147,7 +1147,7 @@ MediaEncoder::readConfig(AVCodecContext* encoderCtx) JAMI_WARN("encoder.json file found, default settings will be erased"); try { Json::Value root; - std::ifstream file = fileutils::ifstream(path); + nowide::ifstream file(path); file >> root; if (!root.isObject()) { JAMI_ERR() << "Invalid encoder configuration: root is not an object"; diff --git a/src/media/video/sinkclient.cpp b/src/media/video/sinkclient.cpp index 480a668a6..126c4d738 100644 --- a/src/media/video/sinkclient.cpp +++ b/src/media/video/sinkclient.cpp @@ -57,6 +57,7 @@ #include #include #include +#include namespace jami { namespace video { @@ -278,7 +279,7 @@ SinkClient::start() noexcept { if (not shm_) { try { - char* envvar = getenv("JAMI_DISABLE_SHM"); + char* envvar = nowide::getenv("JAMI_DISABLE_SHM"); if (envvar) // Do not use SHM if set return true; shm_ = std::make_shared(); diff --git a/src/media/video/winvideo/video_device_impl.cpp b/src/media/video/winvideo/video_device_impl.cpp index adb752423..1baccb572 100644 --- a/src/media/video/winvideo/video_device_impl.cpp +++ b/src/media/video/winvideo/video_device_impl.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include "logger.h" #include "../video_device.h" @@ -159,7 +160,7 @@ VideoDeviceImpl::setup() if (hr != S_OK) { continue; } - auto unique_name = to_string(olestr); + auto unique_name = nowide::narrow(olestr); if (unique_name.empty()) { continue; } @@ -184,7 +185,7 @@ VideoDeviceImpl::setup() hr = pPropBag->Read(L"FriendlyName", &var, 0); } if (SUCCEEDED(hr)) { - this->name = to_string(var.bstrVal); + this->name = nowide::narrow(var.bstrVal); } pPropBag->Write(L"FriendlyName", &var); diff --git a/src/media/video/winvideo/video_device_monitor_impl.cpp b/src/media/video/winvideo/video_device_monitor_impl.cpp index 06323f992..19a0426b4 100644 --- a/src/media/video/winvideo/video_device_monitor_impl.cpp +++ b/src/media/video/winvideo/video_device_monitor_impl.cpp @@ -33,6 +33,7 @@ #include #include #include +#include namespace jami { namespace video { @@ -274,7 +275,7 @@ VideoDeviceMonitorImpl::enumerateVideoInputDevices() if (hr != S_OK) { continue; } - auto unique_name = to_string(olestr); + auto unique_name = nowide::narrow(olestr); if (!unique_name.empty()) { // replace ':' with '_' since ffmpeg uses : to delineate between sources std::replace(unique_name.begin(), unique_name.end(), ':', '_'); diff --git a/src/meson.build b/src/meson.build index d637fb5b0..95a91e7c7 100644 --- a/src/meson.build +++ b/src/meson.build @@ -148,7 +148,8 @@ libjami_dependencies = [ depfmt, depyamlcpp, depjsoncpp, - depzlib + depzlib, + depnowide ] if conf.get('HAVE_OPENSL') == 1 diff --git a/src/plugin/jamipluginmanager.cpp b/src/plugin/jamipluginmanager.cpp index 5c078b5cf..91287690a 100644 --- a/src/plugin/jamipluginmanager.cpp +++ b/src/plugin/jamipluginmanager.cpp @@ -25,7 +25,7 @@ #include "logger.h" -#include +#include #include #include #include "manager.h" @@ -282,7 +282,7 @@ JamiPluginManager::setPluginPreference(const std::string& rootPath, const std::string preferencesValuesFilePath = PluginPreferencesUtils::valuesFilePath(rootPath, acc); std::lock_guard guard(fileutils::getFileLock(preferencesValuesFilePath)); - std::ofstream fs(preferencesValuesFilePath, std::ios::binary); + nowide::ofstream fs(preferencesValuesFilePath, std::ios::binary); if (!fs.good()) { if (force) { loadPlugin(rootPath); diff --git a/src/plugin/pluginpreferencesutils.cpp b/src/plugin/pluginpreferencesutils.cpp index 9671f85ac..11937e4b5 100644 --- a/src/plugin/pluginpreferencesutils.cpp +++ b/src/plugin/pluginpreferencesutils.cpp @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include "logger.h" @@ -66,7 +66,7 @@ PluginPreferencesUtils::processLocaleFile(const std::string& preferenceLocaleFil if (!fileutils::isFile(preferenceLocaleFilePath)) { return {}; } - std::ifstream file(preferenceLocaleFilePath); + nowide::ifstream file(preferenceLocaleFilePath); Json::Value root; Json::CharReaderBuilder rbuilder; rbuilder["collectComments"] = false; @@ -150,7 +150,7 @@ PluginPreferencesUtils::getPreferences(const std::string& rootPath, const std::s { std::string preferenceFilePath = getPreferencesConfigFilePath(rootPath, accountId); std::lock_guard guard(fileutils::getFileLock(preferenceFilePath)); - std::ifstream file(preferenceFilePath); + nowide::ifstream file(preferenceFilePath); Json::Value root; Json::CharReaderBuilder rbuilder; rbuilder["collectComments"] = false; @@ -242,7 +242,7 @@ PluginPreferencesUtils::getUserPreferencesValuesMap(const std::string& rootPath, { const std::string preferencesValuesFilePath = valuesFilePath(rootPath, accountId); std::lock_guard guard(fileutils::getFileLock(preferencesValuesFilePath)); - std::ifstream file(preferencesValuesFilePath, std::ios::binary); + nowide::ifstream file(preferencesValuesFilePath, std::ios::binary); std::map rmap; // If file is accessible @@ -312,7 +312,7 @@ PluginPreferencesUtils::resetPreferencesValuesMap(const std::string& rootPath, const std::string preferencesValuesFilePath = valuesFilePath(rootPath, accountId); std::lock_guard guard(fileutils::getFileLock(preferencesValuesFilePath)); - std::ofstream fs(preferencesValuesFilePath, std::ios::binary); + nowide::ofstream fs(preferencesValuesFilePath, std::ios::binary); if (!fs.good()) { return false; } @@ -331,7 +331,7 @@ PluginPreferencesUtils::setAllowDenyListPreferences(const ChatHandlerList& list) { std::string filePath = getAllowDenyListsPath(); std::lock_guard guard(fileutils::getFileLock(filePath)); - std::ofstream fs(filePath, std::ios::binary); + nowide::ofstream fs(filePath, std::ios::binary); if (!fs.good()) { return; } @@ -347,7 +347,7 @@ PluginPreferencesUtils::getAllowDenyListPreferences(ChatHandlerList& list) { const std::string filePath = getAllowDenyListsPath(); std::lock_guard guard(fileutils::getFileLock(filePath)); - std::ifstream file(filePath, std::ios::binary); + nowide::ifstream file(filePath, std::ios::binary); // If file is accessible if (file.good()) { @@ -384,7 +384,7 @@ PluginPreferencesUtils::addAlwaysHandlerPreference(const std::string& handlerNam Json::Value root; std::lock_guard guard(fileutils::getFileLock(filePath)); - std::ifstream file(filePath); + nowide::ifstream file(filePath); Json::CharReaderBuilder rbuilder; Json::Value preference; rbuilder["collectComments"] = false; @@ -404,7 +404,7 @@ PluginPreferencesUtils::addAlwaysHandlerPreference(const std::string& handlerNam Json::Value root; { std::lock_guard guard(fileutils::getFileLock(filePath)); - std::ifstream file(filePath); + nowide::ifstream file(filePath); Json::CharReaderBuilder rbuilder; Json::Value preference; rbuilder["collectComments"] = false; @@ -428,7 +428,7 @@ PluginPreferencesUtils::addAlwaysHandlerPreference(const std::string& handlerNam root.append(preference); } std::lock_guard guard(fileutils::getFileLock(filePath)); - std::ofstream outFile(filePath); + nowide::ofstream outFile(filePath); if (outFile) { // Save preference.json file with new "always preference" outFile << root.toStyledString(); diff --git a/src/plugin/pluginsutils.cpp b/src/plugin/pluginsutils.cpp index 273782910..0cf5af0ee 100644 --- a/src/plugin/pluginsutils.cpp +++ b/src/plugin/pluginsutils.cpp @@ -22,7 +22,7 @@ #include "logger.h" #include "fileutils.h" #include "archiver.h" -#include +#include #include #if defined(__APPLE__) @@ -139,7 +139,7 @@ std::map parseManifestFile(const std::string& manifestFilePath) { std::lock_guard guard(fileutils::getFileLock(manifestFilePath)); - std::ifstream file(manifestFilePath); + nowide::ifstream file(manifestFilePath); if (file) { try { return checkManifestValidity(file); diff --git a/src/ring_api.cpp b/src/ring_api.cpp index 1def4167f..8134b5486 100644 --- a/src/ring_api.cpp +++ b/src/ring_api.cpp @@ -21,7 +21,7 @@ #include #include #include -#include +#include #ifdef HAVE_CONFIG_H #include "config.h" @@ -49,7 +49,7 @@ init(enum InitFlag flags) noexcept jami::Logger::setSysLog(true); jami::Logger::setConsoleLog(DRING_FLAG_CONSOLE_LOG == (flags & DRING_FLAG_CONSOLE_LOG)); - const char* log_file = getenv("JAMI_LOG_FILE"); + const char* log_file = nowide::getenv("JAMI_LOG_FILE"); if (log_file) { jami::Logger::setFileLog(log_file); diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp index 08e55cb6c..3fc7a8931 100644 --- a/src/sip/sipaccount.cpp +++ b/src/sip/sipaccount.cpp @@ -65,6 +65,7 @@ #include +#include #include #include #include @@ -1388,8 +1389,8 @@ SIPAccount::getLoginName() DWORD size = UNLEN + 1; TCHAR username[UNLEN + 1]; std::string uname; - if (GetUserName((TCHAR*) username, &size)) { - uname = jami::to_string(username); + if (GetUserNameW((TCHAR*) username, &size)) { + uname = nowide::narrow(username); } return uname; #endif diff --git a/src/string_utils.cpp b/src/string_utils.cpp index 6762c8fa4..c61b5a5ef 100644 --- a/src/string_utils.cpp +++ b/src/string_utils.cpp @@ -42,39 +42,6 @@ namespace jami { -#ifdef _WIN32 -std::wstring -to_wstring(const std::string& str, int codePage) -{ - int srcLength = (int) str.length(); - int requiredSize = MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, nullptr, 0); - if (!requiredSize) { - throw std::runtime_error("Can't convert string to wstring"); - } - std::wstring result((size_t) requiredSize, 0); - if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) { - throw std::runtime_error("Can't convert string to wstring"); - } - return result; -} - -std::string -to_string(const std::wstring& wstr, int codePage) -{ - int srcLength = (int) wstr.length(); - int requiredSize = WideCharToMultiByte(codePage, 0, wstr.c_str(), srcLength, nullptr, 0, 0, 0); - if (!requiredSize) { - throw std::runtime_error("Can't convert wstring to string"); - } - std::string result((size_t) requiredSize, 0); - if (!WideCharToMultiByte( - codePage, 0, wstr.c_str(), srcLength, &(*result.begin()), requiredSize, 0, 0)) { - throw std::runtime_error("Can't convert wstring to string"); - } - return result; -} -#endif - std::string to_string(double value) { diff --git a/src/string_utils.h b/src/string_utils.h index 0197f5528..4a81e1664 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -45,11 +45,6 @@ bool_to_str(bool b) noexcept std::string to_string(double value); -#ifdef _WIN32 -std::wstring to_wstring(const std::string& str, int codePage = CP_UTF8); -std::string to_string(const std::wstring& wstr, int codePage = CP_UTF8); -#endif - std::string to_hex_string(uint64_t id); uint64_t from_hex_string(const std::string& str);