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