summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoralejandro-colomar <colomar.6.4.3@gmail.com>2019-08-03 21:58:55 +0200
committeralejandro-colomar <colomar.6.4.3@gmail.com>2019-08-03 21:58:55 +0200
commit4ef9bd7a28fb37ac412ceafd0455a832c91520b1 (patch)
treea46cac229ba69489bc6344c15b5bd0501de32fa9
parentbfb3ae86f97cc362dc6db04111a422e48ce6995c (diff)
Use libalx functions
-rw-r--r--src/save/save.cpp116
1 files changed, 23 insertions, 93 deletions
diff --git a/src/save/save.cpp b/src/save/save.cpp
index 802bf9d..9030266 100644
--- a/src/save/save.cpp
+++ b/src/save/save.cpp
@@ -20,6 +20,9 @@
#include <opencv2/highgui/highgui.hpp>
#include "libalx/base/compiler/restrict.hpp"
+#include "libalx/base/compiler/unused.hpp"
+#include "libalx/base/errno/error.hpp"
+#include "libalx/base/stdio/printf/sbprintf.hpp"
#include "vision-artificial/user/iface.hpp"
@@ -70,78 +73,18 @@ char saved_name[FILENAME_MAX];
void save_init (void)
{
- if (snprintf(home_path, FILENAME_MAX, "%s/",
- getenv(ENV_HOME)) >= FILENAME_MAX) {
+ if (alx_sbprintf(home_path, NULL, "%s/", getenv(ENV_HOME)))
goto err;
- }
- if (snprintf(user_prog_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_PROG_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(saved_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_SAVED_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(labels_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_LABELS_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(labels_fail_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_LABELS_FAIL_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(lighters_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_LIGHTERS_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(lighters_fail_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_LIGHTERS_FAIL_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(objects_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_OBJECTS_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(objects_fail_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_OBJECTS_FAIL_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(coins_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_COINS_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(coins_fail_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_COINS_FAIL_DIR) >= FILENAME_MAX) {
- goto err;
- }
- if (snprintf(resistors_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_RESISTORS_DIR) >= FILENAME_MAX) {
+ if (alx_sbprintf(user_prog_path, NULL, "%s/%s/",
+ home_path, USER_PROG_DIR))
goto err;
- }
- if (snprintf(resistors_fail_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_RESISTORS_FAIL_DIR) >= FILENAME_MAX) {
+ if (alx_sbprintf(saved_path, NULL, "%s/%s/", home_path, USER_SAVED_DIR))
goto err;
- }
saved_name[0] = '\0';
if (mkdir(user_prog_path, 0700)) {
- if (errno != EEXIST) {
- fprintf(stderr, "%s[%i]: %s(): %s", __FILE__, __LINE__,
- __func__, strerror(errno));
- exit(EXIT_FAILURE);
- }
+ if (errno != EEXIST)
+ alx_error(EXIT_FAILURE, user_prog_path);
}
mkdir(saved_path, 0700);
@@ -150,25 +93,21 @@ void save_init (void)
return;
err:
- printf("Path is too large and has been truncated\n");
+ alx_error(EXIT_FAILURE, "Path is too large and has been truncated\n");
}
void save_reset_fpath(void)
{
- if (snprintf(saved_path, FILENAME_MAX, "%s/%s/",
- home_path,
- USER_SAVED_DIR) >= FILENAME_MAX) {
+ if (alx_sbprintf(saved_path, NULL, "%s/%s/", home_path, USER_SAVED_DIR))
goto err;
- }
-
return;
err:
printf("Path is too large and has been truncated\n");
}
void load_image_file (const char *restrict fpath,
- const char *restrict fname)
+ const char *restrict fname)
{
char file_path[FILENAME_MAX];
char file_name[FILENAME_MAX];
@@ -179,23 +118,20 @@ void load_image_file (const char *restrict fpath,
if (!fpath) {
/* Default path */
save_reset_fpath();
- snprintf(file_path, FILENAME_MAX, "%s", saved_path);
+ UNUSED(alx_sbprintf(file_path, NULL, "%s", saved_path));
} else {
- snprintf(file_path, FILENAME_MAX, "%s", fpath);
+ UNUSED(alx_sbprintf(file_path, NULL, "%s", fpath));
}
/* Set file_name */
if (!fname)
user_iface_fname(file_path, saved_name);
else
- snprintf(saved_name, FILENAME_MAX, "%s", fname);
+ UNUSED(alx_sbprintf(saved_name, NULL, "%s", fname));
/* File name */
- if (snprintf(file_name, FILENAME_MAX, "%s/%s",
- file_path,
- saved_name) >= FILENAME_MAX) {
+ if (alx_sbprintf(file_name, NULL, "%s/%s", file_path, saved_name))
goto err_path;
- }
image = cv::imread(file_name, CV_LOAD_IMAGE_COLOR);
if (image.empty())
@@ -216,7 +152,7 @@ void save_cleanup (void)
}
void save_image_file (const char *restrict fpath,
- const char *restrict save_as)
+ const char *restrict save_as)
{
char file_path[FILENAME_MAX];
char file_name[FILENAME_MAX];
@@ -226,36 +162,30 @@ void save_image_file (const char *restrict fpath,
if (!fpath) {
/* Default path */
save_reset_fpath();
- snprintf(file_path, FILENAME_MAX, "%s", saved_path);
+ UNUSED(alx_sbprintf(file_path, NULL, "%s", saved_path));
} else {
- snprintf(file_path, FILENAME_MAX, "%s", fpath);
+ UNUSED(alx_sbprintf(file_path, NULL, "%s", fpath));
}
/* Set file_name */
if (!save_as) {
- snprintf(saved_name, FILENAME_MAX, "%s", SAVED_NAME_DEFAULT);
+ UNUSED(alx_sbprintf(saved_name,NULL, "%s", SAVED_NAME_DEFAULT));
user_iface_fname(saved_path, saved_name);
} else {
- snprintf(saved_name, FILENAME_MAX, "%s", save_as);
+ UNUSED(alx_sbprintf(saved_name, NULL, "%s", save_as));
}
/* Prepend the path */
- if (snprintf(file_name, FILENAME_MAX, "%s/%s",
- file_path,
- saved_name) >= FILENAME_MAX) {
+ if (alx_sbprintf(file_name, NULL, "%s/%s", file_path, saved_name))
goto err_path;
- }
fp = fopen(file_name, "r");
if (fp) {
/* Name in use; ask once more */
fclose(fp);
user_iface_fname(saved_path, saved_name);
- if (snprintf(file_name, FILENAME_MAX, "%s/%s",
- file_path,
- saved_name) >= FILENAME_MAX) {
+ if (alx_sbprintf(file_name,NULL,"%s/%s", file_path, saved_name))
goto err_path;
- }
}
user_iface_log_write(2, saved_name);