Skip to content

Commit

Permalink
common: add win32/wstring.h
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
petrutlucian94 committed Mar 22, 2023
1 parent 504ff07 commit a47caa1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions src/common/win32/wstring.cc
Original file line number Diff line number Diff line change
@@ -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 <boost/locale/encoding_utf.hpp>

using boost::locale::conv::utf_to_utf;

std::wstring to_wstring(const std::string& str)
{
return utf_to_utf<wchar_t>(str.c_str(), str.c_str() + str.size());
}

std::string to_string(const std::wstring& str)
{
return utf_to_utf<char>(str.c_str(), str.c_str() + str.size());
}
18 changes: 18 additions & 0 deletions src/common/win32/wstring.h
Original file line number Diff line number Diff line change
@@ -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 <string>

std::wstring to_wstring(const std::string& str);
std::string to_string(const std::wstring& wstr);
1 change: 1 addition & 0 deletions src/dokan/ceph_dokan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions src/dokan/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "common/ceph_argparse.h"
#include "common/config.h"
#include "common/win32/wstring.h"

#include "global/global_init.h"

Expand Down
14 changes: 0 additions & 14 deletions src/dokan/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@

#include "utils.h"

#include <boost/locale/encoding_utf.hpp>

using boost::locale::conv::utf_to_utf;

std::wstring to_wstring(const std::string& str)
{
return utf_to_utf<wchar_t>(str.c_str(), str.c_str() + str.size());
}

std::string to_string(const std::wstring& str)
{
return utf_to_utf<char>(str.c_str(), str.c_str() + str.size());
}

void to_filetime(time_t t, LPFILETIME pft)
{
// Note that LONGLONG is a 64-bit value
Expand Down
3 changes: 0 additions & 3 deletions src/dokan/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
14 changes: 1 addition & 13 deletions src/tools/rbd_wnbd/rbd_wnbd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include <sys/socket.h>
#include <unistd.h>

#include <boost/locale/encoding_utf.hpp>

#include "wnbd_handler.h"
#include "rbd_wnbd.h"

Expand All @@ -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"
Expand All @@ -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<wchar_t>(str.c_str(), str.c_str() + str.size());
}

std::string to_string(const std::wstring& str)
{
return utf_to_utf<char>(str.c_str(), str.c_str() + str.size());
}

bool is_process_running(DWORD pid)
{
Expand Down

0 comments on commit a47caa1

Please sign in to comment.