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
|
/******************************************************************************
* Copyright (C) 2015 Alejandro Colomar Andrés *
* SPDX-License-Identifier: GPL-2.0-only *
******************************************************************************/
/******************************************************************************
******* headers **************************************************************
******************************************************************************/
#include "mine-sweeper/save/save.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <libalx/base/compiler/unused.h>
#include <libalx/base/errno/error.h>
#include <libalx/base/stdio/printf/sbprintf.h>
#include <libalx/base/stdio/seekc.h>
#include "mine-sweeper/game/core.h"
#include "mine-sweeper/player/iface.h"
/******************************************************************************
******* variables ************************************************************
******************************************************************************/
char home_path [FILENAME_MAX];
char user_game_path [FILENAME_MAX];
char saved_path [FILENAME_MAX];
char saved_name [FILENAME_MAX];
/******************************************************************************
******* global functions *****************************************************
******************************************************************************/
void save_init (void)
{
if (alx_sbprintf(home_path, NULL, "%s/", getenv(ENV_HOME)))
goto err_path;
if (alx_sbprintf(user_game_path,NULL,"%s/%s/",home_path, USER_GAME_DIR))
goto err_path;
if (alx_sbprintf(saved_path, NULL, "%s/%s/", home_path, USER_SAVED_DIR))
goto err_path;
saved_name[0] = '\0';
if (mkdir(user_game_path, 0700)) {
if (errno != EEXIST)
goto err_mkdir;
}
if (mkdir(saved_path, 0700)) {
if (errno != EEXIST)
goto err_mkdir;
return;
}
return;
err_path:
alx_perror(getenv(ENV_HOME));
exit(EXIT_FAILURE);
err_mkdir:
alx_perror(home_path);
}
void save_clr (void)
{
if (alx_sbprintf(saved_path, NULL, "%s/%s/", home_path, USER_SAVED_DIR))
goto err;
return;
err:
alx_perror(home_path);
exit(EXIT_FAILURE);
}
int load_game_file (void)
{
char fname[FILENAME_MAX];
FILE *fp;
int i;
int j;
if (alx_sbprintf(fname, NULL, "%s/%s", saved_path, saved_name))
goto err_path;
fp = fopen(fname, "r");
if (!fp)
return -1;
fscanf(fp, "mine-sweeper saved game");
fscanf(fp, " rows %ti", &game_board.rows);
fscanf(fp, " cols %ti", &game_board.cols);
fscanf(fp, " mines %i", &game_board.mines);
fscanf(fp, " gnd");
for (i = 0; i < game_board.rows; i++) {
fscanf(fp, " %i", &game_board.gnd[i][0]);
for (j = 1; j < game_board.cols; j++)
fscanf(fp, ",%i", &game_board.gnd[i][j]);
}
fscanf(fp, " usr");
for (i = 0; i < game_board.rows; i++) {
fscanf(fp, " %i", &game_board.usr[i][0]);
for (j = 1; j < game_board.cols; j++)
fscanf(fp, ",%i", &game_board.usr[i][j]);
}
fscanf(fp, " flags %i", &game_board.flags);
fscanf(fp, " cleared %i", &game_board.clr);
fclose(fp);
return 0;
err_path:
alx_perror(saved_name);
alx_wait4enter();
exit(EXIT_FAILURE);
}
void save_game_file (char fpath[static restrict FILENAME_MAX])
{
char fname[FILENAME_MAX];
char tmp[FILENAME_MAX];
char old_saved[FILENAME_MAX];
char file_num[6];
FILE *fp;
int i;
int j;
bool x;
/* Don't change saved_name variable if not in default dir */
if (fpath)
UNUSED(alx_sbprintf(old_saved, NULL, "%s", saved_name));
/* Default path & name */
save_clr();
UNUSED(alx_sbprintf(saved_name, NULL, "%s", SAVED_NAME_DEFAULT));
file_num[0] = '\0';
/* Request file name */
player_iface_save_name(fpath, saved_name);
/* Look for an unused name of the type 'name_XXX.mine'. */
x = true;
for (i = 0; x; i++) {
if (!fpath) {
if (alx_sbprintf(fname, NULL, "%s/%s%s%s",
saved_path, saved_name, file_num,
FILE_EXTENSION)) {
goto err_path;
}
} else {
if (alx_sbprintf(fname, NULL, "%s/%s%s%s",
fpath, saved_name, file_num,
FILE_EXTENSION)) {
goto err_path;
}
}
fp = fopen(fname, "r");
if (fp) {
fclose(fp);
file_num[0] = '_';
file_num[1] = '0' + ((i / 100) % 10);
file_num[2] = '0' + ((i / 10) % 10);
file_num[3] = '0' + (i % 10);
file_num[4] = '\0';
} else {
x = false;
if (alx_sbprintf(tmp, NULL, "%s%s%s",
saved_name, file_num, FILE_EXTENSION)) {
goto err_path;
}
UNUSED(alx_sbprintf(saved_name, NULL, "%s", tmp));
}
}
/* Write to a new file */
fp = fopen(fname, "w");
if (!fp) {
alx_perror(fname);
goto err_fopen;
}
fprintf(fp, "mine-sweeper saved game\n");
fprintf(fp, "rows %ti\n", game_board.rows);
fprintf(fp, "cols %ti\n", game_board.cols);
fprintf(fp, "mines %i\n", game_board.mines);
fprintf(fp, "gnd\n");
for (i = 0; i < game_board.rows; i++) {
fprintf(fp, "%i", game_board.gnd[i][0]);
for (j = 1; j < game_board.cols; j++)
fprintf(fp, ",%i", game_board.gnd[i][j]);
fprintf(fp, "\n");
}
fprintf(fp, "usr\n");
for (i = 0; i < game_board.rows; i++) {
fprintf(fp, "%i", game_board.usr[i][0]);
for (j = 1; j < game_board.cols; j++)
fprintf(fp, ",%i", game_board.usr[i][j]);
fprintf(fp, "\n");
}
fprintf(fp, "flags %i\n", game_board.flags);
fprintf(fp, "cleared %i\n", game_board.clr);
fclose(fp);
err_fopen:
/* Don't change saved_name if saving in non-default dir */
if (fpath)
UNUSED(alx_sbprintf(saved_name, NULL, "%s", old_saved));
return;
err_path:
alx_perror(fname);
alx_wait4enter();
exit(EXIT_FAILURE);
}
/******************************************************************************
******* end of file **********************************************************
******************************************************************************/
|