mirror of
https://git.zap.org.au/git/trader.git
synced 2025-06-30 22:19:26 -04:00
Make the load_game and save_game macros more reliable
These macros now follow recommended practices as used by the Linux kernel: in particular, by using the "do { ... } while (0)" construct.
This commit is contained in:
parent
5c28cea858
commit
04513c9d30
18
src/game.c
18
src/game.c
@ -50,7 +50,7 @@ static const int game_file_crypt_key[] = {
|
|||||||
// Macros used in load_game()
|
// Macros used in load_game()
|
||||||
|
|
||||||
#define load_game_scanf(_fmt, _var, _cond) \
|
#define load_game_scanf(_fmt, _var, _cond) \
|
||||||
{ \
|
do { \
|
||||||
if (fgets(buf, BUFSIZE, file) == NULL) { \
|
if (fgets(buf, BUFSIZE, file) == NULL) { \
|
||||||
err_exit("%s: missing field on line %d", filename, lineno); \
|
err_exit("%s: missing field on line %d", filename, lineno); \
|
||||||
} \
|
} \
|
||||||
@ -64,7 +64,7 @@ static const int game_file_crypt_key[] = {
|
|||||||
filename, lineno, buf); \
|
filename, lineno, buf); \
|
||||||
} \
|
} \
|
||||||
lineno++; \
|
lineno++; \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
#define load_game_read_int(_var, _cond) \
|
#define load_game_read_int(_var, _cond) \
|
||||||
load_game_scanf("%d", _var, _cond)
|
load_game_scanf("%d", _var, _cond)
|
||||||
@ -74,15 +74,15 @@ static const int game_file_crypt_key[] = {
|
|||||||
load_game_scanf("%lf", _var, _cond)
|
load_game_scanf("%lf", _var, _cond)
|
||||||
|
|
||||||
#define load_game_read_bool(_var) \
|
#define load_game_read_bool(_var) \
|
||||||
{ \
|
do { \
|
||||||
int b; \
|
int b; \
|
||||||
\
|
\
|
||||||
load_game_scanf("%d", b, (b == false) || (b == true)); \
|
load_game_scanf("%d", b, (b == false) || (b == true)); \
|
||||||
(_var) = b; \
|
(_var) = b; \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
#define load_game_read_string(_var) \
|
#define load_game_read_string(_var) \
|
||||||
{ \
|
do { \
|
||||||
char *s; \
|
char *s; \
|
||||||
int len; \
|
int len; \
|
||||||
\
|
\
|
||||||
@ -101,22 +101,22 @@ static const int game_file_crypt_key[] = {
|
|||||||
\
|
\
|
||||||
strcpy(s, buf); \
|
strcpy(s, buf); \
|
||||||
len = strlen(s); \
|
len = strlen(s); \
|
||||||
if ((len > 0) && (s[len - 1] == '\n')) { \
|
if (len > 0 && s[len - 1] == '\n') { \
|
||||||
s[len - 1] = '\0'; \
|
s[len - 1] = '\0'; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
(_var) = s; \
|
(_var) = s; \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
// Macros used in save_game()
|
// Macros used in save_game()
|
||||||
|
|
||||||
#define save_game_printf(_fmt, _var) \
|
#define save_game_printf(_fmt, _var) \
|
||||||
{ \
|
do { \
|
||||||
snprintf(buf, BUFSIZE, _fmt "\n", _var); \
|
snprintf(buf, BUFSIZE, _fmt "\n", _var); \
|
||||||
scramble(crypt_key, buf, BUFSIZE); \
|
scramble(crypt_key, buf, BUFSIZE); \
|
||||||
fprintf(file, "%s", buf); \
|
fprintf(file, "%s", buf); \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
#define save_game_write_int(_var) \
|
#define save_game_write_int(_var) \
|
||||||
save_game_printf("%d", _var)
|
save_game_printf("%d", _var)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user