Remove various compilation warnings
With the removal of these warnings we slightly improve the robustness of the highscore parsing, handling some more cases of bad parsing/writing and failure to open files.
This commit is contained in:
parent
28b4a46336
commit
4520781f25
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
||||||
CC ?= clang
|
CC ?= clang
|
||||||
CFLAGS += -Wno-visibility -Wno-incompatible-pointer-types -Wall -Wextra -O2 -DINVERT_COLORS -DVT100
|
CFLAGS += -Wno-visibility -Wno-incompatible-pointer-types -Wall -Wextra -O2 -DINVERT_COLORS -DVT100 -O2
|
||||||
LFLAGS +=
|
LFLAGS +=
|
||||||
|
|
||||||
PROGRAM := 2048
|
PROGRAM := 2048
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#define ENGINE_H
|
#define ENGINE_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "gfx.h"
|
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
|
|
||||||
#define fatal(msg)\
|
#define fatal(msg)\
|
||||||
|
@ -34,6 +33,8 @@ enum {
|
||||||
dir_up
|
dir_up
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct gfx_state;
|
||||||
|
|
||||||
int gamestate_end_condition(struct gamestate*);
|
int gamestate_end_condition(struct gamestate*);
|
||||||
void gamestate_new_block(struct gamestate*);
|
void gamestate_new_block(struct gamestate*);
|
||||||
int gamestate_tick(struct gfx_state*, struct gamestate*, int, void (*callback)(struct gfx_state*, struct gamestate*));
|
int gamestate_tick(struct gfx_state*, struct gamestate*, int, void (*callback)(struct gfx_state*, struct gamestate*));
|
||||||
|
|
|
@ -57,7 +57,9 @@ void highscore_reset(void)
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/* fgets is used to avoid queuing that may occur with getchar */
|
/* fgets is used to avoid queuing that may occur with getchar */
|
||||||
fgets(resp, resp_length, stdin);
|
if (fgets(resp, resp_length, stdin) == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
string_to_lower(resp);
|
string_to_lower(resp);
|
||||||
|
|
||||||
const size_t sl = strlen(resp);
|
const size_t sl = strlen(resp);
|
||||||
|
@ -87,7 +89,16 @@ long highscore_load(struct gamestate *g)
|
||||||
if (fd == NULL)
|
if (fd == NULL)
|
||||||
fd = fopen(hsfile, "w+");
|
fd = fopen(hsfile, "w+");
|
||||||
|
|
||||||
fscanf(fd, "%ld", &result);
|
if (fd == NULL) {
|
||||||
|
fprintf(stderr, "load: Failed to open highscore file\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fscanf(fd, "%ld", &result) != 1) {
|
||||||
|
fprintf(stderr, "load: Failed to parse highscore file\n");
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
||||||
if (g) g->score_high = result;
|
if (g) g->score_high = result;
|
||||||
|
@ -105,6 +116,13 @@ void highscore_save(struct gamestate *g)
|
||||||
const char *hsfile = highscore_retrieve_file();
|
const char *hsfile = highscore_retrieve_file();
|
||||||
|
|
||||||
FILE *fd = fopen(hsfile, "w");
|
FILE *fd = fopen(hsfile, "w");
|
||||||
fprintf(fd, "%ld", g->score);
|
if (fd == NULL) {
|
||||||
|
fprintf(stderr, "save: Failed to open highscore file\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fprintf(fd, "%ld", g->score) < 0) {
|
||||||
|
fprintf(stderr, "save: Failed to write highscore file\n");
|
||||||
|
}
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef HIGHSCORE_H
|
#ifndef HIGHSCORE_H
|
||||||
#define HIGHSCORE_H
|
#define HIGHSCORE_H
|
||||||
|
|
||||||
|
#include "engine.h"
|
||||||
|
|
||||||
void highscore_reset(void);
|
void highscore_reset(void);
|
||||||
long highscore_load(struct gamestate *g);
|
long highscore_load(struct gamestate *g);
|
||||||
void highscore_save(struct gamestate *g);
|
void highscore_save(struct gamestate *g);
|
||||||
|
|
|
@ -14,7 +14,7 @@ void draw_then_sleep(struct gfx_state *s, struct gamestate *g)
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct gamestate *g = gamestate_init(argc, argv);
|
struct gamestate *g = gamestate_init(argc, argv);
|
||||||
struct gfx_state *s;
|
struct gfx_state *s = NULL;
|
||||||
|
|
||||||
if (g->opts->interactive)
|
if (g->opts->interactive)
|
||||||
s = gfx_init(g);
|
s = gfx_init(g);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user