Skip to content

Commit

Permalink
Replace std::wstring_convert with MB2Wide/Wide2MB from far2l/utils, see
Browse files Browse the repository at this point in the history
  • Loading branch information
cycleg committed May 9, 2017
1 parent cdff9f8 commit cb4c708
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 56 deletions.
6 changes: 2 additions & 4 deletions src/Crypto.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <locale>
#include <codecvt>
#include <openssl/aes.h>
#include <utils.h>
#include "Crypto.h"

Crypto::Crypto()
Expand All @@ -17,8 +16,7 @@ Crypto::~Crypto()

bool Crypto::init(const std::wstring& keydata)
{
std::string l_keydata = std::wstring_convert<std::codecvt_utf8<wchar_t> >()
.to_bytes(keydata);
std::string l_keydata = StrWide2MB(keydata);
int i, nrounds = 5;
BYTE key[32], iv[32];

Expand Down
26 changes: 10 additions & 16 deletions src/MountPoint.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <string>
#include <locale>
#include <codecvt>
#include <utils.h>
#include "GvfsService.h"
#include "MountPoint.h"

Expand Down Expand Up @@ -50,9 +49,9 @@ MountPoint& MountPoint::operator=(const MountPoint& other)

bool MountPoint::mount(GvfsService* service) throw(GvfsServiceException)
{
std::string resPath = std::wstring_convert<std::codecvt_utf8<wchar_t> >().to_bytes(m_resPath);
std::string userName = std::wstring_convert<std::codecvt_utf8<wchar_t> >().to_bytes(m_user);
std::string password = std::wstring_convert<std::codecvt_utf8<wchar_t> >().to_bytes(m_password);
std::string resPath(StrWide2MB(m_resPath));
std::string userName(StrWide2MB(m_user));
std::string password(StrWide2MB(m_password));

// пароль спрашивают перед монтированием, зачищаем его
if (m_askPassword) m_password.clear();
Expand All @@ -63,10 +62,8 @@ bool MountPoint::mount(GvfsService* service) throw(GvfsServiceException)
if (success)
{
m_bMounted = true;
m_shareName = std::wstring_convert<std::codecvt_utf8<wchar_t> >()
.from_bytes(service->getMountName());
m_mountPointPath = std::wstring_convert<std::codecvt_utf8<wchar_t> >()
.from_bytes(service->getMountPath());
StrMB2Wide(service->getMountName(), m_shareName);
StrMB2Wide(service->getMountPath(), m_mountPointPath);
}
return success;
}
Expand All @@ -75,7 +72,7 @@ bool MountPoint::unmount(GvfsService* service) throw(GvfsServiceException)
{
if (!m_bMounted) return true;

std::string resPath = std::wstring_convert<std::codecvt_utf8<wchar_t> >().to_bytes(this->m_resPath);
std::string resPath(StrWide2MB(this->m_resPath));
if (resPath.empty()) return false;

bool success = false;
Expand All @@ -102,22 +99,19 @@ bool MountPoint::unmount(GvfsService* service) throw(GvfsServiceException)

void MountPoint::mountCheck(GvfsService* service)
{
std::string resPath = std::wstring_convert<std::codecvt_utf8<wchar_t> >().to_bytes(m_resPath);
std::string resPath(StrWide2MB(m_resPath));
if (resPath.empty()) return;

if (service->mounted(resPath))
{
m_bMounted = true;
m_shareName = std::wstring_convert<std::codecvt_utf8<wchar_t> >()
.from_bytes(service->getMountName());
m_mountPointPath = std::wstring_convert<std::codecvt_utf8<wchar_t> >()
.from_bytes(service->getMountPath());
StrMB2Wide(service->getMountName(), m_shareName);
StrMB2Wide(service->getMountPath(), m_mountPointPath);
}
else
{
m_mountPointPath.clear();
m_shareName.clear();
m_bMounted = false;
}

}
11 changes: 4 additions & 7 deletions src/MountPointStorage.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <cstring>
#include <codecvt>
#include <locale>
#include <windows.h>
#include <utils.h>
#include <uuid/uuid.h>

#ifdef USE_OPENSSL
Expand Down Expand Up @@ -187,7 +186,7 @@ void MountPointStorage::GenerateId(std::wstring& id)
char* out = new char[UUID_TEXT_SIZE];
uuid_generate(uuid);
uuid_unparse(uuid, out);
id = std::wstring_convert<std::codecvt_utf8<wchar_t> >().from_bytes(out);
MB2Wide(out, id);
delete[] out;
}

Expand All @@ -214,8 +213,7 @@ void MountPointStorage::Encrypt(const std::wstring& keydata,
std::vector<BYTE>& out)
{
std::vector<BYTE> plain;
std::string buf = std::wstring_convert<std::codecvt_utf8<wchar_t> >()
.to_bytes(in);
std::string buf(StrWide2MB(in));
Crypto crypto;
crypto.init(keydata);
for (const std::string::value_type ch : buf) plain.push_back((BYTE)ch);
Expand Down Expand Up @@ -271,8 +269,7 @@ void MountPointStorage::Decrypt(const std::wstring& keydata,
case 3:
crypto.decrypt(in, plain);
for (const BYTE ch : plain) buf.push_back(ch);
out = std::wstring_convert<std::codecvt_utf8<wchar_t> >()
.from_bytes(buf);
StrMB2Wide(buf, out);
break;
default:
break;
Expand Down
9 changes: 3 additions & 6 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <iostream> // debug output
#include <codecvt>
#include <locale>
#include <utils.h>
#include "Configuration.h"
#include "gvfsdlg.h"
#include "GvfsService.h"
Expand Down Expand Up @@ -318,8 +317,7 @@ int Plugin::setDirectory(HANDLE Plugin, const wchar_t* Dir, int OpMode)
}
catch (const GvfsServiceException& error)
{
std::wstring buf =
std::wstring_convert<std::codecvt_utf8<wchar_t> >().from_bytes(error.what().raw());
std::wstring buf(StrMB2Wide(error.what().raw()));
msgItems[1] = buf.c_str();
m_pPsi.Message(m_pPsi.ModuleNumber, FMSG_WARNING | FMSG_MB_OK,
NULL, msgItems, ARRAYSIZE(msgItems), 0);
Expand Down Expand Up @@ -476,8 +474,7 @@ void Plugin::unmountResource(MountPoint& point)
}
catch (const GvfsServiceException& error)
{
std::wstring buf =
std::wstring_convert<std::codecvt_utf8<wchar_t> >().from_bytes(error.what().raw());
std::wstring buf(StrMB2Wide(error.what().raw()));
msgItems[1] = buf.c_str();
m_pPsi.Message(m_pPsi.ModuleNumber, FMSG_WARNING | FMSG_MB_OK,
NULL, msgItems, ARRAYSIZE(msgItems), 0);
Expand Down
1 change: 0 additions & 1 deletion src/PluginMain.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "Plugin.h"

#include <windows.h>
Expand Down
7 changes: 3 additions & 4 deletions src/RegistryStorage.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <codecvt>
#include <locale>
#include <utils.h>
#include "RegistryStorage.h"

bool RegistryStorage::SetValue(HKEY folder, const std::wstring& field,
Expand All @@ -15,7 +14,7 @@ bool RegistryStorage::SetValue(HKEY folder, const std::wstring& field,
const std::wstring& value) const
{
if (!folder || field.empty()) return false;
std::string buf = std::wstring_convert<std::codecvt_utf8<wchar_t> >().to_bytes(value);
std::string buf(StrWide2MB(value));
LONG res = WINPORT(RegSetValueEx)(folder, field.c_str(), 0, REG_SZ_MB,
(const BYTE*)buf.c_str(), buf.size() + 1);
return res == ERROR_SUCCESS;
Expand Down Expand Up @@ -81,7 +80,7 @@ bool RegistryStorage::GetValue(HKEY folder, const std::wstring& field,
}
if (res == ERROR_SUCCESS)
{
value = std::wstring_convert<std::codecvt_utf8<wchar_t> >().from_bytes(std::string(buf));
MB2Wide(buf, value);
}
} while (res == ERROR_MORE_DATA);
delete[] buf;
Expand Down
20 changes: 9 additions & 11 deletions src/UiCallbacks.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <locale>
#include <codecvt>
#include <string>
#include <vector>
#include <utils.h>
#include "gvfsdlg.h"
#include "TextFormatter.h"
#include "UiCallbacks.h"
Expand All @@ -13,27 +12,26 @@ UiCallbacks::UiCallbacks(PluginStartupInfo& info):

void UiCallbacks::onAskQuestion(char* message, char** choices, int& choice) const
{
const int DIALOG_WIDTH = 78;
int answer = choice;
char** l_choice = choices;
std::vector<std::wstring> messageLines, answers;
TextFormatter formatter;
formatter.TextWidth(70) // look at AskQuestionDlg() in gvfsdlg.cpp
// 3 columns - frame, 1 column - pad
formatter.TextWidth(DIALOG_WIDTH - (3 + 1) * 2)
.WordWrap(true)
.IgnoreEmptyLines(false)
.DropRemain(true);
formatter.FitToLines(
std::wstring_convert<std::codecvt_utf8<wchar_t> >().from_bytes(message),
messageLines
);
formatter.FitToLines(MB2Wide(message), messageLines);
while (*l_choice)
{
answers.push_back(
std::wstring_convert<std::codecvt_utf8<wchar_t> >().from_bytes(*l_choice)
);
answers.push_back(MB2Wide(*l_choice));
l_choice++;
}
formatter.TextWidth(DIALOG_WIDTH - 4 * 2 - 2); // another 2 columns in combobox
for (auto& buf : answers) buf = formatter.FitToLine(buf);
if (!AskQuestionDlg(m_pStartupInfo, messageLines, answers, (unsigned int&)answer))
if (!AskQuestionDlg(m_pStartupInfo, DIALOG_WIDTH, messageLines, answers,
(unsigned int&)answer))
answer = -1;
choice = answer;
}
12 changes: 5 additions & 7 deletions src/gvfsdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,14 @@ bool ConfigurationEditDlg(PluginStartupInfo& info)
}

bool AskQuestionDlg(PluginStartupInfo& info,
const int DIALOG_WIDTH,
const std::vector<std::wstring>& message,
const std::vector<std::wstring>& choices,
unsigned int& choice)
{
const int DIALOG_WIDTH = 78;
int DIALOG_HEIGHT = 6 + message.size(); // (1 + 1) * 2 + 2
// 2 rows - frame, 2 rows - buttons, total: 2 * 2 + 2
// first line in message - title
int DIALOG_HEIGHT = 6 + message.size();
std::vector<InitDialogItem> initItems = {
{ DI_DOUBLEBOX, 2, 1, DIALOG_WIDTH - 3, DIALOG_HEIGHT - 2, 0, 0, 0, 0,
-1, message[0], 0 },
Expand All @@ -279,6 +281,7 @@ bool AskQuestionDlg(PluginStartupInfo& info,
// add choices list
std::vector<FarListItem> choicesList;
unsigned int choicesWidth = 0;
// max width limited in UiCallbacks::onAskQuestion()
for (unsigned int i = 0; i < choices.size(); i++)
{
FarListItem item = {
Expand All @@ -288,11 +291,6 @@ bool AskQuestionDlg(PluginStartupInfo& info,
choices[i].size() : choicesWidth;
choicesList.push_back(item);
}
if (choicesWidth > DIALOG_WIDTH - 8 - 1)
{
// 3 symbols - frame, 1 symbol - pad, 1 - arrow
choicesWidth = DIALOG_WIDTH - 8 - 1;
}
FarList listInfo = { int(choicesList.size()), choicesList.data() };
{
InitDialogItem item = {
Expand Down
1 change: 1 addition & 0 deletions src/gvfsdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ bool ConfigurationEditDlg(PluginStartupInfo& info);
/// Ответ на вопрос в ходе монтирования ресурса.
///
bool AskQuestionDlg(PluginStartupInfo& info,
const int DIALOG_WIDTH,
const std::vector<std::wstring>& message,
const std::vector<std::wstring>& choices,
unsigned int& choice);

0 comments on commit cb4c708

Please sign in to comment.