Ejemplo n.º 1
0
/***
 *	ScintillaGetText()
 *
 *	API-Wrapper
 */
UINT ScintillaGetText(char *text, INT start, INT end) 
{
	TextRange tr;
	tr.chrg.cpMin = start;
	tr.chrg.cpMax = end;
	tr.lpstrText  = text;
	return (UINT)ScintillaMsg(SCI_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&tr));
}
Ejemplo n.º 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(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);
		}
	}
}
Ejemplo n.º 3
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;
		}
	}
}
Ejemplo n.º 4
0
void CleanScintillaBuf(HWND hWnd)
{
	do {
		ScintillaMsg(hWnd, SCI_UNDO);
	} while (ScintillaMsg(hWnd, SCI_CANUNDO));
}
Ejemplo n.º 5
0
void DoCompare(void)
{
	TCHAR		szFile[MAX_PATH];
	BOOL		doMatch		= TRUE;
	LPSTR		compare1	= NULL;
	LPSTR		compare2	= NULL;
	tHexProp	hexProp1	= hexEdit1.GetHexProp();
	tHexProp	hexProp2	= hexEdit2.GetHexProp();
	tCmpResult	cmpResult	= {0};

	if ((hexProp1.bits != hexProp2.bits) || (hexProp1.columns != hexProp2.columns) || (hexProp1.isBin != hexProp2.isBin))
	{
		/* ask which hex edits should be used to have same settings in both */
		CompareDlg	dlg;
		dlg.init((HINSTANCE)g_hModule, nppData);
		if (dlg.doDialog(&hexEdit1, &hexEdit2, currentSC) == IDCANCEL) {
			return;
		}
	}

	/* create file for compare results */
	_tcscpy(cmpResult.szFileName, cmparePath);
	_tcscpy(szFile, ::PathFindFileName(hexEdit1.GetHexProp().szFileName));
	::PathRemoveExtension(szFile);
	::PathAppend(cmpResult.szFileName, szFile);
	_tcscat(cmpResult.szFileName, _T("_"));
	_tcscpy(szFile, ::PathFindFileName(hexEdit2.GetHexProp().szFileName));
	::PathRemoveExtension(szFile);
	_tcscat(cmpResult.szFileName, szFile);
	_tcscat(cmpResult.szFileName, _T(".cmp"));
	cmpResult.hFile = ::CreateFile(cmpResult.szFileName, 
		GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 
		NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		
	if (cmpResult.hFile != INVALID_HANDLE_VALUE)
	{
		LPSTR	buffer1 = (LPSTR)new CHAR[COMP_BLOCK+1];
		LPSTR	buffer2 = (LPSTR)new CHAR[COMP_BLOCK+1];

		/* get text size to comapre */
		INT		maxLength1 = ScintillaMsg(nppData._scintillaMainHandle, SCI_GETTEXTLENGTH) + 1;
		INT		maxLength2 = ScintillaMsg(nppData._scintillaSecondHandle, SCI_GETTEXTLENGTH) + 1;

		/* get max length */
		INT		curPos		= 0;
		INT		maxLength	= maxLength1;
		INT		minLength	= maxLength1;
        if (maxLength2 > maxLength1) {
			maxLength = maxLength2;
        } else {
			minLength = maxLength2;
        }

		while (curPos < minLength)
		{
		    CHAR	val	    = FALSE;
			INT	    posSrc	= 0;
			INT	    length1	= ((maxLength1 - curPos) > COMP_BLOCK ? COMP_BLOCK : (maxLength1 % COMP_BLOCK));
			INT	    length2	= ((maxLength2 - curPos) > COMP_BLOCK ? COMP_BLOCK : (maxLength2 % COMP_BLOCK));

		    ScintillaGetText(nppData._scintillaMainHandle, buffer1, curPos, length1 - 1);
            ScintillaGetText(nppData._scintillaSecondHandle, buffer2, curPos, length2 - 1);

			while ((posSrc < length1) && (posSrc < length2))
			{
				DWORD	hasWritten	= 0;

				if (buffer1[posSrc] != buffer2[posSrc])
				{
					val		= TRUE;
					doMatch	= FALSE;
				}

				/* increment source buffer */
				posSrc++;

				/* write to file */
				if (hexProp1.bits == 1) {
					::WriteFile(cmpResult.hFile, &val, sizeof(val), &hasWritten, NULL);
                    val	= FALSE;
				} else if ((posSrc % hexProp1.bits) == 0) {
					::WriteFile(cmpResult.hFile, &val, sizeof(val), &hasWritten, NULL);
                    val	= FALSE;
				}
			}

			/* increment file position */
			curPos += posSrc;
		}

		if (doMatch == TRUE)
		{
			if (NLMessageBox((HINSTANCE)g_hModule, nppData._nppHandle, _T("MsgBox CompMatch"), MB_OK) == FALSE)
				::MessageBox(nppData._nppHandle, _T("Files Match."), _T("Hex-Editor Compare"), MB_OK);
			::CloseHandle(cmpResult.hFile);
			::DeleteFile(cmpResult.szFileName);
		}
		else
		{
			DWORD	hasWritten	= 0;
			CHAR    val		    = TRUE;

            for (UINT i = (minLength / hexProp1.bits); i < (maxLength / hexProp1.bits); i++) {
			    ::WriteFile(cmpResult.hFile, &val, sizeof(val), &hasWritten, NULL);
            }

            /* create two structures for each view */
			tCmpResult* pCmpResult1 = (tCmpResult*)new tCmpResult;
			tCmpResult* pCmpResult2 = (tCmpResult*)new tCmpResult;

            if ((pCmpResult1 != NULL) && (pCmpResult2 != NULL))
            {
                /* set data */
			    *pCmpResult1 = cmpResult;
			    *pCmpResult2 = cmpResult;

                hexEdit1.SetCompareResult(pCmpResult1, pCmpResult2);
                hexEdit2.SetCompareResult(pCmpResult2, pCmpResult1);
            }
            else
            {
		        delete pCmpResult1;
		        delete pCmpResult2;
            }
		}

		delete [] buffer1;
		delete [] buffer2;
	}
}
Ejemplo n.º 6
0
eError replaceLittleToBig(HWND hTarget, HWND hSource, INT startSrc, INT startTgt, INT lengthOld, INT lengthNew)
{
	tHexProp hexProp;

	if (currentSC == MAIN_VIEW)
		hexProp = hexEdit1.GetHexProp();
	else
		hexProp = hexEdit2.GetHexProp();

	if (hexProp.isLittle == TRUE)
	{
		if (startSrc % hexProp.bits)
		{
			return E_START;
		}
		if ((lengthOld % hexProp.bits) || (lengthNew % hexProp.bits))
		{
			return E_STRIDE;
		}
	}

	char*	text = (char*)new char[lengthNew+1];

	if (text != NULL)
	{
		if (hSource)
		{
			/* get new text */
			::SendMessage(hSource, SCI_SETSELECTIONSTART, startSrc, 0);
			::SendMessage(hSource, SCI_SETSELECTIONEND, startSrc + lengthNew, 0);
			::SendMessage(hSource, SCI_GETSELTEXT, 0, (LPARAM)text);
		}

		/* set in target */
		if (hexProp.isLittle == FALSE)
		{
			ScintillaMsg(hTarget, SCI_SETTARGETSTART, startTgt);
			ScintillaMsg(hTarget, SCI_SETTARGETEND, startTgt + lengthOld);
			ScintillaMsg(hTarget, SCI_REPLACETARGET, lengthNew, (LPARAM)text);
		}
		else
		{
			INT		length	  = (lengthOld < lengthNew ? lengthNew:lengthOld);
			INT		posSource = startSrc;
			INT		posTarget = 0;

			ScintillaMsg(hTarget, SCI_BEGINUNDOACTION);

			for (INT i = 0; i < length; i++)
			{
				/* set position of change */
				posTarget = posSource - (posSource % hexProp.bits) + ((hexProp.bits-1) - (posSource % hexProp.bits)) + startTgt;

				if ((i < lengthOld) && (i < lengthNew))
				{
					ScintillaMsg(hTarget, SCI_SETTARGETSTART, posTarget);
					ScintillaMsg(hTarget, SCI_SETTARGETEND, posTarget + 1);
					ScintillaMsg(hTarget, SCI_REPLACETARGET, 1, (LPARAM)&text[i]);
				}
				else if (i < lengthOld)
				{
					/* old string is longer as the new one */
					ScintillaMsg(hTarget, SCI_SETTARGETSTART, posTarget);
					ScintillaMsg(hTarget, SCI_SETTARGETEND, posTarget + 1);
					ScintillaMsg(hTarget, SCI_REPLACETARGET, 0, (LPARAM)'\0');

					if (!((i+1) % hexProp.bits))
						posSource = startSrc + lengthNew - 1;
				}
				else if (i < lengthNew)
				{
					/* new string is longer as the old one */
					ScintillaMsg(hTarget, SCI_SETCURRENTPOS, posTarget - hexProp.bits + 1);
					ScintillaMsg(hTarget, SCI_ADDTEXT, 1, (LPARAM)&text[i]);
					if (!((i+1) % hexProp.bits))
						posSource += hexProp.bits;
					posSource--;
				}

				posSource++;
			}

			ScintillaMsg(hTarget, SCI_ENDUNDOACTION);
		}
		delete [] text;

		return E_OK;
	}

	return E_MEMORY;
}
Ejemplo n.º 7
0
BOOL LittleEndianChange(HWND hTarget, HWND hSource, LPINT offset, LPINT length)
{
	if ((hTarget == NULL) || (hSource == NULL) || (offset == NULL) || (length == NULL))
		return FALSE;

	tHexProp	hexProp	= pCurHexEdit->GetHexProp();
	BOOL		bRet	= FALSE;
	UINT		lenSrc  = 0;
	UINT		lenCpy	= 0;
	LPSTR		buffer  = NULL;
	INT			posBeg	= *offset;
	INT			posEnd	= *offset + *length;

	/* get source length of scintilla context */
	lenSrc = ScintillaMsg(hSource, SCI_GETTEXTLENGTH);

	if (posBeg < 0)
		posBeg = 0;

	/* calculate positions alligned */
	posBeg -= (posBeg % hexProp.bits);
	if (posEnd % hexProp.bits) {
		posEnd += hexProp.bits - (posEnd % hexProp.bits);
	}
	if (posEnd > (INT)lenSrc) {
		posEnd = lenSrc;
	}

	if (*length <= FIND_BLOCK)
	{
		/* create a buffer to copy data */
		buffer = (LPSTR)new CHAR[(posEnd - posBeg) + 1];

		if (buffer != NULL)
		{
			/* to clear the content of context begin UNDO */
			::SendMessage(hTarget, SCI_BEGINUNDOACTION, 0, 0);
			::SendMessage(hTarget, SCI_CLEARALL, 0, 0);

			/* copy text into the buffer */
			lenCpy = ScintillaGetText(hSource, buffer, posBeg, posEnd);

			/* convert when property is little */
			if (hexProp.isLittle == TRUE)
			{
				LPSTR temp  = (LPSTR)new CHAR[lenCpy + 1];
				LPSTR pText	= buffer;

				/* it must be unsigned */
				for (UINT i = 0; i < lenCpy; i++) {
					temp[i] = buffer[i];
				}

				UINT offset = (lenCpy) % hexProp.bits;
				UINT max	= (lenCpy) / hexProp.bits + 1;

				for (UINT i = 1; i <= max; i++)
				{
					if (i == max)
					{
						for (UINT j = 1; j <= offset; j++)
						{
							*pText = temp[lenCpy-j];
							pText++;
						}
					}
					else
					{
						for (SHORT j = 1; j <= hexProp.bits; j++)
						{
							*pText = temp[hexProp.bits*i-j];
							pText++;
						}
					}
				}
				*pText = NULL;
				delete [] temp;
			}

			/* add text to target */
			::SendMessage(hTarget, SCI_ADDTEXT, lenCpy, (LPARAM)buffer);
			::SendMessage(hTarget, SCI_ENDUNDOACTION, 0, 0);

			/* everything is fine, set return values */
			*offset = posBeg;
			*length = posEnd - posBeg;
			bRet = TRUE;
		}
		delete [] buffer;
	}
	return bRet;
}
Ejemplo n.º 8
0
INT_PTR CALLBACK FindReplaceDlg::run_dlgProc(UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch (Message) 
	{
        case WM_INITDIALOG :
		{
			initDialog();
			goToCenter();
			return TRUE;
		}
		case WM_ACTIVATE :
        {
			UINT	posBeg;
			UINT	posEnd;

			::SendMessage(_hParentHandle, HEXM_GETSEL, (WPARAM)&posBeg, (LPARAM)&posEnd);
			::EnableWindow(::GetDlgItem(_hSelf, IDC_CHECK_IN_SEL), ((posBeg == posEnd)?FALSE:TRUE));

			/* change language */
			NLChangeDialog(_hInst, _nppData._nppHandle, _hSelf, _T("FindReplace"));

            break;
        }
		case WM_COMMAND : 
		{
			switch (LOWORD(wParam))
			{
				case IDOK :
                {
					onFind(FALSE);
					return TRUE;
                }
				case IDC_REPLACE:
                {
					onReplace();
					break;
                }
				case IDC_COUNT :
				{
					processAll(COUNT);
					break;
                }
				case IDC_REPLACEALL :
                {
					HWND	hSci = getCurrentHScintilla();
					ScintillaMsg(hSci, SCI_BEGINUNDOACTION);
					processAll(REPLACE_ALL);
					ScintillaMsg(hSci, SCI_ENDUNDOACTION);
					return TRUE;
                }
				case IDC_COMBO_DATATYPE:
				{
					if (HIWORD(wParam) == CBN_SELCHANGE)
						changeCoding();
					break;
				}
				case IDC_CHECK_TRANSPARENT :
				{
					setTrans();
					break;
				}
				case IDC_RADIO_DIRUP :
				case IDC_RADIO_DIRDOWN :
				{
					_whichDirection = isChecked(IDC_RADIO_DIRDOWN);
					return TRUE;
				}
				case IDC_CHECK_MATCHCASE :
				{
					_isMatchCase = isChecked(IDC_CHECK_MATCHCASE);
					return TRUE;
				}
				case IDC_CHECK_WRAP :
				{
					_isWrap = isChecked(IDC_CHECK_WRAP);
					return TRUE;
				}
				case IDC_CHECK_IN_SEL :
				{
					_isInSel = isChecked(IDC_CHECK_IN_SEL);
					return TRUE;
				}
				case IDCANCEL :
				{
					display(FALSE);
					::SetFocus(_hParentHandle);
					break;
				}
				default :
					break;
			}
			break;
		}
		case WM_NOTIFY:
		{
			NMHDR	nmhdr = *((LPNMHDR)lParam);

			if ((nmhdr.idFrom == IDC_SWITCH) && (nmhdr.code == TCN_SELCHANGE))
			{
				_findReplace = TabCtrl_GetCurSel(::GetDlgItem(_hSelf, IDC_SWITCH));
				updateDialog();
			}
			break;
		}
		case WM_HSCROLL :
		{
			if ((HWND)lParam == ::GetDlgItem(_hSelf, IDC_SLIDER_PERCENTAGE))
			{
				setTrans();
			}
			return TRUE;
		}
	}
	return FALSE;
}
Ejemplo n.º 9
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);
	}
}
Ejemplo n.º 10
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);
	}
}
Ejemplo n.º 11
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);
}