]> git.localhorst.tv Git - blank.git/blob - src/runtime.cpp
get world seed from command line arguments
[blank.git] / src / runtime.cpp
1 #include "runtime.hpp"
2
3 #include "app.hpp"
4
5 #include <cctype>
6 #include <cstdlib>
7 #include <iostream>
8
9 using namespace std;
10
11
12 namespace blank {
13
14 Runtime::Runtime()
15 : name("blank")
16 , mode(NORMAL)
17 , n(0)
18 , t(0)
19 , seed(0) {
20
21 }
22
23
24 void Runtime::ReadArgs(int argc, const char *const *argv) {
25         if (argc <= 0) return;
26         name = argv[0];
27
28         bool options = true;
29         bool error = false;
30
31         for (int i = 1; i < argc; ++i) {
32                 const char *arg = argv[i];
33                 if (!arg || arg[0] == '\0') {
34                         cerr << "warning: found empty argument at position " << i << endl;
35                         continue;
36                 }
37                 if (options && arg[0] == '-') {
38                         if (arg[1] == '\0') {
39                                 cerr << "warning: incomplete option list at position " << i << endl;
40                         } else if (arg[1] == '-') {
41                                 if (arg[2] == '\0') {
42                                         // stopper
43                                         options = false;
44                                 } else {
45                                         // long option
46                                         cerr << "unknown option " << arg << endl;
47                                         error = true;
48                                 }
49                         } else {
50                                 // short options
51                                 for (int j = 1; arg[j] != '\0'; ++j) {
52                                         switch (arg[j]) {
53                                                 case 'n':
54                                                         ++i;
55                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
56                                                                 cerr << "missing argument to -n" << endl;
57                                                                 error = true;
58                                                         } else {
59                                                                 n = strtoul(argv[i], nullptr, 10);
60                                                         }
61                                                         break;
62                                                 case 's':
63                                                         ++i;
64                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
65                                                                 cerr << "missing argument to -s" << endl;
66                                                                 error = true;
67                                                         } else {
68                                                                 seed = strtoul(argv[i], nullptr, 10);
69                                                         }
70                                                         break;
71                                                 case 't':
72                                                         ++i;
73                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
74                                                                 cerr << "missing argument to -t" << endl;
75                                                                 error = true;
76                                                         } else {
77                                                                 t = strtoul(argv[i], nullptr, 10);
78                                                         }
79                                                         break;
80                                                 case '-':
81                                                         // stopper
82                                                         options = false;
83                                                         break;
84                                                 default:
85                                                         cerr << "unknown option " << arg[j] << endl;
86                                                         error = true;
87                                                         break;
88                                         }
89                                 }
90                         }
91                 } else if (isdigit(arg[0])) {
92                         // positional number interpreted as -n
93                         n = strtoul(arg, nullptr, 10);
94                 } else {
95                         cerr << "unable to interpret argument "
96                                 << i << " (" << arg << ")" << endl;
97                         error = true;
98                 }
99         }
100
101         if (error) {
102                 mode = ERROR;
103                 return;
104         }
105
106         if (n > 0) {
107                 if (t > 0) {
108                         mode = FIXED_FRAME_LIMIT;
109                 } else {
110                         mode = FRAME_LIMIT;
111                 }
112         } else if (t > 0) {
113                 mode = TIME_LIMIT;
114         } else {
115                 mode = NORMAL;
116         }
117 }
118
119 int Runtime::Execute() {
120         if (mode == ERROR) {
121                 return 1;
122         }
123
124         Application app(seed);
125         switch (mode) {
126                 default:
127                 case NORMAL:
128                         app.Run();
129                         break;
130                 case FRAME_LIMIT:
131                         app.RunN(n);
132                         break;
133                 case TIME_LIMIT:
134                         app.RunT(t);
135                         break;
136                 case FIXED_FRAME_LIMIT:
137                         app.RunS(n, t);
138                         break;
139         }
140         return 0;
141 }
142
143 }