-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
204 lines (157 loc) · 4.02 KB
/
main.cpp
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
//test headers
#include "IniFileHandler.h"
#include "Logger.h"
#include "FixedTest.h"
#include "Com.h"
#include "Window.h"
#include "Interpreter.h"
#include "Tools.h"
#include <windows.h>
#include <vector>
#include <conio.h>
#include <thread>
void start();
int createGUI(HINSTANCE hInstance, int nCmdShow);
int parseCmdParameters();
int parsePort();
string sFilePath, sPort;
vector<string> svParameters;
bool bContinue = true;
int iError;
Tools tools;
Interpreter interpreter;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
{
char c;
bool bStopTest = false;
iError = ERROR_SUCCESS;
int _iError = ERROR_SUCCESS;
int iCount = 0;
if (0 == strcmp(pCmdLine, "") )
_iError = createGUI(hInstance, nCmdShow);
else
{
svParameters = tools.parseCmdLine(pCmdLine);
_iError = parseCmdParameters();
if (_iError == ERROR_SUCCESS)
{
if (!AllocConsole())
MessageBoxA(NULL, "console allocation error", WINDOW_TITLE, MB_OK | MB_ICONERROR);
cout << "starting the thread" << endl;
thread t1 (&start);
t1.detach();
cout << "Test in progress" << endl;
cout << "Press ESC to stop the test" << endl;
while(bContinue && !bStopTest)
{
c = getch();
//27 for esc key
if (c == 27)
{
interpreter.stopTest();
cout << "Quiting test" << endl;
bStopTest = true;
cout << "Testing stopped. Press enter to finish" << endl;
//getchar();
}
iCount++;
}
cout << "Testing finished" << endl;
FreeConsole();
return iError;
}
else
{//error parsing parameters
tools.showCmdHelp();
return _iError;
}
}
return _iError;
}
void start()
{
iError = interpreter.loadIniFile(sFilePath, sPort);
bContinue = false;
cout << "Testing finished. Press enter to finish" << endl;
}
//------------------------------------------------------------------------------
// Parse the cmd line parameters to start testing
// Return:error code signaling if operation succeded or sytax error
//------------------------------------------------------------------------------
int parseCmdParameters()
{
if (svParameters.size() > 2)
{
return ERROR_CMD_SYNTAX;
}
else
{
sFilePath = svParameters.at(0);
//is 2 parameters, then second is a port
if( 2 == svParameters.size() )
{
string sTemp;
sTemp = svParameters.at(1);
if(sTemp.at(0) == '/')
{
if (ERROR_SUCCESS == parsePort() )
{
sPort = sTemp.substr(1, sTemp.npos);
return ERROR_SUCCESS;
}
else
return ERROR_CMD_SYNTAX;
}
else
return ERROR_CMD_SYNTAX;
}
else
sPort = "";
}
return ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// parses the given port from the cmd line
// Port -> /COMxxx where 0 < xxx < 257
// Return: _iError code signaling if operation succeded or _iError
//------------------------------------------------------------------------------
int parsePort()
{
// sPort = /COMXXX
string sTemp = svParameters.at(1).substr(1, sTemp.npos);
//sTemp = COMXXX
if("COM" == sTemp.substr(0, 3) )
{
string sNumber = sTemp.substr(3, sTemp.npos);
int iNumber = strtol(sNumber.c_str(), NULL, 10);
if (iNumber > 0 && iNumber < 257)
return ERROR_SUCCESS;
}
return ERROR_CMD_SYNTAX;
}
//------------------------------------------------------------------------------
// starts the GUI
// Parameters:
// IN:
// - HINSTANCE hInstance -> handle to the instance
// - int nCmdShow -> format for how to show the window
// Return: _iError code signaling if operation succeded or _iError
//------------------------------------------------------------------------------
int createGUI(HINSTANCE hInstance, int nCmdShow)
{
Window win;
win.sethInstance(hInstance);
if (!win.Create(WINDOW_TITLE , WS_OVERLAPPEDWINDOW))
{
return ERROR_CREATE_GUI;
}
ShowWindow(win.window(), nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return ERROR_SUCCESS;
}