savegame.c: consistent indentation

This commit is contained in:
wdlkmpx 2021-01-14 23:21:06 +08:00
parent 7c49070107
commit 565348506b

View File

@ -17,22 +17,22 @@
#include "game.h"
gint parse_save_game(gchar *sgame, gchar **rules, gint *score, gint **board, gint **next) {
gint parse_save_game(gchar *sgame, gchar **rules, gint *score, gint **board, gint **next)
{
struct stat buf;
gchar *sdata, *psdata, *srules;
FILE *f;
gint i, val;
gint rw, rh, rc, rn, rd, rlen;
if(stat(sgame, &buf) != 0 || !S_ISREG(buf.st_mode) || buf.st_size < 20 ||
(f = fopen(sgame, "r")) == NULL) {
if (stat(sgame, &buf) != 0 || !S_ISREG(buf.st_mode) || buf.st_size < 20 || (f = fopen(sgame, "r")) == NULL) {
return 0;
}
sdata = g_malloc(buf.st_size);
if(fread(sdata, 1, buf.st_size, f) != buf.st_size) {
g_free(sdata);
if (fread(sdata, 1, buf.st_size, f) != buf.st_size) {
g_free (sdata);
return 0;
}
@ -43,56 +43,58 @@ gint parse_save_game(gchar *sgame, gchar **rules, gint *score, gint **board, gin
srules[rlen] = 0;
memcpy(srules, sdata, rlen);
if(!rules_get_from_str(srules, &rw, &rh, &rc, &rn, &rd) ||
if (!rules_get_from_str(srules, &rw, &rh, &rc, &rn, &rd) ||
sscanf(sdata + rlen, "%010u", score) != 1 ||
buf.st_size != rlen + 10 + rw * rh * 2 + rn * 2) {
printf("[%s]\n", srules);
g_free(srules);
g_free(sdata);
g_free (srules);
g_free (sdata);
return 0;
}
g_free(srules);
g_free (srules);
*board = g_malloc(rw * rh * sizeof(gint));
*next = g_malloc(rn * sizeof(gint));
psdata = sdata + rlen + 10;
for(i = 0; i < rw * rh; i++, psdata += 2) {
if(sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
g_free(*next);
g_free(*board);
g_free(sdata);
for (i = 0; i < rw * rh; i++, psdata += 2) {
if (sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
g_free (*next);
g_free (*board);
g_free (sdata);
return 0;
}
(*board)[i] = val;
}
for(i = 0; i < rn; i++, psdata += 2) {
if(sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
g_free(*next);
g_free(*board);
g_free(sdata);
for (i = 0; i < rn; i++, psdata += 2) {
if (sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
g_free (*next);
g_free (*board);
g_free (sdata);
return 0;
}
(*next)[i] = val;
}
*rules = g_strndup(sdata, rlen);
g_free(sdata);
g_free (sdata);
return 1;
}
/* check that save game name is in form YYYY-MM-DD-HHMMSS.sav and have correct content
return string "DD.MM.YYYY HH:MM:SS (score)" on success, NULL on failure */
gchar *is_valid_save_game(gchar *name, gchar *path) {
gchar *is_valid_save_game(gchar *name, gchar *path)
{
guint i, y, m, d, h, min, s;
gint score, *board, *next;
gchar *sgame;
gchar *rules;
if((i = sscanf(name, "%04u-%02u-%02u-%02u%02u%02u", &y, &m, &d, &h, &min, &s)) != 6 ||
if ((i = sscanf(name, "%04u-%02u-%02u-%02u%02u%02u", &y, &m, &d, &h, &min, &s)) != 6 ||
!m || m > 12 || !d || d > 31 || h > 23 || min > 59 || s > 61 ||
(strcmp(name + strlen(name) - 4, ".sav") != 0)) {
return NULL;
@ -101,13 +103,13 @@ gchar *is_valid_save_game(gchar *name, gchar *path) {
sgame=g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
i = parse_save_game(sgame, &rules, &score, &board, &next);
g_free(sgame);
g_free (sgame);
if(!i) return NULL;
if (!i) return NULL;
g_free(rules);
g_free(board);
g_free(next);
g_free (rules);
g_free (board);
g_free (next);
return g_strdup_printf("%02d.%02d.%04d %02d:%02d:%02d (%d)",
d, m, y, h, min, s, score);
@ -118,7 +120,9 @@ gchar *is_valid_save_game(gchar *name, gchar *path) {
*/
}
gint get_saved_games(gchar ***gamelist) {
gint get_saved_games(gchar ***gamelist)
{
gchar *datapath;
struct stat buf;
struct dirent *dir_entry;
@ -127,18 +131,19 @@ gint get_saved_games(gchar ***gamelist) {
gint num = 0;
datapath = g_strconcat(getenv("HOME"), G_DIR_SEPARATOR_S, ".gtkballs", NULL);
if(stat(datapath, &buf) != 0) { /* no ~/.gtkballs */
if(mkdir(datapath, 0700) != 0) { /* and cannot create it... */
g_free(datapath);
if (stat(datapath, &buf) != 0) { /* no ~/.gtkballs */
if (mkdir(datapath, 0700) != 0) { /* and cannot create it... */
g_free (datapath);
return -1;
}
} else if(!S_ISDIR(buf.st_mode)) { /* ~/.gtkballs is not a directory */
g_free(datapath);
} else if (!S_ISDIR(buf.st_mode)) { /* ~/.gtkballs is not a directory */
g_free (datapath);
return -1;
}
if((directory = opendir(datapath))) {
while((dir_entry = readdir(directory))) {
if((game = is_valid_save_game(dir_entry->d_name, datapath)) != NULL) {
if ((directory = opendir(datapath)))
{
while ((dir_entry = readdir(directory))) {
if ((game = is_valid_save_game(dir_entry->d_name, datapath)) != NULL) {
num++;
games = g_realloc(games, sizeof(gchar *) * num * 2);
games[(num - 1) * 2] = game;
@ -147,12 +152,14 @@ gint get_saved_games(gchar ***gamelist) {
}
closedir(directory);
}
g_free(datapath);
g_free (datapath);
*gamelist = games;
return num;
}
gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors) {
gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors)
{
FILE *f;
gchar *fname;
time_t nowtime;
@ -163,15 +170,15 @@ gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors) {
strftime(ftime, sizeof(ftime), "%Y-%m-%d-%H%M%S", localtime(&nowtime));
fname = g_strconcat(getenv("HOME"), G_DIR_SEPARATOR_S, ".gtkballs",
G_DIR_SEPARATOR_S, ftime, ".sav", NULL);
if((f = fopen(fname, "w")) != NULL) {
if ((f = fopen(fname, "w")) != NULL) {
chmod(fname, 0600);
/* TODO: check for errors ! */
fprintf(f, rules);
fprintf(f, "%010d", score);
for(i = 0; i < rules_get_width() * rules_get_height(); i++) {
for (i = 0; i < rules_get_width() * rules_get_height(); i++) {
fprintf(f, "%02d", board[i]);
}
for(i = 0; i < rules_get_next(); i++) {
for (i = 0; i < rules_get_next(); i++) {
fprintf(f, "%02d", nextcolors[i]);
}
fclose(f);
@ -179,7 +186,7 @@ gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors) {
return fname;
}
g_free(fname);
g_free (fname);
return NULL;
}