Skip to content

Commit

Permalink
1. Рефакторинг.
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Jan 4, 2015
1 parent ca7856d commit e73fe1a
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 101 deletions.
16 changes: 8 additions & 8 deletions far/PluginA.cpp
Expand Up @@ -748,7 +748,7 @@ static char *InsertQuoteA(char *Str)

if (*Str != '"')
{
memmove(Str + 1, Str, ++l);
std::copy_n(Str, ++l, Str + 1);
*Str = '"';
}

Expand Down Expand Up @@ -1962,7 +1962,7 @@ static char* WINAPI RemoveLeadingSpacesA(char *Str) noexcept
;

if (ChPtr != Str)
memmove(Str, ChPtr, strlen(ChPtr) + 1);
std::copy_n(ChPtr, strlen(ChPtr) + 1, Str);

return Str;
}
Expand Down Expand Up @@ -1999,9 +1999,9 @@ static char* WINAPI TruncStrA(char *Str, int MaxLength) noexcept
{
if (MaxLength > 3)
{
char *MovePos = Str + Length - MaxLength + 3;
memmove(Str + 3, MovePos, strlen(MovePos) + 1);
memcpy(Str, "...", 3);
const auto MovePos = Str + Length - MaxLength + 3;
std::copy_n(MovePos, strlen(MovePos) + 1, Str + 3);
std::copy_n("...", 3, Str);
}

Str[MaxLength] = 0;
Expand Down Expand Up @@ -2041,9 +2041,9 @@ static char* WINAPI TruncPathStrA(char *Str, int MaxLength) noexcept
if (!lpStart || (lpStart - Str > MaxLength - 5))
return TruncStrA(Str, MaxLength);

char *lpInPos = lpStart + 3 + (nLength - MaxLength);
memmove(lpStart + 3, lpInPos, strlen(lpInPos) + 1);
memcpy(lpStart, "...", 3);
const auto lpInPos = lpStart + 3 + (nLength - MaxLength);
std::copy_n(lpInPos, strlen(lpInPos) + 1, lpStart + 3);
std::copy_n("...", 3, lpStart);
}
return Str;
}
Expand Down
6 changes: 5 additions & 1 deletion far/changelog
@@ -1,4 +1,8 @@
drkns 03.01.2015 12:36:25 +0200 - build 4232
drkns 04.01.2015 19:27:27 +0200 - build 4233

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

drkns 03.01.2015 12:36:25 +0200 - build 4232

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

Expand Down
3 changes: 2 additions & 1 deletion far/common/make_vector.hpp
Expand Up @@ -52,7 +52,8 @@ VTE_GENERATE(MAKE_VECTOR_VTE)
#undef MAKE_VECTOR_VTE

#else
template<class T, class... Args> std::vector<typename std::remove_const<typename std::decay<T>::type>::type> make_vector(T&& value, Args&&... args)
template<class T, class... Args>
std::vector<typename std::remove_const<typename std::decay<T>::type>::type> make_vector(T&& value, Args&&... args)
{
return std::vector<typename std::remove_const<typename std::decay<T>::type>::type>{std::forward<T>(value), std::forward<Args>(args)...};
}
Expand Down
9 changes: 9 additions & 0 deletions far/common/preprocessor.hpp
Expand Up @@ -95,3 +95,12 @@ friend inline void swap(Type& a, Type& b) noexcept { a.swap(b); }

#define SCOPED_ACTION(RAII_type) \
RAII_type ADD_SUFFIX(scoped_object_, __LINE__)

#ifdef __GNUC__
#define DO_PRAGMA(x) _Pragma(#x)
#define PACK_PUSH(n) DO_PRAGMA(pack(n))
#define PACK_POP() DO_PRAGMA(pack())
#else
#define PACK_PUSH(n) __pragma(pack(push, n))
#define PACK_POP() __pragma(pack(pop))
#endif
12 changes: 6 additions & 6 deletions far/dialog.cpp
Expand Up @@ -251,7 +251,7 @@ static size_t ConvertItemEx2(const DialogItemEx *ItemEx, FarGetDialogItem *Item)
MenuItemEx* item=ListBox->GetItemPtr(ii);
listItems[ii].Flags=item->Flags;
listItems[ii].Text=text;
wmemcpy(text, item->strName.data(), item->strName.size()+1);
std::copy_n(item->strName.data(), item->strName.size() + 1, text);
text+=item->strName.size()+1;
listItems[ii].Reserved[0]=listItems[ii].Reserved[1]=0;
}
Expand All @@ -262,13 +262,13 @@ static size_t ConvertItemEx2(const DialogItemEx *ItemEx, FarGetDialogItem *Item)
}
wchar_t* p=(wchar_t*)((char*)(Item->Item)+offsetStrings);
Item->Item->Data = p;
wmemcpy(p, str.data(), sz+1);
std::copy_n(str.data(), sz + 1, p);
p+=sz+1;
Item->Item->History = p;
wmemcpy(p, ItemEx->strHistory.data(), ItemEx->strHistory.size()+1);
std::copy_n(ItemEx->strHistory.data(), ItemEx->strHistory.size() + 1, p);
p+=ItemEx->strHistory.size()+1;
Item->Item->Mask = p;
wmemcpy(p, ItemEx->strMask.data(), ItemEx->strMask.size()+1);
std::copy_n(ItemEx->strMask.data(), ItemEx->strMask.size() + 1, p);
}
}
return size;
Expand Down Expand Up @@ -4934,7 +4934,7 @@ intptr_t Dialog::SendMessage(intptr_t Msg,intptr_t Param1,void* Param2)

if (did->PtrData)
{
wmemmove(did->PtrData,Ptr,Len);
std::copy_n(Ptr, Len, did->PtrData);
did->PtrData[Len]=L'\0';
}
};
Expand Down Expand Up @@ -5661,7 +5661,7 @@ intptr_t Dialog::SendMessage(intptr_t Msg,intptr_t Param1,void* Param2)

if (did->PtrData)
{
wmemmove(did->PtrData,Ptr,Len);
std::copy_n(Ptr, Len, did->PtrData);
did->PtrData[Len]=L'\0';
}
};
Expand Down
26 changes: 13 additions & 13 deletions far/edit.cpp
Expand Up @@ -1196,7 +1196,7 @@ int Edit::ProcessKey(const Manager::Key& Key)
}
else
{
wmemmove(m_Str+m_CurPos,m_Str+m_CurPos+1,m_StrSize-m_CurPos);
std::copy_n(m_Str + m_CurPos + 1, m_StrSize - m_CurPos, m_Str + m_CurPos);
m_StrSize--;
m_Str=(wchar_t *)xf_realloc(m_Str,(m_StrSize+1)*sizeof(wchar_t));
}
Expand Down Expand Up @@ -1547,7 +1547,7 @@ int Edit::InsertKey(int Key)

if (!m_Flags.Check(FEDITLINE_OVERTYPE))
{
wmemmove(m_Str+m_CurPos+1,m_Str+m_CurPos,m_StrSize-m_CurPos);
std::copy_n(m_Str + m_CurPos, m_StrSize - m_CurPos, m_Str + m_CurPos + 1);

if (m_SelStart!=-1)
{
Expand Down Expand Up @@ -1586,8 +1586,9 @@ int Edit::InsertKey(int Key)
void Edit::GetString(wchar_t *Str,int MaxSize) const
{
//xwcsncpy(Str, this->Str,MaxSize);
wmemmove(Str,m_Str,std::min(m_StrSize,MaxSize-1));
Str[std::min(m_StrSize,MaxSize-1)]=0;
const auto Size = std::min(m_StrSize, MaxSize - 1);
std::copy_n(m_Str, Size, Str);
Str[Size] = 0;
Str[MaxSize-1]=0;
}

Expand Down Expand Up @@ -1739,7 +1740,7 @@ void Edit::SetBinaryString(const wchar_t *Str,int Length)

m_Str=NewStr;
m_StrSize=Length;
wmemcpy(m_Str,Str,Length);
std::copy_n(Str, Length, m_Str);
m_Str[Length]=0;

if (GetTabExpandMode() == EXPAND_ALLTABS)
Expand Down Expand Up @@ -1917,10 +1918,10 @@ void Edit::InsertBinaryString(const wchar_t *Str,int Length)
}

m_Str=NewStr;
wmemcpy(m_Str + m_CurPos, Str, Length);
std::copy_n(Str, Length, m_Str + m_CurPos);
SetPrevCurPos(m_CurPos);
m_CurPos+=Length;
wmemcpy(m_Str + m_CurPos, TmpStr.data(), TmpStr.size());
std::copy_n(TmpStr.data(), TmpStr.size(), m_Str + m_CurPos);
m_Str[m_StrSize]=0;

if (GetTabExpandMode() == EXPAND_ALLTABS)
Expand Down Expand Up @@ -2031,15 +2032,14 @@ void Edit::InsertTab()

bool Edit::ReplaceTabs()
{
wchar_t *TabPtr;
int Pos=0;

if (m_Flags.Check(FEDITLINE_READONLY))
return false;

bool changed=false;
wchar_t *TabPtr;
int Pos = 0;
bool changed = false;

while ((TabPtr = wmemchr(m_Str+Pos, L'\t', m_StrSize-Pos)))
while ((TabPtr = std::find(m_Str + Pos, m_Str + m_StrSize, L'\t')) != m_Str + m_StrSize)
{
changed=true;
Pos=(int)(TabPtr-m_Str);
Expand Down Expand Up @@ -2303,7 +2303,7 @@ void Edit::DeleteBlock()

if (To>m_StrSize)To=m_StrSize;

wmemmove(m_Str+From,m_Str+To,m_StrSize-To+1);
std::copy_n(m_Str + To, m_StrSize - To + 1, m_Str + From);
m_StrSize-=To-From;

if (m_CurPos>From)
Expand Down
10 changes: 5 additions & 5 deletions far/editor.cpp
Expand Up @@ -285,19 +285,19 @@ int Editor::GetRawData(wchar_t **DestBuf,int& SizeDestBuf,int TextFormat)
while (CurPtr)
{
CurPtr->GetBinaryString(&SaveStr,&EndSeq,Length);
wmemcpy(PDest,SaveStr,Length);
std::copy_n(SaveStr, Length, PDest);
PDest+=Length;

size_t LenEndSeq;
if (!TextFormat)
{
LenEndSeq=StrLength(EndSeq);
wmemcpy(PDest,EndSeq,LenEndSeq);
std::copy_n(EndSeq, LenEndSeq, PDest);
}
else
{
LenEndSeq=StrLength(GlobalEOL);
wmemcpy(PDest,GlobalEOL,LenEndSeq);
std::copy_n(GlobalEOL, LenEndSeq, PDest);
}

PDest+=LenEndSeq;
Expand Down Expand Up @@ -4946,7 +4946,7 @@ struct Editor::EditorUndoData : ::noncopyable
this->m_Str.reset(Length + 1);

if (this->m_Str)
wmemmove(this->m_Str.get(), Str, Length);
std::copy_n(Str, Length, this->m_Str.get());
}
else
this->m_Str.reset();
Expand Down Expand Up @@ -7596,7 +7596,7 @@ bool Editor::SetCodePage(uintptr_t codepage, bool *BOM)
auto first = Lines.begin();
if (first->m_StrSize > 0 && first->m_Str[0] == Utf::BOM_CHAR)
{
wmemmove(first->m_Str, first->m_Str+1, first->m_StrSize); // with trailing L'\0'
std::copy_n(first->m_Str + 1, first->m_StrSize, first->m_Str); // with trailing L'\0'
--first->m_StrSize;
*BOM = true;
}
Expand Down
4 changes: 2 additions & 2 deletions far/elevation.cpp
Expand Up @@ -731,8 +731,8 @@ int elevation::fMoveToRecycleBin(SHFILEOPSTRUCT& FileOpStruct)
if(Initialize())
{
if(SendCommand(C_FUNCTION_MOVETORECYCLEBIN) && Write(FileOpStruct)
&& Write(FileOpStruct.pFrom,FileOpStruct.pFrom?(StrLength(FileOpStruct.pFrom)+1+1)*sizeof(WCHAR):0) // achtung! +1
&& Write(FileOpStruct.pTo,FileOpStruct.pTo?(StrLength(FileOpStruct.pTo)+1+1)*sizeof(WCHAR):0)) // achtung! +1
&& Write(FileOpStruct.pFrom, FileOpStruct.pFrom? (wcslen(FileOpStruct.pFrom) + 1 + 1) * sizeof(wchar_t) : 0) // achtung! +1
&& Write(FileOpStruct.pTo, FileOpStruct.pTo? (wcslen(FileOpStruct.pTo) + 1 + 1) * sizeof(wchar_t) : 0)) // achtung! +1
{
int OpResult = 0;
if(Read(OpResult) && Read(FileOpStruct.fAnyOperationsAborted))
Expand Down
8 changes: 4 additions & 4 deletions far/farwinapi.cpp
Expand Up @@ -1352,8 +1352,8 @@ HANDLE FindFirstStream(const string& FileName,STREAM_INFO_LEVELS InfoLevel,LPVOI
Handle->NextOffset = StreamInfo->NextEntryOffset;
if (StreamInfo->StreamNameLength)
{
memcpy(pFsd->cStreamName,StreamInfo->StreamName,StreamInfo->StreamNameLength);
pFsd->cStreamName[StreamInfo->StreamNameLength/sizeof(WCHAR)]=L'\0';
std::copy_n(StreamInfo->StreamName, StreamInfo->StreamNameLength/sizeof(wchar_t), pFsd->cStreamName);
pFsd->cStreamName[StreamInfo->StreamNameLength / sizeof(wchar_t)] = L'\0';
pFsd->StreamSize=StreamInfo->StreamSize;
Ret=Handle;
}
Expand Down Expand Up @@ -1389,8 +1389,8 @@ BOOL FindNextStream(HANDLE hFindStream,LPVOID lpFindStreamData)
Handle->NextOffset = pStreamInfo->NextEntryOffset?Handle->NextOffset+pStreamInfo->NextEntryOffset:0;
if (pStreamInfo->StreamNameLength && pStreamInfo->StreamNameLength < sizeof(pFsd->cStreamName))
{
memcpy(pFsd->cStreamName,pStreamInfo->StreamName,pStreamInfo->StreamNameLength);
pFsd->cStreamName[pStreamInfo->StreamNameLength/sizeof(WCHAR)]=L'\0';
std::copy_n(pStreamInfo->StreamName, pStreamInfo->StreamNameLength / sizeof(wchar_t), pFsd->cStreamName);
pFsd->cStreamName[pStreamInfo->StreamNameLength / sizeof(wchar_t)] = L'\0';
pFsd->StreamSize=pStreamInfo->StreamSize;
Ret=TRUE;
}
Expand Down
4 changes: 2 additions & 2 deletions far/fileedit.cpp
Expand Up @@ -1647,8 +1647,8 @@ int FileEditor::LoadFile(const string& Name,int &UserBreak)
}

size_t Offset = StrLength > 3 ? StrLength - 3 : 0;
const wchar_t *eol;
if ((eol=wmemchr(Str+Offset,L'\r',StrLength-Offset)) || (eol=wmemchr(Str+Offset,L'\n',StrLength-Offset)))
const auto eol = std::find_if(Str + Offset, Str + StrLength, IsEol);
if (eol != Str + StrLength)
{
m_editor->GlobalEOL = eol;
LastLineCR=1;
Expand Down
2 changes: 1 addition & 1 deletion far/filelist.cpp
Expand Up @@ -5572,7 +5572,7 @@ void FileList::PluginToFileListItem(PluginPanelItem *pi,FileListItem *fi)

if (pi->Description)
{
auto Str = new wchar_t[StrLength(pi->Description)+1];
auto Str = new wchar_t[wcslen(pi->Description)+1];
wcscpy(Str, pi->Description);
fi->DizText = Str;
fi->DeleteDiz=true;
Expand Down
54 changes: 28 additions & 26 deletions far/fileowner.cpp
Expand Up @@ -75,43 +75,45 @@ static bool SidToNameCached(PSID Sid, string& Name, const string& Computer)
{
bool Result = false;

class sid_cache
class sid: noncopyable
{
public:
typedef block_ptr<SID> sid;

private:
struct hash
sid(PSID rhs)
{
size_t operator ()(const sid& Sid) const
{
return CRC32(0, Sid.get(), GetLengthSid(Sid.get()));
}
};
DWORD Size = GetLengthSid(rhs);
m_Data.reset(Size);
CopySid(Size, m_Data.get(), rhs);
}

struct equal
sid(sid&& rhs) { *this = std::move(rhs); }

MOVE_OPERATOR_BY_SWAP(sid);

void swap(sid& rhs)
{
bool operator ()(const sid& Sid1, const sid& Sid2) const
{
return EqualSid(Sid1.get(), Sid2.get()) != FALSE;
}
};
using std::swap;
swap(m_Data, rhs.m_Data);
}

public:
typedef std::unordered_map<sid, string, hash, equal> map;
bool operator==(const sid& rhs) const
{
return EqualSid(m_Data.get(), rhs.m_Data.get()) != FALSE;
}

static sid make_sid(PSID Sid)
size_t get_hash() const
{
DWORD Size = GetLengthSid(Sid);
sid Copy(Size);
CopySid(Size, Copy.get(), Sid);
return Copy;
};
return CRC32(0, m_Data.get(), GetLengthSid(m_Data.get()));
}

private:
block_ptr<SID> m_Data;
};

static sid_cache::map SIDCache;
struct sid_hash { size_t operator()(const sid& Sid) const { return Sid.get_hash(); } };

static std::unordered_map<sid, string, sid_hash> SIDCache;

auto SidCopy = sid_cache::make_sid(Sid);
sid SidCopy(Sid);
auto ItemIterator = SIDCache.find(SidCopy);

if (ItemIterator != SIDCache.cend())
Expand Down
2 changes: 1 addition & 1 deletion far/flink.cpp
Expand Up @@ -610,7 +610,7 @@ int MkSymLink(const string& Target, const string& LinkName, ReparsePointTypes Li
strFullLink += PtrSelName;
else
{
strFullLink += L"Disk_" + Target[0];
strFullLink.append(L"Disk_").append(Target, 0, 1);
}
}

Expand Down
4 changes: 2 additions & 2 deletions far/format.cpp
Expand Up @@ -152,7 +152,7 @@ BaseFormat& BaseFormat::operator<<(wchar_t Value)
BaseFormat& BaseFormat::operator<<(LPCWSTR Data)
{
Data = NullToEmpty(Data);
return Put(Data, StrLength(Data));
return Put(Data, wcslen(Data));
}

BaseFormat& BaseFormat::operator<<(const string& String)
Expand Down Expand Up @@ -224,7 +224,7 @@ BaseFormat& BaseFormat::ToString(T Value)
{
UpperBuf(Buffer, ARRAYSIZE(Buffer));
}
return Put(Buffer, StrLength(Buffer));
return Put(Buffer, wcslen(Buffer));
}


Expand Down

0 comments on commit e73fe1a

Please sign in to comment.