From 6b8b932f5ec038b35070f91022aed595c74a6eef Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Wed, 16 Nov 2022 10:50:14 +0200 Subject: [PATCH] common: add win32/wstring.h Windows APIs heavily use wchar. ceph-dokan and rbd-wnbd have some duplicated helpers that convert wstrings to/from utf8 strings. To avoid duplication and allow reusing those helpers, we're moving them to common/win32/wstring.h. We're using the "win32" subfolder because it's unlikely that this will ever be used on other platforms. Signed-off-by: Lucian Petrut (cherry picked from commit a47caa156a4e667d6b9ff7236cc9148ba37ea772) --- src/common/CMakeLists.txt | 1 + src/common/win32/wstring.cc | 27 +++++++++++++++++++++++++++ src/common/win32/wstring.h | 18 ++++++++++++++++++ src/dokan/ceph_dokan.cc | 1 + src/dokan/options.cc | 1 + src/dokan/utils.cc | 14 -------------- src/dokan/utils.h | 3 --- src/tools/rbd_wnbd/rbd_wnbd.cc | 14 +------------- 8 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 src/common/win32/wstring.cc create mode 100644 src/common/win32/wstring.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 498b2662868..1297df32405 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -173,6 +173,7 @@ elseif(AIX) list(APPEND common_srcs aix_errno.cc) elseif(WIN32) list(APPEND common_srcs win32/errno.cc) + list(APPEND common_srcs win32/wstring.cc) endif() if(WITH_EVENTTRACE) diff --git a/src/common/win32/wstring.cc b/src/common/win32/wstring.cc new file mode 100644 index 00000000000..1f9b49a58f7 --- /dev/null +++ b/src/common/win32/wstring.cc @@ -0,0 +1,27 @@ +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2022 Cloudbase Solutions + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "wstring.h" + +#include + +using boost::locale::conv::utf_to_utf; + +std::wstring to_wstring(const std::string& str) +{ + return utf_to_utf(str.c_str(), str.c_str() + str.size()); +} + +std::string to_string(const std::wstring& str) +{ + return utf_to_utf(str.c_str(), str.c_str() + str.size()); +} diff --git a/src/common/win32/wstring.h b/src/common/win32/wstring.h new file mode 100644 index 00000000000..cb308c70dbb --- /dev/null +++ b/src/common/win32/wstring.h @@ -0,0 +1,18 @@ +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2022 Cloudbase Solutions + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#pragma once + +#include + +std::wstring to_wstring(const std::string& str); +std::string to_string(const std::wstring& wstr); diff --git a/src/dokan/ceph_dokan.cc b/src/dokan/ceph_dokan.cc index 4738d096ac4..9e115222cab 100644 --- a/src/dokan/ceph_dokan.cc +++ b/src/dokan/ceph_dokan.cc @@ -35,6 +35,7 @@ #include "common/dout.h" #include "common/errno.h" #include "common/version.h" +#include "common/win32/wstring.h" #include "global/global_init.h" diff --git a/src/dokan/options.cc b/src/dokan/options.cc index 4cfe08cdbe2..4c968e13af5 100644 --- a/src/dokan/options.cc +++ b/src/dokan/options.cc @@ -17,6 +17,7 @@ #include "common/ceph_argparse.h" #include "common/config.h" +#include "common/win32/wstring.h" #include "global/global_init.h" diff --git a/src/dokan/utils.cc b/src/dokan/utils.cc index 4705359c4d0..576cceb9916 100644 --- a/src/dokan/utils.cc +++ b/src/dokan/utils.cc @@ -12,20 +12,6 @@ #include "utils.h" -#include - -using boost::locale::conv::utf_to_utf; - -std::wstring to_wstring(const std::string& str) -{ - return utf_to_utf(str.c_str(), str.c_str() + str.size()); -} - -std::string to_string(const std::wstring& str) -{ - return utf_to_utf(str.c_str(), str.c_str() + str.size()); -} - void to_filetime(time_t t, LPFILETIME pft) { // Note that LONGLONG is a 64-bit value diff --git a/src/dokan/utils.h b/src/dokan/utils.h index aa52b4d3a3c..f6a6a8e6a5c 100644 --- a/src/dokan/utils.h +++ b/src/dokan/utils.h @@ -12,8 +12,5 @@ #include "include/compat.h" -std::wstring to_wstring(const std::string& str); -std::string to_string(const std::wstring& str); - void to_filetime(time_t t, LPFILETIME pft); void to_unix_time(FILETIME ft, time_t *t); diff --git a/src/tools/rbd_wnbd/rbd_wnbd.cc b/src/tools/rbd_wnbd/rbd_wnbd.cc index a2a772c9403..b6a0e6bbd46 100644 --- a/src/tools/rbd_wnbd/rbd_wnbd.cc +++ b/src/tools/rbd_wnbd/rbd_wnbd.cc @@ -20,8 +20,6 @@ #include #include -#include - #include "wnbd_handler.h" #include "rbd_wnbd.h" @@ -38,6 +36,7 @@ #include "common/errno.h" #include "common/version.h" #include "common/win32/service.h" +#include "common/win32/wstring.h" #include "common/admin_socket_client.h" #include "global/global_init.h" @@ -54,17 +53,6 @@ #define dout_prefix *_dout << "rbd-wnbd: " using namespace std; -using boost::locale::conv::utf_to_utf; - -std::wstring to_wstring(const std::string& str) -{ - return utf_to_utf(str.c_str(), str.c_str() + str.size()); -} - -std::string to_string(const std::wstring& str) -{ - return utf_to_utf(str.c_str(), str.c_str() + str.size()); -} bool is_process_running(DWORD pid) { -- 2.39.5