示例#1
0
void FindReplaceDlg::getSelText(tComboInfo* info)
{
	if (info == NULL)
		return;

	UINT	posBeg	= 0;
	UINT	posEnd	= 0;

	/* get selection and set find text */
	::SendMessage(_hParentHandle, HEXM_GETSEL, (WPARAM)&posBeg, (LPARAM)&posEnd);

	INT	offset	= (INT)(posBeg < posEnd ? posBeg : posEnd);
	INT	length	= (abs(static_cast<int>(posEnd)-static_cast<int>(posBeg)) > COMBO_STR_MAX ? COMBO_STR_MAX : abs(static_cast<int>(posEnd)-static_cast<int>(posBeg)));
	info->length = length;

	if (info->length != 0)
	{
		PSTR text_temp = new(std::nothrow)CHAR[info->length+1];
		if ( text_temp == NULL ) {
			return;
			}

		std::unique_ptr<_Null_terminated_ CHAR[ ]> text( text_temp );
		text_temp = nullptr;


		/* convert and select and get the text */
		if (LittleEndianChange(_hSCI, getCurrentHScintilla(), &offset, &length) == TRUE)
		{
			ScintillaMsg(_hSCI, SCI_SETSELECTIONSTART, posBeg - offset, 0);
			ScintillaMsg(_hSCI, SCI_SETSELECTIONEND, posEnd - offset, 0);
			ScintillaMsg(_hSCI, SCI_TARGETFROMSELECTION, 0, 0);
			ScintillaMsg(_hSCI, SCI_GETSELTEXT, 0, (LPARAM)text.get());

			/* encode the text in dependency of selected data type */
			memcpy_s(info->text, sizeof( info->text ), text.get(), info->length);

			CleanScintillaBuf(_hSCI);
		}
	}
}
示例#2
0
void FindReplaceDlg::getSelText(tComboInfo* info)
{
	if (info == NULL)
		return;

	UINT	posBeg	= 0;
	UINT	posEnd	= 0;

	/* get selection and set find text */
	::SendMessage(_hParentHandle, HEXM_GETSEL, (WPARAM)&posBeg, (LPARAM)&posEnd);

	INT	offset	= (INT)(posBeg < posEnd ? posBeg : posEnd);
	INT	length	= (abs((INT)(posEnd-posBeg)) > COMBO_STR_MAX ? COMBO_STR_MAX : abs((INT)(posEnd-posBeg)));
	info->length = length;

	if (info->length != 0)
	{
		CHAR	*text	= (CHAR*)new CHAR[info->length+1];
		if (text != NULL)
		{
			/* convert and select and get the text */
			if (LittleEndianChange(_hSCI, getCurrentHScintilla(), &offset, &length) == TRUE)
			{
				ScintillaMsg(_hSCI, SCI_SETSELECTIONSTART, posBeg - offset, 0);
				ScintillaMsg(_hSCI, SCI_SETSELECTIONEND, posEnd - offset, 0);
				ScintillaMsg(_hSCI, SCI_TARGETFROMSELECTION, 0, 0);
				ScintillaMsg(_hSCI, SCI_GETSELTEXT, 0, (LPARAM)text);

				/* encode the text in dependency of selected data type */
				memcpy(info->text, text, info->length);

				CleanScintillaBuf(_hSCI);
			}
			delete [] text;
		}
	}
}
示例#3
0
void FindReplaceDlg::processAll(UINT process)
{
	HWND	hSciSrc		= getCurrentHScintilla();
	INT		lenSrc		= ScintillaMsg(hSciSrc, SCI_GETLENGTH);
	INT		cnt			= 0;
	INT		cntError	= 0;
	INT		offset		= 0;
	INT		length		= 0;
	INT		posBeg		= 0;
	INT		posEnd		= 0;
	BOOL	loopEnd		= FALSE;
	eError	isRep		= E_OK;

	/* get strings */
	_pFindCombo->getText(&_find);
	_pReplaceCombo->getText(&_replace);

	if (_find.length != 0)
	{
		/* selection dependent start position */
		if ((_isInSel == TRUE) && (process == REPLACE_ALL))
		{
			::SendMessage(_hParentHandle, HEXM_GETSEL, (WPARAM)&posBeg, (LPARAM)&lenSrc);
		}

		/* settings */
		ScintillaMsg(_hSCI, SCI_SETSEARCHFLAGS, _isMatchCase ? SCFIND_MATCHCASE : 0, 0);

		/* keep sure that end and begin at the same position */
		posEnd = posBeg;

		do {
			/* copy data into scintilla handle (encoded if necessary) and select string */
			offset = posBeg;
			length = FIND_BLOCK;
			if (LittleEndianChange(_hSCI, hSciSrc, &offset, &length) == TRUE)
			{
				ScintillaMsg(_hSCI, SCI_SETTARGETSTART, posBeg - offset);
				ScintillaMsg(_hSCI, SCI_SETTARGETEND, length);

				/* search */
				while (ScintillaMsg(_hSCI, SCI_SEARCHINTARGET, _find.length, (LPARAM)&_find.text) != -1)
				{
					switch (process)
					{
						case COUNT:
							cnt++;
							break;
						
						case REPLACE_ALL:
							ScintillaMsg(_hSCI, SCI_REPLACETARGET, _replace.length, (LPARAM)&_replace.text);
							isRep = replaceLittleToBig(	hSciSrc, _hSCI, 
														ScintillaMsg(_hSCI, SCI_GETTARGETSTART, 0, 0),
														ScintillaMsg(_hSCI, SCI_GETTARGETSTART, 0, 0) + offset,
														_find.length, 
														_replace.length );
							if (isRep == E_STRIDE)
							{
								LITTLE_REPLACE_ERROR;
								CleanScintillaBuf(_hSCI);
								return;
							}
							else if (isRep == E_OK)
							{
								cnt++;
							}
							else if (isRep == E_START)
							{
								cntError++;
							}

							/* calc offset */
							lenSrc += (_replace.length - _find.length);
							break;

						default:
							break;
					}
					ScintillaMsg(_hSCI, SCI_SETTARGETSTART, ScintillaMsg(_hSCI, SCI_GETTARGETEND));
					ScintillaMsg(_hSCI, SCI_SETTARGETEND, length);
				}

				/* calculate offset or end loop */
				posBeg = offset + length;
				if (posBeg < lenSrc) {
					posBeg -= (_find.length - 1);
				} else {
					loopEnd = TRUE;
				}
			}
		} while (loopEnd == FALSE);
	}

	TCHAR	TEMP[128];
	TCHAR	text[128];

	/* display result */
	if (cnt == 0)
	{
		if (NLGetText(_hInst, _hParent, _T("CantFind"), TEMP, 128)) {
			_tcscpy(text, TEMP);
			if (NLGetText(_hInst, _hParent, _T("Find"), TEMP, 128)) {
				::MessageBox(_hParent, text, TEMP, MB_OK);
			} else {
				::MessageBox(_hParent, text, _T("Find"), MB_OK);
			}
		} else {
			::MessageBox(_hSelf, _T("Can't find"), _T("Find"), MB_OK);
		}
	}
	else
	{
		switch (process)
		{
			case COUNT:
			{
				if (NLGetText(_hInst, _hParent, _T("Tokens Found"), TEMP, 128)) {
					_stprintf(text, TEMP, cnt);
				} else {
					_stprintf(text, _T("%i tokens are found."), cnt);
				}

				if (NLGetText(_hInst, _hParent, _T("Count"), TEMP, 128)) {
					::MessageBox(_hParent, text, TEMP, MB_OK);
				} else {
					::MessageBox(_hParent, text, _T("Count"), MB_OK);
				}

				_pFindCombo->addText(_find);
				break;
			}
			case REPLACE_ALL:
			{
				UINT	pos;
				::SendMessage(_hParentHandle, HEXM_GETPOS, 0, (LPARAM)&pos);
				::SendMessage(_hParentHandle, HEXM_SETPOS, 0, (LPARAM)pos);

				if (NLGetText(_hInst, _hParent, _T("Tokens Replaced"), TEMP, 128)) {
					_stprintf(text, TEMP, cnt);
				} else {
					_stprintf(text, _T("%i tokens are replaced.\n"), cnt);
				}

				if (cntError != 0)
				{
					if (NLGetText(_hInst, _hParent, _T("Tokens Skipped"), TEMP, 128)) {
						_stprintf(text, TEMP, text, cntError);
					} else {
						_stprintf(text, _T("%s%i tokens are skipped, because of alignment error.\n"), text, cntError);
					}
				}

				if (NLGetText(_hInst, _hParent, _T("Replace"), TEMP, 128)) {
					::MessageBox(_hParent, text, TEMP, MB_OK);
				} else {
					::MessageBox(_hParent, text, _T("Replace"), MB_OK);
				}

				_pFindCombo->addText(_find);
				_pReplaceCombo->addText(_replace);
				break;
			}
			default:
				break;
		}
		_pFindCombo->addText(_find);
	}
}
示例#4
0
void FindReplaceDlg::onReplace(void)
{
	HWND	hSciSrc	= getCurrentHScintilla();
	INT		lenSrc  = ScintillaMsg(hSciSrc, SCI_GETLENGTH);
	lenSrc; //avoid compiler warning  C4189
	INT		lenStr	= 0;
	INT		offset	= 0;
	INT		length  = 0;
	INT		posBeg  = 0;
	INT		posEnd  = 0;
	eError	isRep	= E_OK;

	_pFindCombo->getText(&_find);
	_pReplaceCombo->getText(&_replace);

	/* get selection and correct anchor and cursor position */
	::SendMessage(_hParentHandle, HEXM_GETSEL, (WPARAM)&posBeg, (LPARAM)&posEnd);
	if (posEnd < posBeg) {
		UINT posTmp = posBeg;
		posBeg = posEnd;
		posEnd = posTmp;
	}

	/* copy data into scintilla handle (encoded if necessary) and select string */
	offset = posBeg;
	length = posEnd - posBeg;
	lenStr = length;
	if (LittleEndianChange(_hSCI, hSciSrc, &offset, &length) == TRUE)
	{
		LPSTR	text	= (LPSTR)new CHAR[lenStr+1];

		if (text != NULL)
		{
			/* get selection and compare if it is equal to expected text */
			ScintillaMsg(_hSCI, SCI_SETSELECTIONSTART, posBeg - offset, 0);
			ScintillaMsg(_hSCI, SCI_SETSELECTIONEND, posEnd - offset, 0);
    		ScintillaMsg(_hSCI, SCI_GETSELTEXT, 0, (LPARAM)text);

			/* make difference between match case modes */
    		if (((_isMatchCase == TRUE) && (memcmp(text, _find.text, lenStr) == 0)) ||
				((_isMatchCase == FALSE) && (_stricmp(text, _find.text) == 0)))
    		{
    			ScintillaMsg(_hSCI, SCI_TARGETFROMSELECTION);
    			ScintillaMsg(_hSCI, SCI_REPLACETARGET, _replace.length, (LPARAM)&_replace.text);
    			isRep = replaceLittleToBig(hSciSrc, _hSCI, posBeg - offset, posBeg, lenStr, _replace.length);
    			if (isRep == E_OK)
    			{
    				::SendMessage(_hParentHandle, HEXM_SETPOS, 0, posBeg + _replace.length);
    				_pFindCombo->addText(_find);
    				_pReplaceCombo->addText(_replace);
    			}
    		}

    		if (isRep == E_OK) {
    			onFind(FALSE);
    		} else {
    			LITTLE_REPLACE_ERROR;
    		}

    		delete [] text;
		}
   		CleanScintillaBuf(_hSCI);
	}
}
示例#5
0
void FindReplaceDlg::onFind(BOOL isVolatile)
{
	/* get current scintilla */
	HWND		hSciSrc		= getCurrentHScintilla();
	INT			lenSrc		= ScintillaMsg(hSciSrc, SCI_GETLENGTH);
	INT			offset		= 0;
	INT			length		= 0;
	INT			posBeg		= 0;
	INT			posEnd		= 0;
	INT			wrapPos		= 0;
	BOOL		loopEnd		= FALSE;
	BOOL		doWrap		= FALSE;
	BOOL		wrapDone	= FALSE;
	tComboInfo	info		= {0};

	if (_hSCI == NULL)
	{
		/* create new scintilla handle */
		_hSCI = (HWND)::SendMessage(_nppData._nppHandle, NPPM_CREATESCINTILLAHANDLE, 0, (LPARAM)_hSelf);
	}

	/* in dependency of find type get search information from combo or directly from source */
	if (isVolatile == FALSE)
	{
		_pFindCombo->getText(&_find);
		info = _find;
		if (info.length == 0)
			return;
	}
	else
	{
		getSelText(&info);
		if (info.length == 0) {
			if (NLMessageBox(_hInst, _hParent, _T("MsgBox SelectSomething"), MB_OK) == FALSE)
				::MessageBox(_hParent, _T("Select something in the text!"), _T("Hex-Editor"), MB_OK);
			return;
		}
	}

	/* set match case */
	ScintillaMsg(_hSCI, SCI_SETSEARCHFLAGS, _isMatchCase ? SCFIND_MATCHCASE : 0, 0);

	/* get selection and correct anchor and cursor position */
	::SendMessage(_hParentHandle, HEXM_GETSEL, (WPARAM)&posBeg, (LPARAM)&posEnd);
	if (posEnd < posBeg) {
		UINT posTmp = posBeg;
		posBeg = posEnd;
		posEnd = posTmp;
	}
	wrapPos = posBeg;

	do {
		BOOL	isConverted = FALSE;

		/* copy data into scintilla handle (encoded if necessary) and select string */

		if ((wrapDone == TRUE) && (lenSrc < FIND_BLOCK)) {
			if (_whichDirection == DIR_DOWN) {
				length = wrapPos + info.length + 1;
			} else {
				length = FIND_BLOCK;
			}
		} else {
			length = FIND_BLOCK;
		}

		if (_whichDirection == DIR_DOWN)
		{
			offset = posBeg;
			if (LittleEndianChange(_hSCI, hSciSrc, &offset, &length) == TRUE)
			{
				ScintillaMsg(_hSCI, SCI_SETTARGETSTART, posEnd - offset);
				ScintillaMsg(_hSCI, SCI_SETTARGETEND, length);
				isConverted = TRUE;
			}
		}
		else
		{
			posEnd -= FIND_BLOCK;
			offset = posEnd;
			if (LittleEndianChange(_hSCI, hSciSrc, &offset, &length) == TRUE)
			{
				ScintillaMsg(_hSCI, SCI_SETTARGETSTART, posBeg);
				ScintillaMsg(_hSCI, SCI_SETTARGETEND, posEnd - offset);
				isConverted = TRUE;
			}
		}

		if (isConverted == TRUE)
		{
			/* find string */
			INT posFind = ScintillaMsg(_hSCI, SCI_SEARCHINTARGET, info.length, (LPARAM)info.text);
			if (posFind != -1)
			{
				/* found */
				posFind += offset;
				::SendMessage(_hParentHandle, HEXM_SETSEL, posFind, posFind + info.length);
				if (isVolatile == FALSE) {
					_pFindCombo->addText(info);
				}
				loopEnd = TRUE;
			}
			else
			{
				/* calculate new start find position */
				if (_whichDirection == DIR_DOWN)
				{
					posBeg = offset + length;

					/* test if out of bound */
					if ((posBeg >= lenSrc) && (wrapDone == FALSE)) {
						posBeg = 0;
						/* notify wrap is done */
						doWrap = TRUE;
					} else if (posBeg != lenSrc) {
						/* calculate new start find position */
						posBeg -= (info.length + 1);
					}

					/* indicate that wrap is still done */
					wrapDone = doWrap;

				} 
				else
				{
					/* indicate wrap done next time */
					wrapDone = doWrap;

					posBeg = offset;

					/* test if out of bound */
					if ((posBeg <= 0) && (wrapDone == FALSE)) {
						posBeg = lenSrc;
						/* notify wrap is done */
						doWrap = TRUE;
					} else if (posBeg != 0) {
						/* calculate new start find position */
						posBeg += (info.length + 1);
					}
				}

				/* if wrap was done and posBeg is jump over the wrapPos (start pos on function call)... */
				if ((wrapDone == TRUE) &&
					(((_whichDirection == DIR_DOWN) && (posBeg >= wrapPos)) ||
					 ((_whichDirection == DIR_UP  ) && (posEnd <= wrapPos))))
				{
					/* ... leave the function */
					TCHAR	text[128];
					TCHAR	TEMP[128];

					if (NLGetText(_hInst, _hParent, _T("CantFind"), TEMP, 128)) {
						_tcscpy(text, TEMP);
						if (NLGetText(_hInst, _hParent, (_findReplace == TRUE)?_T("Replace"):_T("Find"), TEMP, 128)) {
							::MessageBox(_hParent, text, TEMP, MB_OK);
						} else {
							::MessageBox(_hParent, text, (_findReplace == TRUE)?_T("Replace"):_T("Find"), MB_OK);
						}
					} else {
						::MessageBox(_hSelf, _T("Can't find"),(_findReplace == TRUE)?_T("Replace"):_T("Find"), MB_OK);
					}

					loopEnd = TRUE;
				}

				/* for further calculation */
				posEnd = posBeg;
			}
			CleanScintillaBuf(_hSCI);
		}
		else
		{
			loopEnd = TRUE;
		}
	} while (loopEnd == FALSE);
}