From 76226044f5d14aadbe52d6314a517d0cf4c65374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Thu, 8 Sep 2022 14:23:40 -0400 Subject: [PATCH] Revert "fileutils: migrate to boost::nowide" Causes issues with yaml-cpp. Reverting temporarily until we fix the issue. Change-Id: I2df0823554ad87880bc965bdff4f4707a6129789 --- 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 | 147 ++++++++++++------ 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, 276 insertions(+), 239 deletions(-) delete mode 100644 contrib/src/nowide/SHA512SUMS delete mode 100644 contrib/src/nowide/package.json delete mode 100644 contrib/src/nowide/rules.mak diff --git a/CMakeLists.txt b/CMakeLists.txt index 44d8d2f27..e8bdef9cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -164,8 +164,7 @@ 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/nowide/include;" + "${CMAKE_CURRENT_SOURCE_DIR}/contrib/build/libgit2/include" ) endif() @@ -282,7 +281,6 @@ 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 273084c3d..2b7e512fa 100644 --- a/README +++ b/README @@ -81,11 +81,11 @@ B) With Meson cd ../../ mkdir build -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 +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 Done ! diff --git a/compat/msvc/package.json b/compat/msvc/package.json index 084588d6f..d4c4193a2 100644 --- a/compat/msvc/package.json +++ b/compat/msvc/package.json @@ -13,8 +13,7 @@ "yaml-cpp", "libarchive", "webrtc-audio-processing", - "libgit2", - "nowide" + "libgit2" ], "configuration": "ReleaseLib_win32", "project_paths": ["ring-daemon.vcxproj"] diff --git a/configure.ac b/configure.ac index 8b503fbea..dcdb75c4a 100644 --- a/configure.ac +++ b/configure.ac @@ -618,8 +618,6 @@ 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 deleted file mode 100644 index b1fe7ed5d..000000000 --- a/contrib/src/nowide/SHA512SUMS +++ /dev/null @@ -1 +0,0 @@ -c3748921b85648aa0e89970f2ab24588cbc72d05edd7ddf4f61a607d9ecbddd45e9e6799d2ed83386c43045b9487693e027494a81b11a6a7bdfaa939d1251938 nowide_standalone_v11.2.0.tar.gz diff --git a/contrib/src/nowide/package.json b/contrib/src/nowide/package.json deleted file mode 100644 index 298b3950e..000000000 --- a/contrib/src/nowide/package.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "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 deleted file mode 100644 index f88875fae..000000000 --- a/contrib/src/nowide/rules.mak +++ /dev/null @@ -1,21 +0,0 @@ -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 ca6a0c166..65f560514 100644 --- a/meson.build +++ b/meson.build @@ -33,7 +33,6 @@ 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 dba8eea8d..472bb91de 100644 --- a/src/archiver.cpp +++ b/src/archiver.cpp @@ -48,8 +48,7 @@ extern "C" { #endif #include -#include -#include +#include using namespace std::literals; @@ -106,7 +105,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 - nowide::ifstream ifs(i.second); + std::ifstream ifs = fileutils::ifstream(i.second); std::string fileContent((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); root[i.first] = fileContent; @@ -219,7 +218,7 @@ gzFile openGzip(const std::string& path, const char* mode) { #ifdef _WIN32 - return gzopen_w(nowide::widen(path).c_str(), mode); + return gzopen_w(jami::to_wstring(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 4b30eb6ba..41fa2e747 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 nowide::ifstream input_; + mutable std::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(); - input_.open(info_.path, std::ios::in | std::ios::binary); + fileutils::openStream(input_, 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 nowide::ifstream input_; + mutable std::ifstream input_; mutable std::vector> subtransfer_; }; @@ -519,7 +519,7 @@ OutgoingFileTransfer::OutgoingFileTransfer(DRing::DataTransferId tid, const InternalCompletionCb& cb) : DataTransfer(tid, cb) { - input_.open(info.path, std::ios::binary); + fileutils::openStream(input_, info.path, std::ios::binary); if (!input_) throw std::runtime_error("input file open failed"); @@ -574,7 +574,7 @@ private: DRing::DataTransferId internalId_; - nowide::ofstream fout_; + std::ofstream fout_; std::mutex cbMtx_ {}; std::function onFilenameCb_ {}; }; @@ -614,7 +614,7 @@ IncomingFileTransfer::requestFilename(const std::function& channel, channel_->shutdown(); return; } - stream_.open(info_.path, std::ios::binary | std::ios::in); + fileutils::openStream(stream_, 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) { - stream_.open(info_.path, std::ios::binary | std::ios::out); + fileutils::openStream(stream_, info_.path, std::ios::binary | std::ios::out); if (!stream_) return; @@ -925,7 +925,7 @@ public: } void saveWaiting() { - nowide::ofstream file(waitingPath_, std::ios::trunc | std::ios::binary); + std::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)) { - nowide::ifstream transfer(path, std::ios::binary); + std::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 cbf6b02a7..4965a7c62 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()); } - nowide::ofstream stream_; + std::ofstream stream_; std::string sha3Sum_ {}; }; @@ -114,7 +114,7 @@ public: void cancel() override; private: - nowide::ifstream stream_; + std::ifstream stream_; size_t start_ {0}; size_t end_ {0}; }; diff --git a/src/debug_utils.h b/src/debug_utils.h index 41237c7bd..25238219d 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_ = nowide::ofstream(filename, std::ios_base::out | std::ios_base::binary); + f_ = std::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: - nowide::ofstream f_; + std::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 9678cec1f..324e26b2d 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -43,6 +43,7 @@ #ifdef _WIN32 #include +#include "string_utils.h" #endif #include @@ -74,14 +75,12 @@ #include #include -#include +#include #include #include #include -#include -#include -#include +#include #include #include #include @@ -100,22 +99,34 @@ #include #endif -inline const std::string -getProtectedEnvString(const char* name) -{ - char* envvar_ = nowide::getenv(name); - return envvar_ ? envvar_ : ""; -} -#define PROTECTED_GETENV(str) getProtectedEnvString(str) - #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") +#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 -#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") +const wchar_t* +winGetEnv(const wchar_t* name) +{ + const DWORD buffSize = 65535; + static wchar_t buffer[buffSize]; + if (GetEnvironmentVariable(name, buffer, buffSize)) { + return buffer; + } else { + return L""; + } +} + +#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") #endif #define PIDFILE ".ring.pid" @@ -207,10 +218,10 @@ isFile(const std::string& path, bool resolveSymlink) #ifdef _WIN32 if (resolveSymlink) { struct _stat64i32 s; - if (_wstat(nowide::widen(path).c_str(), &s) == 0) + if (_wstat(jami::to_wstring(path).c_str(), &s) == 0) return S_ISREG(s.st_mode); } else { - DWORD attr = GetFileAttributesW(nowide::widen(path).c_str()); + DWORD attr = GetFileAttributes(jami::to_wstring(path).c_str()); if ((attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY) && !(attr & FILE_ATTRIBUTE_REPARSE_POINT)) return true; @@ -253,7 +264,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 = GetFileAttributesW(nowide::widen(path).c_str()); + DWORD attr = GetFileAttributes(jami::to_wstring(path).c_str()); if (attr & FILE_ATTRIBUTE_REPARSE_POINT) return true; #endif @@ -278,13 +289,13 @@ writeTime(const std::string& path) ext_params.dwSecurityQosFlags = SECURITY_ANONYMOUS; ext_params.lpSecurityAttributes = nullptr; ext_params.hTemplateFile = nullptr; - HANDLE h = CreateFile2(nowide::widen(path).c_str(), + HANDLE h = CreateFile2(jami::to_wstring(path).c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &ext_params); #elif _WIN32 - HANDLE h = CreateFileW(nowide::widen(path).c_str(), + HANDLE h = CreateFileW(jami::to_wstring(path).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, @@ -404,7 +415,7 @@ std::vector loadFile(const std::string& path, const std::string& default_dir) { std::vector buffer; - nowide::ifstream file(getFullPath(default_dir, path), std::ios::binary); + std::ifstream file = ifstream(getFullPath(default_dir, path), std::ios::binary); if (!file) throw std::runtime_error("Can't read file: " + path); file.seekg(0, std::ios::end); @@ -422,7 +433,7 @@ std::string loadTextFile(const std::string& path, const std::string& default_dir) { std::string buffer; - nowide::ifstream file(getFullPath(default_dir, path)); + std::ifstream file = ifstream(getFullPath(default_dir, path)); if (!file) throw std::runtime_error("Can't read file: " + path); file.seekg(0, std::ios::end); @@ -439,7 +450,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) { - nowide::ofstream file(path, std::ios::trunc | std::ios::binary); + std::ofstream file = fileutils::ofstream(path, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", path.c_str()); return; @@ -648,10 +659,9 @@ get_cache_dir(const char* pkg) + DIR_SEPARATOR_STR + pkg; #else #ifdef _WIN32 - const std::string cache_home(JAMI_CACHE_HOME); - + const std::wstring cache_home(JAMI_CACHE_HOME); if (not cache_home.empty()) - return cache_home; + return jami::to_string(cache_home); #else const std::string cache_home(XDG_CACHE_HOME); if (not cache_home.empty()) @@ -687,7 +697,7 @@ get_home_dir() #elif defined _WIN32 TCHAR path[MAX_PATH]; if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_PROFILE, nullptr, 0, path))) { - return nowide::narrow(path); + return jami::to_string(path); } return program_dir; #else @@ -724,9 +734,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::string data_home(JAMI_DATA_HOME); + const std::wstring data_home(JAMI_DATA_HOME); if (not data_home.empty()) - return data_home + DIR_SEPARATOR_STR + pkg; + return jami::to_string(data_home) + DIR_SEPARATOR_STR + pkg; if (!strcmp(pkg, "ring")) { return get_home_dir() + DIR_SEPARATOR_STR + ".local" + DIR_SEPARATOR_STR @@ -784,9 +794,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::string xdg_env(JAMI_CONFIG_HOME); + const std::wstring xdg_env(JAMI_CONFIG_HOME); if (not xdg_env.empty()) { - configdir = xdg_env + DIR_SEPARATOR_STR + pkg; + configdir = jami::to_string(xdg_env) + DIR_SEPARATOR_STR + pkg; } else if (!strcmp(pkg, "ring")) { configdir = fileutils::get_home_dir() + DIR_SEPARATOR_STR + ".config" + DIR_SEPARATOR_STR + pkg; @@ -822,14 +832,14 @@ recursive_mkdir(const std::string& path, mode_t mode) #ifndef _WIN32 if (mkdir(path.data(), mode) != 0) { #else - if (_wmkdir(nowide::widen(path.data()).c_str()) != 0) { + if (_wmkdir(jami::to_wstring(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(nowide::widen(path.data()).c_str()) != 0) { + if (_wmkdir(jami::to_wstring(path.data()).c_str()) != 0) { #endif JAMI_ERR("Could not create directory."); return false; @@ -843,13 +853,8 @@ recursive_mkdir(const std::string& path, mode_t mode) bool eraseFile_win32(const std::string& path, bool dosync) { - HANDLE h = CreateFileW(nowide::widen(path).c_str(), - GENERIC_WRITE, - 0, - 0, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - 0); + HANDLE h + = CreateFileA(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; @@ -975,10 +980,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 !RemoveDirectoryW(nowide::widen(path).c_str()); + return !RemoveDirectory(jami::to_wstring(path).c_str()); #endif - return nowide::remove(path.c_str()); + return std::remove(path.c_str()); } int @@ -996,13 +1001,53 @@ 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) { - nowide::ifstream file; - int64_t size = 0; + std::ifstream file; + int64_t size; try { - file.open(path, std::ios::binary | std::ios::in); + openStream(file, path, std::ios::binary | std::ios::in); file.seekg(0, std::ios_base::end); size = file.tellg(); file.close(); @@ -1017,11 +1062,11 @@ sha3File(const std::string& path) sha3_512_ctx ctx; sha3_512_init(&ctx); - nowide::ifstream file; + std::ifstream file; try { if (!fileutils::isFile(path)) return {}; - file.open(path, std::ios::binary | std::ios::in); + openStream(file, path, std::ios::binary | std::ios::in); if (!file) return {}; std::vector buffer(8192, 0); @@ -1068,7 +1113,7 @@ int accessFile(const std::string& file, int mode) { #ifdef _WIN32 - return _waccess(nowide::widen(file).c_str(), mode); + return _waccess(jami::to_wstring(file).c_str(), mode); #else return access(file.c_str(), mode); #endif diff --git a/src/fileutils.h b/src/fileutils.h index 31c88cf28..3b798758b 100644 --- a/src/fileutils.h +++ b/src/fileutils.h @@ -132,6 +132,19 @@ 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 a4e7b62ab..a7340fe87 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_)); - nowide::ifstream file; - file.exceptions(nowide::ifstream::failbit | nowide::ifstream::badbit); - file.open(savePath_); + std::ifstream file; + file.exceptions(std::ifstream::failbit | std::ifstream::badbit); + fileutils::openStream(file, 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()); - nowide::ofstream file; - file.exceptions(nowide::ifstream::failbit | nowide::ifstream::badbit); - file.open(path, std::ios::trunc); + std::ofstream file; + file.exceptions(std::ifstream::failbit | std::ifstream::badbit); + fileutils::openStream(file, 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 77fe376ec..94ed89f90 100644 --- a/src/jamidht/account_manager.cpp +++ b/src/jamidht/account_manager.cpp @@ -30,6 +30,7 @@ #include #include +#include #include namespace jami { diff --git a/src/jamidht/archive_account_manager.cpp b/src/jamidht/archive_account_manager.cpp index d8ffa25bb..f372bb82e 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_); - nowide::ifstream src(sourcePath, std::ios::in | std::ios::binary); + std::ifstream src(sourcePath, std::ios::in | std::ios::binary); if (!src) return false; - nowide::ofstream dst(destinationPath, std::ios::out | std::ios::binary); + std::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 f3b5a513b..755327a16 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 { - nowide::ofstream file(path_ + DIR_SEPARATOR_STR "contacts", std::ios::trunc | std::ios::binary); + std::ofstream file(path_ + DIR_SEPARATOR_STR "contacts", std::ios::trunc | std::ios::binary); msgpack::pack(file, contacts_); } void ContactList::saveTrustRequests() const { - nowide::ofstream file(path_ + DIR_SEPARATOR_STR "incomingTrustRequests", + std::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 { - nowide::ofstream file(path_ + DIR_SEPARATOR_STR "knownDevices", std::ios::trunc | std::ios::binary); + std::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 1866aac0b..0f6c193ac 100644 --- a/src/jamidht/conversation.cpp +++ b/src/jamidht/conversation.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #ifdef ENABLE_PLUGIN #include "manager.h" @@ -302,7 +301,7 @@ public: } void saveFetched() { - nowide::ofstream file(fetchedPath_, std::ios::trunc | std::ios::binary); + std::ofstream file(fetchedPath_, std::ios::trunc | std::ios::binary); msgpack::pack(file, fetchedDevices_); } @@ -321,7 +320,7 @@ public: } void saveSending() { - nowide::ofstream file(sendingPath_, std::ios::trunc | std::ios::binary); + std::ofstream file(sendingPath_, std::ios::trunc | std::ios::binary); msgpack::pack(file, sending_); } @@ -341,7 +340,7 @@ public: void saveLastDisplayed() const { - nowide::ofstream file(lastDisplayedPath_, std::ios::trunc | std::ios::binary); + std::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 54dc4991b..f06459686 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")); - nowide::ofstream file(path + DIR_SEPARATOR_STR + "convRequests", - std::ios::trunc | std::ios::binary); + std::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) { - nowide::ofstream file(path + DIR_SEPARATOR_STR + "convInfo", std::ios::trunc | std::ios::binary); + std::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 a390a8562..b11d9cc19 100644 --- a/src/jamidht/conversationrepository.cpp +++ b/src/jamidht/conversationrepository.cpp @@ -29,8 +29,7 @@ using random_device = dht::crypto::random_device; #include -#include -#include +#include #include #include #include @@ -375,7 +374,7 @@ add_initial_files(GitRepository& repo, // /admins std::string adminPath = adminsPath + "/" + parentCert->getId().toString() + ".crt"; - auto file = nowide::ofstream(adminPath, std::ios::trunc | std::ios::binary); + auto file = fileutils::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; @@ -390,7 +389,7 @@ add_initial_files(GitRepository& repo, // /devices std::string devicePath = devicesPath + "/" + deviceId + ".crt"; - file = nowide::ofstream(devicePath, std::ios::trunc | std::ios::binary); + file = fileutils::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; @@ -409,7 +408,7 @@ add_initial_files(GitRepository& repo, continue; std::string crlPath = crlsPath + DIR_SEPARATOR_STR + deviceId + DIR_SEPARATOR_STR + dht::toHex(crl->getNumber()) + ".crl"; - file = nowide::ofstream(crlPath, std::ios::trunc | std::ios::binary); + file = fileutils::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; @@ -430,7 +429,7 @@ add_initial_files(GitRepository& repo, return false; } - auto file = nowide::ofstream(invitedMemberPath, std::ios::trunc | std::ios::binary); + auto file = fileutils::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; @@ -1553,7 +1552,7 @@ ConversationRepository::Impl::validateDevice() JAMI_ERR("Current device's certificate is invalid. A migration is needed"); return false; } - auto file = nowide::ofstream(devicePath, std::ios::trunc | std::ios::binary); + auto file = fileutils::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; @@ -1575,7 +1574,7 @@ ConversationRepository::Impl::validateDevice() path = memberPath; fullpath += path; if (fileutils::isFile(fullpath)) { - auto file = nowide::ofstream(fullpath, std::ios::trunc | std::ios::binary); + auto file = fileutils::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; @@ -2418,7 +2417,7 @@ ConversationRepository::createConversation(const std::weak_ptr& acc // Move to wanted directory auto newPath = conversationsPath + "/" + id; - if (nowide::rename(tmpPath.c_str(), newPath.c_str())) { + if (std::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 {}; @@ -2727,7 +2726,7 @@ ConversationRepository::addMember(const std::string& uri) return {}; } - auto file = nowide::ofstream(devicePath, std::ios::trunc | std::ios::binary); + auto file = fileutils::ofstream(devicePath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", devicePath.c_str()); return {}; @@ -2913,7 +2912,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 = nowide::ofstream(devicePath, std::ios::trunc | std::ios::binary); + auto file = fileutils::ofstream(devicePath, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", devicePath.c_str()); return; @@ -3159,7 +3158,7 @@ ConversationRepository::join() JAMI_ERR("Error when creating %s. Abort create conversations", membersPath.c_str()); return {}; } - auto file = nowide::ofstream(memberFile, std::ios::trunc | std::ios::binary); + auto file = fileutils::ofstream(memberFile, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not write data to %s", memberFile.c_str()); return {}; @@ -3322,7 +3321,7 @@ ConversationRepository::voteKick(const std::string& uri, const std::string& type return {}; } auto votePath = fileutils::getFullPath(voteDirectory, adminUri); - auto voteFile = nowide::ofstream(votePath, std::ios::trunc | std::ios::binary); + auto voteFile = fileutils::ofstream(votePath, std::ios::trunc | std::ios::binary); if (!voteFile.is_open()) { JAMI_ERR("Could not write data to %s", votePath.c_str()); return {}; @@ -3362,7 +3361,7 @@ ConversationRepository::voteUnban(const std::string& uri, const std::string& typ return {}; } auto votePath = fileutils::getFullPath(voteDirectory, adminUri); - auto voteFile = nowide::ofstream(votePath, std::ios::trunc | std::ios::binary); + auto voteFile = fileutils::ofstream(votePath, std::ios::trunc | std::ios::binary); if (!voteFile.is_open()) { JAMI_ERR("Could not write data to %s", votePath.c_str()); return {}; @@ -3400,7 +3399,7 @@ ConversationRepository::Impl::resolveBan(const std::string& type, const std::str return false; } - if (nowide::rename(originFilePath.c_str(), destFilePath.c_str())) { + if (std::rename(originFilePath.c_str(), destFilePath.c_str())) { JAMI_ERR("Error when moving %s to %s. Abort resolving vote", originFilePath.c_str(), destFilePath.c_str()); @@ -3451,7 +3450,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 (nowide::rename(originFilePath.c_str(), destFilePath.c_str())) { + if (std::rename(originFilePath.c_str(), destFilePath.c_str())) { JAMI_ERR("Error when moving %s to %s. Abort resolving vote", originFilePath.c_str(), destFilePath.c_str()); @@ -3662,7 +3661,7 @@ ConversationRepository::updateInfos(const std::map& pr return {}; std::string repoPath = git_repository_workdir(repo.get()); auto profilePath = repoPath + "profile.vcf"; - auto file = nowide::ofstream(profilePath, std::ios::trunc | std::ios::binary); + auto file = fileutils::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 ad5322085..51e15134a 100644 --- a/src/jamidht/gitserver.cpp +++ b/src/jamidht/gitserver.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -491,4 +492,4 @@ GitServer::stop() pimpl_->stop(); } -} // namespace jami +} // namespace jami \ No newline at end of file diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp index f3eb0e809..76df7d129 100644 --- a/src/jamidht/jamiaccount.cpp +++ b/src/jamidht/jamiaccount.cpp @@ -103,8 +103,6 @@ #include #include #include -#include -#include using namespace std::placeholders; @@ -819,7 +817,7 @@ JamiAccount::saveConfig() const auto accountConfig = getPath() + DIR_SEPARATOR_STR + "config.yml"; std::lock_guard lock(fileutils::getFileLock(accountConfig)); - nowide::ofstream fout(accountConfig); + std::ofstream fout = fileutils::ofstream(accountConfig); fout << accountOut.c_str(); JAMI_DBG("Exported account to %s", accountConfig.c_str()); } catch (const std::exception& e) { @@ -1434,8 +1432,8 @@ JamiAccount::setAccountDetails(const std::map& details proxyServerCached_ = {}; auto proxyCachePath = cachePath_ + DIR_SEPARATOR_STR "dhtproxy"; auto proxyListCachePath = cachePath_ + DIR_SEPARATOR_STR "dhtproxylist"; - nowide::remove(proxyCachePath.c_str()); - nowide::remove(proxyListCachePath.c_str()); + std::remove(proxyCachePath.c_str()); + std::remove(proxyListCachePath.c_str()); } if (not managerUri_.empty() and managerUri_.rfind("http", 0) != 0) { managerUri_ = "https://" + managerUri_; @@ -2579,7 +2577,7 @@ std::set> loadIdList(const std::string& path) { std::set> ids; - nowide::ifstream file(path); + std::ifstream file = fileutils::ifstream(path); if (!file.is_open()) { JAMI_DBG("Could not load %s", path.c_str()); return ids; @@ -2603,7 +2601,7 @@ template> void saveIdList(const std::string& path, const List& ids) { - nowide::ofstream file(path, std::ios::trunc | std::ios::binary); + std::ofstream file = fileutils::ofstream(path, std::ios::trunc | std::ios::binary); if (!file.is_open()) { JAMI_ERR("Could not save to %s", path.c_str()); return; @@ -2817,7 +2815,7 @@ JamiAccount::getDhtProxyServer(const std::string& serverList) // Cache it! fileutils::check_dir(cachePath_.c_str(), 0700); std::string proxyCachePath = cachePath_ + DIR_SEPARATOR_STR "dhtproxy"; - nowide::ofstream file(proxyCachePath); + std::ofstream file = fileutils::ofstream(proxyCachePath); JAMI_DBG("Cache DHT proxy server: %s", proxyServerCached_.c_str()); if (file.is_open()) file << proxyServerCached_; @@ -3105,7 +3103,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; - nowide::ofstream req(cachedFile, std::ios::trunc | std::ios::binary); + std::ofstream req = fileutils::ofstream(cachedFile, std::ios::trunc | std::ios::binary); if (!req.is_open()) { JAMI_ERR("Could not write data to %s", cachedFile.c_str()); return; @@ -3790,7 +3788,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 = nowide::ifstream(pathV4)) { + if (auto turnV4File = std::ifstream(pathV4)) { std::string content((std::istreambuf_iterator(turnV4File)), std::istreambuf_iterator()); std::lock_guard lk(this_->cachedTurnMutex_); @@ -3798,7 +3796,7 @@ JamiAccount::cacheTurnServers() } auto pathV6 = this_->cachePath_ + DIR_SEPARATOR_STR + "domains" + DIR_SEPARATOR_STR + "v6." + server; - if (auto turnV6File = nowide::ifstream(pathV6)) { + if (auto turnV6File = std::ifstream(pathV6)) { std::string content((std::istreambuf_iterator(turnV6File)), std::istreambuf_iterator()); std::lock_guard lk(this_->cachedTurnMutex_); @@ -3809,7 +3807,7 @@ JamiAccount::cacheTurnServers() { if (turnV4) { // Cache value to avoid a delay when starting up Jami - nowide::ofstream turnV4File(pathV4); + std::ofstream turnV4File(pathV4); turnV4File << turnV4.toString(); } else fileutils::remove(pathV4, true); @@ -3821,7 +3819,7 @@ JamiAccount::cacheTurnServers() { if (turnV6) { // Cache value to avoid a delay when starting up Jami - nowide::ofstream turnV6File(pathV6); + std::ofstream turnV6File(pathV6); turnV6File << turnV6.toString(); } else fileutils::remove(pathV6, true); @@ -4069,7 +4067,7 @@ JamiAccount::sendProfile(const std::string& peerUri, const std::string& deviceId std::lock_guard lock(fileutils::getFileLock(path)); if (fileutils::isFile(path)) return; - nowide::ofstream file(path); + fileutils::ofstream(path); }); } catch (const std::exception& e) { diff --git a/src/jamidht/namedirectory.cpp b/src/jamidht/namedirectory.cpp index a05e1800d..3ca71db87 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_)); - nowide::ofstream file(cachePath_, std::ios::trunc | std::ios::binary); + std::ofstream file = fileutils::ofstream(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_)); - nowide::ifstream file(cachePath_); + std::ifstream file = fileutils::ifstream(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 ab390d934..d26a66a55 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -45,8 +45,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -336,10 +335,8 @@ public: virtual void consume(jami::Logger::Msg& msg) override { - static bool with_color = !(nowide::getenv("NO_COLOR") || - nowide::getenv("NO_COLORS") || - nowide::getenv("NO_COLOUR") || - nowide::getenv("NO_COLOURS")); + static bool with_color = !(getenv("NO_COLOR") || getenv("NO_COLORS") || getenv("NO_COLOUR") + || getenv("NO_COLOURS")); printLogImpl(msg, with_color); } @@ -457,7 +454,7 @@ public: thread_.join(); } - nowide::ofstream file; + std::ofstream file; if (not path.empty()) { file.open(path, std::ofstream::out | std::ofstream::app); enable(true); @@ -505,7 +502,7 @@ private: cv_.notify_one(); } - void do_consume(nowide::ofstream& file, const std::vector& messages) + void do_consume(std::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 c1441b74d..717d5499a 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -114,10 +114,9 @@ using random_device = dht::crypto::random_device; #include #include -#include +#include #include -#include -#include +#include #include #include #include @@ -145,8 +144,8 @@ bool Manager::isIOSExtension = {false}; static void copy_over(const std::string& srcPath, const std::string& destPath) { - nowide::ifstream src(srcPath.c_str()); - nowide::ofstream dest(destPath.c_str()); + std::ifstream src = fileutils::ifstream(srcPath.c_str()); + std::ofstream dest = fileutils::ofstream(destPath.c_str()); dest << src.rdbuf(); src.close(); dest.close(); @@ -176,7 +175,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; - nowide::rename(old_dir.c_str(), new_dir.c_str()); + std::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); @@ -185,7 +184,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; - nowide::rename(old_dest.c_str(), new_dest.c_str()); + std::rename(old_dest.c_str(), new_dest.c_str()); } } fileutils::removeAll(old_dir); @@ -207,7 +206,7 @@ static void setDhtLogLevel() { #ifndef RING_UWP - char* envvar = nowide::getenv(DHTLOGLEVEL); + char* envvar = getenv(DHTLOGLEVEL); int level = 0; if (envvar != nullptr) { @@ -237,7 +236,7 @@ static void setSipLogLevel() { #ifndef RING_UWP - char* envvar = nowide::getenv(SIPLOGLEVEL); + char* envvar = getenv(SIPLOGLEVEL); int level = 0; @@ -281,7 +280,7 @@ static void setGnuTlsLogLevel() { #ifndef RING_UWP - char* envvar = nowide::getenv("RING_TLS_LOGLEVEL"); + char* envvar = getenv("RING_TLS_LOGLEVEL"); int level = RING_TLS_LOGLEVEL; if (envvar != nullptr) { @@ -481,7 +480,7 @@ Manager::ManagerPimpl::parseConfiguration() bool result = true; try { - nowide::ifstream file(path_); + std::ifstream file = fileutils::ifstream(path_); YAML::Node parsedFile = YAML::Load(file); file.close(); const int error_count = base_.loadAccountMap(parsedFile); @@ -1749,7 +1748,7 @@ Manager::saveConfig() #endif std::lock_guard lock(fileutils::getFileLock(pimpl_->path_)); - nowide::ofstream fout(pimpl_->path_); + std::ofstream fout = fileutils::ofstream(pimpl_->path_); fout << out.c_str(); } catch (const YAML::Exception& e) { JAMI_ERR("%s", e.what()); @@ -2846,7 +2845,7 @@ Manager::loadAccountMap(const YAML::Node& node) if (fileutils::isFile(configFile)) { try { if (auto a = accountFactory.createAccount(JamiAccount::ACCOUNT_TYPE, dir)) { - nowide::ifstream file(configFile); + std::ifstream file = fileutils::ifstream(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 2caa2dc34..c23f423a5 100644 --- a/src/media/audio/pulseaudio/pulselayer.cpp +++ b/src/media/audio/pulseaudio/pulselayer.cpp @@ -36,6 +36,7 @@ #include #include +#include #include #include diff --git a/src/media/audio/ringbuffer.h b/src/media/audio/ringbuffer.h index 721aa8166..6a530dcfa 100644 --- a/src/media/audio/ringbuffer.h +++ b/src/media/audio/ringbuffer.h @@ -34,6 +34,7 @@ #include #include #include +#include namespace jami { diff --git a/src/media/audio/sound/audiofile.cpp b/src/media/audio/sound/audiofile.cpp index 4ec043d65..280aecfe5 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 a549f5d16..668f22d63 100644 --- a/src/media/libav_utils.cpp +++ b/src/media/libav_utils.cpp @@ -29,7 +29,6 @@ #include "video/video_base.h" #include "logger.h" -#include #include #include #include @@ -99,7 +98,7 @@ static void setAvLogLevel() { #ifndef RING_UWP - char* envvar = nowide::getenv(AVLOGLEVEL); + char* envvar = 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 fc70e14c7..0e865d2b9 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; - nowide::ifstream file(path); + std::ifstream file = fileutils::ifstream(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 126c4d738..480a668a6 100644 --- a/src/media/video/sinkclient.cpp +++ b/src/media/video/sinkclient.cpp @@ -57,7 +57,6 @@ #include #include #include -#include namespace jami { namespace video { @@ -279,7 +278,7 @@ SinkClient::start() noexcept { if (not shm_) { try { - char* envvar = nowide::getenv("JAMI_DISABLE_SHM"); + char* envvar = 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 1baccb572..adb752423 100644 --- a/src/media/video/winvideo/video_device_impl.cpp +++ b/src/media/video/winvideo/video_device_impl.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include "logger.h" #include "../video_device.h" @@ -160,7 +159,7 @@ VideoDeviceImpl::setup() if (hr != S_OK) { continue; } - auto unique_name = nowide::narrow(olestr); + auto unique_name = to_string(olestr); if (unique_name.empty()) { continue; } @@ -185,7 +184,7 @@ VideoDeviceImpl::setup() hr = pPropBag->Read(L"FriendlyName", &var, 0); } if (SUCCEEDED(hr)) { - this->name = nowide::narrow(var.bstrVal); + this->name = to_string(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 19a0426b4..06323f992 100644 --- a/src/media/video/winvideo/video_device_monitor_impl.cpp +++ b/src/media/video/winvideo/video_device_monitor_impl.cpp @@ -33,7 +33,6 @@ #include #include #include -#include namespace jami { namespace video { @@ -275,7 +274,7 @@ VideoDeviceMonitorImpl::enumerateVideoInputDevices() if (hr != S_OK) { continue; } - auto unique_name = nowide::narrow(olestr); + auto unique_name = to_string(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 95a91e7c7..d637fb5b0 100644 --- a/src/meson.build +++ b/src/meson.build @@ -148,8 +148,7 @@ libjami_dependencies = [ depfmt, depyamlcpp, depjsoncpp, - depzlib, - depnowide + depzlib ] if conf.get('HAVE_OPENSL') == 1 diff --git a/src/plugin/jamipluginmanager.cpp b/src/plugin/jamipluginmanager.cpp index 91287690a..5c078b5cf 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)); - nowide::ofstream fs(preferencesValuesFilePath, std::ios::binary); + std::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 11937e4b5..9671f85ac 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 {}; } - nowide::ifstream file(preferenceLocaleFilePath); + std::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)); - nowide::ifstream file(preferenceFilePath); + std::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)); - nowide::ifstream file(preferencesValuesFilePath, std::ios::binary); + std::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)); - nowide::ofstream fs(preferencesValuesFilePath, std::ios::binary); + std::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)); - nowide::ofstream fs(filePath, std::ios::binary); + std::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)); - nowide::ifstream file(filePath, std::ios::binary); + std::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)); - nowide::ifstream file(filePath); + std::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)); - nowide::ifstream file(filePath); + std::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)); - nowide::ofstream outFile(filePath); + std::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 0cf5af0ee..273782910 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)); - nowide::ifstream file(manifestFilePath); + std::ifstream file(manifestFilePath); if (file) { try { return checkManifestValidity(file); diff --git a/src/ring_api.cpp b/src/ring_api.cpp index 8134b5486..1def4167f 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 = nowide::getenv("JAMI_LOG_FILE"); + const char* log_file = 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 3fc7a8931..08e55cb6c 100644 --- a/src/sip/sipaccount.cpp +++ b/src/sip/sipaccount.cpp @@ -65,7 +65,6 @@ #include -#include #include #include #include @@ -1389,8 +1388,8 @@ SIPAccount::getLoginName() DWORD size = UNLEN + 1; TCHAR username[UNLEN + 1]; std::string uname; - if (GetUserNameW((TCHAR*) username, &size)) { - uname = nowide::narrow(username); + if (GetUserName((TCHAR*) username, &size)) { + uname = jami::to_string(username); } return uname; #endif diff --git a/src/string_utils.cpp b/src/string_utils.cpp index c61b5a5ef..6762c8fa4 100644 --- a/src/string_utils.cpp +++ b/src/string_utils.cpp @@ -42,6 +42,39 @@ 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 4a81e1664..0197f5528 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -45,6 +45,11 @@ 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);