-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.c
118 lines (95 loc) · 2.89 KB
/
save.c
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
#include<stdio.h>
#include "save.h"
#include "prompt.h"
#include "structs.h"
#include "strings.h"
#include "backgammon.h"
void loadBoard(const char *bString, struct board *dest) {
for (int i = 0; i < 2; i++) { // for both players
(*dest).bar[i] = bString[2 * i] - '0';
(*dest).outside[i] = bString[2 * i + 1] - '0';
}
for (int i = 2; i < BOARD_SIZE + 2; i++) { // i=2 will start at bString[4]
(*dest).fields[i - 2].pieces = bString[2 * i] - '0';
(*dest).fields[i - 2].player = bString[2 * i + 1] - '0';
}
for (int i = SAVELEN - 7; i < SAVELEN - 3; i++) {
(*dest).move.moves[i - SAVELEN + 7] = bString[i] - '0';
}
(*dest).move.player = bString[SAVELEN - 3] - '0';
for (int i = SAVELEN - 2; i < SAVELEN; i++) {
(*dest).won[i - SAVELEN + 2] = bString[i] - '0';
}
}
void serializeBoard(struct board *board, char *destination) {
for (int i = 0; i < 2; i++) {
destination[2 * i] = (*board).bar[i] + '0';
destination[2 * i + 1] = (*board).outside[i] + '0';
}
for (int i = 2; i < BOARD_SIZE + 2; i++) {
destination[2 * i] = (*board).fields[i - 2].pieces + '0';
destination[2 * i + 1] = (*board).fields[i - 2].player + '0';
}
for (int i = SAVELEN - 7; i < SAVELEN - 3; i++) {
destination[i] = (*board).move.moves[i - SAVELEN + 7] + '0';
}
destination[SAVELEN - 3] = (*board).move.player + '0';
for (int i = SAVELEN - 2; i < SAVELEN; i++) {
destination[i] = (*board).won[i - SAVELEN + 2] + '0';
}
}
void clearTemp(void) {
FILE *file;
file = fopen(TEMPFILE_NAME, "w");
fclose(file);
}
void saveMoveToTemp(struct board *board) {
char boardString[SAVELEN + 1];
boardString[SAVELEN] = '\0';
FILE *file;
file = fopen(TEMPFILE_NAME, "a");
serializeBoard(board, boardString);
fprintf(file, "%s\n", boardString);
fclose(file);
}
void saveBoardToFile(char *path) {
FILE *tmpFile, *destFile;
tmpFile = fopen(TEMPFILE_NAME, "r");
destFile = fopen(path, "w");
char buffer;
while ((buffer = fgetc(tmpFile)) != EOF)
fputc(buffer, destFile);
fclose(tmpFile);
fclose(destFile);
}
void promptSave(void) {
char filename[FILENAME_MAXLENGTH];
prompt(PROMPT_SAVE, "%s", filename);
saveBoardToFile(filename);
}
void restoreTempFile(char *filename) {
FILE *tmpFile, *srcFile;
srcFile = fopen(filename, "r");
tmpFile = fopen(TEMPFILE_NAME, "w");
char buffer;
while ((buffer = fgetc(srcFile)) != EOF)
fputc(buffer, tmpFile);
fclose(tmpFile);
fclose(srcFile);
}
void promptLoad(struct board *board) {
char filename[FILENAME_MAXLENGTH];
char boardString[SAVELEN + 1];
FILE *file;
int firstAttempt = 1;
do {
prompt(firstAttempt ? PROMPT_LOAD : PROMPT_LOAD_INVALID, "%s", filename);
file = fopen(filename, "r");
firstAttempt = 0;
} while (!file);
fseek(file, -SAVELEN - 1, SEEK_END);
fgets(boardString, SAVELEN + 1, file);
fclose(file);
restoreTempFile(filename);
loadBoard(boardString, board);
}