-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
284 lines (265 loc) · 7.77 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
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
#define _CRT_SECURE_NO_DEPRECATE
#include "main.h"
#include "Zhn_cpp.h"
void ASMtestdet(int &x32, uint64_t &x64 ,int & r32,uint64_t & r64) {
r32 = 1 << x32;
r64 = (uint64_t)1 << x32;
x64 = 1;
x64 <<= x32;
cout << "0" << oct << r32 <<" 0"<<r64<<" 0"<<x64<< dec << endl;
}
void ASMtest() {
//uint32_t res,res2;
//int x32=40,r32;
}
// catching time as seconds+millis (seconds since year 1970)
long GetTimeMillis() {
struct _timeb tbuf;
_ftime64_s(&tbuf);
return ((long)(1000 * tbuf.time) + tbuf.millitm);
}
// builing an appropriate message depending on the elapsed time te-ts
void PrintTime(long ts,long te){
UINT dt=te-ts,dtmil=dt%1000,dts=dt/1000,dth=dts/3600; dth=dth%1000;
cerr << endl<<"total elapsed time ";
UINT dtm=dts/60; dts=dts%60 , dth=dtm/60, dtm=dtm%60;
if(dth) cerr <<dth<<"h ";
if(dth || dtm) cerr <<dtm<<"m ";
cerr <<dts <<"s ";
if(dtmil<10) cerr << "00"; else if(dtmil<100) cerr << '0';
cerr <<dtmil<<"ms "<<endl; return;
}
void PrintTimeCout(long ts, long te){
UINT dt = te - ts, dtmil = dt % 1000, dts = dt / 1000, dth = dts / 3600; dth = dth % 1000;
cout << endl << "total elapsed time ";
UINT dtm = dts / 60; dts = dts % 60, dth = dtm / 60, dtm = dtm % 60;
if (dth) cout << dth << "h ";
if (dth || dtm) cout << dtm << "m ";
cout << dts << "s ";
if (dtmil<10) cout << "00"; else if (dtmil<100) cout << '0';
cout << dtmil << "ms " << endl; return;
}
int Search_ccd(char * ww)
{ // List of 2 char command, 9 commands
const char * ccd[]={"-i" , // input name including extension
"-o" , // output or second filename
"-c", // main command option + check boxes
"-v" , // value 0 to 9 default 0
"-b" , // bit field 0 to 9 default is 0
"-s", // strings 0 to 9
}; // puzzle processing specificities
char wt[4];
strncpy_s(wt,4,ww,2);
wt[2]=0;
for(int i=0;i<6;i++)
if(!strcmp(wt,ccd[i]))
return i;
return -1;
}
//#include "Go_0.cpp"
SGO sgo;
extern void Go_0();
extern FINPUT finput;
int main(int narg, char *argv[]) {
cerr << "mainstart" << endl;
if (0) {
ASMtest(); return 0;
}
long tdeb=GetTimeMillis();
char * finput_name=0,*foutput_name=0,* ww;
char * s_strings[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };//optionnal 10 strings
uint32_t command = 0,
vx[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, //integers 0 to 9
bfx[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };// bit fields
for(int i=0;i<narg;i++) {
ww = argv[i];
int ir=Search_ccd(ww);
if(ir<0) continue;
if (ir == 3){// -vn-xxxx
if (ww[3] - '-') continue; //must be -vn- with n 0_9
int ind=ww[2] - '0';
if (ind < 0 || ind>9)continue;
vx[ind] = atoi(&ww[4]);
continue;
}
else if (ir == 4){// -bn- followed by a bit field bits rigth to left max 8 bits
if (ww[3] - '-') continue;
int ind = ww[2] - '0';
if (ind < 0 || ind>9)continue;
int length = (int)strlen(&ww[4]);
if (length > 8)continue;
for (int i = 0; i < length; i++) if (ww[4 + i] == '1') bfx[ind] |= (1 << i);
}
else if (ir == 5){// -sn- followed by a string
if (ww[3] - '-') continue; //must be -vn- with n 0_9
int ind = ww[2] - '0';
if (ind < 0 || ind>9)continue;
s_strings[ind] = &ww[4];
}
else{
switch (ir) {
case 0: finput_name = &ww[2]; break; // -i
case 1: foutput_name = &ww[2]; break; // -o
case 2: command = atoi(&ww[2]); break; //-c
}// end command
}
}// end loop on options
if(finput_name) cerr <<" file1 (input) " << finput_name<<endl;
if(foutput_name) cerr <<" file2 (output) " << foutput_name<<endl;
cerr << "command " << command<<endl;
// store command line parameters
sgo.command = command;
sgo.bfx = bfx;
sgo.finput_name = finput_name;
sgo.foutput_name = foutput_name;
sgo.s_strings = s_strings;
sgo.vx = vx;
Go_0();
cerr << " print cout time " << endl;
long tfin=GetTimeMillis();
PrintTimeCout(tdeb,tfin);
PrintTime(tdeb, tfin);
return 0;
}
void SGO::ParseInt(char * ze, int delimiter){
// bfx[0] 1 to 8 parameters
memset(tparse, 0, sizeof tparse);
nparse = 0;
if (!bfx[0]) return;
//cout << ze << "go parse delimiter "<<(char) delimiter << endl;
char * w = ze,temp[20];
int pos = 0, i = -1;
while (1){// scan the entry
++i;
if (i > 200) break; // safety code
if (w[i] == delimiter || w[i]==0){
//cout << &w[i] << " seen param" << endl;
int n = i - pos; // length of the parameter
if (n > 15 || n < 1)goto next;
if (!(bfx[0] & (1 << nparse))) goto next;
strncpy(temp, &w[pos], n); temp[n] = 0;
//cout << temp << "parse" << endl;
tparse[nparse] = atoi_nodot(temp);
next:
if (!w[i]) return;
if (++nparse > 7)return;
pos = i + 1;
}
}
return;
}
void SGO::Parse_zin() {
strcpy(zinparsed, finput.ze);
nitems = 1; items[0] = zinparsed;
int ll = (int)strlen(zinparsed);
for (int i = 81; i < ll; i++) {
if (!zinparsed[i]) return;
if (zinparsed[i] == ';') {
items[nitems++] = &zinparsed[i + 1];
zinparsed[i] = 0;
}
}
}
int SGO::atoi_nodot(char * o) {// ignore dot in entry
//this is to convert serate a.b in integer 10a+b
int ll = (int)strlen(o), n = 0;
char zs[10];
if (ll > 8)ll = 8;
for (int i = 0; i < ll; i++) if (o[i] - '.') zs[n++] = o[i];
zs[n] = 0;
return atoi(zs);
}
int SGO::Canonical_serate(char * d, char * o) {// send back aa.b or a.b
int ctl = 0;
while (o[0] < '0' || o[0]>'9') {
o++; // kill leading not digit
if (++ctl > 3) return 0;
}
d[0] = o[0]; d[1] = o[1]; d[2] = o[2];
if (o[1] == '.') {//a.??
if (d[2]<'0' || d[2]>'9')d[2] = '0';
return 3;
}
else if (o[2] == '.') {//aa.
d[3] = o[3];
if (d[3]<'0' || d[3]>'9')d[3] = '0';
return 4;
}
else {
if (d[1]<'0' || d[1]>'9') {
d[1] = '.'; d[2] = '0';
return 3;
}
else {
d[2] = '.'; d[3] = '0';
return 4;
}
}
}
int SGO::Canonical_EREPED() {
strcpy(zinparsed, finput.ze);
strcpy(&zinparsed[81], " ED=");
int ll = (int)strlen(finput.ze);
for (int i = 81; i < ll - 9; i++) {// locate first ED=
if (finput.ze[i] - 'E') continue;
char * w = &finput.ze[i];
if (!strncpy(w, "ED=", 3)) return 0;
w += 3;
char * wd = &zinparsed[85];
int ir = Canonical_serate(wd, w);
if (!ir) return 0; //failed to recognize
wd += ir; *wd++ = '/'; // ER done
while (w[0] != '/') {
if (!w[0])return 0;// missing ep ed
w++;
}
w++; //skip /
ir = Canonical_serate(wd, w);
if (!ir) return 0; //failed to recognize
wd += ir; *wd++ = '/'; // EP done
while (w[0] != '/') {
if (!w[0])return 0;// missing ep ed
w++;
}
w++; //skip /
ir = Canonical_serate(wd, w);
if (!ir) return 0; //failed to recognize
wd += ir; *wd = 0; // finished
return 1; // now zeparsed in canonical form
}
return 0; // failed to recognize
}
int SGO::Canonical_401_11() {
strcpy(zinparsed, finput.ze);
// fout1 << zinparsed << "studied" << endl;
zinparsed[81] = ';';
int ll = (int)strlen(finput.ze);
for (int i = 81; i < ll - 9; i++) {// locate first ED=
if (finput.ze[i] - 'E') continue;
char * w = &finput.ze[i];
if (!strncpy(w, "ED=", 3)) return 0;
w += 3;
char * wd = &zinparsed[82];
int ir = Canonical_serate(wd, w);
if (!ir) return 0; //failed to recognize
wd += ir; *wd++ = ';'; // ER done
while (w[0] != '/') {
if (!w[0])return 0;// missing ep ed
w++;
}
w++; //skip /
ir = Canonical_serate(wd, w);
if (!ir) return 0; //failed to recognize
wd += ir; *wd++ = ';'; // EP done
while (w[0] != '/') {
if (!w[0])return 0;// missing ep ed
w++;
}
w++; //skip /
ir = Canonical_serate(wd, w);
if (!ir) return 0; //failed to recognize
wd += ir; *wd = 0; // finished
return 1; // now zeparsed in canonical form
}
return 0; // failed to recognize
}