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
|
/******************************************************************************
* Copyright (C) 2015 Alejandro Colomar Andrés *
* SPDX-License-Identifier: GPL-2.0-only *
******************************************************************************/
/******************************************************************************
******* headers **************************************************************
******************************************************************************/
#include "mine-sweeper/save/score.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ALX_NO_PREFIX
#include <libalx/base/errno/error.h>
#include <libalx/base/compiler/size.h>
#include <libalx/base/stdio/printf/sbprintf.h>
#include <libalx/base/stdio/seekc.h>
#include "mine-sweeper/game/core.h"
#include "mine-sweeper/game/iface.h"
#include "mine-sweeper/player/iface.h"
#include "mine-sweeper/save/save.h"
/******************************************************************************
******* macros ***************************************************************
******************************************************************************/
/******************************************************************************
******* variables ************************************************************
******************************************************************************/
/******************************************************************************
******* static functions *****************************************************
******************************************************************************/
/******************************************************************************
******* global functions *****************************************************
******************************************************************************/
void save_score (const struct Game_Iface_Score *game_iface_score)
{
struct tm *date_format;
char *fname;
char player_name[BUFSIZ];
FILE *fp;
time_t date_secs;
/* File name */
switch (game_iface_score->level) {
case GAME_IFACE_LVL_BEGINNER:
fname = HISCORES_BEGINNER_FILE;
break;
case GAME_IFACE_LVL_INTERMEDIATE:
fname = HISCORES_INTERMEDIATE_FILE;
break;
case GAME_IFACE_LVL_EXPERT:
fname = HISCORES_EXPERT_FILE;
break;
}
/* Date & time */
date_secs = time(NULL);
date_format = localtime(&date_secs);
/* Player name (foo is default) */
player_iface_score_name(player_name);
/* Write to file (append) */
errno = 0;
fp = fopen(fname, "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_fp:
perrorx(fname);
}
void print_scores (void)
{
char cmd[_POSIX_ARG_MAX];
if (sbprintf(cmd, NULL, "less %s %s %s", HISCORES_BEGINNER_FILE,
HISCORES_INTERMEDIATE_FILE,
HISCORES_EXPERT_FILE)) {
goto err;
}
if (system(cmd))
goto err;
return;
err:
perrorx(cmd);
}
/******************************************************************************
******* static functions *****************************************************
******************************************************************************/
/******************************************************************************
******* end of file **********************************************************
******************************************************************************/
|