aboutsummaryrefslogtreecommitdiff
path: root/src/calculer.c
blob: 705fb7423f2d8ed36a41d6cf47bcf4232c669aa9 (plain)
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
/*
 * Copyright (C) 2024  Tristan Riehs
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "disp.h"

static int lower = 0;
static int upper = 100;

static int ms;

static FILE *logfile;
static char logbuf[64];
#define LOGBUF_OFFSET 11

/* Return a random number x such that lower <= x < upper. */
static int
calculer_rand(void)
{
	return lower + rand()%(upper-lower);
}

/* Compute the number of milliseconds TP contains. */
static intptr_t
tp_ms(struct timespec const *tp)
{
	return tp->tv_sec*1000 + tp->tv_nsec/1000000;
}

/* Called just before prompting the user. The returned pointer will be given
   back as a parameter of measure_after. */
static void *
measure_before(void)
{
	struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	return (void *)tp_ms(&tp);
}

/* Called just after the user prompt has been read and checked. */
int
measure_after(void *data, int correct)
{
	int t0 = (intptr_t) data;
	struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	int t1 = tp_ms(&tp);
	ms = t1 - t0;
	size_t bytes_written =
		LOGBUF_OFFSET + sprintf(logbuf + LOGBUF_OFFSET,
					"%d\t%d\n", ms, correct);

	return bytes_written != fwrite(logbuf, 1, bytes_written, logfile);
}

/* Only use in print_usage. */
#define print_opt_noarg(opt, desc) \
	fprintf(stream, "  -"opt"\t\t"desc"\n")

/* Only use in print_usage. */
#define print_opt_arg(opt, arg, desc) \
	fprintf(stream, "  -"opt" "arg"\t"desc"\n")

void
print_usage(FILE *stream)
{
	fprintf(stream, "Usage: calculer [OPTIONS]\n");
	fprintf(stream, "OPTIONS\n");
	print_opt_noarg("V", "Print version and exit.");
	print_opt_noarg("h", "Print help and exit.");
	print_opt_arg("l", "LOGFILE", "Use LOGFILE as log file.\n");
	print_opt_arg("m", "MIN",
		      "Set MIN as the minimum value numbers can take.\n");
	print_opt_arg("M", "MAX",
		      "Set MAX as the maximum value numbers can take.\n");
	print_opt_arg("i", "INTERFACE={cli, tui, gui}",
		      "\n\t\tSelect the interface type, the default is \
\"gui\".");
	fprintf(stream, "Consult the calculer(1) manual page for further \
information.\n");
}

void
print_help(void)
{
	printf("calculer -- train mental calculus\n");
	printf("calculer is licensed under the GPL3 License.\n");
	print_usage(stdout);
}

void
print_version(void)
{
	printf("%s\n", VERSION);
}

void
set_disp_name(char **dest, char const *optarg)
{
	if (strcmp(optarg, "cli") == 0)
	{
		*dest = DISP_CLI;
	}
	else if (strcmp(optarg, "gui") == 0)
	{
		*dest = DISP_RAY;
	}
	else if (strcmp(optarg, "tui") == 0)
	{
		*dest = DISP_TUI;
	}
	else
	{
		fprintf(stderr, "calculer: invalid interface \"%s\"\n", optarg);
		print_usage(stderr);
		exit(1);
	}
}

int
main(int argc, char *argv[])
{
	char *logpath = "./calculer.log";
	char *disp_name = DISP_RAY;
	struct disp *disp;
	srand(time(NULL));

	/* Store the prefix "YYYY-MM-DD" in LOGBUF. */
	time_t now = time(NULL);
	struct tm *tm = localtime(&now);
	size_t bytes_written = strftime(logbuf, sizeof(logbuf), "%F\t", tm);
	assert(bytes_written);

	int opt;
	while ((opt = getopt(argc, argv, ":Vhl:m:M:i:")) != -1)
	{
		switch (opt)
		{
		case 'V':
			print_version();
			exit(0);
		case 'h':
			print_help();
			exit(0);
		case 'l':
			logpath = optarg;
			break;
		case 'm':
			lower = atoi(optarg);
			break;
		case 'M':
			upper = atoi(optarg);
			break;
		case 'i':
			set_disp_name(&disp_name, optarg);
			break;
		case ':':
			fprintf(stderr,
				"calculer: option %d requires an argument\n",
				optopt);
			print_usage(stderr);
			exit(1);
		default:
			print_usage(stderr);
			exit(1);
		}
	}

	logfile = fopen(logpath, "a");
	assert(logfile);	/* TODO: handle non-fatal error properly */
	disp = get_disp(disp_name);
	disp->init();

	while (1)
	{
		int x, y, res;
		int input;

		x = calculer_rand();
		y = calculer_rand();
		res = x+y;
		disp->display_calc(x, y);
		void *measure_data = measure_before();
		input = disp->read_input();
		if ((input == DISP_QUIT) || (input == DISP_ERR))
		{
			destroy_disp(disp);
			fclose(logfile);
			exit(0);
		}
#ifndef NDEBUG
		else if (input == DISP_RELOAD)
		{
			disp_reload(disp);
			continue;
		}
#endif
		measure_after(measure_data, input == res);
		disp->display_res(input == res, ms);
	}

	fclose(logfile);
	return 0;
}