Debian Codegraph @

src/savegame.c

raw symbols ◀ ▶ SCIP
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* savegame.c - save/load game etc
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 */
8 #include <gtk/gtk.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <dirent.h>
15 #include <time.h>
16 #include <stdlib.h>
17
18 #include "game.h"
19
20 gint parse_save_game(gchar *sgame, gchar **rules, gint *score, gint **board, gint **next)
21 {
22 struct stat buf;
23 gchar *sdata, *psdata, *srules;
24 FILE *f;
25 gint i, val;
26 gint rw, rh, rc, rn, rd, rlen;
27
28 if (stat(sgame, &buf) != 0 || !S_ISREG(buf.st_mode) || buf.st_size < 20 || (f = fopen(sgame, "r")) == NULL) {
29 return 0;
30 }
31
32 sdata = g_malloc(buf.st_size);
33
34 if (fread(sdata, 1, buf.st_size, f) != (size_t) buf.st_size) {
35 g_free (sdata);
36 return 0;
37 }
38
39 fclose(f);
40
41 rlen = rules_get_str_len();
42 srules = g_malloc(rlen + 1);
43 srules[rlen] = 0;
44 memcpy(srules, sdata, rlen);
45
46 if (!rules_get_from_str(srules, &rw, &rh, &rc, &rn, &rd) ||
47 sscanf(sdata + rlen, "%010u", score) != 1 ||
48 buf.st_size != rlen + 10 + rw * rh * 2 + rn * 2) {
49 printf("[%s]\n", srules);
50 g_free (srules);
51 g_free (sdata);
52 return 0;
53 }
54
55 g_free (srules);
56
57 *board = g_malloc(rw * rh * sizeof(gint));
58 *next = g_malloc(rn * sizeof(gint));
59
60 psdata = sdata + rlen + 10;
61 for (i = 0; i < rw * rh; i++, psdata += 2) {
62 if (sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
63 g_free (*next);
64 g_free (*board);
65 g_free (sdata);
66 return 0;
67 }
68 (*board)[i] = val;
69 }
70
71 for (i = 0; i < rn; i++, psdata += 2) {
72 if (sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
73 g_free (*next);
74 g_free (*board);
75 g_free (sdata);
76 return 0;
77 }
78 (*next)[i] = val;
79 }
80
81 *rules = g_strndup(sdata, rlen);
82 g_free (sdata);
83
84 return 1;
85 }
86
87
88 /* check that save game name is in form YYYY-MM-DD-HHMMSS.sav and have correct content
89 return string "DD.MM.YYYY HH:MM:SS (score)" on success, NULL on failure */
90 gchar *is_valid_save_game(gchar *name, gchar *path)
91 {
92 guint i, y, m, d, h, min, s;
93 gint score, *board, *next;
94 gchar *sgame;
95 gchar *rules;
96
97 if ((i = sscanf(name, "%04u-%02u-%02u-%02u%02u%02u", &y, &m, &d, &h, &min, &s)) != 6 ||
98 !m || m > 12 || !d || d > 31 || h > 23 || min > 59 || s > 61 ||
99 (strcmp(name + strlen(name) - 4, ".sav") != 0)) {
100 return NULL;
101 }
102
103 sgame=g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
104
105 i = parse_save_game(sgame, &rules, &score, &board, &next);
106 g_free (sgame);
107
108 if (!i) return NULL;
109
110 g_free (rules);
111 g_free (board);
112 g_free (next);
113
114 return g_strdup_printf("%02d.%02d.%04d %02d:%02d:%02d (%d)",
115 d, m, y, h, min, s, score);
116 /* FIXME: return rules back
117 return g_strdup_printf("%02d.%02d.%04d %02d:%02d:%02d (%d [%dx%d %d %d %d])",
118 d, m, y, h, min, s, score,
119 rules.xsize, rules.ysize, rules.colors, rules.next, rules.destroy);
120 */
121 }
122
123
124 gint get_saved_games(gchar ***gamelist)
125 {
126 gchar *datapath;
127 struct stat buf;
128 struct dirent *dir_entry;
129 DIR *directory;
130 gchar **games = NULL, *game;
131 gint num = 0;
132
133 datapath = g_strconcat(getenv("HOME"), G_DIR_SEPARATOR_S, ".gtkballs", NULL);
134 if (stat(datapath, &buf) != 0) { /* no ~/.gtkballs */
135 if (mkdir(datapath, 0700) != 0) { /* and cannot create it... */
136 g_free (datapath);
137 return -1;
138 }
139 } else if (!S_ISDIR(buf.st_mode)) { /* ~/.gtkballs is not a directory */
140 g_free (datapath);
141 return -1;
142 }
143 if ((directory = opendir(datapath)))
144 {
145 while ((dir_entry = readdir(directory))) {
146 if ((game = is_valid_save_game(dir_entry->d_name, datapath)) != NULL) {
147 num++;
148 games = g_realloc(games, sizeof(gchar *) * num * 2);
149 games[(num - 1) * 2] = game;
150 games[(num - 1) * 2 + 1] = g_strdup_printf("%s%s%s", datapath, G_DIR_SEPARATOR_S, dir_entry->d_name);
151 }
152 }
153 closedir(directory);
154 }
155 g_free (datapath);
156 *gamelist = games;
157 return num;
158 }
159
160
161 gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors)
162 {
163 FILE *f;
164 gchar *fname;
165 time_t nowtime;
166 gchar ftime[]="0000-00-00-000000";
167 gint i;
168
169 nowtime = time(NULL);
170 strftime(ftime, sizeof(ftime), "%Y-%m-%d-%H%M%S", localtime(&nowtime));
171 fname = g_strconcat(getenv("HOME"), G_DIR_SEPARATOR_S, ".gtkballs",
172 G_DIR_SEPARATOR_S, ftime, ".sav", NULL);
173 if ((f = fopen(fname, "w")) != NULL) {
174 chmod(fname, 0600);
175 /* TODO: check for errors ! */
176 fprintf(f, "%s", rules);
177 fprintf(f, "%010d", score);
178 for (i = 0; i < rules_get_width() * rules_get_height(); i++) {
179 fprintf(f, "%02d", board[i]);
180 }
181 for (i = 0; i < rules_get_next(); i++) {
182 fprintf(f, "%02d", nextcolors[i]);
183 }
184 fclose(f);
185 } else {
186 return fname;
187 }
188
189 g_free (fname);
190
191 return NULL;
192 }