#include "pch.h"
|
#include "AnsiParser.h"
|
|
static inline int AnsiToT(CStringA & Src, CString & Dest)
|
{
|
#ifdef UNICODE
|
int j, l;
|
l = Src.GetLength();
|
j = MultiByteToWideChar(CP_ACP, 0, Src, l, Dest.GetBuffer(l), l);
|
Dest.ReleaseBuffer(j);
|
#else
|
Dest = Src;
|
#endif
|
return 0;
|
}
|
static inline double GetTickCountmS()
|
{
|
LARGE_INTEGER perfreq;
|
LARGE_INTEGER percounter1; //,percounter2;
|
double time1; //,time2,timeinter1;
|
QueryPerformanceFrequency(&perfreq);
|
QueryPerformanceCounter(&percounter1);
|
// percounter1.QuadPart=rdtsc();
|
/* _asm {
|
RDTSC;
|
mov percounter1.LowPart,eax;
|
mov percounter1.HighPart,edx;
|
}
|
*/
|
time1 = (double)percounter1.QuadPart / perfreq.QuadPart;
|
// time2=(double)percounter2.QuadPart/perfreq.QuadPart ;
|
// timeinter1=time2-time1;
|
return (time1 * 1000);
|
};
|
|
CAnsiParser::CAnsiParser()
|
{
|
ClrScreen();
|
}
|
|
int CAnsiParser::AttachWnd(HWND hWnd)
|
{
|
CString s1;
|
if (!IsWindow(hWnd))
|
{
|
return false;
|
}
|
if (m_WndAttached)
|
{
|
Dettach();
|
}
|
GetClassName(hWnd, s1.GetBuffer(256), 256);
|
s1.ReleaseBuffer();
|
CWnd * pCWnd = CWnd::FromHandlePermanent(hWnd);
|
if (s1.Find(_T("Edit")) == 0)
|
{
|
if (pCWnd != NULL)
|
{
|
m_pWnd = (CEdit *)pCWnd;
|
m_AttachedType = 0;
|
}
|
else
|
{
|
ASSERT(CWnd::FromHandlePermanent(hWnd) == NULL);
|
|
if (hWnd == NULL)
|
return FALSE;
|
|
m_Wnd.Attach(hWnd);
|
m_pWnd = &m_Wnd;
|
m_AttachedType = 1;
|
}
|
m_pWnd->ModifyStyle(ES_AUTOHSCROLL | WS_HSCROLL, ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL | ES_SELECTIONBAR | ES_WANTRETURN);
|
m_pWnd->Invalidate();
|
m_pWnd->SetLimitText(10485760);
|
m_WndAttached = 1;
|
}
|
else if (s1.Find(_T("RichEdit")) == 0)
|
{
|
if (pCWnd != NULL)
|
{
|
m_pRichWnd = (CRichEditCtrl *)pCWnd;
|
m_AttachedType = 0;
|
}
|
else
|
{
|
ASSERT(CWnd::FromHandlePermanent(hWnd) == NULL);
|
|
if (hWnd == NULL)
|
return FALSE;
|
|
m_RichWnd.Attach(hWnd);
|
m_pRichWnd = &m_RichWnd;
|
m_AttachedType = 1;
|
}
|
m_pRichWnd->ModifyStyle(NULL, ES_MULTILINE | ES_AUTOVSCROLL | ES_SELECTIONBAR | ES_WANTRETURN);
|
m_pRichWnd->Invalidate();
|
m_WndAttached = 2;
|
}
|
|
return true;
|
}
|
|
int CAnsiParser::Dettach()
|
{
|
if (m_WndAttached == 1)
|
{
|
m_WndAttached = 0;
|
if (m_AttachedType == 1)
|
{
|
m_Wnd.Detach();
|
}
|
m_pWnd = NULL;
|
}
|
else if (m_WndAttached == 2)
|
{
|
m_WndAttached = 0;
|
if (m_AttachedType == 1)
|
{
|
m_RichWnd.Detach();
|
}
|
m_pRichWnd = NULL;
|
}
|
return 0;
|
}
|
|
int CAnsiParser::ClrScreen()
|
{
|
int clri;
|
for (clri = 0; clri < 200; clri++)
|
{
|
memset(screenT[clri], 0, 160);
|
}
|
for (clri = 0; clri < 200; clri++)
|
{
|
memset(screenM[clri], CurMode, 160);
|
}
|
|
for (clri = 0; clri < 200; clri++)
|
{
|
memset(screenT[clri], 32, 80);
|
}
|
return 0;
|
}
|
|
int CAnsiParser::Locate(int x, int y)
|
{
|
CursorX = x;
|
CursorY = y;
|
return 0;
|
}
|
|
int CAnsiParser::SetColor(int color)
|
{
|
|
return 0;
|
}
|
|
int CAnsiParser::SetScreenWH(int w, int h)
|
{
|
screenwidth = w;
|
screenheight = h;
|
return 0;
|
}
|
int CAnsiParser::PutIn(unsigned char * str, int len1)
|
{
|
if (len1 <= 0) return 0;
|
int ThisTime = (int)GetTickCountmS();
|
|
memcpy(InBuf + nBufDataLen, str, len1);
|
nBufDataLen += len1;
|
|
nNeedUpdateCount += len1;
|
if (nNeedUpdateCount > 320 || ThisTime - LastUpdateTime > 200) { bNeedUpdate = 1; }
|
int j;
|
for (j = 0; j < nBufDataLen; j++)
|
{
|
if (nBufDataLen - j < 1) { break; } //µÈ´ýÊý¾Ý
|
if (InBuf[j] == 27)
|
{
|
if (nBufDataLen - j < 10) { break; } //µÈ´ýÊý¾Ý
|
// continue;
|
if (InBuf[j + 1] == '[')
|
{
|
// AppendText("[");
|
if (InBuf[j + 2] == 's') { StoredX = CursorX; StoredY = CursorY; j += 2; continue; }//±£´æ¹â±êλÖÃ
|
if (InBuf[j + 2] == 'u') { CursorX = StoredX; CursorY = StoredY; j += 2; continue; }//»Ö¸´¹â±êλÖÃ
|
if (InBuf[j + 2] == '0') { if (InBuf[j + 3] == 'm') { CurMode = 0x70; j += 3; continue; } }//»Ö¸´ÊôÐÔ
|
if (InBuf[j + 2] == '2') { if (InBuf[j + 3] == 'J') { CursorX = 0; CursorY = 0; j += 3; ClrScreen(); continue; } }//ÇåÆÁ
|
if (InBuf[j + 2] == '1')
|
{
|
if (InBuf[j + 3] == ';')
|
if (InBuf[j + 4] == '3' || InBuf[j + 4] == '4')
|
if (InBuf[j + 5] >= '0'&&InBuf[j + 5] <= '7'&&InBuf[j + 6] == 'm')
|
{
|
CurMode = InBuf[j + 5]; j += 6; continue;
|
}
|
|
}
|
if (InBuf[j + 2] >= '0'&&InBuf[j + 2] <= '9')
|
{
|
if (InBuf[j + 3] == ';')
|
{
|
if (InBuf[j + 4] >= '0'&&InBuf[j + 4] <= '9')
|
{
|
if (InBuf[j + 5] == 'H') { CursorY = InBuf[j + 2] - 0x30 - 1; CursorX = InBuf[j + 4] - 0x30 - 1; j += 5; continue; }
|
if (InBuf[j + 5] >= '0'&&InBuf[j + 5] <= '9')
|
{
|
if (InBuf[j + 6] == 'H') { CursorY = InBuf[j + 2] - 0x31; CursorX = (InBuf[j + 4] - 0x30) * 10 + InBuf[j + 5] - 0x31; j += 6; continue; }
|
}
|
// AppendText("H1e");
|
}
|
// AppendText("H1e");
|
}
|
if (InBuf[j + 3] >= '0'&&InBuf[j + 3] <= '9')
|
{
|
if (InBuf[j + 4] == ';')
|
{
|
if (InBuf[j + 5] >= '0'&&InBuf[j + 5] <= '9')
|
{
|
if (InBuf[j + 6] == 'H') { CursorY = (InBuf[j + 2] - 0x30) * 10 + InBuf[j + 3] - 0x31; CursorX = InBuf[j + 5] - 0x31; j += 6; continue; }
|
if (InBuf[j + 6] >= '0'&&InBuf[j + 6] <= '9')
|
{
|
if (InBuf[j + 7] == 'H') { CursorY = (InBuf[j + 2] - 0x30) * 10 + InBuf[j + 3] - 0x31; CursorX = (InBuf[j + 5] - 0x30) * 10 + InBuf[j + 6] - 0x31; j += 7; continue; }
|
}
|
}
|
}
|
}
|
}
|
|
//ɾ³ý²»¿Éʶ±ðµÄ¿ØÖÆ´®,ÌØÕ÷ÊÇÒÔ×Öĸ½áÊø
|
int k = 0;
|
for (k = 0; k < 20; k++)
|
{
|
if (InBuf[j + 2 + k] >= 'a'&&InBuf[j + 2 + k] <= 'z' || InBuf[j + 2 + k] >= 'A'&&InBuf[j + 2 + k] <= 'Z')
|
{
|
break;
|
}
|
}
|
j += k + 2; continue;
|
}
|
//'ESC' ºóÃæ¸úµÄ²»ÊÇ'[',ÄÇôɾ³ýºóÒ»¸ö×Ö·û
|
j += 1; continue;
|
}
|
else if (InBuf[j] == 0) { }
|
else if (InBuf[j] < 32)
|
{
|
if (InBuf[j] == '\r')
|
{
|
CursorX = 1; CursorY++;
|
if (CursorY >= screenheight) { CursorY = 0; }
|
}
|
}
|
else {
|
screenT[CursorY][CursorX] = InBuf[j];
|
screenM[CursorY][CursorX] = CurMode;
|
CursorX++;
|
if (CursorX >= screenwidth) //×î´óÔÊÐíÖµÊÇ79
|
{
|
CursorX = 1;
|
CursorY++; if (CursorY >= screenheight) { CursorY = 0; }
|
}
|
}
|
// TextOut(hcurDC,16,16,s1,s1.GetLength());
|
}
|
//½«recv2ÒÆλµ½×î×ó¶Ë
|
if (j == nBufDataLen) { nBufDataLen = 0; }
|
else
|
{
|
nBufDataLen = nBufDataLen - j;
|
memmove(InBuf, InBuf + j, nBufDataLen);
|
}
|
if (bNeedUpdate) UpdateScreen();
|
return 0;
|
}
|
|
int CAnsiParser::UpdateScreen()
|
{
|
CString s1,s2;
|
s1.Empty();
|
int ThisTime = (int)GetTickCountmS();
|
for (int i = 0; i < 29; i++)
|
{
|
CStringA sA1 = screenT[i];
|
sA1.TrimRight();
|
AnsiToT(sA1, s2);
|
s1.Append(s2 + "\r\n");
|
}
|
// m_edit_display.UpdateData(FALSE);
|
// m_edit_display.UpdateWindow();
|
int nStart, nEnd;
|
if (m_WndAttached == 1)
|
{
|
m_pWnd->GetSel(nStart, nEnd);
|
m_pWnd->SetWindowText(s1);
|
m_pWnd->SetSel(nStart, nEnd);
|
}
|
else if (m_WndAttached == 2)
|
{
|
// m_pRichWnd->GetSel(nStart, nEnd);
|
m_pRichWnd->SetWindowText(s1);
|
// m_pRichWnd->SetSel(nStart, nEnd);
|
}
|
// m_edit2.LineIndex(0);
|
// m_edit2.SetSel(0, 99999999, 0);
|
// m_edit2.ReplaceSel(s1);
|
LastUpdateTime = ThisTime;
|
nNeedUpdateCount = 0;
|
bNeedUpdate = 0;
|
return 0;
|
}
|