Skip to content

Commit

Permalink
save/load results in find all dialog
Browse files Browse the repository at this point in the history
Copy/delete items in find all dialog
  • Loading branch information
zodiacon committed Jun 1, 2022
1 parent 082771d commit dafd3f7
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
76 changes: 72 additions & 4 deletions RegExp/FindAllDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "FindAllDlg.h"
#include "SortHelper.h"
#include "Helpers.h"
#include <ListViewhelper.h>
#include <ThemeHelper.h>
#include <ClipboardHelper.h>

CFindAllDlg::CFindAllDlg(IMainFrame* frame) : m_pFrame(frame) {
}
Expand Down Expand Up @@ -75,13 +78,13 @@ void CFindAllDlg::DoSort(const SortInfo* si) {

auto compare = [&](auto& i1, auto& i2) {
switch (si->SortColumn) {
case 0: return SortHelper::Sort(i1.Path, i2.Path, si->SortColumn);
case 1: return SortHelper::Sort(i1.Name, i2.Name, si->SortColumn);
case 2: return SortHelper::Sort(i1.Data, i2.Data, si->SortColumn);
case 0: return SortHelper::Sort(i1.Path, i2.Path, si->SortAscending);
case 1: return SortHelper::Sort(i1.Name, i2.Name, si->SortAscending);
case 2: return SortHelper::Sort(i1.Data, i2.Data, si->SortAscending);
}
return false;
};
std::sort(m_Items.begin(), m_Items.end(), compare);
std::ranges::sort(m_Items, compare);
}

bool CFindAllDlg::OnDoubleClickList(HWND, int row, int, const POINT&) {
Expand Down Expand Up @@ -234,3 +237,68 @@ LRESULT CFindAllDlg::OnSearchComplete(UINT msg, WPARAM cancelled, LPARAM, BOOL&)
m_Progress.ShowWindow(SW_HIDE);
return 0;
}

LRESULT CFindAllDlg::OnSaveResults(WORD, WORD wID, HWND, BOOL&) {
CSimpleFileDialog dlg(FALSE, L"txt", nullptr, OFN_EXPLORER | OFN_ENABLESIZING | OFN_OVERWRITEPROMPT,
L"Text Files (*.txt)\0*.txt\0All Files\0*.*\0", m_hWnd);
ThemeHelper::Suspend();
if (dlg.DoModal() == IDOK) {
CString text;
for (int i = 0; i < (int)m_Items.size(); i++)
text += GetColumnText(m_List, i, 0) + L"\t" + GetColumnText(m_List, i, 1) + L"\t" + GetColumnText(m_List, i, 2) + L"\r\n";
if (!Helpers::WriteToFile(dlg.m_szFileName, text))
m_pFrame->DisplayError(L"Error saving results");
}
ThemeHelper::Resume();
return 0;
}

LRESULT CFindAllDlg::OnLoadResults(WORD, WORD wID, HWND, BOOL&) {
CSimpleFileDialog dlg(TRUE, L"txt", nullptr, OFN_EXPLORER | OFN_ENABLESIZING,
L"Text Files (*.txt)\0*.txt\0All Files\0*.*\0", m_hWnd);
ThemeHelper::Suspend();
if (dlg.DoModal() == IDOK) {
CString text;
if(!Helpers::ReadFileText(dlg.m_szFileName, text))
m_pFrame->DisplayError(L"Error loading results");
else {
if (IsDlgButtonChecked(IDC_APPEND) == BST_UNCHECKED) {
m_Items.clear();
}
int cr = 0, start = 0;
while ((cr = text.Find(L"\r\n", start)) > 0) {
ListItem item;
item.Path = text.Tokenize(L"\t", start);
item.Name = text.Tokenize(L"\t", start);
item.Data = text.Tokenize(L"\t", start);
m_Items.push_back(std::move(item));
start = cr + 2;
}
m_List.SetItemCountEx((int)m_Items.size(), LVSICF_NOINVALIDATEALL | LVSICF_NOSCROLL);
m_List.RedrawItems(m_List.GetTopIndex(), m_List.GetTopIndex() + m_List.GetCountPerPage());
}
}
ThemeHelper::Resume();
return 0;
}

LRESULT CFindAllDlg::OnCopy(WORD, WORD wID, HWND, BOOL&) {
ClipboardHelper::CopyText(m_hWnd, m_List.GetSelectedCount() == 0 ?
ListViewHelper::GetAllRowsAsString(m_List) : ListViewHelper::GetSelectedRowsAsString(m_List));
return 0;
}

LRESULT CFindAllDlg::OnDelete(WORD, WORD wID, HWND, BOOL&) {
if (m_List.GetSelectedCount() == 0)
m_Items.clear();
else {
int n = -1, offset = 0;
while ((n = m_List.GetNextItem(n, LVIS_SELECTED)) >= 0) {
m_Items.erase(m_Items.begin() + n - offset);
offset++;
}
}
m_List.SetItemCountEx((int)m_Items.size(), LVSICF_NOINVALIDATEALL | LVSICF_NOSCROLL);
m_List.RedrawItems(m_List.GetTopIndex(), m_List.GetTopIndex() + m_List.GetCountPerPage());
return 0;
}
8 changes: 8 additions & 0 deletions RegExp/FindAllDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class CFindAllDlg :
COMMAND_ID_HANDLER(IDC_FIND, OnFind)
COMMAND_ID_HANDLER(IDCANCEL, OnCloseCmd)
COMMAND_ID_HANDLER(IDC_CANCEL, OnCancel)
COMMAND_ID_HANDLER(IDC_SAVE, OnSaveResults)
COMMAND_ID_HANDLER(IDC_LOAD, OnLoadResults)
COMMAND_ID_HANDLER(IDC_DELETE, OnDelete)
COMMAND_ID_HANDLER(IDC_COPY, OnCopy)
COMMAND_CODE_HANDLER(EN_CHANGE, OnTextChanged)
COMMAND_CODE_HANDLER(BN_CLICKED, OnClick)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
Expand Down Expand Up @@ -66,6 +70,10 @@ class CFindAllDlg :
LRESULT OnTextChanged(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
LRESULT OnClick(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
LRESULT OnSearchComplete(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnSaveResults(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
LRESULT OnLoadResults(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
LRESULT OnCopy(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
LRESULT OnDelete(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

IMainFrame* m_pFrame;
RegistrySearcher m_Searcher;
Expand Down
28 changes: 28 additions & 0 deletions RegExp/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,31 @@ CString Helpers::GetWin32PathFromNTPath(PCWSTR ntpath) {

return L"";
}

bool Helpers::WriteToFile(PCWSTR path, CString const& text) {
return WriteToFile(path, text, text.GetLength() * sizeof(TCHAR));
}

bool Helpers::WriteToFile(PCWSTR path, void const* data, DWORD size) {
HANDLE hFile = ::CreateFile(path, GENERIC_WRITE, 0, nullptr, OPEN_ALWAYS, 0, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
return false;

DWORD bytes;
auto ok = ::WriteFile(hFile, data, size, &bytes, nullptr);
::CloseHandle(hFile);
return ok;
}

bool Helpers::ReadFileText(PCWSTR path, CString& text) {
HANDLE hFile = ::CreateFile(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
return false;

DWORD bytes;
auto size = ::GetFileSize(hFile, nullptr);
auto buffer = text.GetBufferSetLength(size / sizeof(WCHAR));
auto ok = ::ReadFile(hFile, buffer, size, &bytes, nullptr);
::CloseHandle(hFile);
return ok;
}
3 changes: 3 additions & 0 deletions RegExp/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ struct Helpers abstract final {
static CString GetProcessNameById(DWORD pid);
static bool CloseHandle(HANDLE hObject, DWORD pid);
static CString GetWin32PathFromNTPath(PCWSTR ntpath);
static bool WriteToFile(PCWSTR path, CString const& text);
static bool WriteToFile(PCWSTR path, void const* data, DWORD size);
static bool ReadFileText(PCWSTR path, CString& text);
};

Binary file modified RegExp/RegExp.rc
Binary file not shown.
5 changes: 4 additions & 1 deletion RegExp/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
#define IDC_HANDLECOUNT 1050
#define IDC_FILTER 1051
#define IDC_CLEAR 1052
#define IDC_DELETE 1053
#define IDC_BUTTON2 1054
#define IDC_COPY 1054
#define ID_FILE_RUNASADMIN 32771
#define ID_OPTIONS_ALWAYSONTOP 32772
#define ID_VIEW_GOBACK 32779
Expand Down Expand Up @@ -198,7 +201,7 @@
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 181
#define _APS_NEXT_COMMAND_VALUE 32884
#define _APS_NEXT_CONTROL_VALUE 1053
#define _APS_NEXT_CONTROL_VALUE 1055
#define _APS_NEXT_SYMED_VALUE 115
#endif
#endif

0 comments on commit dafd3f7

Please sign in to comment.