⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asmeditview.cpp

📁 编写Win32汇编语言的语法编辑器
💻 CPP
字号:
// ASMEditView.cpp : implementation of the CASMEditView class
//

#include "stdafx.h"
#include "ASMEdit.h"

#include "ASMEditDoc.h"
#include "CntrItem.h"
#include "ASMEditView.h"

#include "SetCompDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define KEYMAXCOUNT 64

CString SegmentKeyWords[KEYMAXCOUNT];
CString OperationKeyWords[KEYMAXCOUNT];
CString CrcEmbKeyWords[KEYMAXCOUNT];

/////////////////////////////////////////////////////////////////////////////
// CASMEditView

IMPLEMENT_DYNCREATE(CASMEditView, CRichEditView)

BEGIN_MESSAGE_MAP(CASMEditView, CRichEditView)
//{{AFX_MSG_MAP(CASMEditView)
ON_WM_DESTROY()
ON_WM_CHAR()
ON_COMMAND(ID_Menu_Comp, OnMenuComp)
	ON_COMMAND(ID_MENUITEM_FormatCode, OnMENUITEMFormatCode)
	ON_COMMAND(ID_MENUITEM_Run, OnMENUITEMRun)
	ON_COMMAND(ID_MENUITEM_InsertKJ, OnMENUITEMInsertKJ)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CASMEditView construction/destruction

CASMEditView::CASMEditView()
{
	// TODO: add construction code here
	for(int s = 0;s < KEYMAXCOUNT;s ++)
	{
		SegmentKeyWords[s] = "";
		OperationKeyWords[s] = "";
		CrcEmbKeyWords[s] = "";

	}
	
	SegmentKeyWords[0] = ".486";
	SegmentKeyWords[1] = ".386";
	SegmentKeyWords[2] = ".586";
	SegmentKeyWords[3] = ".model";
	SegmentKeyWords[4] = ".data";
	SegmentKeyWords[5] = ".code";
	SegmentKeyWords[6] = "start:";
	SegmentKeyWords[7] = "end start";
	SegmentKeyWords[8] = "includelib";
	SegmentKeyWords[9] = "include";
	SegmentKeyWords[10] = ".radix";
	SegmentKeyWords[11] = "=";
	SegmentKeyWords[12] = "<lable>";
	SegmentKeyWords[13] = "end <lable>";
	SegmentKeyWords[14] = ".const";
	SegmentKeyWords[15] = "option casemap:";
	SegmentKeyWords[16] = ".data?";
	SegmentKeyWords[17] = ".WHILE";
	SegmentKeyWords[18] = ".ENDW";
	SegmentKeyWords[19] = ".IF";
	SegmentKeyWords[20] = ".ELSE";
	SegmentKeyWords[21] = ".ENDIF";

	CrcEmbKeyWords[0] = ".if";
	CrcEmbKeyWords[1] = ".else";
	CrcEmbKeyWords[2] = ".endif";
	CrcEmbKeyWords[3] = ".while";
	CrcEmbKeyWords[4] = ".endw";


	char tmp[MAX_PATH];
	DWORD size = MAX_PATH;
	GetCurrentDirectory(size,tmp);
	m_CurPath = tmp;
}

CASMEditView::~CASMEditView()
{
	//	
}

BOOL CASMEditView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CRichEditView::PreCreateWindow(cs);
}

void CASMEditView::OnInitialUpdate()
{
	CRichEditView::OnInitialUpdate();
	// Set the printing margins (720 twips = 1/2 inch).
	SetMargins(CRect(720, 720, 720, 720));
}

void CASMEditView::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used.
	CRichEditView::OnDestroy();
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
	{
		pActiveItem->Deactivate();
		ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
	}
}


/////////////////////////////////////////////////////////////////////////////
// CASMEditView diagnostics

#ifdef _DEBUG
void CASMEditView::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CASMEditView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

CASMEditDoc* CASMEditView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CASMEditDoc)));
	return (CASMEditDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CASMEditView message handlers

void CASMEditView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if(nChar == 13 || nChar == 32)
		//FormatCode();
		FormatCurrentLine();
	
	CRichEditView::OnChar(nChar, nRepCnt, nFlags);
}

void CASMEditView::FormatCode()
{
	CRichEditCtrl& pCtl = GetRichEditCtrl();
	CString str;
	
	long nowb,nowe;
	pCtl.GetSel(nowb,nowe); 
	
	pCtl.GetWindowText(str);
	int strlen = str.GetLength(); 
	
	for(int i = 0;i < KEYMAXCOUNT;i ++)
	{
		if(SegmentKeyWords[i] == "")
			break;
		else
		{
			int findsit = 0;
			while(findsit != -1)
			{
				findsit = str.Find( SegmentKeyWords[i], findsit);
				if(findsit != -1)
				{
					//能够找到关键字
						SetKeyWordColor(findsit,findsit + SegmentKeyWords[i].GetLength(),1,SegmentKeyWords[i]);
						//AfxMessageBox(SegmentKeyWords[i]);
						findsit = findsit + SegmentKeyWords[i].GetLength(); 
					
				}
			}
		}
	}
	
	//处理逗号","
	int commasit = 0;
	
	while(commasit != -1)
	{
		commasit = str.Find(",",commasit); 
		if(commasit != -1)
		{
			SetKeyWordColor(commasit,commasit + 1,3,",");
			
			commasit ++;
		}
	}
	
	//处理注释
	int notesit = 0;
	int noteend;
	while(notesit != -1)
	{
		notesit = str.Find(";",notesit); 
		if(notesit != -1)
		{
			noteend = str.Find(0x0d,notesit);
			if(noteend != -1)
				SetKeyWordColor(notesit,noteend,2,str.Mid(notesit,noteend - notesit));
			else
				SetKeyWordColor(notesit,noteend,2,str.Mid(notesit));
			
			notesit = noteend;
		}
	}
	
	pCtl.SetSel(nowb,nowe);
	CHARFORMAT cf;
	cf.cbSize = sizeof(cf); 
	cf.dwMask = CFM_COLOR;
	cf.dwEffects = 0;
	cf.crTextColor = RGB(0, 0, 0); 
	pCtl.SetSelectionCharFormat(cf);
}

void CASMEditView::SetKeyWordColor(long bb, long ee, int style,CString keywords)
{
	CRichEditCtrl& pCtl = GetRichEditCtrl();
	CString str;
	
	pCtl.SetSel(bb,ee);
	
	CHARFORMAT cf2;
	
    cf2.cbSize = sizeof(cf2); 
	
	cf2.dwMask = CFM_COLOR;
	cf2.dwEffects = 0;
	
	if(style == 1)
		cf2.crTextColor = RGB(0,0, 255); 
	if(style == 2)
		cf2.crTextColor = RGB(0,128,0);
	if(style == 3)
	{
		cf2.crTextColor = RGB(255,0,0);
	}
	
	pCtl.SetSelectionCharFormat(cf2);
	pCtl.ReplaceSel(keywords); 
}

void CASMEditView::OnMenuComp() 
{
	// TODO: Add your command handler code here
	//FormatCode();
	//FormatCurrentLine();

	CSetCompDlg m_dlg;
	if(m_dlg.DoModal() == IDOK)
	{
		if(GetDocument()->GetPathName() != "")
		{
			CString str;
			str = m_dlg.m_CompilarCmdStr;
			int s = str.Replace("[this asm]", GetDocument()->GetPathName());
			if(s == 0)
				AfxMessageBox("Compilar cmd string error!");
			else
			{
				str = str + " > " +  m_CurPath + "\\tmp.txt";
				system(str);

				CString tmp;
				tmp = m_CurPath + "\\tmp.txt";
				ShellExecute(NULL, "open", tmp, NULL, NULL, SW_SHOW);
			}
			
		}
		else
			AfxMessageBox("asm file error!");
	}
}


void CASMEditView::FormatCurrentLine()
{
	//CString str;
	//
	//CString strText, strLine;
    //int nLineLength = pmyRichEditCtrl.LineLength(-1);
    //pmyRichEditCtrl.GetLine(-1, strText.GetBuffer(nLineLength));
    //strText.ReleaseBuffer(nLineLength);
	//上面代码不知道为什么取不到当前行的字符串,所以用下面的笨方法。
	
	
	CRichEditCtrl& pCtl = GetRichEditCtrl();
	CString str;
	
	long nowb,nowe;
	pCtl.GetSel(nowb,nowe); 
	
	pCtl.GetWindowText(str);
	
	long sittmp = nowb;
	if(sittmp > 1)
	{
		while(sittmp != 0)
		{
			sittmp --;
			
			if(str.GetAt(sittmp) == 13)
				break;
			
		}
		
		str = str.Mid(sittmp,nowe - sittmp);
		
		//处理段关键字
		for(int i = 0;i < KEYMAXCOUNT;i ++)
		{
			if(SegmentKeyWords[i] == "")
				break;
			else
			{
				int findsit = 0;
				while(findsit != -1)
				{
					findsit = str.Find( SegmentKeyWords[i], findsit);
					if(findsit != -1)
					{
						//能够找到关键字
						SetKeyWordColor(findsit + sittmp,findsit + sittmp + SegmentKeyWords[i].GetLength(),1,SegmentKeyWords[i]);
						//AfxMessageBox(SegmentKeyWords[i]);
						findsit = findsit + SegmentKeyWords[i].GetLength(); 
					}
				}
			}
		}
		
		//处理逗号","
		int commasit = 0;
		
		while(commasit != -1)
		{
			commasit = str.Find(",",commasit); 
			if(commasit != -1)
			{
				SetKeyWordColor(commasit + sittmp,commasit + sittmp + 1,3,",");
				
				commasit ++;
			}
		}
		
		//处理注释
		int notesit = 0;
		int noteend;
		while(notesit != -1)
		{
			notesit = str.Find(";",notesit); 
			if(notesit != -1)
			{
				noteend = str.Find(0x0a,notesit);
				if(noteend != -1)
					SetKeyWordColor(notesit+sittmp,noteend+sittmp,2,str.Mid(notesit,noteend - notesit));
				else
				{
					int ss = str.GetLength(); 
					SetKeyWordColor(notesit+sittmp,(sittmp + str.GetLength()),2,str.Mid(notesit));
				}
				
				notesit = noteend;
			}
		}
		
		pCtl.SetSel(nowb,nowe);
		CHARFORMAT cf;
		cf.cbSize = sizeof(cf); 
		cf.dwMask = CFM_COLOR;
		cf.dwEffects = 0;
		cf.crTextColor = RGB(0, 0, 0); 
		pCtl.SetSelectionCharFormat(cf);
	}
	
}

void CASMEditView::OnMENUITEMFormatCode() 
{
	// TODO: Add your command handler code here
	FormatCode();
}

void CASMEditView::OnMENUITEMRun() 
{
	// TODO: Add your command handler code here
	OnMenuComp() ;

	CString str;
	str = GetDocument()->GetPathName();
	if(str != "")
	{
		if(str.GetLength() > 3) 
		if(str.Right(3) == "asm" || str.Right(3) == "ASM")
		{
			str.SetAt(str.GetLength() - 1,'e');  
			str.SetAt(str.GetLength() - 2,'x');
			str.SetAt(str.GetLength() - 3,'e');
			ShellExecute(NULL, "open", str, NULL, NULL, SW_SHOW);
		}
	}

}

void CASMEditView::OnMENUITEMInsertKJ() 
{
	// TODO: Add your command handler code here
	CRichEditCtrl& pCtl = GetRichEditCtrl();

	CString str;
	str = ".486\r\n.model flat , stdcall\r\noption casemap: none\r\n\r\n.data\r\n\r\n.code\r\nstart:\r\n\r\nend start";
	pCtl.SetWindowText(str);
	
	FormatCode();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -