Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Aug 8, 2016
1 parent 58be372 commit 40e9ed6
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 113 deletions.
98 changes: 55 additions & 43 deletions far/PluginA.cpp

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions far/changelog
@@ -1,3 +1,7 @@
drkns 08.08.2016 23:38:23 +0200 - build 4754

1. Рефакторинг.

drkns 07.08.2016 21:59:50 +0200 - build 4753

1. После 4747 не работали адаптеры.
Expand Down
6 changes: 3 additions & 3 deletions far/clipboard.cpp
Expand Up @@ -36,7 +36,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "clipboard.hpp"
#include "console.hpp"
#include "strmix.hpp"
#include "codepage.hpp"

default_clipboard_mode::mode default_clipboard_mode::m_Mode = default_clipboard_mode::system;

Expand Down Expand Up @@ -393,7 +393,7 @@ bool Clipboard::GetHDROPAsText(string& data) const
{
for (const auto& i: enum_substrings(StartA))
{
data.append(wide(std::string(i.data(), i.size()), CP_ACP)).append(L"\r\n");
data.append(unicode::from(CP_ACP, i)).append(L"\r\n");
}
}
Result = true;
Expand Down Expand Up @@ -431,7 +431,7 @@ bool Clipboard::GetVText(string& data) const
{
if (const auto OemData = os::memory::global::lock<const char*>(hClipData))
{
data = wide(OemData.get());
data = unicode::from(CP_OEMCP, OemData.get());
Result = true;
}
}
Expand Down
34 changes: 26 additions & 8 deletions far/codepage.hpp
Expand Up @@ -40,49 +40,67 @@ namespace unicode
#define NOT_PTR(T) typename T, ENABLE_IF(!std::is_pointer<T>::value)

size_t to(uintptr_t Codepage, const wchar_t* Data, size_t Size, char* Buffer, size_t BufferSize, bool* UsedDefaultChar = nullptr);

template<NOT_PTR(T)>
size_t to(uintptr_t Codepage, const T& Data, char* Buffer, size_t BufferSize, bool* UsedDefaultChar = nullptr)
auto to(uintptr_t Codepage, const T& Data, char* Buffer, size_t BufferSize, bool* UsedDefaultChar = nullptr)
{
return to(Codepage, Data.data(), Data.size(), Buffer, BufferSize, UsedDefaultChar);
}

template<NOT_PTR(T)>
size_t to(uintptr_t Codepage, const wchar_t* Data, size_t Size, T& Buffer, bool* UsedDefaultChar = nullptr)
auto to(uintptr_t Codepage, const wchar_t* Data, size_t Size, T& Buffer, bool* UsedDefaultChar = nullptr)
{
return to(Codepage, Data, Size, Buffer.data(), Buffer.size(), UsedDefaultChar);
}

template<NOT_PTR(T), NOT_PTR(Y)>
size_t to(uintptr_t Codepage, const T& Data, Y& Buffer, bool* UsedDefaultChar = nullptr)
auto to(uintptr_t Codepage, const T& Data, Y& Buffer, bool* UsedDefaultChar = nullptr)
{
return to(Codepage, Data.data(), Data.size(), Buffer.data(), Buffer.size(), UsedDefaultChar);
}

std::string to(uintptr_t Codepage, const wchar_t* Data, size_t Size, bool* UsedDefaultChar = nullptr);

inline auto to(uintptr_t Codepage, const wchar_t* Data, bool* UsedDefaultChar = nullptr)
{
return to(Codepage, Data, wcslen(Data), UsedDefaultChar);
}

template<NOT_PTR(T)>
std::string to(uintptr_t Codepage, const T& Data, bool* UsedDefaultChar = nullptr)
auto to(uintptr_t Codepage, const T& Data, bool* UsedDefaultChar = nullptr)
{
return to(Codepage, Data.data(), Data.size(), UsedDefaultChar);
}

size_t from(uintptr_t Codepage, const char* Data, size_t Size, wchar_t* Buffer, size_t BufferSize);

template<NOT_PTR(T)>
size_t from(uintptr_t Codepage, const T& Data, wchar_t* Buffer, size_t BufferSize)
auto from(uintptr_t Codepage, const T& Data, wchar_t* Buffer, size_t BufferSize)
{
return from(Codepage, Data.data(), Data.size(), Buffer, BufferSize);
}

template<NOT_PTR(T)>
size_t from(uintptr_t Codepage, const char* Data, size_t Size, T& Buffer)
auto from(uintptr_t Codepage, const char* Data, size_t Size, T& Buffer)
{
return from(Codepage, Data, Size, Buffer.data(), Buffer.size());
}

template<NOT_PTR(T), NOT_PTR(Y)>
size_t from(uintptr_t Codepage, const T& Data, Y& Buffer)
auto from(uintptr_t Codepage, const T& Data, Y& Buffer)
{
return from(Codepage, Data.data(), Data.size(), Buffer.data(), Buffer.size());
}

string from(uintptr_t Codepage, const char* Data, size_t Size);

inline auto from(uintptr_t Codepage, const char* Data)
{
return from(Codepage, Data, strlen(Data));
}

template<NOT_PTR(T)>
string from(uintptr_t Codepage, const T& Data)
auto from(uintptr_t Codepage, const T& Data)
{
return from(Codepage, Data.data(), Data.size());
}
Expand Down
2 changes: 1 addition & 1 deletion far/config.cpp
Expand Up @@ -3045,7 +3045,7 @@ void Options::ShellOptions(bool LastCommand, const MOUSE_EVENT_RECORD *MouseEven
}
catch (const far_exception& e)
{
Message(MSG_WARNING, 1, MSG(MError), wide(e.get_message(), CP_UTF8).data(), MSG(MOk));
Message(MSG_WARNING, 1, MSG(MError), unicode::from(CP_UTF8, e.get_message()).data(), MSG(MOk));
strLanguage = SavedLanguage;
}

Expand Down
45 changes: 23 additions & 22 deletions far/configdb.cpp
Expand Up @@ -36,6 +36,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "configdb.hpp"
#include "sqlitedb.hpp"
#include "strmix.hpp"
#include "codepage.hpp"
#include "pathmix.hpp"
#include "config.hpp"
#include "datetime.hpp"
Expand Down Expand Up @@ -277,16 +278,16 @@ class iGeneralConfigDb: public GeneralConfig, public SQLiteDb
if (!key || !name || !type || !value)
continue;

const auto Key = wide(key, CP_UTF8);
const auto Name = wide(name, CP_UTF8);
const auto Key = unicode::from(CP_UTF8, key);
const auto Name = unicode::from(CP_UTF8, name);

if (!strcmp(type,"qword"))
{
SetValue(Key, Name, strtoull(value, nullptr, 16));
}
else if (!strcmp(type,"text"))
{
SetValue(Key, Name, wide(value, CP_UTF8));
SetValue(Key, Name, unicode::from(CP_UTF8, value));
}
else if (!strcmp(type,"hex"))
{
Expand Down Expand Up @@ -640,12 +641,12 @@ class HierarchicalConfigDb: public HierarchicalConfig, public SQLiteDb
const auto key_name = key.Attribute("name");
if (!key_name)
return;
const auto KeyName = wide(key_name, CP_UTF8);
const auto KeyName = unicode::from(CP_UTF8, key_name);
const auto key_description = key.Attribute("description");
string KeyDescription;
if (key_description)
{
KeyDescription = wide(key_description, CP_UTF8);
KeyDescription = unicode::from(CP_UTF8, key_description);
}
const auto Key = CreateKey(root, KeyName, key_description? &KeyDescription : nullptr);
if (!Key.get())
Expand All @@ -660,15 +661,15 @@ class HierarchicalConfigDb: public HierarchicalConfig, public SQLiteDb
if (!name || !type)
continue;

string Name = wide(name, CP_UTF8);
string Name = unicode::from(CP_UTF8, name);

if (value && !strcmp(type, "qword"))
{
SetValue(Key, Name, strtoull(value, nullptr, 16));
}
else if (value && !strcmp(type, "text"))
{
string Value = wide(value, CP_UTF8);
string Value = unicode::from(CP_UTF8, value);
SetValue(Key, Name, Value);
}
else if (value && !strcmp(type, "hex"))
Expand Down Expand Up @@ -761,7 +762,7 @@ class HighlightHierarchicalConfigDb: public HierarchicalConfigDb
e.SetAttribute("type", "color");
e.SetAttribute("background", to_hex_string(Color.BackgroundColor).data());
e.SetAttribute("foreground", to_hex_string(Color.ForegroundColor).data());
e.SetAttribute("flags", Utf8String(FlagsToString(Color.Flags, ColorFlagNames)).data());
e.SetAttribute("flags", unicode::to(CP_UTF8, FlagsToString(Color.Flags, ColorFlagNames)).data());
}
else
{
Expand All @@ -780,7 +781,7 @@ class HighlightHierarchicalConfigDb: public HierarchicalConfigDb
if (const auto foreground = e.Attribute("foreground"))
Color.ForegroundColor = std::strtoul(foreground, nullptr, 16);
if (const auto flags = e.Attribute("flags"))
Color.Flags = StringToFlags(wide(flags, CP_UTF8), ColorFlagNames);
Color.Flags = StringToFlags(unicode::from(CP_UTF8, flags), ColorFlagNames);

std::vector<char> Blob(sizeof(FarColor));
std::memcpy(Blob.data(), &Color, sizeof(Color));
Expand Down Expand Up @@ -864,7 +865,7 @@ class ColorsConfigDb: public ColorsConfig, public SQLiteDb
auto& Color = *reinterpret_cast<const FarColor*>(Blob.data());
e.SetAttribute("background", to_hex_string(Color.BackgroundColor).data());
e.SetAttribute("foreground", to_hex_string(Color.ForegroundColor).data());
e.SetAttribute("flags", Utf8String(FlagsToString(Color.Flags, ColorFlagNames)).data());
e.SetAttribute("flags", unicode::to(CP_UTF8, FlagsToString(Color.Flags, ColorFlagNames)).data());
}

stmtEnumAllValues.Reset();
Expand All @@ -883,14 +884,14 @@ class ColorsConfigDb: public ColorsConfig, public SQLiteDb
if (!name)
continue;

string Name = wide(name, CP_UTF8);
const auto Name = unicode::from(CP_UTF8, name);

if(background && foreground && flags)
{
FarColor Color = {};
Color.BackgroundColor = std::strtoul(background, nullptr, 16);
Color.ForegroundColor = std::strtoul(foreground, nullptr, 16);
Color.Flags = StringToFlags(wide(flags, CP_UTF8), ColorFlagNames);
Color.Flags = StringToFlags(unicode::from(CP_UTF8, flags), ColorFlagNames);
SetValue(Name, Color);
}
else
Expand Down Expand Up @@ -1106,8 +1107,8 @@ class AssociationsConfigDb: public AssociationsConfig, public SQLiteDb
if (!mask)
continue;

const auto Mask = wide(mask, CP_UTF8);
const auto Description = wide(NullToEmpty(description), CP_UTF8);
const auto Mask = unicode::from(CP_UTF8, mask);
const auto Description = unicode::from(CP_UTF8, NullToEmpty(description));

id = AddType(id, Mask, Description);
if (!id)
Expand All @@ -1127,7 +1128,7 @@ class AssociationsConfigDb: public AssociationsConfig, public SQLiteDb
if (se->QueryIntAttribute("enabled", &enabled) != tinyxml::XML_NO_ERROR)
continue;

SetCommand(id, type, wide(command, CP_UTF8), enabled != 0);
SetCommand(id, type, unicode::from(CP_UTF8, command), enabled != 0);
}

}
Expand Down Expand Up @@ -1660,7 +1661,7 @@ class PluginsHotkeysConfigDb: public PluginsHotkeysConfig, public SQLiteDb
if (!key)
continue;

string Key = wide(key, CP_UTF8);
const auto Key = unicode::from(CP_UTF8, key);

for (const auto& se: xml_enum(*e, "hotkey"))
{
Expand All @@ -1670,10 +1671,10 @@ class PluginsHotkeysConfigDb: public PluginsHotkeysConfig, public SQLiteDb

GUID Guid;

if (!stype || !guid || !StrToGuid(wide(guid, CP_UTF8), Guid))
if (!stype || !guid || !StrToGuid(unicode::from(CP_UTF8, guid), Guid))
continue;

string Hotkey = wide(hotkey, CP_UTF8);
const auto Hotkey = unicode::from(CP_UTF8, hotkey);

if (!strcmp(stype,"drive"))
SetHotkey(Key, Guid, hotkey_type::drive_menu, Hotkey);
Expand Down Expand Up @@ -2374,7 +2375,7 @@ enum dbcheck: int

HierarchicalConfigUniquePtr config_provider::CreatePluginsConfig(const string& guid, bool Local)
{
return CreateHierarchicalConfig<HierarchicalConfigDb>(CHECK_NONE, L"PluginsData\\" + guid + L".db", Utf8String(guid).data(), Local, true);
return CreateHierarchicalConfig<HierarchicalConfigDb>(CHECK_NONE, L"PluginsData\\" + guid + L".db", unicode::to(CP_UTF8, guid).data(), Local, true);
}

HierarchicalConfigUniquePtr config_provider::CreateFiltersConfig()
Expand Down Expand Up @@ -2423,7 +2424,7 @@ bool config_provider::Export(const string& File)
{
representation_destination Representation;
auto& root = Representation.GetRoot();
root.SetAttribute("version", Utf8String(std::to_wstring(FAR_VERSION.Major) + L"." + std::to_wstring(FAR_VERSION.Minor) + L"." + std::to_wstring(FAR_VERSION.Build)).data());
root.SetAttribute("version", (std::to_string(FAR_VERSION.Major) + "." + std::to_string(FAR_VERSION.Minor) + "." + std::to_string(FAR_VERSION.Build)).data());

GeneralCfg()->Export(Representation);
LocalGeneralCfg()->Export(Representation);
Expand All @@ -2449,7 +2450,7 @@ bool config_provider::Export(const string& File)
if (std::regex_search(i.strFileName, uuid_regex()))
{
auto& PluginRoot = Representation.CreateChild(e, "plugin");
PluginRoot.SetAttribute("guid", Utf8String(i.strFileName).data());
PluginRoot.SetAttribute("guid", unicode::to(CP_UTF8, i.strFileName).data());
Representation.SetRoot(PluginRoot);
CreatePluginsConfig(i.strFileName)->Export(Representation);
}
Expand Down Expand Up @@ -2496,7 +2497,7 @@ bool config_provider::Import(const string& Filename)
const auto guid = plugin->Attribute("guid");
if (!guid)
continue;
auto Guid = Upper(wide(guid, CP_UTF8));
const auto Guid = Upper(unicode::from(CP_UTF8, guid));

if (std::regex_search(Guid, uuid_regex()))
{
Expand Down
2 changes: 1 addition & 1 deletion far/editor.cpp
Expand Up @@ -213,7 +213,7 @@ void Editor::KeepInitParameters() const
if (Global->GetSearchHex())
{
// BUGBUG, it's unclear how to represent unicode in hex
const auto AnsiStr = narrow(strLastSearchStr);
const auto AnsiStr = unicode::to(m_codepage, strLastSearchStr);
Global->StoreSearchString(BlobToHexWString(AnsiStr.data(), AnsiStr.size(), 0), true);
}
else
Expand Down
2 changes: 1 addition & 1 deletion far/farexcpt.cpp
Expand Up @@ -428,7 +428,7 @@ static bool ProcessGenericException(EXCEPTION_POINTERS *xp, const wchar_t* Funct
strBuf.append(L" (");
if (Message)
{
strBuf.append(L"std::exception: ").append(wide(Message, CP_UTF8));
strBuf.append(L"std::exception: ").append(unicode::from(CP_UTF8, Message));
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion far/farrtl.cpp
Expand Up @@ -8,6 +8,7 @@ farrtl.cpp
#pragma hdrstop

#include "strmix.hpp"
#include "codepage.hpp"

#ifdef MEMCHECK
#undef DuplicateString
Expand Down Expand Up @@ -226,7 +227,7 @@ static string FindStr(const void* Data, size_t Size)

if (std::all_of(ABegin, AEnd, [](char c){ return c >= ' ' || IsEol(c); }))
{
return string(wide(std::string(ABegin, AEnd)));
return string(unicode::from(CP_ACP, ABegin, AEnd - ABegin));
}

const auto WBegin = reinterpret_cast<const wchar_t*>(Data), WEnd = WBegin + Size / sizeof(wchar_t);
Expand Down
2 changes: 1 addition & 1 deletion far/findfile.cpp
Expand Up @@ -771,7 +771,7 @@ intptr_t FindFiles::MainDlgProc(Dialog* Dlg, intptr_t Msg, intptr_t Param1, void
if (Param2)
{
// BUGBUG, it's unclear how to represent unicode in hex
const auto AnsiStr = narrow(Src);
const auto AnsiStr = unicode::to(CodePage, Src);
strDataStr = BlobToHexWString(AnsiStr.data(), AnsiStr.size(), 0);
}
else
Expand Down
2 changes: 1 addition & 1 deletion far/language.cpp
Expand Up @@ -410,7 +410,7 @@ void Language::init(const string& Path, int CountNeed)
// Проведем проверку на количество строк в LNG-файлах
if (CountNeed != -1 && CountNeed != static_cast<int>(size()))
{
throw MAKE_FAR_EXCEPTION(Utf8String(m_FileName) + ": language data is incorrect or damaged");
throw MAKE_FAR_EXCEPTION(unicode::to(CP_UTF8, m_FileName) + ": language data is incorrect or damaged");
}

// try to load Far<LNG>.lng.custom file(s)
Expand Down
4 changes: 0 additions & 4 deletions far/plclass.cpp
Expand Up @@ -493,10 +493,6 @@ Plugin::Plugin(plugin_factory* Factory, const string& ModuleName):
SetGuid(FarGuid);
}

Plugin::~Plugin()
{
}

void Plugin::SetGuid(const GUID& Guid)
{
m_Guid = Guid;
Expand Down
2 changes: 1 addition & 1 deletion far/plclass.hpp
Expand Up @@ -195,7 +195,7 @@ class Plugin: noncopyable
public:
NONCOPYABLE(Plugin);
Plugin(plugin_factory* Factory, const string& ModuleName);
virtual ~Plugin();
virtual ~Plugin() = default;

virtual bool GetGlobalInfo(GlobalInfo *Info);
virtual bool SetStartupInfo(PluginStartupInfo *Info);
Expand Down

0 comments on commit 40e9ed6

Please sign in to comment.