Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3ef010d
PatchUnicode-1051 現在のドキュメント内容をGrep
Apr 23, 2021
26d06ce
PatchUnicode-1051 現在のドキュメント内容をGrep v0.2 コンフリクト修正
Apr 23, 2021
5d97aa6
PatchUnicode-1051 現在のドキュメント内容をGrep v0.3 x64 build時のエラーを修正
Apr 23, 2021
20ecc3b
PatchUnicode-1051 現在のドキュメント内容をGrep v0.4 x64でタグジャンプできるように修正
Apr 22, 2021
bf00ef6
PatchUnicode-1051 v0.5 Patch未適用部分を対応
dep5 Apr 23, 2021
7cb86d8
PatchUnicode-1051 v0.6 DoGrep引数対応 #125
dep5 Apr 22, 2021
5f850ad
PatchUnicode-1051 v0.7 コード変更に対応 #1034
dep5 Apr 22, 2021
0af275c
PatchUnicode-1051 v0.8 除外ファイル・フォルダに対応
dep5 Apr 22, 2021
0a50998
PatchUnicode-1051 v0.9 値の重複を変更,TABを空白に
dep5 Apr 22, 2021
0e1d31f
PatchUnicode-1051 v0.10 初期化エラー対応
dep5 Apr 22, 2021
7e67ff7
PatchUnicode-1051 v0.11 ファイルからテキストへ変更(Grep)
dep5 Apr 22, 2021
29885eb
PatchUnicode-1051 v0.12 ヘッダーファイル追加 #1615
dep5 Apr 22, 2021
80a2d99
PatchUnicode-1051 v0.13 EOL_CRLF変更 #1610
dep5 Apr 22, 2021
031566c
PatchUnicode-1051 v0.14 conflict解消のため一部元に戻す
dep5 Apr 25, 2021
b9563aa
Merge remote-tracking branch 'upstream/master' into origin/grepthisdoc
dep5 Apr 25, 2021
03052bf
PatchUnicode-1051 v0.15 merge後再適用
dep5 Apr 25, 2021
55760d9
PatchUnicode-1051 v0.16 とりあえず変数再宣言
dep5 Apr 25, 2021
cb0f3ce
PatchUnicode-1051 v0.17 レビュー指摘のセキュリティ対策
dep5 Apr 25, 2021
afa9574
PatchUnicode-1051 v0.18 Grep置換のチェックボックスをグレーアウト
dep5 Apr 25, 2021
9379966
Merge remote-tracking branch 'upstream/master' into origin/grepthisdoc
dep5 May 1, 2021
d422f96
PatchUnicode-1051 v0.19 引数の間違いを修正
dep5 May 1, 2021
4f3205b
PatchUnicode-1051 v0.20 ファイルへのタグジャンプができなくなっていたのを修正
dep5 May 1, 2021
773539e
conflict解消
dep5 Jun 7, 2021
f520b12
PatchUnicode-1051 v0.21 マッチ回数が0だったのを修正,ifの位置を変更
dep5 Jun 7, 2021
89b539e
PatchUnicode-1051 v0.22 Secutiry HotSpotの修正
dep5 Jun 7, 2021
45898ca
Revert "PatchUnicode-1051 v0.22 Secutiry HotSpotの修正"
dep5 Jun 9, 2021
6a565c2
PatchUnicode-1051 v0.22 Secutiry HotSpotの修正
beru Jun 9, 2021
e1523cc
PatchUnicode-1051 v0.23 通常時に対象ファイルに:HWND:を設定できないようにする
dep5 Jun 9, 2021
10383bf
security hotspotに対応
dep5 Jun 9, 2021
639e0de
_count2に変更
dep5 Jun 10, 2021
ce88164
bug修正
dep5 Jun 10, 2021
1046cfc
PatchUnicode-1051 v0.24 タグジャンプのジャンプ先をwstringで書き直した
dep5 Jun 14, 2021
7d92af7
PatchUnicode-1051 v0.25 必要のないボタン類をグレーアウトする
dep5 Jun 16, 2021
1f088c1
ダイアログのキャンセル時に入力文字を保存しない
dep5 Jun 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 248 additions & 37 deletions sakura_core/CGrepAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include "CSearchAgent.h"
#include "dlg/CDlgCancel.h"
#include "_main/CAppMode.h"
#include "_main/CMutex.h"
#include "env/CShareData.h"
#include "env/CSakuraEnvironment.h"
#include "COpeBlk.h"
#include "window/CEditWnd.h"
#include "charset/CCodeMediator.h"
Expand All @@ -46,6 +49,7 @@
#include <iterator>
#include <deque>
#include <memory>
#include "apiwrap/StdApi.h"
#include "apiwrap/StdControl.h"
#include "CSelectLang.h"
#include "sakura_rc.h"
Expand Down Expand Up @@ -105,6 +109,93 @@ std::wstring FormatPathList( const ContainerType& containter )
return strPatterns;
}

class CFileLoadOrWnd{
CFileLoad m_cfl;
HWND m_hWnd;
int m_nLineCurrent;
int m_nLineNum;
public:
CFileLoadOrWnd(const SEncodingConfig& encode, HWND hWnd)
: m_cfl(encode)
, m_hWnd(hWnd)
, m_nLineCurrent(0)
, m_nLineNum(0)
{
}
~CFileLoadOrWnd(){
}
ECodeType FileOpen(const WCHAR* pszFile, bool bBigFile, ECodeType charCode, int nFlag)
{
if( m_hWnd ){
DWORD_PTR dwMsgResult = 0;
if( 0 == ::SendMessageTimeout(m_hWnd, MYWM_GETLINECOUNT, 0, 0, SMTO_NORMAL, 10000, &dwMsgResult) ){
// エラーかタイムアウト
throw CError_FileOpen();
}
m_nLineCurrent = 0;
m_nLineNum = (int)dwMsgResult;
::SendMessageAny(m_hWnd, MYWM_GETFILEINFO, 0, 0);
const EditInfo* editInfo = &GetDllShareData().m_sWorkBuffer.m_EditInfo_MYWM_GETFILEINFO;
return editInfo->m_nCharCode;
}
return m_cfl.FileOpen(pszFile, bBigFile, charCode, nFlag);
}
EConvertResult ReadLine(CNativeW* buffer, CEol* pcEol){
if( m_hWnd ){
const int max_size = (int)GetDllShareData().m_sWorkBuffer.GetWorkBufferCount<const WCHAR>();
const WCHAR* pLineData = GetDllShareData().m_sWorkBuffer.GetWorkBuffer<const WCHAR>();
buffer->SetStringHoldBuffer(L"", 0);
if( m_nLineNum <= m_nLineCurrent ){
return RESULT_FAILURE;
}
int nLineOffset = 0;
int nLineLen = 0; //初回用仮値
do{
// m_sWorkBuffer#m_Workの排他制御。外部コマンド出力/TraceOut/Diffが対象
LockGuard<CMutex> guard( CShareData::GetMutexShareWork() );
{
nLineLen = ::SendMessageAny(m_hWnd, MYWM_GETLINEDATA, m_nLineCurrent, nLineOffset);
if( nLineLen == 0 ){ return RESULT_FAILURE; } // EOF => 正常終了
if( nLineLen < 0 ){ return RESULT_FAILURE; } // 何かエラー
buffer->AllocStringBuffer(max_size);
buffer->AppendString(pLineData, t_min(nLineLen, max_size));
}
nLineOffset += max_size;
}while(max_size < nLineLen);
if( 0 < nLineLen ){
if( 1 < nLineLen && (*buffer)[nLineLen - 2] == WCODE::CR &&
(*buffer)[nLineLen - 1] == WCODE::LF){
pcEol->SetType(EEolType::cr_and_lf);
}else{
pcEol->SetTypeByString(buffer->GetStringPtr() + nLineLen - 1, 1);
}
}
m_nLineCurrent++;
return RESULT_COMPLETE;
}
return m_cfl.ReadLine(buffer, pcEol);
}
LONGLONG GetFileSize(){
if( m_hWnd ){
return 0;
}
return m_cfl.GetFileSize();
}
int GetPercent(){
if( m_hWnd ){
return (int)(m_nLineCurrent * 100.0 / m_nLineNum);
}
return m_cfl.GetPercent();
}

void FileClose(){
if( m_hWnd ){
return;
}
m_cfl.FileClose();
}
};

CGrepAgent::CGrepAgent()
: m_bGrepMode( false ) /* Grepモードか */
, m_bGrepRunning( false ) /* Grep処理中 */
Expand Down Expand Up @@ -207,6 +298,60 @@ void CGrepAgent::AddTail( CEditView* pcEditView, const CNativeW& cmem, bool bAdd
}
}

int GetHwndTitle(HWND& hWndTarget, CNativeW* pmemTitle, WCHAR* pszWindowName, WCHAR* pszWindowPath, const WCHAR* pszFile)
{
if( 0 != wcsncmp(L":HWND:", pszFile, 6) ){
return 0; // ハンドルGrepではない
}
#ifdef _WIN64
_stscanf(pszFile + 6, L"%016I64x", &hWndTarget);
#else
_stscanf(pszFile + 6, L"%08x", &hWndTarget);
#endif
if( pmemTitle ){
const wchar_t* p = L"Window:[";
pmemTitle->SetStringHoldBuffer(p, 8);
}
if( !IsSakuraMainWindow(hWndTarget) ){
return -1;
}
::SendMessageAny(hWndTarget, MYWM_GETFILEINFO, 0, 0);
EditInfo* editInfo = &(GetDllShareData().m_sWorkBuffer.m_EditInfo_MYWM_GETFILEINFO);
if( '\0' == editInfo->m_szPath[0] ){
// Grepかアウトプットか無題
WCHAR szTitle[_MAX_PATH]{};
WCHAR szGrep[100]{};
editInfo->m_bIsModified = false;
const EditNode* node = CAppNodeManager::getInstance()->GetEditNode(hWndTarget);
WCHAR* pszTagName = szTitle;
if( editInfo->m_bIsGrep ){
// Grepは検索キーとタグがぶつかることがあるので単に(Grep)と表示
pszTagName = szGrep;
wcsncpy_s(pszTagName, _countof(szGrep), L"(Grep)", _TRUNCATE);
}
CFileNameManager::getInstance()->GetMenuFullLabel_WinListNoEscape(szTitle, _countof(szTitle), editInfo, node->m_nId, -1, NULL );
#ifdef _WIN64
auto_sprintf(pszWindowName, L":HWND:[%016I64x]%s", hWndTarget, pszTagName);
#else
auto_sprintf(pszWindowName, L":HWND:[%08x]%s", hWndTarget, pszTagName);
#endif
if( pmemTitle ){
pmemTitle->AppendString(szTitle);
}
pszWindowPath[0] = L'\0';
}else{
SplitPath_FolderAndFile(editInfo->m_szPath, pszWindowPath, pszWindowName);
if( pmemTitle ){
pmemTitle->AppendString(pszWindowName);
}
}
if( pmemTitle ){
pmemTitle->AppendString(L"]");
}
return 1;
}


/*! Grep実行

@param[in] pcmGrepKey 検索パターン
Expand Down Expand Up @@ -458,13 +603,28 @@ DWORD CGrepAgent::DoGrep(
}
}

cmemMessage.AppendString( LS( STR_GREP_SEARCH_TARGET ) ); //L"検索対象 "
HWND hWndTarget = NULL;
WCHAR szWindowName[_MAX_PATH];
WCHAR szWindowPath[_MAX_PATH];
{
// 解析済みのファイルパターン配列を取得する
const auto& vecSearchFileKeys = cGrepEnumKeys.m_vecSearchFileKeys;
std::wstring strPatterns = FormatPathList( vecSearchFileKeys );
cmemMessage.AppendString( strPatterns.c_str(), strPatterns.length() );
int nHwndRet = GetHwndTitle(hWndTarget, &cmemWork, szWindowName, szWindowPath, pcmGrepFile->GetStringPtr());
if( -1 == nHwndRet ){
cmemMessage.AppendString(L"HWND handle error.\n");
if( sGrepOption.bGrepHeader ){
AddTail(pcViewDst, cmemMessage, sGrepOption.bGrepStdout);
}
return 0;
}else if( 0 == nHwndRet ){
{
// 解析済みのファイルパターン配列を取得する
const auto& vecSearchFileKeys = cGrepEnumKeys.m_vecSearchFileKeys;
std::wstring strPatterns = FormatPathList( vecSearchFileKeys );
cmemWork.SetString( strPatterns.c_str(), strPatterns.length() );
}
}
}
cmemMessage.AppendString( LS( STR_GREP_SEARCH_TARGET ) ); //L"検索対象 "
cmemMessage += cmemWork;
cmemMessage.AppendString( L"\r\n" );

cmemMessage.AppendString( LS( STR_GREP_SEARCH_FOLDER ) ); //L"フォルダ "
Expand Down Expand Up @@ -586,38 +746,87 @@ DWORD CGrepAgent::DoGrep(

int nGrepTreeResult = 0;

for( int nPath = 0; nPath < (int)vPaths.size(); nPath++ ){
bool bOutputBaseFolder = false;
std::wstring sPath = ChopYen( vPaths[nPath] );
int nTreeRet = DoGrepTree(
pcViewDst,
&cDlgCancel,
pcmGrepKey->GetStringPtr(),
cmemReplace,
cGrepEnumKeys,
cGrepExceptAbsFiles,
cGrepExceptAbsFolders,
sPath.c_str(),
sPath.c_str(),
sSearchOption,
sGrepOption,
pattern,
&cRegexp,
0,
bOutputBaseFolder,
&nHitCount,
cmemMessage,
cUnicodeBuffer
);
if( nTreeRet == -1 ){
nGrepTreeResult = -1;
break;
if( hWndTarget ){
for( HWND hwnd = hWndTarget; NULL != hwnd; hwnd = NULL ){
bool bOutputBaseFolder = false;
bool bOutputFolderName = false;
// 複数ウィンドウループ予約
auto nPathLen = wcsnlen_s(szWindowPath, _countof(szWindowPath));
std::wstring currentFile = szWindowPath;
if( currentFile.size() ){
currentFile += L'\\';
nPathLen += 1;
}
currentFile += szWindowName;
int nHitCount = nGrepTreeResult;
int nTreeRet = DoGrepFile(
pcViewDst,
&cDlgCancel,
hwnd,
pcmGrepKey->GetStringPtr(),
szWindowName,
sSearchOption,
sGrepOption,
pattern,
&cRegexp,
&nHitCount,
currentFile.c_str(),
szWindowPath,
(sGrepOption.bGrepSeparateFolder && sGrepOption.bGrepOutputBaseFolder ? L"" : szWindowPath),
(sGrepOption.bGrepSeparateFolder ? szWindowName : currentFile.c_str() + nPathLen),
bOutputBaseFolder,
bOutputFolderName,
cmemMessage,
cUnicodeBuffer
);
if( nTreeRet == -1 ){
nGrepTreeResult = -1;
break;
}
nGrepTreeResult += nTreeRet;
}
if( 0 < cmemMessage.GetStringLength() ){
AddTail( pcViewDst, cmemMessage, sGrepOption.bGrepStdout );
pcViewDst->GetCommander().Command_GOFILEEND( false );
if( !CEditWnd::getInstance()->UpdateTextWrap() )
CEditWnd::getInstance()->RedrawAllViews( pcViewDst );
cmemMessage.Clear();
}
nHitCount = nGrepTreeResult;
}else{
for( int nPath = 0; nPath < (int)vPaths.size(); nPath++ ){
bool bOutputBaseFolder = false;
std::wstring sPath = ChopYen( vPaths[nPath] );
int nTreeRet = DoGrepTree(
pcViewDst,
&cDlgCancel,
pcmGrepKey->GetStringPtr(),
cmemReplace,
cGrepEnumKeys,
cGrepExceptAbsFiles,
cGrepExceptAbsFolders,
sPath.c_str(),
sPath.c_str(),
sSearchOption,
sGrepOption,
pattern,
&cRegexp,
0,
bOutputBaseFolder,
&nHitCount,
cmemMessage,
cUnicodeBuffer
);
if( nTreeRet == -1 ){
nGrepTreeResult = -1;
break;
}
nGrepTreeResult += nTreeRet;
}
if( 0 < cmemMessage.GetStringLength() ) {
AddTail( pcViewDst, cmemMessage, sGrepOption.bGrepStdout );
cmemMessage._SetStringLength(0);
}
nGrepTreeResult += nTreeRet;
}
if( 0 < cmemMessage.GetStringLength() ) {
AddTail( pcViewDst, cmemMessage, sGrepOption.bGrepStdout );
cmemMessage._SetStringLength(0);
}
if( -1 == nGrepTreeResult && sGrepOption.bGrepHeader ){
const wchar_t* p = LS( STR_GREP_SUSPENDED ); //L"中断しました。\r\n"
Expand Down Expand Up @@ -781,6 +990,7 @@ int CGrepAgent::DoGrepTree(
nRet = DoGrepFile(
pcViewDst,
pcDlgCancel,
NULL,
pszKey,
lpFileName,
sSearchOption,
Expand Down Expand Up @@ -1101,6 +1311,7 @@ static void OutputPathInfo(
int CGrepAgent::DoGrepFile(
CEditView* pcViewDst, //!<
CDlgCancel* pcDlgCancel, //!< [in] Cancelダイアログへのポインタ
HWND hWndTarget, //!< [in] 対象Windows(NULLでファイル)
const wchar_t* pszKey, //!< [in] 検索パターン
const WCHAR* pszFile, //!< [in] 処理対象ファイル名(表示用)
const SSearchOption& sSearchOption, //!< [in] 検索オプション
Expand Down Expand Up @@ -1132,7 +1343,7 @@ int CGrepAgent::DoGrepFile(
if( !CDocTypeManager().GetTypeConfigMini( CDocTypeManager().GetDocumentTypeOfPath( pszFile ), &type ) ){
return -1;
}
CFileLoad cfl( type->m_encoding ); // 2012/12/18 Uchi 検査するファイルのデフォルトの文字コードを取得する様に
CFileLoadOrWnd cfl( type->m_encoding, hWndTarget ); // 2012/12/18 Uchi 検査するファイルのデフォルトの文字コードを取得する様に
int nOldPercent = 0;

int nKeyLen = wcslen( pszKey );
Expand Down
1 change: 1 addition & 0 deletions sakura_core/CGrepAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class CGrepAgent : public CDocListenerEx{
int DoGrepFile(
CEditView* pcViewDst,
CDlgCancel* pcDlgCancel,
HWND hWndTarget,
const wchar_t* pszKey,
const WCHAR* pszFile,
const SSearchOption& sSearchOption,
Expand Down
2 changes: 2 additions & 0 deletions sakura_core/String_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -1323,5 +1323,7 @@
#define STR_FILEDIALOG_EOL 35039
#define STR_FILEDIALOG_MRU 35040
#define STR_FILEDIALOG_OPENFOLDER 35041
#define STR_DLGGREP_THISDOC 35045
#define STR_DLGGREP_THISDOC_ERROR 35046

// Now using max number 35044 by STR_STATUS_FONTZOOM_1
2 changes: 2 additions & 0 deletions sakura_core/_main/CControlTray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ static LRESULT CALLBACK CControlTrayWndProc( HWND, UINT, WPARAM, LPARAM );
//Stonee, 2001/07/01 多重起動された場合は前回のダイアログを前面に出すようにした。
void CControlTray::DoGrep()
{
m_cDlgGrep.m_bEnableThisText = false;

//Stonee, 2001/06/30
//前回のダイアログがあれば前面に (suggested by genta)
if ( ::IsWindow(m_cDlgGrep.GetHwnd()) ){
Expand Down
Loading