summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoralejandro-colomar <colomar.6.4.3@gmail.com>2019-08-09 23:26:22 +0200
committeralejandro-colomar <colomar.6.4.3@gmail.com>2019-08-09 23:26:22 +0200
commit4f78e1f03d56a837b40cd5301d77feccd3a9b1dd (patch)
treeb888d49d9beb50c2410ffaf5132856c5f05f655a
parent18b0effdfcc59a8ee7c9f80056d056cb24b05be4 (diff)
Replace: snprintf() -> alx_sbprintf()
-rwxr-xr-xsrc/menu/parser.c10
-rwxr-xr-xsrc/player/iface.c25
-rwxr-xr-xsrc/save/save.c64
-rwxr-xr-xsrc/save/score.c21
4 files changed, 57 insertions, 63 deletions
diff --git a/src/menu/parser.c b/src/menu/parser.c
index 70b3b17..a4af1fe 100755
--- a/src/menu/parser.c
+++ b/src/menu/parser.c
@@ -12,7 +12,8 @@
#include <stdio.h>
#include <stdlib.h>
-#include "libalx/base/stdlib/seed.h"
+#include <libalx/base/stdio/printf/sbprintf.h>
+#include <libalx/base/stdlib/seed.h>
#include "mine-sweeper/about/about.h"
#include "mine-sweeper/ctrl/start.h"
@@ -159,15 +160,16 @@ static void parse_file (char *argument)
// FIXME
fp = fopen(argument, "r");
if (!fp)
- goto err_fp;
+ goto err;
fclose(fp);
saved_path[0] = '\0';
- snprintf(saved_name, FILENAME_MAX, argument);
+ if (alx_sbprintf(saved_name, NULL, "%s", argument))
+ goto err;
return;
-err_fp:
+err:
printf("--file argument not valid\n");
printf("It must be a valid file name (relative to saved dir)\n");
exit(EXIT_FAILURE);
diff --git a/src/player/iface.c b/src/player/iface.c
index 5e4bf2f..19056f2 100755
--- a/src/player/iface.c
+++ b/src/player/iface.c
@@ -11,6 +11,9 @@
#include <stddef.h>
#include <stdio.h>
+#include <libalx/base/compiler/unused.h>
+#include <libalx/base/stdio/printf/sbprintf.h>
+
#include "mine-sweeper/game/iface.h"
#include "mine-sweeper/player/clui.h"
#include "mine-sweeper/player/tui.h"
@@ -75,8 +78,8 @@ int player_iface_start (ptrdiff_t *row, ptrdiff_t *col)
char subtitle[TITLE_SIZE];
int fail;
- snprintf(title, TITLE_SIZE, "Start:");
- snprintf(subtitle, TITLE_SIZE, "00:00 | 0");
+ UNUSED(alx_sbprintf(title, NULL, "Start:"));
+ UNUSED(alx_sbprintf(subtitle, NULL, "00:00 | 0"));
/* Start position */
player_iface_position.row = 0;
@@ -135,16 +138,16 @@ void player_iface (const struct Game_Iface_Out *out,
case GAME_IFACE_STATE_CHEATED:
case GAME_IFACE_STATE_PLAYING:
case GAME_IFACE_STATE_PAUSE:
- snprintf(title, TITLE_SIZE, "Mines: %i/%i",
- out->flags, out->mines);
+ UNUSED(alx_sbprintf(title, NULL, "Mines: %i/%i",
+ out->flags, out->mines));
break;
case GAME_IFACE_STATE_GAMEOVER:
- snprintf(title, TITLE_SIZE, "GAME OVER");
+ UNUSED(alx_sbprintf(title, NULL, "GAME OVER"));
break;
case GAME_IFACE_STATE_SAFE:
- snprintf(title, TITLE_SIZE, "You win!");
+ UNUSED(alx_sbprintf(title, NULL, "You win!"));
break;
}
/* Subtitle */
@@ -154,14 +157,14 @@ void player_iface (const struct Game_Iface_Out *out,
secs = ((int)score->time % 60);
if (score->time >= 3600) {
- snprintf(subtitle, TITLE_SIZE, "%02i:%02i:%02i | %i",
- hours, mins, secs, score->clicks);
+ UNUSED(alx_sbprintf(subtitle,NULL,"%02i:%02i:%02i | %i",
+ hours, mins, secs, score->clicks));
} else {
- snprintf(subtitle, TITLE_SIZE, "%02i:%02i | %i",
- mins, secs, score->clicks);
+ UNUSED(alx_sbprintf(subtitle, NULL, "%02i:%02i | %i",
+ mins, secs, score->clicks));
}
} else {
- snprintf(subtitle, TITLE_SIZE, "N/A");
+ UNUSED(alx_sbprintf(subtitle, NULL, "N/A"));
}
/* Request player action */
diff --git a/src/save/save.c b/src/save/save.c
index 31cf5c2..cfe48f3 100755
--- a/src/save/save.c
+++ b/src/save/save.c
@@ -15,8 +15,10 @@
#include <sys/stat.h>
-#include "libalx/base/errno/error.h"
-#include "libalx/base/stdio/seekc.h"
+#include <libalx/base/compiler/unused.h>
+#include <libalx/base/errno/error.h>
+#include <libalx/base/stdio/printf/sbprintf.h>
+#include <libalx/base/stdio/seekc.h>
#include "mine-sweeper/game/core.h"
#include "mine-sweeper/player/iface.h"
@@ -37,18 +39,12 @@
void save_init (void)
{
- if (snprintf(home_path, sizeof(home_path), "%s/",
- getenv(ENV_HOME)) >= FILENAME_MAX) {
+ if (alx_sbprintf(home_path, NULL, "%s/", getenv(ENV_HOME)))
goto err_path;
- }
- if (snprintf(user_game_path, sizeof(user_game_path), "%s/%s/",
- home_path, USER_GAME_DIR) >= FILENAME_MAX) {
+ if (alx_sbprintf(user_game_path,NULL,"%s/%s/",home_path, USER_GAME_DIR))
goto err_path;
- }
- if (snprintf(saved_path, sizeof(saved_path), "%s/%s/",
- home_path, USER_SAVED_DIR) >= FILENAME_MAX) {
+ if (alx_sbprintf(saved_path, NULL, "%s/%s/", home_path, USER_SAVED_DIR))
goto err_path;
- }
saved_name[0] = '\0';
if (mkdir(user_game_path, 0700)) {
@@ -60,29 +56,24 @@ void save_init (void)
goto err_mkdir;
return;
}
-
return;
err_path:
- alx_perror("Path is too large and has been truncated");
+ alx_perror(getenv(ENV_HOME));
exit(EXIT_FAILURE);
err_mkdir:
- alx_perror(NULL);
+ alx_perror(home_path);
}
void save_clr (void)
{
- if (snprintf(saved_path, sizeof(saved_path), "%s/%s/",
- home_path, USER_SAVED_DIR) >= FILENAME_MAX) {
- goto err_path;
- }
-
+ if (alx_sbprintf(saved_path, NULL, "%s/%s/", home_path, USER_SAVED_DIR))
+ goto err;
return;
-
-err_path:
- alx_perror("Path is too large and has been truncated");
+err:
+ alx_perror(home_path);
exit(EXIT_FAILURE);
}
@@ -93,10 +84,8 @@ int load_game_file (void)
int i;
int j;
- if (snprintf(fname, sizeof(fname), "%s/%s",
- saved_path, saved_name) >= FILENAME_MAX) {
+ if (alx_sbprintf(fname, NULL, "%s/%s", saved_path, saved_name))
goto err_path;
- }
fp = fopen(fname, "r");
if (!fp)
@@ -126,7 +115,7 @@ int load_game_file (void)
err_path:
- alx_perror("Path is too large and has been truncated");
+ alx_perror(saved_name);
alx_wait4enter();
exit(EXIT_FAILURE);
}
@@ -144,11 +133,11 @@ void save_game_file (char fpath[static restrict FILENAME_MAX])
/* Don't change saved_name variable if not in default dir */
if (fpath)
- snprintf(old_saved, sizeof(old_saved), "%s", saved_name);
+ UNUSED(alx_sbprintf(old_saved, NULL, "%s", saved_name));
/* Default path & name */
save_clr();
- snprintf(saved_name, sizeof(saved_name), "%s", SAVED_NAME_DEFAULT);
+ UNUSED(alx_sbprintf(saved_name, NULL, "%s", SAVED_NAME_DEFAULT));
file_num[0] = '\0';
/* Request file name */
@@ -158,15 +147,15 @@ void save_game_file (char fpath[static restrict FILENAME_MAX])
x = true;
for (i = 0; x; i++) {
if (!fpath) {
- if (snprintf(fname, sizeof(fname), "%s/%s%s%s",
+ if (alx_sbprintf(fname, NULL, "%s/%s%s%s",
saved_path, saved_name, file_num,
- FILE_EXTENSION) >= FILENAME_MAX) {
+ FILE_EXTENSION)) {
goto err_path;
}
} else {
- if (snprintf(fname, sizeof(fname), "%s/%s%s%s",
+ if (alx_sbprintf(fname, NULL, "%s/%s%s%s",
fpath, saved_name, file_num,
- FILE_EXTENSION) >= FILENAME_MAX) {
+ FILE_EXTENSION)) {
goto err_path;
}
}
@@ -181,12 +170,11 @@ void save_game_file (char fpath[static restrict FILENAME_MAX])
file_num[4] = '\0';
} else {
x = false;
- if (snprintf(tmp, sizeof(tmp), "%s%s%s",
- saved_name, file_num,
- FILE_EXTENSION) >= FILENAME_MAX) {
+ if (alx_sbprintf(tmp, NULL, "%s%s%s",
+ saved_name, file_num, FILE_EXTENSION)) {
goto err_path;
}
- snprintf(saved_name, sizeof(saved_name), "%s", tmp);
+ UNUSED(alx_sbprintf(saved_name, NULL, "%s", tmp));
}
}
@@ -221,13 +209,13 @@ void save_game_file (char fpath[static restrict FILENAME_MAX])
err_fopen:
/* Don't change saved_name if saving in non-default dir */
if (fpath)
- snprintf(saved_name, sizeof(saved_name), "%s", old_saved);
+ UNUSED(alx_sbprintf(saved_name, NULL, "%s", old_saved));
return;
err_path:
- alx_perror("Path is too large and has been truncated");
+ alx_perror(fname);
alx_wait4enter();
exit(EXIT_FAILURE);
}
diff --git a/src/save/score.c b/src/save/score.c
index b7693a2..79121d9 100755
--- a/src/save/score.c
+++ b/src/save/score.c
@@ -14,9 +14,10 @@
#include <stdlib.h>
#include <time.h>
-#include "libalx/base/errno/error.h"
-#include "libalx/base/compiler/size.h"
-#include "libalx/base/stdio/seekc.h"
+#include <libalx/base/errno/error.h>
+#include <libalx/base/compiler/size.h>
+#include <libalx/base/stdio/printf/sbprintf.h>
+#include <libalx/base/stdio/seekc.h>
#include "mine-sweeper/game/core.h"
#include "mine-sweeper/game/iface.h"
@@ -97,24 +98,24 @@ void save_score (const struct Game_Iface_Score *game_iface_score)
return;
err_fp:
- alx_perror("Score could not be saved");
+ alx_perror(fname);
}
void print_scores (void)
{
char cmd[_POSIX_ARG_MAX];
- if (snprintf(cmd, sizeof(cmd), "less %s %s %s",
- HISCORES_BEGINNER_FILE,
- HISCORES_INTERMEDIATE_FILE,
- HISCORES_EXPERT_FILE) >= SSIZEOF(cmd)) {
+ if (alx_sbprintf(cmd, NULL, "less %s %s %s",
+ HISCORES_BEGINNER_FILE,
+ HISCORES_INTERMEDIATE_FILE,
+ HISCORES_EXPERT_FILE)) {
goto err;
}
if (system(cmd))
- alx_perror(NULL);
+ goto err;
return;
err:
- alx_perror("Path is too large and has been truncated");
+ alx_perror(cmd);
}