-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht1.cpp
175 lines (156 loc) · 3.63 KB
/
t1.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
#include <iostream>
#include <ncurses.h>
#include <getopt.h>
#include <string>
#include <stdexcept>
#include <vector>
using std::string;
using std::stoi;
using std::cerr;
using std::cout;
using std::endl;
using std::invalid_argument;
using std::out_of_range;
using std::vector;
const int ERROR_BAD_TUTORIAL_NUMBER = 1;
void PrintHelp() {
cerr << "Usage:\n";
cerr << "-h prints this help\n";
cerr << "-t n selects tutorial n - default is 0\n";
}
void HandleOptions(int argc, char ** argv, int & tutorial_number) {
int c;
while ((c = getopt(argc, argv, "ht:")) != -1) {
switch (c) {
case 'h':
PrintHelp();
exit(0);
case 't':
try {
tutorial_number = ::stoi(string(optarg));
} catch (const invalid_argument & ia) {
cerr << "Cannot convert to integer: " << optarg;
cerr << endl;
exit(ERROR_BAD_TUTORIAL_NUMBER);
} catch (const out_of_range & oor) {
cerr << "Value too large: " << optarg;
cerr << endl;
exit(ERROR_BAD_TUTORIAL_NUMBER);
}
break;
default:
// Use default of tutorial number 0.
break;
}
}
}
void T0();
void T1();
void T2();
void T3();
void T4();
void T5();
void T6();
void T7();
void RunTutorial(int);
vector<void (*)()> tutorials = {
T0, T1, T2, T3, T4, T5,
T6, T7
};
int main(int argc, char * argv[]) {
int tutorial_number = 0;
HandleOptions(argc, argv, tutorial_number);
RunTutorial(tutorial_number);
exit(0);
}
void RunTutorial(int tutorial_number) {
if (tutorial_number < 0 ||
tutorial_number >= int(tutorials.size())) {
cerr << "Tutorial number must be in the range of 0 to ";
cerr << tutorials.size() - 1 << endl;
exit(ERROR_BAD_TUTORIAL_NUMBER);
}
tutorials[tutorial_number]();
}
void T0() {
initscr();
endwin();
cout << "Nothing will have appeared to happen.\n";
}
void T1() {
initscr();
erase();
endwin();
cout << "Nothing will have appeared to happen.\n";
}
void T2() {
initscr();
erase();
refresh();
endwin();
cout << "Nothing will have appeared to happen.\n";
}
void T3() {
initscr();
erase();
addstr("Hit enter");
refresh();
getch();
endwin();
cout << "Screen will have erased and program will have waited\n";
cout << "for you to hit enter.\n";
}
void T4() {
initscr();
box(stdscr, 0, 0);
erase();
addstr("Hit enter");
refresh();
getch();
endwin();
cout << "Screen will have erased and program will have waited\n";
cout << "for you to hit enter. No box visible.\n";
}
void T5() {
initscr();
erase();
box(stdscr, 0, 0);
mvaddstr(0, 2, "Hit enter");
refresh();
getch();
endwin();
cout << "Screen will have:\n";
cout << "* erased\n";
cout << "* program will have waited for you to hit enter\n";
cout << "* a box was visible.\n";
}
void T6() {
initscr();
curs_set(0);
erase();
box(stdscr, 0, 0);
mvaddstr(0, 2, "Hit enter");
refresh();
getch();
endwin();
cout << "Screen will have:\n";
cout << "* erased\n";
cout << "* program will have waited for you to hit enter\n";
cout << "* a box was visible\n";
cout << "* cursor was invisible\n";
cout << "* cursor came back when program ended\n";
}
void T7() {
initscr();
curs_set(0);
erase();
box(stdscr, 0, 0);
mvaddstr(0, 2, "Hit enter");
refresh();
getch();
endwin();
cout << "Screen will have:\n";
cout << "* erased\n";
cout << "* program will have waited for you to hit enter\n";
cout << "* a box was visible.\n";
}