summaryrefslogtreecommitdiffstats
path: root/src/CBot/tests/old TstCBot/TstCBotView.cpp
blob: 3ee9094b34007f15c12dff529df159898f2c44af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// TstCBotView.cpp : implementation of the CTstCBotView class
//

#include "stdafx.h"
#include "TstCBot.h"

#include "TstCBotDoc.h"
#include "TstCBotView.h"
#include "BotConsoleDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTstCBotView

IMPLEMENT_DYNCREATE(CTstCBotView, CView)

BEGIN_MESSAGE_MAP(CTstCBotView, CView)
	//{{AFX_MSG_MAP(CTstCBotView)
	ON_WM_SIZE()
	ON_COMMAND(ID_CP1, OnCp1)
	ON_COMMAND(ID_EXE, OnExe)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTstCBotView construction/destruction

CTstCBotView::CTstCBotView()
{
	// TODO: add construction code here
	m_pEdit = NULL;
	m_pProg = NULL;
}

CTstCBotView::~CTstCBotView()
{
}

BOOL CTstCBotView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

void CTstCBotView::OnActivateView( BOOL bActivate, CView* pActivateView, CView* pDeactiveView )
{
	if ( m_pEdit == NULL)
	{
		m_pEdit = new CEdit();
		CRect		rect;
		GetClientRect( rect );

		m_pEdit->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN|ES_NOHIDESEL|ES_AUTOVSCROLL, 
						 rect, this, IDC_EDIT1 );
		m_pEdit->SetTabStops(12);
		LoadEdition("CBotTest.txt");
		m_pEdit->SetFocus();
	}
}


/////////////////////////////////////////////////////////////////////////////
// CTstCBotView drawing

void CTstCBotView::OnDraw(CDC* pDC)
{
	CTstCBotDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTstCBotView diagnostics

#ifdef _DEBUG
void CTstCBotView::AssertValid() const
{
	CView::AssertValid();
}

void CTstCBotView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CTstCBotView message handlers

void CTstCBotView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);

	if ( m_pEdit != NULL )
	{
		CRect		rect;
		GetClientRect( rect );
		m_pEdit->MoveWindow( rect );
		m_pEdit->SetFocus();
	}	
}

void CTstCBotView::SaveEdition(const char* filename)
{
	CString		program;

	m_pEdit->GetWindowText(program);

	FILE*	pf = fopen(filename, "wb");
	if (pf==NULL) return;

	fputs (program, pf);
	fclose(pf);
}

void CTstCBotView::LoadEdition(const char* filename)
{
	CString		program("{ int x = 10000; while (x > 0) x = x-1; }");

	FILE*	pf = fopen(filename, "r");
	if (pf!=NULL)
	{
		char	buffer[10000];
		program.Empty();

		while (NULL != fgets (buffer, 100000, pf))
		{
			program += buffer;
			program = program.Left(program.GetLength()-1) + "\r\n";
		}

		fclose(pf);
	}

	m_pEdit->SetWindowText(program);
}



// compile le programme
#include <stdio.h>

void CTstCBotView::OnCp1() 
{
    CString		program;

	SaveEdition("CBotTest.txt");

	m_pEdit->GetWindowText(program);

	CString			TextError;
	int				code, start, end;
	
	if ( m_pProg == NULL ) m_pProg = new CBotProgram();

	CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp();

	if (m_pProg->Compile(program, pApp->m_Liste))
	{
		CString done = "Compilation sans erreur.\nLes fonctions suivantes sont externes:\n";

		for ( int i = 0; i < pApp->m_Liste.RetSize(); i++)
		{
			done += CString(pApp->m_Liste[i]) + "\n";
		}

		AfxMessageBox( done );
	}
	else
	{
		m_pProg->GetError(code, start, end);
		delete m_pProg;
		m_pProg = NULL;

		m_pEdit->SetSel( start, end );
		m_pEdit->SetFocus();				// met en �vidence la partie avec probl�me

		TextError.LoadString( code );
		if (TextError.IsEmpty())
		{
			char	buf[100];
			sprintf(buf, "Erreur num�ro %d.", code);
			TextError = buf;
		}
		AfxMessageBox( TextError );
	}

	m_pEdit->SetFocus();
}


//////////////////////////////////////////////////////


void CTstCBotView::OnExe() 
{
	CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp();

	if( m_pProg	== NULL)
	{
		AfxMessageBox("Pas de programme compil� !");
		return;
	}

	if( pApp->m_Liste.RetSize() == 0 )
	{
		AfxMessageBox("Aucune fonction marqu�e \"extern\" !");
		return;
	}



	CBotConsoleDlg dlg;
	dlg.DoModal();				// dialogue pour faire la console

	if ( dlg.m_code>0 )
	{
		CString	TextError;

		m_pEdit->SetSel( dlg.m_start, dlg.m_end );
		m_pEdit->SetFocus();				// met en �vidence la partie avec probl�me

		TextError.LoadString( dlg.m_code );
		if (TextError.IsEmpty())
		{
			char	buf[100];
			sprintf(buf, "Erreur num�ro %d.", dlg.m_code);
			TextError = buf;
		}
//		AfxMessageBox( TextError );
	}

	m_pEdit->SetFocus();

	return;
}



void CTstCBotView::OnFileSave() 
{
	// TODO: Add your command handler code here
	SaveEdition("CBotTest.txt");
}

void CTstCBotView::OnFileSaveAs() 
{
	CFileDialog		*pDlg;
	CString			s;
	
	pDlg = new CFileDialog(FALSE, "TXT", NULL,
						   OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY,
						   "cboxtest|*.txt", this);
	if ( pDlg == NULL )  return;
	
	if ( pDlg->DoModal() == IDOK )  // choix du fichier ...
	{
		SaveEdition(pDlg->GetPathName());
	}

	delete pDlg;	
}

#if 0
void test()
{
	int	y,z;

	for (;;);
	for (x = 0; y = 1; z = 3) int q = 6;
	for (int x = 0; int y = 1; int z = 3) int q = 6;
	// pour voir
}
#endif