-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursary.cc
851 lines (764 loc) · 25.3 KB
/
cursary.cc
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#include <algorithm>
#include <bits/types/wint_t.h>
#include <cctype>
#include <cstddef>
#include <cstdio>
#include <linux/limits.h>
#include <sys/select.h>
#include <unistd.h>
#include <cstdlib>
#include <cstdlib>
#include <cwchar>
#include <fstream>
#include <string>
#include <filesystem>
#include <vector>
#include <ncurses.h>
#include <limits.h>
#include <iostream>
#include <chrono>
#define ctrl(x) (x & 0x1F)
using std::string;
using std::vector;
using std::ios;
using std::fstream;
using std::cerr;
using std::endl;
/**
* Stores all vocabulary and their amount
*/
struct VocInfo {
int vocNum;
vector<string> en;
vector<string> ja;
vector<string> furi;
};
const string opt1 = "Japanese -> English";
const string opt2 = "English -> Japanese";
const string opt3 = "Japanese <-> English";
const string opt4 = "Dictionaries";
const string opt5 = "Exit";
int corUTrans = 0; // number of correct user translations
/**
* Saves all vocs and their amount inside a struct
*
* @param dict Name of the dictionary file where all vocs are stored
* @return Struct containing all vocs and how many there are
*/
VocInfo getVocs(string dict) {
fstream dictFile (dict, ios::in);
VocInfo Vocs;
string line;
if (!dictFile) throw "File \""+dict+"\" not found.";
while (!dictFile.eof()) {
getline(dictFile,line);
if (dictFile.eof()) break;
while ( (line.empty()) && (!dictFile.eof()) ) getline(dictFile,line);
Vocs.en.push_back(line);
getline(dictFile,line);
Vocs.ja.push_back(line);
getline(dictFile,line);
Vocs.furi.push_back(line);
}
dictFile.close();
Vocs.vocNum = Vocs.en.size();
return Vocs;
}
/**
* Puts each translation of one word into a single element of a vector
*
* @param trans String containing the translation
* @return Vector containing all translations as single elements
*/
vector<string> partitionAllTrans(string trans) {
const char delim = ';';
vector<string> allTrans;
while (trans.find_first_of(delim) != -1) {
allTrans.push_back(trans.substr( 0,trans.find_first_of(delim) ));
trans.erase(0,trans.find_first_of(delim)+1);
}
allTrans.push_back(trans);
return allTrans;
}
/**
* Checks if user given translations are a subset of the accepted translations.
*
* @param uTrans User translations separated by semicolons
* @param trans Correct translations from dictionary file
* @return True if at least one user trans fits the dictionary file translations
*/
bool isSubSet(string uTrans, string trans) {
vector<string> vecUTrans = partitionAllTrans(uTrans);
vector<string> vecTrans = partitionAllTrans(trans);
int vocsRight = 0;
for (int i=0; i<vecUTrans.size(); ++i) {
for (int j=0; j<vecTrans.size();++j) {
std::transform(vecUTrans[i].begin(),vecUTrans[i].end(),vecUTrans[i].begin(),::tolower); // to lower case
std::transform(vecTrans[j].begin(),vecTrans[j].end(),vecTrans[j].begin(),::tolower); // to lower case
if (vecUTrans[i] == vecTrans[j]) {
++vocsRight;
break;
}
}
}
if (vocsRight == vecUTrans.size()) return true;
else return false;
}
/**
* Gets remaining translations that user did not know
*
* @param uTrans Translations entered by the user
* @param trans Correct translations from dictionary file
* @ return All remaining correct translations
*/
vector<string> getRemTrans(string uTrans, string trans) {
vector<string> vecUTrans = partitionAllTrans(uTrans);
vector<string> vecTrans = partitionAllTrans(trans);
vector<string> vecTransOgCase = vecTrans; /* dictionary translations vector with original case */
vector<string> remTrans = vecTrans; /* remaining translations the user did not know */
if (isSubSet(uTrans, trans)) {
for (int i=0; i<vecUTrans.size();++i) {
for (int j=0; j<=vecTrans.size();++j) {
std::transform(vecUTrans[i].begin(),vecUTrans[i].end(),vecUTrans[i].begin(),::tolower); // to lower case
std::transform(vecTrans[j].begin(),vecTrans[j].end(),vecTrans[j].begin(),::tolower); // to lower case
if (vecUTrans[i] == vecTrans[j]) {
remTrans.erase(remTrans.begin()+j);
vecTrans.erase(vecTrans.begin()+j);
break;
}
}
if (vecTrans.size() == 0) break;
}
}
return remTrans;
}
/**
* Creates a user input box around a given window
*
* @param winName The window where the user input box is positioned
*/
void mkInputBox(WINDOW * winName) {
int left,right,top,bottom,tlc,trc,blc,brc;
top = left = right = tlc = trc = blc = brc = 23; //space
bottom = 0;
wborder(winName, left, right, top, bottom, tlc, trc, blc, brc);
wrefresh(winName);
}
void mkRisingPoints(){
noecho(); curs_set(false);
string point = "1";
int y = 30;
WINDOW * xp = newwin(10, 1, 20, 40);
refresh();
init_pair(2, COLOR_GREEN, COLOR_BLACK);
wattron(xp,COLOR_PAIR(2));
while (y>21) {
--y;
wmove(xp, y, 1);
wprintw(xp,point.c_str());
wrefresh(xp);
wmove(xp, y, 0);
usleep(1E4);
wclrtoeol(xp);
}
wattroff(xp,COLOR_PAIR(2));
wclear(xp);
}
/**
* Queries the user for a single japanese to english translation
*
* @param queries Window containing the japanese vocabulary that is to be translated by the user
* @param reply Window containing information whether the user translation is correct or not
* @param uInput Window where the user enters his translation
* @param Vocs Structure containing all vocabulary and their amount
* @param idx Index of the vocabulary to be queried
* @param curVoc Number of current vocabulary (to show how many words were queried so far)
* @return Number which is -1 if ctrl(o) is pressed (go back to main menu) and 0 else
*/
int queryJaToEn(WINDOW * queries, WINDOW * reply, WINDOW * uInput, WINDOW * userStats, VocInfo Vocs, int idx, int curVoc) {
curs_set(true); cbreak(); nonl(); noecho(); intrflush(stdscr, false); keypad(uInput, true);
refresh();
/* colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
/* colors */
int queriesWidth = 60;
int userStatsHeight = 6;
int userStatsWidth = 20;
int maxInputLen = 25;
char uTrans[maxInputLen];
std::fill(uTrans, uTrans+maxInputLen, 0);
bool isFuriVisible = false;
/* print query */
string ja = Vocs.ja[idx];
string en = Vocs.en[idx];
string furi = Vocs.furi[idx];
wattron(queries,COLOR_PAIR(1));
mvwprintw(queries, 1, queriesWidth/2 - ja.length()/3, ja.c_str()); // divided by 6 because one ja char has a length of 3
wattroff(queries,COLOR_PAIR(1));
wrefresh(queries);
/* print query */
wmove(uInput, 0, 1);
/* get user input */
for (int i=0;i<maxInputLen;++i) {
char u = wgetch(uInput);
if (u == 13) break;
else if (u == ctrl('o')) return -1;
else if ( (int) u == ctrl(KEY_BACKSPACE) ) {
if (i>0) {
wmove(uInput, 0, i);
wclrtoeol(uInput);
uTrans[i] = 0; // empty corresponding uTrans entry
i-=2;
}
}
/* clear the whole line and delete all saved data in uTrans */
else if (u == ctrl('n')) {
wmove(uInput, 0, 0);
wclrtoeol(uInput);
wmove(uInput, 0, 1);
std::fill(uTrans, uTrans+maxInputLen, 0); // reset uTrans
i = -1;
}
/* toggle furigana visibility */
else if (u == ctrl('f')) {
curs_set(false);
if (isFuriVisible) {
wattron(queries, A_INVIS);
isFuriVisible = false;
} else isFuriVisible = true;
(furi.empty()) ? : mvwprintw(queries, 0, queriesWidth/2-furi.length()/3-1, "[%s]",furi.c_str()); // only print if not empty
wattroff(queries, A_INVIS);
wrefresh(queries);
i -= 1;
}
else {
wrefresh(uInput);
curs_set(true);
uTrans[i] = u;
wprintw(uInput, "%c",u);
}
}
/* get user input */
wclear(reply);
/* getting all remaining translations and storing them in a string separated by semicolons */
string remainTrans;
string delim = ";";
vector<string> remTrans = getRemTrans(uTrans, en);
for (int i=0;i<remTrans.size();++i) (i==0) ? remainTrans += remTrans[i] : remainTrans += delim+remTrans[i];
/* getting all remaining translations and storing them in a string separated by semicolons */
/* if translation is correct */
if (isSubSet(uTrans, en)) {
++corUTrans;
wattron(reply, COLOR_PAIR(2));
box(reply, 0, 0);
wattroff(reply, COLOR_PAIR(2));
string remRply; // all remaining replies
int startReplyIdx; // index for where reply text in reply window shall start
(remTrans.size()>0) ? remRply = "also correct:" : remRply = "correct";
string totReply = remRply+" "+remainTrans;
startReplyIdx = queriesWidth/2-totReply.length()/2;
/* checking if reply text fits inside of reply window and if not adjust reply text */
if (startReplyIdx < 1){
string lenAdjstRply = totReply.substr(0,queriesWidth-6)+"..."; // length adjusted string, as it otherwise would not fit in reply window
mvwprintw(reply, 2, 1, lenAdjstRply.c_str());
}
else {
mvwprintw(reply, 2, startReplyIdx, totReply.c_str());
}
/* checking if reply text fits inside of reply window and if not adjust reply text */
}
/* if translation is correct */
/* if translation is false */
else {
wattron(reply, COLOR_PAIR(1));
box(reply, 0, 0);
wattroff(reply, COLOR_PAIR(1));
string jpReply;
(furi.empty()) ? (jpReply = ja) : (jpReply = ja+" ["+furi+"]");
int jpReplyIdx = queriesWidth/2 - jpReply.length()/2;
wattron(reply, COLOR_PAIR(1));
mvwprintw(reply, 1, jpReplyIdx, jpReply.c_str());
wattroff(reply, COLOR_PAIR(1));
int enReplyIdx = queriesWidth/2-remainTrans.length()/2;
/* checking if reply text fits inside of reply window and if not adjust reply text */
if (enReplyIdx < 1){
string lenAdjstRply = remainTrans.substr(0,queriesWidth-5)+"..."; // length adjusted string, as it otherwise would not fit in reply window
mvwprintw(reply, 3, 1, lenAdjstRply.c_str());
}
else {
mvwprintw(reply, 3, enReplyIdx, remainTrans.c_str());
}
/* checking if reply text fits inside of reply window and if not adjust reply text */
}
/* if translation is false */
wrefresh(reply);
wclear(queries);
refresh();
wmove(uInput, 0, 0); wclrtoeol(uInput);
if (*uTrans == ctrl('o')) return -1;
/* fill stats window */
/* box */
wattron(userStats, COLOR_PAIR(4));
box(userStats, 0, 0);
wattroff(userStats, COLOR_PAIR(4));
/* box */
/* header */
mvwprintw(userStats, 0, 2, "Statistics");
/* header */
string userStatsMessage = "Total: ";
wattron(userStats, COLOR_PAIR(2));
mvwprintw(userStats, userStatsHeight/2, userStatsWidth/2-1, "%d",corUTrans);
wattroff(userStats, COLOR_PAIR(2));
wprintw(userStats, "/");
wprintw(userStats, "%d",curVoc+1);
mvwprintw(userStats, userStatsHeight-1, 1, "%s%d",userStatsMessage.c_str(),Vocs.vocNum);
wrefresh(userStats);
/* fill stats window */
return 0;
}
/**
* Queries the user for a single english to japanese translation
*
* @param queries Window containing the english vocabulary that is to be translated by the user
* @param reply Window containing information whether the user translation is correct or not
* @param uInput Window where the user enters his translation
* @param Vocs Structure containing all vocabulary and their amount
* @param idx Index of the vocabulary to be queried
* @param curVoc Number of current vocabulary (to show how many words were queried so far)
* @return Number which is -1 if ctrl(o) is pressed (go back to main menu) and 0 else
*/
int queryEnToJa(WINDOW * queries, WINDOW * reply, WINDOW * uInput, WINDOW * userStats, VocInfo Vocs, int idx, int curVoc) {
curs_set(true); cbreak(); echo(); nonl(); intrflush(stdscr, false); keypad(uInput, true);
refresh();
/* colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
/* colors */
int queriesWidth = 60;
int userStatsHeight = 6;
int userStatsWidth = 20;
int maxInputLen = 25;
char uTrans[maxInputLen];
std::fill(uTrans, uTrans+maxInputLen, 0);
/* print query */
string en = Vocs.en[idx];
string ja = Vocs.ja[idx];
string furi = Vocs.furi[idx];
wattron(queries,COLOR_PAIR(1));
//mvwprintw(queries, 1, queriesWidth/2-en.length()/2, en.c_str());
int startQryIdx = queriesWidth/2-en.length()/2;
/* checking if query text fits inside of query window and if not adjust query text */
if (startQryIdx < 1){
string lenAdjstQry = en.substr(0,queriesWidth-5)+"..."; // length adjusted string, as it otherwise would not fit in query window
mvwprintw(queries, 1, 1, lenAdjstQry.c_str());
}
else {
mvwprintw(queries, 1, startQryIdx, en.c_str());
}
/* checking if query text fits inside of query window and if not adjust query text */
wattroff(queries,COLOR_PAIR(1));
wrefresh(queries);
/* print query */
wmove(uInput, 0, 1);
/* get user input */
for (int i=0;i<maxInputLen;++i) {
char u = wgetch(uInput);
if (u == 13) break;
else if (u == ctrl('o')) return -1;
else if ((int) u == ctrl(KEY_BACKSPACE)) {
wmove(uInput, 0, 0);
wclrtoeol(uInput);
wmove(uInput, 0, 1);
std::fill(uTrans, uTrans+maxInputLen, 0); // reset uTrans
i = -1;
}
/* clear the whole line and delete all saved data in uTrans */
else if (u == ctrl('n')) {
wmove(uInput, 0, 0);
wclrtoeol(uInput);
wmove(uInput, 0, 1);
std::fill(uTrans, uTrans+maxInputLen, 0); // reset uTrans
i = -1;
}
else uTrans[i] = u;
}
/* get user input */
wclear(reply);
if (isSubSet(uTrans, ja)) {
++corUTrans;
string answer0 = "correct";
wattron(reply, COLOR_PAIR(2));
box(reply, 0, 0);
wattroff(reply, COLOR_PAIR(2));
mvwprintw(reply, 2, queriesWidth/2-answer0.length()/2, answer0.c_str());
}
else if ( isSubSet(uTrans, furi) && (!furi.empty()) ) {
++corUTrans;
string kanjiExis = "kanji notation: ";
wattron(reply, COLOR_PAIR(2));
box(reply, 0, 0);
wattroff(reply, COLOR_PAIR(2));
wmove(reply, 2, queriesWidth/2-kanjiExis.length()/2-ja.length()/6-ja.length()/6);
wprintw(reply, kanjiExis.c_str());
wattron(reply,COLOR_PAIR(1));
wprintw(reply, ja.c_str());
wattroff(reply,COLOR_PAIR(1));
}
else {
wattron(reply, COLOR_PAIR(1));
box(reply, 0, 0);
wattroff(reply, COLOR_PAIR(1));
int jpReplyIdx;
int enReplyIdx;
string jpReply;
(furi.empty()) ? (jpReply=ja) : (jpReply=ja+" ["+furi+"]");
jpReplyIdx = queriesWidth/2 - jpReply.length()/2;
mvwprintw(reply, 3, jpReplyIdx, jpReply.c_str());
enReplyIdx = queriesWidth/2-en.length()/2;
wattron(reply, COLOR_PAIR(1));
/* checking if reply text fits inside of reply window and if not adjust reply text */
if (enReplyIdx < 1){
string lenAdjstRply = en.substr(0,queriesWidth-5)+"..."; // length adjusted string, as it otherwise would not fit in reply window
mvwprintw(reply, 1, 1, lenAdjstRply.c_str());
}
else {
mvwprintw(reply, 1, enReplyIdx, en.c_str());
}
/* checking if reply text fits inside of reply window and if not adjust reply text */
wattroff(reply, COLOR_PAIR(1));
}
wrefresh(reply);
wclear(queries);
refresh();
wmove(uInput, 0, 0); wclrtoeol(uInput);
if (*uTrans == ctrl('o')) return -1;
/* fill stats window */
/* box */
wattron(userStats, COLOR_PAIR(4));
box(userStats, 0, 0);
wattroff(userStats, COLOR_PAIR(4));
/* box */
/* header */
mvwprintw(userStats, 0, 2, "Statistics");
/* header */
string userStatsMessage = "Total: ";
wattron(userStats, COLOR_PAIR(2));
mvwprintw(userStats, userStatsHeight/2, userStatsWidth/2-1, "%d", corUTrans);
wattroff(userStats, COLOR_PAIR(2));
wprintw(userStats, "/");
wprintw(userStats, "%d",curVoc);
mvwprintw(userStats, userStatsHeight-1, 1, "%s%d",userStatsMessage.c_str(),Vocs.vocNum);
wrefresh(userStats);
/* fill stats window */
return 0;
}
/**
* Queries the user for mixed translations, meaning english to japanese and vice versa
*
* @param queries Window containing the english or japanese vocabulary that is to be translated by the user
* @param reply Window containing information whether the user translation is correct or not
* @param uInput Window where the user enters his translation
* @param Vocs Structure containing all vocabulary and their amount
* @param idx Index of the vocabulary to be queried
* @param curVoc number of current vocabulary (to show how many words were queried so far)
* @return Number which is -1 if ctrl(o) is pressed (go back to main menu) and 0 else
*/
int queryMixed(WINDOW * queries, WINDOW * reply, WINDOW * uInput, WINDOW * userStats, VocInfo Vocs, int idx, int curVoc) {
curs_set(true); cbreak(); echo(); nonl(); intrflush(stdscr, false); keypad(stdscr, true);
refresh();
/* randomly choose to query either ja->en or en->ja */
int rndm = rand() % 2;
int status;
if (rndm == 0) status = queryJaToEn(queries, reply, uInput, userStats, Vocs, idx, curVoc);
else status = queryEnToJa(queries, reply, uInput, userStats, Vocs, idx, curVoc);
/* randomly choose to query either ja->en or en->ja */
wclear(queries);
wmove(uInput, 0, 0); wclrtoeol(uInput);
return status;
//refresh();
}
/**
* Queries the user for all vocabulary found in the dictionary file
*
* @param dict Name of the dictionary file
* @param uOption Query option selected by user (english to japanese, japanese to english or mixed)
*/
void queryAll(string dict,int uOption) {
cbreak(); echo(); nonl(); intrflush(stdscr, false); keypad(stdscr, true);
clear();
/* colors */
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
/* colors */
int maxY, maxX; getmaxyx(stdscr, maxY, maxX);
/* frame with option name */
attron(COLOR_PAIR(3));
box(stdscr, 0, 0);
attroff(COLOR_PAIR(3));
/* frame with option name */
/* queries window */
int queriesHeight, queriesWidth, queriesPosY, queriesPosX;
queriesHeight = 3;
queriesWidth = 60;
queriesPosY = maxY/4-queriesHeight/2;
queriesPosX = maxX/2-queriesWidth/2;
WINDOW * queries = newwin(queriesHeight, queriesWidth, queriesPosY, queriesPosX);
refresh();
/* queries window */
/* reply window */
int replyHeight, replyWidth, replyY, replyX;
replyHeight = 5;
replyWidth = 60;
replyY = 2*maxY/4-replyHeight/2;
replyX = maxX/2-replyWidth/2;
WINDOW * reply = newwin(replyHeight, replyWidth, replyY, replyX);
refresh();
/* reply window */
/* user statistics window */
int userStatsHeight, userStatsWidth, userStatsY, userStatsX;
userStatsY = maxY/16;
userStatsX = 6*maxX/7;
userStatsHeight = 7;
userStatsWidth = 20;
WINDOW * userStats = newwin(userStatsHeight, userStatsWidth, userStatsY, userStatsX);
refresh();
/* user statistics window */
/* user results window */
int resultsHeight, resultsWidth, resultsY, resultsX;
userStatsY = maxY/2-resultsHeight/2;
userStatsX = maxX/2-resultsWidth/2;
userStatsHeight = 12;
userStatsWidth = 25;
WINDOW * results = newwin(resultsHeight, resultsWidth, resultsY, resultsX);
refresh();
box(results, 0, 0);
wrefresh(results);
/* user results window */
/* user input */
int uInputHeight = 2; int uInputWidth = 30;
int uInputPosY = 3*maxY/4-uInputHeight/2; int uInputPosX = (maxX-uInputWidth)/2;
WINDOW * uInput = newwin(uInputHeight, uInputWidth, uInputPosY, uInputPosX);
refresh();
mkInputBox(uInput);
/* user input */
VocInfo Vocs = getVocs(dict);
/* to query in a random order */
srand(time(NULL)); // init random seed based on sys time
vector<int> indexes;
for (int t=0; t<Vocs.vocNum;++t) indexes.push_back(t);
std::random_shuffle(indexes.begin(),indexes.end());
/* to query in a random order */
int status = 0;
if (uOption == 0) {
wattron(stdscr, COLOR_PAIR(3));
mvwprintw(stdscr,0, 2, opt1.c_str());
wattroff(stdscr, COLOR_PAIR(3));
for (int i=0; i<Vocs.vocNum; ++i) {
status = queryJaToEn(queries, reply, uInput, userStats, Vocs, indexes[i],i);
if (status == -1) break;
}
}
if (uOption == 1) {
wattron(stdscr, COLOR_PAIR(3));
mvwprintw(stdscr,0, 2, opt2.c_str());
wattroff(stdscr, COLOR_PAIR(3));
for (int i=0; i<Vocs.vocNum; ++i) {
status = queryEnToJa(queries, reply, uInput, userStats, Vocs, indexes[i],i);
if (status == -1) break;
}
}
if (uOption == 2) {
wattron(stdscr, COLOR_PAIR(3));
mvwprintw(stdscr,0, 2, opt3.c_str());
wattroff(stdscr, COLOR_PAIR(3));
for (int i=0; i<Vocs.vocNum; ++i) {
status = queryMixed(queries, reply, uInput, userStats, Vocs, indexes[i],i);
if (status == -1) break;
}
}
if (status != -1) getch();
corUTrans = 0;
}
/**
* Creates a menu where the user selects a query type (english to japanese, japanese to english, mixed)
*
* @param opts Window holding all options
* @param choices The options which the user can select
*/
int selectionMenu(WINDOW * opts, vector<string> choices) {
keypad(opts, true);
/* colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
/* colors */
int selected = 0;
while (true) {
for (int i=0;i<choices.size();++i) {
if (selected == i) wattron(opts, COLOR_PAIR(1));
mvwprintw(opts, (opts->_maxy+1)/5+2*i, 1, choices[i].c_str());
wattroff(opts, COLOR_PAIR(1));
}
int uDir = wgetch(opts);
if ( (uDir==(int) 'k') || (uDir==KEY_UP) ) {
--selected;
if (selected == -1) selected = choices.size()-1;
}
else if ( (uDir==(int) 'j') || (uDir==KEY_DOWN) ) {
++selected;
if (selected == choices.size()) selected = 0;
}
if (uDir == 13) break;
}
return selected;
}
/**
* Creates window showing all dictionary files and lets user choose one
* @param projectDir The main project directory
* @return Name of the selected dictionary file
*/
string dictSelect(string projectDir) {
int y,x;
int dictSelectH, dictSelectW;
int choice;
vector<string> dicts;
string dictsDir = "/dicts";
getmaxyx(stdscr, y, x);
projectDir += dictsDir;
/* get all file names in dictionary subfolder */
for (const auto & entry : std::filesystem::directory_iterator(projectDir)){
string dictFileName = entry.path();
dictFileName.erase(0,dictFileName.find_last_of('/')+1);
dicts.push_back(dictFileName);
}
/* get all file names in dictionary subfolder */
/* dictionary select window */
dictSelectW = 20;
dictSelectH = 2*dicts.size()+3;
WINDOW * dictsSelect = newwin(dictSelectH, dictSelectW, y/2-dictSelectH, 2*x/3);
refresh();
box(dictsSelect, 0, 0);
wrefresh(dictsSelect);
mvwprintw(dictsSelect, 0, 2, "Dictionaries");
/* dictionary select window */
choice = selectionMenu(dictsSelect, dicts);
werase(dictsSelect);
return dicts[choice];
}
/**
* Creates the window containing the option select window
*
* @param query1 Query type prompting japanese to english translations
* @param query2 Query type prompting english to japanese translations
* @param query3 Query type prompting mixed translations
* @param exit Option to quit the program
* @return Number of the selected option
*/
int mkOptsWin(string query1, string query2, string query3, string exit, string dicts){
curs_set(false); cbreak(); noecho(); nonl(); intrflush(stdscr, false); keypad(stdscr, true);
clear();
/* colors */
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
/* colors */
int maxY, maxX; getmaxyx(stdscr, maxY, maxX);
int optsWidth = query3.length()+7;
int optsHeight = 13;
WINDOW* opts = newwin(optsHeight, optsWidth, maxY/2-optsHeight, maxX/2-optsWidth/2);
refresh();
/* box with name */
wattron(opts, COLOR_PAIR(3));
box(opts, 0, 0);
string tag = "Options";
mvwprintw(opts, 0, optsWidth/2-tag.length()/2-1, tag.c_str());
wattroff(opts, COLOR_PAIR(3));
/* box with name */
wrefresh(opts);
//string choices[] = {query1,query2,query3,opt4};
vector<string> choices;
choices.push_back(query1); choices.push_back(query2); choices.push_back(query3); choices.push_back(exit);
choices.push_back(dicts);
return selectionMenu(opts, choices);
}
/**
* Creates the start up window
*
* @param name The name of the program
* @param subtitle A subtitle
*/
void mkStartWin(string name, string subtitle) {
/* colors */
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
/* colors */
/* header */
noecho(); curs_set(false);
int horPadding = 8;
int titleHeight = 5; int titleWidth = name.length()+horPadding;
int maxY, maxX; getmaxyx(stdscr, maxY, maxX);
int titlePosY = 2*maxY/5-titleHeight/2; int titlePosX = maxX/2-titleWidth/2;
int curPosY = 0;
while (curPosY <= titlePosY) {
clear();
WINDOW * title = newwin(titleHeight, titleWidth, curPosY, titlePosX);
++curPosY;
refresh();
/* box */
wattron(title, COLOR_PAIR(3));
box(title, 0,0);
wattroff(title, COLOR_PAIR(3));
wrefresh(title);
/* box */
/* box text */
wattron(title,COLOR_PAIR(1)); wattron(title, A_BOLD);
mvwprintw(title, titleHeight/2, horPadding/2, name.c_str());
wattroff(title, A_BOLD); wattroff(title,COLOR_PAIR(1));
wrefresh(title);
/* box text */
usleep(2E4);
}
/* header */
/* sub header */
int lenEnter = subtitle.length();
attron(A_BLINK);
mvprintw(titlePosY+titleHeight, maxX/2-(lenEnter+8)/2, subtitle.c_str());
printw(" (");
attron(COLOR_PAIR(3));
printw("Enter");
attroff(COLOR_PAIR(3));
printw(")");
attroff(A_BLINK);
int uChar = getch();
while (uChar != 13) uChar = getch();
/* sub header */
}
int main(int argc, char** argv) {
vector<string> dicts;
char buffer[250];
string path = __FILE__;
int pos = path.find("/cursary.cc");
size_t len = path.copy(buffer, pos, 0);
buffer[len] = '\0';
string dictSubDir = "/dicts/";
string dictFile = dictSubDir+"enja.txt";
string dict = buffer + dictFile;
setlocale(LC_ALL, "");
initscr(); cbreak(); noecho(); nonl(); intrflush(stdscr, false); keypad(stdscr, true); curs_set(false);
if (!has_colors()) throw "TERMINAL DOES NOT SUPPORT COLORS.";
try {
start_color();
mkStartWin("Cursary: Your Friendly Neighborhood Voc Trainer", "Insert Coin");
while (true) {
char uOption = mkOptsWin(opt1,opt2,opt3,opt4,opt5);
if (uOption == 4) break;
else if (uOption == 3) dict = buffer+dictSubDir+dictSelect(buffer);
else if ( (uOption==0)||(uOption==1)||(uOption==2) ) queryAll(dict,uOption);
else continue;
}
}
catch (string message) {
endwin();
cerr << message << endl;
return -1;
}
endwin();
return 0;
}