Skip to content

Commit

Permalink
refactor(WeaselDeployer): string convertions with macro
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed Jan 23, 2025
1 parent aba0609 commit 30e5adf
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
12 changes: 4 additions & 8 deletions WeaselDeployer/Configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#include "WeaselDeployer.h"

static void CreateFileIfNotExist(std::string filename) {
std::filesystem::path file_path =
WeaselUserDataPath() / string_to_wstring(filename, CP_UTF8);
std::filesystem::path file_path = WeaselUserDataPath() / u8tow(filename);
DWORD dwAttrib = GetFileAttributes(file_path.c_str());
if (!(INVALID_FILE_ATTRIBUTES != dwAttrib &&
0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))) {
Expand All @@ -34,15 +33,12 @@ Configurator::Configurator() {

void Configurator::Initialize() {
RIME_STRUCT(RimeTraits, weasel_traits);
std::string shared_dir =
wstring_to_string(WeaselSharedDataPath().wstring(), CP_UTF8);
std::string user_dir =
wstring_to_string(WeaselUserDataPath().wstring(), CP_UTF8);
std::string shared_dir = wtou8(WeaselSharedDataPath().wstring());
std::string user_dir = wtou8(WeaselUserDataPath().wstring());
weasel_traits.shared_data_dir = shared_dir.c_str();
weasel_traits.user_data_dir = user_dir.c_str();
weasel_traits.prebuilt_data_dir = weasel_traits.shared_data_dir;
std::string distribution_name =
wstring_to_string(get_weasel_ime_name(), CP_UTF8);
std::string distribution_name = wtou8(get_weasel_ime_name());
weasel_traits.distribution_name = distribution_name.c_str();
weasel_traits.distribution_code_name = WEASEL_CODE_NAME;
weasel_traits.distribution_version = WEASEL_VERSION;
Expand Down
9 changes: 4 additions & 5 deletions WeaselDeployer/DictManagementDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void DictManagementDialog::Populate() {
RimeUserDictIterator iter = {0};
api_->user_dict_iterator_init(&iter);
while (const char* dict = api_->next_user_dict(&iter)) {
std::wstring txt = string_to_wstring(dict, CP_UTF8);
std::wstring txt = u8tow(dict);
user_dict_list_.AddString(txt.c_str());
}
api_->user_dict_iterator_destroy(&iter);
Expand Down Expand Up @@ -121,7 +121,7 @@ LRESULT DictManagementDialog::OnBackup(WORD, WORD code, HWND, BOOL&) {
WCHAR dict_name[100] = {0};
user_dict_list_.GetText(sel, dict_name);
path += std::wstring(L"\\") + dict_name + L".userdb.txt";
std::string dict_name_str = wstring_to_string(dict_name, CP_UTF8);
std::string dict_name_str = wtou8(dict_name);
if (!api_->backup_user_dict(dict_name_str.c_str())) {
MSG_BY_IDS(IDS_STR_ERR_EXPORT_UNKNOWN, IDS_STR_SAD, MB_OK | MB_ICONERROR);
return 0;
Expand Down Expand Up @@ -200,7 +200,7 @@ LRESULT DictManagementDialog::OnExport(WORD, WORD code, HWND, BOOL&) {
char path[MAX_PATH] = {0};
WideCharToMultiByte(CP_UTF8, 0, selected_path.c_str(), -1, path,
_countof(path), NULL, NULL);
std::string dict_name_str = wstring_to_string(dict_name, CP_UTF8);
std::string dict_name_str = wtou8(dict_name);
int result = api_->export_user_dict(dict_name_str.c_str(), path);
if (result < 0) {
MSG_BY_IDS(IDS_STR_ERR_UNKNOWN, IDS_STR_SAD, MB_OK | MB_ICONERROR);
Expand Down Expand Up @@ -250,8 +250,7 @@ LRESULT DictManagementDialog::OnImport(WORD, WORD code, HWND, BOOL&) {
char path[MAX_PATH] = {0};
WideCharToMultiByte(CP_UTF8, 0, selected_path.c_str(), -1, path,
_countof(path), NULL, NULL);
int result = api_->import_user_dict(
wstring_to_string(dict_name, CP_UTF8).c_str(), path);
int result = api_->import_user_dict(wtou8(dict_name).c_str(), path);
if (result < 0) {
MSG_BY_IDS(IDS_STR_ERR_UNKNOWN, IDS_STR_SAD, MB_OK | MB_ICONERROR);
} else {
Expand Down
8 changes: 4 additions & 4 deletions WeaselDeployer/SwitcherSettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void SwitcherSettingsDialog::Populate() {
if (!strcmp(item.schema_id, schema_id) &&
recruited.find(info) == recruited.end()) {
recruited.insert(info);
std::wstring itemwstr = string_to_wstring(item.name, CP_UTF8);
std::wstring itemwstr = u8tow(item.name);
schema_list_.AddItem(k, 0, itemwstr.c_str());
schema_list_.SetItemData(k, (DWORD_PTR)info);
schema_list_.SetCheckState(k, TRUE);
Expand All @@ -46,13 +46,13 @@ void SwitcherSettingsDialog::Populate() {
RimeSchemaInfo* info = (RimeSchemaInfo*)item.reserved;
if (recruited.find(info) == recruited.end()) {
recruited.insert(info);
std::wstring itemwstr = string_to_wstring(item.name, CP_UTF8);
std::wstring itemwstr = u8tow(item.name);
schema_list_.AddItem(k, 0, itemwstr.c_str());
schema_list_.SetItemData(k, (DWORD_PTR)info);
++k;
}
}
std::wstring txt = string_to_wstring(api_->get_hotkeys(settings_), CP_UTF8);
std::wstring txt = u8tow(api_->get_hotkeys(settings_));
hotkeys_.SetWindowTextW(txt.c_str());
loaded_ = true;
modified_ = false;
Expand All @@ -71,7 +71,7 @@ void SwitcherSettingsDialog::ShowDetails(RimeSchemaInfo* info) {
if (const char* description = api_->get_schema_description(info)) {
(details += "\n\n") += description;
}
std::wstring txt = string_to_wstring(details.c_str(), CP_UTF8);
std::wstring txt = u8tow(details.c_str());
description_.SetWindowTextW(txt.c_str());
}

Expand Down
2 changes: 1 addition & 1 deletion WeaselDeployer/UIStyleSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool UIStyleSettings::GetPresetColorSchemes(

// check if a file exists
static inline bool IfFileExist(std::string filename) {
DWORD dwAttrib = GetFileAttributes(string_to_wstring(filename).c_str());
DWORD dwAttrib = GetFileAttributes(acptow(filename).c_str());
return (INVALID_FILE_ATTRIBUTES != dwAttrib &&
0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
Expand Down
4 changes: 2 additions & 2 deletions WeaselDeployer/UIStyleSettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void UIStyleSettingsDialog::Populate() {
int active_index = -1;
settings_->GetPresetColorSchemes(&preset_);
for (size_t i = 0; i < preset_.size(); ++i) {
std::wstring txt = string_to_wstring(preset_[i].name.c_str(), CP_UTF8);
std::wstring txt = u8tow(preset_[i].name);
color_schemes_.AddString(txt.c_str());
if (preset_[i].color_scheme_id == active) {
active_index = i;
Expand Down Expand Up @@ -72,7 +72,7 @@ void UIStyleSettingsDialog::Preview(int index) {
return;
image_.Destroy();
// it is from ansi coding, not utf8
image_.Load(string_to_wstring(file_path).c_str());
image_.Load(acptow(file_path).c_str());
if (!image_.IsNull()) {
preview_.SetBitmap(image_);
}
Expand Down

0 comments on commit 30e5adf

Please sign in to comment.