Skip to content
Permalink
Browse files

refactoring & fixes to 5049

  • Loading branch information...
alabuzhev committed Sep 30, 2017
1 parent 6983713 commit 471efa53372db697714c3b41ea6a118adef6ee85
Showing with 138 additions and 109 deletions.
  1. +6 −0 far/changelog
  2. +39 −4 far/common/bytes_view.hpp
  3. +18 −38 far/configdb.cpp
  4. +8 −8 far/filefilter.cpp
  5. +13 −14 far/filelist.cpp
  6. +1 −1 far/findfile.cpp
  7. +1 −1 far/findfile.hpp
  8. +10 −10 far/hilight.cpp
  9. +4 −8 far/processname.cpp
  10. +16 −6 far/strmix.cpp
  11. +4 −2 far/strmix.hpp
  12. +1 −1 far/vbuild.m4
  13. +17 −16 far/viewer.cpp
@@ -1,3 +1,9 @@
drkns 30.09.2017 14:00:05 +0100 - build 5051

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

2. Уточнение 5049 - криво работали маски с [] и выравнивание расширений.

zg 30.09.2017 15:01:48 +0300 - build 5050

1. частично исправлена прямая отрисовка диалога на экран.
@@ -56,13 +56,31 @@ class bytes: public range<char*>

bytes() = default;

bytes(void* Data, size_t Size): range<char*>(reinterpret_cast<char*>(Data), reinterpret_cast<char*>(Data) + Size) {}
static bytes copy(const bytes_view& Object)
{
bytes Bytes;
Bytes = Object;
return Bytes;
}

template<typename T>
explicit bytes(T& Object):
bytes(&Object, sizeof(Object))
static bytes copy(const T& Object)
{
return copy(bytes_view(Object));
}

template<typename T>
static bytes reference(T& Object)
{
static_assert(std::is_trivially_copyable<T>::value);

bytes Bytes;
static_cast<range<char*>&>(Bytes) =
{
static_cast<char*>(static_cast<void*>(&Object)),
static_cast<char*>(static_cast<void*>(&Object)) + sizeof(Object)
};
return Bytes;
}

~bytes()
@@ -71,7 +89,7 @@ class bytes: public range<char*>
delete[] static_cast<const char*>(data());
}

auto& operator=(const bytes_view& rhs)
bytes& operator=(const bytes_view& rhs)
{
if (data())
{
@@ -87,8 +105,25 @@ class bytes: public range<char*>
return *this;
}

operator bytes_view() const
{
return { data(), size() };
}

private:
movable<bool> m_Allocated{ false };
};

template<typename T>
T deserialise(const bytes_view& Bytes)
{
static_assert(std::is_trivially_copyable<T>::value);
if (Bytes.size() != sizeof(T))
throw std::runtime_error("Incorrect blob size: " + std::to_string(Bytes.size()) + ", expected " + std::to_string(sizeof(T)));

T Value;
std::memcpy(&Value, Bytes.data(), sizeof(T));
return Value;
}

#endif // BYTES_VIEW_HPP_3707377A_7C4B_4B2E_89EC_6411A1988FB3
@@ -295,8 +295,7 @@ class iGeneralConfigDb: public GeneralConfig, public SQLiteDb

if (!strcmp(type,"hex"))
{
const auto Blob = HexStringToBlob(value);
SetValue(Key, Name, bytes_view(Blob.data(), Blob.size()));
SetValue(Key, Name, HexStringToBlob(value));
continue;
}
}
@@ -530,18 +529,18 @@ class HierarchicalConfigDb: public HierarchicalConfig, public SQLiteDb
return true;
}

virtual void SerializeBlob(const char* Name, const void* Blob, size_t Size, tinyxml::XMLElement& e) const
virtual void SerializeBlob(const char* Name, const bytes_view& Blob, tinyxml::XMLElement& e) const
{
e.SetAttribute("type", "hex");
e.SetAttribute("value", BlobToHexString(Blob, Size).data());
e.SetAttribute("value", BlobToHexString(Blob).data());
}

virtual void Export(representation_destination& Representation) const override
{
Export(Representation, root_key(), CreateChild(Representation.GetRoot(), "hierarchicalconfig"));
}

virtual std::vector<char> DeserializeBlob(const char* Name, const char* Type, const char* Value, const tinyxml::XMLElement& e) const
virtual bytes DeserializeBlob(const char* Name, const char* Type, const char* Value, const tinyxml::XMLElement& e) const
{
return HexStringToBlob(Value);
}
@@ -581,10 +580,7 @@ class HierarchicalConfigDb: public HierarchicalConfig, public SQLiteDb

case column_type::blob:
case column_type::unknown:
{
const auto Blob = Stmt->GetColBlob(1);
SerializeBlob(name, Blob.data(), Blob.size(), e);
}
SerializeBlob(name, Stmt->GetColBlob(1), e);
break;
}
}
@@ -637,19 +633,16 @@ class HierarchicalConfigDb: public HierarchicalConfig, public SQLiteDb
}
else if (value && !strcmp(type, "text"))
{
string Value = encoding::utf8::get_chars(value);
SetValue(Key, Name, Value);
SetValue(Key, Name, encoding::utf8::get_chars(value));
}
else if (value && !strcmp(type, "hex"))
{
const auto Blob = HexStringToBlob(value);
SetValue(Key, Name, bytes_view(Blob.data(), Blob.size()));
SetValue(Key, Name, HexStringToBlob(value));
}
else
{
// custom types, value is optional
const auto Blob = DeserializeBlob(name, type, value, *e);
SetValue(Key, Name, bytes_view(Blob.data(), Blob.size()));
SetValue(Key, Name, DeserializeBlob(name, type, value, *e));
}
}

@@ -709,7 +702,7 @@ class HighlightHierarchicalConfigDb: public HierarchicalConfigDb
using HierarchicalConfigDb::HierarchicalConfigDb;

private:
virtual void SerializeBlob(const char* Name, const void* Blob, size_t Size, tinyxml::XMLElement& e) const override
virtual void SerializeBlob(const char* Name, const bytes_view& Blob, tinyxml::XMLElement& e) const override
{
static const char* ColorKeys[] =
{
@@ -721,19 +714,19 @@ class HighlightHierarchicalConfigDb: public HierarchicalConfigDb

if (std::any_of(CONST_RANGE(ColorKeys, i) { return !strcmp(Name, i); }))
{
auto& Color = *static_cast<const FarColor*>(Blob);
const auto Color = deserialise<FarColor>(Blob);
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", encoding::utf8::get_bytes(FlagsToString(Color.Flags, ColorFlagNames)).data());
}
else
{
return HierarchicalConfigDb::SerializeBlob(Name, Blob, Size, e);
return HierarchicalConfigDb::SerializeBlob(Name, Blob, e);
}
}

virtual std::vector<char> DeserializeBlob(const char* Name, const char* Type, const char* Value, const tinyxml::XMLElement& e) const override
virtual bytes DeserializeBlob(const char* Name, const char* Type, const char* Value, const tinyxml::XMLElement& e) const override
{
if(!strcmp(Type, "color"))
{
@@ -746,14 +739,10 @@ class HighlightHierarchicalConfigDb: public HierarchicalConfigDb
if (const auto flags = e.Attribute("flags"))
Color.Flags = StringToFlags(encoding::utf8::get_chars(flags), ColorFlagNames);

std::vector<char> Blob(sizeof(FarColor));
std::memcpy(Blob.data(), &Color, sizeof(Color));
return Blob;
}
else
{
return HierarchicalConfigDb::DeserializeBlob(Name, Type, Value, e);
return bytes::copy(Color);
}

return HierarchicalConfigDb::DeserializeBlob(Name, Type, Value, e);
}
};

@@ -801,10 +790,7 @@ class ColorsConfigDb: public ColorsConfig, public SQLiteDb
if (!Stmt->Bind(Name).Step())
return false;

const auto Blob = Stmt->GetColBlob(0);
if (Blob.size() != sizeof(Value))
throw MAKE_FAR_EXCEPTION(L"Incorrect blob size");
Value = *reinterpret_cast<const FarColor*>(Blob.data());
Value = deserialise<FarColor>(Stmt->GetColBlob(0));
return true;
}

@@ -819,10 +805,7 @@ class ColorsConfigDb: public ColorsConfig, public SQLiteDb
auto& e = CreateChild(root, "object");

e.SetAttribute("name", stmtEnumAllValues.GetColTextUTF8(0));
const auto Blob = stmtEnumAllValues.GetColBlob(1);
if (Blob.size() != sizeof(FarColor))
throw MAKE_FAR_EXCEPTION(L"Incorrect blob size");
auto& Color = *reinterpret_cast<const FarColor*>(Blob.data());
const auto Color = deserialise<FarColor>(stmtEnumAllValues.GetColBlob(1));
e.SetAttribute("background", to_hex_string(Color.BackgroundColor).data());
e.SetAttribute("foreground", to_hex_string(Color.ForegroundColor).data());
e.SetAttribute("flags", encoding::utf8::get_bytes(FlagsToString(Color.Flags, ColorFlagNames)).data());
@@ -1409,10 +1392,7 @@ class PluginsCacheConfigDb: public PluginsCacheConfig, public SQLiteDb
if (!Stmt->Bind(id).Step())
return false;

const auto Blob = Stmt->GetColBlob(0);
if (Blob.size() != sizeof(*Version))
throw MAKE_FAR_EXCEPTION(L"Incorrect blob size");
*Version = *reinterpret_cast<const VersionInfo*>(Blob.data());
*Version = deserialise<VersionInfo>(Stmt->GetColBlob(0));
return true;
}

@@ -738,10 +738,10 @@ void FileFilter::InitFilter()
}

DWORD Flags[FFFT_COUNT] = {};
cfg->GetValue(root, L"FoldersFilterFFlags", bytes(Flags));
cfg->GetValue(root, L"FoldersFilterFFlags", bytes::reference(Flags));

for (DWORD i=FFFT_FIRST; i < FFFT_COUNT; i++)
FoldersFilter->SetFlags((enumFileFilterFlagsType)i, Flags[i]);
FoldersFilter->SetFlags(static_cast<enumFileFilterFlagsType>(i), Flags[i]);
}

for (;;)
@@ -764,8 +764,8 @@ void FileFilter::InitFilter()
NewItem.SetMask(UseMask != 0, strMask);

FILETIME DateAfter = {}, DateBefore = {};
cfg->GetValue(key,L"DateAfter", bytes(DateAfter));
cfg->GetValue(key,L"DateBefore", bytes(DateBefore));
cfg->GetValue(key,L"DateAfter", bytes::reference(DateAfter));
cfg->GetValue(key,L"DateBefore", bytes::reference(DateBefore));

unsigned long long UseDate = 0;
cfg->GetValue(key, L"UseDate", UseDate);
@@ -800,10 +800,10 @@ void FileFilter::InitFilter()
NewItem.SetAttr(UseAttr != 0, (DWORD)AttrSet, (DWORD)AttrClear);

DWORD Flags[FFFT_COUNT] = {};
cfg->GetValue(key,L"FFlags", bytes(Flags));
cfg->GetValue(key,L"FFlags", bytes::reference(Flags));

for (DWORD i=FFFT_FIRST; i < FFFT_COUNT; i++)
NewItem.SetFlags((enumFileFilterFlagsType)i, Flags[i]);
NewItem.SetFlags(static_cast<enumFileFilterFlagsType>(i), Flags[i]);

FilterData().emplace_back(std::move(NewItem));
}
@@ -821,10 +821,10 @@ void FileFilter::InitFilter()
//Авто фильтры они только для файлов, папки не должны к ним подходить
NewItem.SetAttr(true, 0, FILE_ATTRIBUTE_DIRECTORY);
DWORD Flags[FFFT_COUNT] = {};
cfg->GetValue(key,L"FFlags", bytes(Flags));
cfg->GetValue(key,L"FFlags", bytes::reference(Flags));

for (DWORD i=FFFT_FIRST; i < FFFT_COUNT; i++)
NewItem.SetFlags((enumFileFilterFlagsType)i, Flags[i]);
NewItem.SetFlags(static_cast<enumFileFilterFlagsType>(i), Flags[i]);

TempFilterData().emplace_back(std::move(NewItem));
}
@@ -7931,26 +7931,25 @@ bool FileList::ConvertName(const string_view& SrcName,string &strDest,int MaxLen
return SrcLength > MaxLength;
}

const wchar_t *DotIt;
string_view Extension;

if (!ShowStatus &&
((!(FileAttr&FILE_ATTRIBUTE_DIRECTORY) && (m_ViewSettings.Flags&PVS_ALIGNEXTENSIONS))
|| ((FileAttr&FILE_ATTRIBUTE_DIRECTORY) && (m_ViewSettings.Flags&PVS_FOLDERALIGNEXTENSIONS)))
&& SrcLength<=MaxLength &&
(DotIt = std::find(ALL_CONST_RANGE(SrcName) ,L'.')) != SrcName.cend() && DotIt != SrcName.cbegin() &&
(SrcName.size() > 2 || SrcName[0] != L'.') && std::find(DotIt + 1, SrcName.cend(), L' ') == SrcName.cend())
((!(FileAttr & FILE_ATTRIBUTE_DIRECTORY) && (m_ViewSettings.Flags & PVS_ALIGNEXTENSIONS)) ||
((FileAttr & FILE_ATTRIBUTE_DIRECTORY) && (m_ViewSettings.Flags & PVS_FOLDERALIGNEXTENSIONS))) &&
SrcLength <= MaxLength &&
(Extension = PointToExt(SrcName)).size() > 1 && Extension.size() != SrcName.size() &&
(SrcName.size() > 2 || SrcName[0] != L'.') && std::find(ALL_CONST_RANGE(Extension), L' ') == SrcName.cend())
{
int DotLength = SrcName.cend() - (DotIt + 1);
int NameLength=DotLength? static_cast<int>(DotIt - SrcName.cbegin()) : SrcLength;
int DotPos = std::max(MaxLength - std::max(DotLength,3), NameLength + 1);
Extension.remove_prefix(1);
auto Name = SrcName.substr(0, SrcName.size() - Extension.size());
const auto DotPos = std::max(MaxLength - std::max(Extension.size(), size_t(3)), Name.size());

strDest = make_string(SrcName.substr(0, NameLength));

if (DotPos>0 && NameLength>0 && SrcName[NameLength-1]==L' ')
strDest += L'.';
if (Name.size() > 1 && Name[Name.size() - 2] != L' ')
Name.remove_suffix(1);

strDest.append(ALL_CONST_RANGE(Name));
strDest.resize(DotPos, L' ');
strDest.append(DotIt + 1, DotLength);
strDest.append(ALL_CONST_RANGE(Extension));
strDest.resize(MaxLength, L' ');
}
else
@@ -443,7 +443,7 @@ void background_searcher::ReleaseInFileSearch()
clear_and_shrink(readBufferA);
clear_and_shrink(readBuffer);
clear_and_shrink(skipCharsTable);
hexFindString.clear();
hexFindString = {};
m_CodePages.clear();
InFileSearchInited=false;
}
@@ -222,7 +222,7 @@ class background_searcher: noncopyable

std::vector<char> readBufferA;
std::vector<wchar_t> readBuffer;
std::vector<char> hexFindString;
bytes hexFindString;
std::vector<size_t> skipCharsTable;
struct CodePageInfo;
std::list<CodePageInfo> m_CodePages;
@@ -222,9 +222,9 @@ static void LoadFilter(const HierarchicalConfig* cfg, const HierarchicalConfig::
}

FILETIME DateAfter = {};
cfg->GetValue(key,HLS.DateAfter, bytes(DateAfter));
cfg->GetValue(key,HLS.DateAfter, bytes::reference(DateAfter));
FILETIME DateBefore = {};
cfg->GetValue(key,HLS.DateBefore, bytes(DateBefore));
cfg->GetValue(key,HLS.DateBefore, bytes::reference(DateBefore));
unsigned long long UseDate = 0;
cfg->GetValue(key, HLS.UseDate, UseDate);
unsigned long long DateType = 0;
@@ -273,14 +273,14 @@ static void LoadFilter(const HierarchicalConfig* cfg, const HierarchicalConfig::
HData.SetSortGroup(SortGroup);

highlight::element Colors = {};
cfg->GetValue(key,HLS.NormalColor, bytes(Colors.Color[highlight::color::normal].FileColor));
cfg->GetValue(key,HLS.SelectedColor, bytes(Colors.Color[highlight::color::selected].FileColor));
cfg->GetValue(key,HLS.CursorColor, bytes(Colors.Color[highlight::color::normal_current].FileColor));
cfg->GetValue(key,HLS.SelectedCursorColor, bytes(Colors.Color[highlight::color::selected_current].FileColor));
cfg->GetValue(key,HLS.MarkCharNormalColor, bytes(Colors.Color[highlight::color::normal].MarkColor));
cfg->GetValue(key,HLS.MarkCharSelectedColor, bytes(Colors.Color[highlight::color::selected].MarkColor));
cfg->GetValue(key,HLS.MarkCharCursorColor, bytes(Colors.Color[highlight::color::normal_current].MarkColor));
cfg->GetValue(key,HLS.MarkCharSelectedCursorColor, bytes(Colors.Color[highlight::color::selected_current].MarkColor));
cfg->GetValue(key,HLS.NormalColor, bytes::reference(Colors.Color[highlight::color::normal].FileColor));
cfg->GetValue(key,HLS.SelectedColor, bytes::reference(Colors.Color[highlight::color::selected].FileColor));
cfg->GetValue(key,HLS.CursorColor, bytes::reference(Colors.Color[highlight::color::normal_current].FileColor));
cfg->GetValue(key,HLS.SelectedCursorColor, bytes::reference(Colors.Color[highlight::color::selected_current].FileColor));
cfg->GetValue(key,HLS.MarkCharNormalColor, bytes::reference(Colors.Color[highlight::color::normal].MarkColor));
cfg->GetValue(key,HLS.MarkCharSelectedColor, bytes::reference(Colors.Color[highlight::color::selected].MarkColor));
cfg->GetValue(key,HLS.MarkCharCursorColor, bytes::reference(Colors.Color[highlight::color::normal_current].MarkColor));
cfg->GetValue(key,HLS.MarkCharSelectedCursorColor, bytes::reference(Colors.Color[highlight::color::selected_current].MarkColor));

unsigned long long MarkChar;
if (cfg->GetValue(key, HLS.MarkChar, MarkChar))
Oops, something went wrong.

0 comments on commit 471efa5

Please sign in to comment.
You can’t perform that action at this time.