zxd
2023-10-21 89cd7441c184d37f90d4d5311b348e938119765f
提交 | 用户 | age
0ed438 1 #include "pch.h"
65f713 2 #include "AnsiParser.h"
0ed438 3
Q 4 static inline int AnsiToT(CStringA & Src, CString & Dest)
5 {
6 #ifdef UNICODE
7     int j, l;
8     l = Src.GetLength();
9     j = MultiByteToWideChar(CP_ACP, 0, Src, l, Dest.GetBuffer(l), l);
10     Dest.ReleaseBuffer(j);
11 #else
12     Dest = Src;
13 #endif
14     return 0;
15 }
16 static inline double GetTickCountmS()
17 {
18     LARGE_INTEGER perfreq;
19     LARGE_INTEGER percounter1;    //,percounter2;
20     double time1;    //,time2,timeinter1;
21     QueryPerformanceFrequency(&perfreq);
22     QueryPerformanceCounter(&percounter1);
23     //    percounter1.QuadPart=rdtsc();
24     /*    _asm {
25                 RDTSC;
26                 mov percounter1.LowPart,eax;
27                 mov    percounter1.HighPart,edx;
28         }
29     */
30     time1 = (double)percounter1.QuadPart / perfreq.QuadPart;
31     //    time2=(double)percounter2.QuadPart/perfreq.QuadPart  ;
32     //    timeinter1=time2-time1;
33     return (time1 * 1000);
34 };
35
36 CAnsiParser::CAnsiParser()
37 {
38     ClrScreen();
39 }
40
41 int CAnsiParser::AttachWnd(HWND hWnd)
42 {
43     CString s1;
44     if (!IsWindow(hWnd))
45     {
46         return false;
47     }
48     if (m_WndAttached)
49     {
50         Dettach();
51     }
52     GetClassName(hWnd, s1.GetBuffer(256), 256);
53     s1.ReleaseBuffer();
54     CWnd * pCWnd = CWnd::FromHandlePermanent(hWnd);
55     if (s1.Find(_T("Edit")) == 0)
56     {
57         if (pCWnd != NULL)
58         {
59             m_pWnd = (CEdit *)pCWnd;
60             m_AttachedType = 0;
61         }
62         else
63         {
64             ASSERT(CWnd::FromHandlePermanent(hWnd) == NULL);
65
66             if (hWnd == NULL)
67                 return FALSE;
68
69             m_Wnd.Attach(hWnd);
70             m_pWnd = &m_Wnd;
71             m_AttachedType = 1;
72         }
73         m_pWnd->ModifyStyle(ES_AUTOHSCROLL | WS_HSCROLL, ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL | ES_SELECTIONBAR | ES_WANTRETURN);
74         m_pWnd->Invalidate();
75         m_pWnd->SetLimitText(10485760);
76         m_WndAttached = 1;
77     }
78     else if (s1.Find(_T("RichEdit")) == 0)
79     {
80         if (pCWnd != NULL)
81         {
82             m_pRichWnd = (CRichEditCtrl *)pCWnd;
83             m_AttachedType = 0;
84         }
85         else
86         {
87             ASSERT(CWnd::FromHandlePermanent(hWnd) == NULL);
88
89             if (hWnd == NULL)
90                 return FALSE;
91
92             m_RichWnd.Attach(hWnd);
93             m_pRichWnd = &m_RichWnd;
94             m_AttachedType = 1;
95         }
96         m_pRichWnd->ModifyStyle(NULL, ES_MULTILINE | ES_AUTOVSCROLL | ES_SELECTIONBAR | ES_WANTRETURN);
97         m_pRichWnd->Invalidate();
98         m_WndAttached = 2;
99     }
100
101     return true;
102 }
103
104 int CAnsiParser::Dettach()
105 {
106     if (m_WndAttached == 1)
107     {
108         m_WndAttached = 0;
109         if (m_AttachedType == 1)
110         {
111             m_Wnd.Detach();
112         }
113         m_pWnd = NULL;
114     }
115     else if (m_WndAttached == 2)
116     {
117         m_WndAttached = 0;
118         if (m_AttachedType == 1)
119         {
120             m_RichWnd.Detach();
121         }
122         m_pRichWnd = NULL;
123     }
124     return 0;
125 }
126
127 int CAnsiParser::ClrScreen()
128 {
129     int    clri;
130     for (clri = 0; clri < 200; clri++)
131     {
132         memset(screenT[clri], 0, 160);
133     }
134     for (clri = 0; clri < 200; clri++)
135     {
136         memset(screenM[clri], CurMode, 160);
137     }
138     
139     for (clri = 0; clri < 200; clri++)
140     {
141         memset(screenT[clri], 32, 80);
142     }
143     return 0;
144 }
145
146 int CAnsiParser::Locate(int x, int y)
147 {
148     CursorX = x;
149     CursorY = y;
150     return 0;
151 }
152
153 int CAnsiParser::SetColor(int color)
154 {
155
156     return 0;
157 }
158
159 int CAnsiParser::SetScreenWH(int w, int h)
160 {
161     screenwidth = w;
162     screenheight = h;
163     return 0;
164 }
165 int CAnsiParser::PutIn(unsigned char * str, int len1)
166 {
167     if (len1 <= 0) return 0;
168     int ThisTime = (int)GetTickCountmS();
169
170     memcpy(InBuf + nBufDataLen, str, len1);
171     nBufDataLen += len1;
172
173     nNeedUpdateCount += len1;
174     if (nNeedUpdateCount > 320 || ThisTime - LastUpdateTime > 200) { bNeedUpdate = 1; }
175     int j;
176     for (j = 0; j < nBufDataLen; j++)
177     {
178         if (nBufDataLen - j < 1) { break; } //等待数据
179         if (InBuf[j] == 27)
180         {
181             if (nBufDataLen - j < 10) { break; } //等待数据
182 //                continue;
183             if (InBuf[j + 1] == '[')
184             {
185                 //                    AppendText("[");
186                 if (InBuf[j + 2] == 's') { StoredX = CursorX; StoredY = CursorY; j += 2; continue; }//保存光标位置
187                 if (InBuf[j + 2] == 'u') { CursorX = StoredX; CursorY = StoredY; j += 2; continue; }//恢复光标位置
188                 if (InBuf[j + 2] == '0') { if (InBuf[j + 3] == 'm') { CurMode = 0x70; j += 3; continue; } }//恢复属性
189                 if (InBuf[j + 2] == '2') { if (InBuf[j + 3] == 'J') { CursorX = 0; CursorY = 0; j += 3; ClrScreen(); continue; } }//清屏
190                 if (InBuf[j + 2] == '1')
191                 {
192                     if (InBuf[j + 3] == ';')
193                         if (InBuf[j + 4] == '3' || InBuf[j + 4] == '4')
194                             if (InBuf[j + 5] >= '0'&&InBuf[j + 5] <= '7'&&InBuf[j + 6] == 'm')
195                             {
196                                 CurMode = InBuf[j + 5]; j += 6; continue;
197                             }
198
199                 }
200                 if (InBuf[j + 2] >= '0'&&InBuf[j + 2] <= '9')
201                 {
202                     if (InBuf[j + 3] == ';')
203                     {
204                         if (InBuf[j + 4] >= '0'&&InBuf[j + 4] <= '9')
205                         {
206                             if (InBuf[j + 5] == 'H') { CursorY = InBuf[j + 2] - 0x30 - 1; CursorX = InBuf[j + 4] - 0x30 - 1; j += 5; continue; }
207                             if (InBuf[j + 5] >= '0'&&InBuf[j + 5] <= '9')
208                             {
209                                 if (InBuf[j + 6] == 'H') { CursorY = InBuf[j + 2] - 0x31; CursorX = (InBuf[j + 4] - 0x30) * 10 + InBuf[j + 5] - 0x31; j += 6; continue; }
210                             }
211                             //                                AppendText("H1e");
212                         }
213                         //                            AppendText("H1e");
214                     }
215                     if (InBuf[j + 3] >= '0'&&InBuf[j + 3] <= '9')
216                     {
217                         if (InBuf[j + 4] == ';')
218                         {
219                             if (InBuf[j + 5] >= '0'&&InBuf[j + 5] <= '9')
220                             {
221                                 if (InBuf[j + 6] == 'H') { CursorY = (InBuf[j + 2] - 0x30) * 10 + InBuf[j + 3] - 0x31; CursorX = InBuf[j + 5] - 0x31; j += 6; continue; }
222                                 if (InBuf[j + 6] >= '0'&&InBuf[j + 6] <= '9')
223                                 {
224                                     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; }
225                                 }
226                             }
227                         }
228                     }
229                 }
230
231                 //删除不可识别的控制串,特征是以字母结束
232                 int k = 0;
233                 for (k = 0; k < 20; k++)
234                 {
235                     if (InBuf[j + 2 + k] >= 'a'&&InBuf[j + 2 + k] <= 'z' || InBuf[j + 2 + k] >= 'A'&&InBuf[j + 2 + k] <= 'Z')
236                     {
237                         break;
238                     }
239                 }
240                 j += k + 2; continue;
241             }
242             //'ESC' 后面跟的不是'[',那么删除后一个字符
243             j += 1; continue;
244         }
245         else if (InBuf[j] == 0)    {        }
246         else if (InBuf[j] < 32)
247         {
248             if (InBuf[j] == '\r')
249             {
250                 CursorX = 1; CursorY++;    
251                 if (CursorY >= screenheight) { CursorY = 0; }
252             }
253         }
254         else    {
255             screenT[CursorY][CursorX] = InBuf[j];
256             screenM[CursorY][CursorX] = CurMode;
257             CursorX++;
258             if (CursorX >= screenwidth)  //最大允许值是79
259             {
260                 CursorX = 1;
261                 CursorY++; if (CursorY >= screenheight) { CursorY = 0; }
262             }
263         }
264         //            TextOut(hcurDC,16,16,s1,s1.GetLength());
265     }
266     //将recv2移位到最左端
267     if (j == nBufDataLen) { nBufDataLen = 0; }
268     else
269     {
270         nBufDataLen = nBufDataLen - j;
271         memmove(InBuf, InBuf + j, nBufDataLen);
272     }
273     if (bNeedUpdate)    UpdateScreen();
274     return 0;
275 }
276
277 int CAnsiParser::UpdateScreen()
278 {
279     CString s1,s2;
280     s1.Empty();
281     int ThisTime = (int)GetTickCountmS();
282     for (int i = 0; i < 29; i++)
283     {
284         CStringA sA1 = screenT[i];
285         sA1.TrimRight();
286         AnsiToT(sA1, s2);
287         s1.Append(s2 + "\r\n");
288     }
289     //        m_edit_display.UpdateData(FALSE);
290     //        m_edit_display.UpdateWindow();
291     int nStart, nEnd;
292     if (m_WndAttached == 1)
293     {
294         m_pWnd->GetSel(nStart, nEnd);
295         m_pWnd->SetWindowText(s1);
296         m_pWnd->SetSel(nStart, nEnd);
297     }
298     else if (m_WndAttached == 2)
299     {
300 //        m_pRichWnd->GetSel(nStart, nEnd);
301         m_pRichWnd->SetWindowText(s1);
302 //        m_pRichWnd->SetSel(nStart, nEnd);
303     }
304     //        m_edit2.LineIndex(0);
305     //        m_edit2.SetSel(0, 99999999, 0);
306     //        m_edit2.ReplaceSel(s1);
307     LastUpdateTime = ThisTime;
308     nNeedUpdateCount = 0;
309     bNeedUpdate = 0;
310     return 0;
311 }