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
|
/******************************************************************************
* Copyright (C) 2015 Alejandro Colomar Andrés *
* SPDX-License-Identifier: GPL-2.0-only *
******************************************************************************/
/******************************************************************************
******* headers **************************************************************
******************************************************************************/
/* Standard C ----------------------------------------------------------------*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Project -------------------------------------------------------------------*/
#include "game.h"
#include "game_iface.h"
#include "player_iface.h"
#include "save.h"
#include "score.h"
/******************************************************************************
******* macros ***************************************************************
******************************************************************************/
# define BUFF_SIZE (1024)
# define BUFF_SIZE_TEXT (1048576)
/******************************************************************************
******* variables ************************************************************
******************************************************************************/
char var_path [FILENAME_MAX];
char var_hiscores_path [FILENAME_MAX];
char var_boards_beginner_path [FILENAME_MAX];
char var_boards_intermediate_path [FILENAME_MAX];
char var_boards_expert_path [FILENAME_MAX];
char var_boards_custom_path [FILENAME_MAX];
char var_hiscores_beginner_name [FILENAME_MAX];
char var_hiscores_intermediate_name [FILENAME_MAX];
char var_hiscores_expert_name [FILENAME_MAX];
/******************************************************************************
******* static functions *****************************************************
******************************************************************************/
static void snprint_scores_file (char *dest, int destsize, char *file_name);
/******************************************************************************
******* global functions *****************************************************
******************************************************************************/
void score_init (void)
{
if (snprintf(var_path, FILENAME_MAX, "%s/%s/",
INSTALL_VAR_DIR, VAR_DIR) >= FILENAME_MAX) {
goto err_path;
}
if (snprintf(var_hiscores_path, FILENAME_MAX, "%s/%s/",
var_path, HISCORES_DIR) >= FILENAME_MAX) {
goto err_path;
}
if (snprintf(var_boards_beginner_path, FILENAME_MAX, "%s/%s/",
var_path, BOARDS_BEGINNER_DIR) >= FILENAME_MAX) {
goto err_path;
}
if (snprintf(var_boards_intermediate_path, FILENAME_MAX, "%s/%s/",
var_path, BOARDS_INTERMEDIATE_DIR) >= FILENAME_MAX) {
goto err_path;
}
if (snprintf(var_boards_expert_path, FILENAME_MAX, "%s/%s/",
var_path, BOARDS_EXPERT_DIR) >= FILENAME_MAX) {
goto err_path;
}
if (snprintf(var_boards_custom_path, FILENAME_MAX, "%s/%s/",
var_path, BOARDS_CUSTOM_DIR) >= FILENAME_MAX) {
goto err_path;
}
snprintf(var_hiscores_beginner_name, FILENAME_MAX, "%s",
HISCORES_BEGINNER_NAME);
snprintf(var_hiscores_intermediate_name, FILENAME_MAX, "%s",
HISCORES_INTERMEDIATE_NAME);
snprintf(var_hiscores_expert_name, FILENAME_MAX, "%s",
HISCORES_EXPERT_NAME);
return;
err_path:
printf("Path is too large and has been truncated\n");
getchar();
exit(EXIT_FAILURE);
}
void save_score (const struct Game_Iface_Score *game_iface_score)
{
struct tm *date_format;
char file_name [FILENAME_MAX];
char player_name [BUFF_SIZE];
FILE *fp;
time_t date_secs;
/* File name */
switch (game_iface_score->level) {
case GAME_IFACE_LEVEL_BEGINNER:
if (snprintf(file_name, FILENAME_MAX, "%s/%s",
var_hiscores_path,
var_hiscores_beginner_name) >=
FILENAME_MAX) {
goto err_path;
}
break;
case GAME_IFACE_LEVEL_INTERMEDIATE:
if (snprintf(file_name, FILENAME_MAX, "%s/%s",
var_hiscores_path,
var_hiscores_intermediate_name) >=
FILENAME_MAX) {
goto err_path;
}
break;
case GAME_IFACE_LEVEL_EXPERT:
if (snprintf(file_name, FILENAME_MAX, "%s/%s",
var_hiscores_path,
var_hiscores_expert_name) >=
FILENAME_MAX) {
goto err_path;
}
break;
}
/* Date & time */
date_secs = time(NULL);
date_format = localtime(&date_secs);
/* Player name (foo is default) */
player_iface_score_name(player_name, BUFF_SIZE);
/* Write to file (append) */
errno = 0;
fp = fopen(file_name, "a");
if (!fp)
goto err_fp;
fprintf(fp, "\n");
fprintf(fp, "name %s\n", player_name);
fprintf(fp, "date %i\n", (int)date_secs);
fprintf(fp, "{\n");
fprintf(fp, " isdst %i\n", date_format->tm_isdst);
fprintf(fp, " yday %i\n", date_format->tm_yday);
fprintf(fp, " wday %i\n", date_format->tm_wday);
fprintf(fp, " year %i\n", date_format->tm_year);
fprintf(fp, " mon %i\n", date_format->tm_mon);
fprintf(fp, " mday %i\n", date_format->tm_mday);
fprintf(fp, " hour %i\n", date_format->tm_hour);
fprintf(fp, " min %i\n", date_format->tm_min);
fprintf(fp, " sec %i\n", date_format->tm_sec);
fprintf(fp, "}\n");
fprintf(fp, "time %i\n", game_iface_score->time);
fprintf(fp, "clicks %i\n", game_iface_score->clicks);
fprintf(fp, "file %s\n", saved_name);
fclose(fp);
return;
err_path:
printf("Path is too large and has been truncated\n");
printf("Score could not be saved!\n");
return;
err_fp:
printf("errno: %i", errno);
printf("Score could not be saved!\n");
return;
}
void snprint_scores (char *dest, int destsize)
{
char file_name [FILENAME_MAX];
char tmp1 [BUFF_SIZE_TEXT];
char tmp2 [BUFF_SIZE_TEXT];
/* Beginner */
if (snprintf(file_name, FILENAME_MAX, "%s/%s",
var_hiscores_path,
var_hiscores_beginner_name) >=
FILENAME_MAX) {
goto err_path;
}
(void)snprint_scores_file(dest, destsize, file_name);
/* Intermediate */
if (snprintf(file_name, FILENAME_MAX, "%s/%s",
var_hiscores_path,
var_hiscores_intermediate_name) >=
FILENAME_MAX) {
goto err_path;
}
(void)snprint_scores_file(tmp1, destsize, file_name);
(void)snprintf(tmp2, destsize, "%s%s", dest, tmp1);
(void)snprintf(dest, destsize, "%s", tmp2);
/* Expert */
if (snprintf(file_name, FILENAME_MAX, "%s/%s",
var_hiscores_path,
var_hiscores_expert_name) >=
FILENAME_MAX) {
goto err_path;
}
(void)snprint_scores_file(tmp1, destsize, file_name);
(void)snprintf(tmp2, destsize, "%s%s", dest, tmp1);
(void)snprintf(dest, destsize, "%s", tmp2);
return;
err_path:
printf("Path is too large and has been truncated\n");
printf("Score could not be shown!\n");
}
/******************************************************************************
******* static functions *****************************************************
******************************************************************************/
static void snprint_scores_file (char *dest, int destsize, char *file_name)
{
FILE *fp;
int c;
char tmp [BUFF_SIZE_TEXT];
char title [BUFF_SIZE];
char name [BUFF_SIZE];
int year;
int mon;
int day;
int time;
int hours;
int mins;
int secs;
int clicks;
char file [FILENAME_MAX];
fp = fopen(file_name, "r");
if (!fp)
return;
/* Title */
fscanf(fp, "%[^\n]s", title);
/* For some reason, a space after "%[^\n]s" doesn't skip spaces */
fscanf(fp, " ");
/* Print */
(void)snprintf(dest, destsize,
"_______________________________________________________\n"
"%s\n\n"
"name date clicks time file\n\n",
title);
while ((c = getc(fp)) != EOF) {
ungetc(c, fp);
/* Read */
fscanf(fp, "name %s ", name);
fscanf(fp, "date %*i ");
fscanf(fp, "{ ");
fscanf(fp, " isdst %*i ");
fscanf(fp, " yday %*i ");
fscanf(fp, " wday %*i ");
fscanf(fp, " year %i ", &year);
fscanf(fp, " mon %i ", &mon);
fscanf(fp, " mday %i ", &day);
fscanf(fp, " hour %*i ");
fscanf(fp, " min %*i ");
fscanf(fp, " sec %*i ");
fscanf(fp, "} ");
fscanf(fp, "time %i ", &time);
fscanf(fp, "clicks %i ", &clicks);
fscanf(fp, "file %s ", file);
/* Adjust date & time */
year += 1900;
hours = (time / 3600);
mins = ((time % 3600) / 60);
secs = (time % 60);
/* Print */
(void)snprintf(tmp, BUFF_SIZE_TEXT,
"%s"
"%s\n"
" %4i/%2i/%2i %i %i:%02i:%02i \t%s\n\n",
dest,
name,
year, 1 + mon, day,
clicks,
hours, mins, secs,
file);
(void)snprintf(dest, BUFF_SIZE_TEXT, "%s", tmp);
}
fclose(fp);
}
/******************************************************************************
******* end of file **********************************************************
******************************************************************************/
|