diff options
author | alex <alex@ASUS> | 2018-09-23 02:23:47 +0200 |
---|---|---|
committer | alex <alex@ASUS> | 2018-09-23 02:23:47 +0200 |
commit | de3563c11a24982727211e058e18e77624ffac2b (patch) | |
tree | 668cce9f681a8fb9203aca531504a67a6d925750 | |
parent | d947164aed58efaf5158c55314b9d95ce76688f9 (diff) |
Add alx_sscan_fname() for use in GUI menu; Change alx_getnum.c -> alx_input.c
-rw-r--r-- | bin/Makefile | 2 | ||||
-rw-r--r-- | libalx/inc/alx_input.h (renamed from libalx/inc/alx_getnum.h) | 12 | ||||
-rw-r--r-- | libalx/obj/Makefile | 48 | ||||
-rw-r--r-- | libalx/src/alx_input.c (renamed from libalx/src/alx_getnum.c) | 56 | ||||
-rw-r--r-- | libalx/src/alx_ncur.c | 81 | ||||
-rw-r--r-- | modules/menu/obj/Makefile | 4 | ||||
-rw-r--r-- | modules/menu/src/menu_clui.c | 2 | ||||
-rw-r--r-- | modules/menu/src/menu_gui.c | 10 |
8 files changed, 126 insertions, 89 deletions
diff --git a/bin/Makefile b/bin/Makefile index 5f4b88c..6211f13 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -32,7 +32,7 @@ _ALL = mine-sweeper mine-sweeper.exe ALL = $(BIN_NAME) -MAIN_OBJ_LIBALX = alx_file.o alx_getnum.o alx_ncur.o alx_seed.o +MAIN_OBJ_LIBALX = alx_lib.o MAIN_OBJ_ABOUT = about_mod.o MAIN_OBJ_CTRL = ctrl_mod.o MAIN_OBJ_GAME = game_mod.o diff --git a/libalx/inc/alx_getnum.h b/libalx/inc/alx_input.h index af569d2..0fef5d0 100644 --- a/libalx/inc/alx_getnum.h +++ b/libalx/inc/alx_input.h @@ -6,8 +6,8 @@ /******************************************************************************
******* include guard ********************************************************
******************************************************************************/
-# ifndef ALX_GETNUM_H
- # define ALX_GETNUM_H
+# ifndef ALX_INPUT_H
+ # define ALX_INPUT_H
/******************************************************************************
@@ -15,6 +15,7 @@ ******************************************************************************/
#include <math.h>
#include <stdarg.h>
+ #include <stdbool.h>
#include <stdint.h>
@@ -33,6 +34,11 @@ double M,
const char *str);
+ int alx_sscan_fname (const char *fpath,
+ char *fname,
+ bool exist,
+ const char *str);
+
double alx_getdbl (double m,
double def,
double M,
@@ -51,7 +57,7 @@ /******************************************************************************
******* include guard ********************************************************
******************************************************************************/
-# endif /* alx_getnum.h */
+# endif /* alx_input.h */
/******************************************************************************
diff --git a/libalx/obj/Makefile b/libalx/obj/Makefile index 717c7af..84eae78 100644 --- a/libalx/obj/Makefile +++ b/libalx/obj/Makefile @@ -10,36 +10,36 @@ SRC_DIR = $(LIBALX_DIR)/src/ # dependencies -_ALL = alx_cmp.o alx_file.o alx_getnum.o alx_mask.o alx_math.o alx_ncur.o alx_seed.o +_ALL = alx_cmp.o alx_file.o alx_input.o alx_mask.o alx_math.o alx_ncur.o alx_seed.o ALL = $(_ALL) alx_lib.o -CMP_INC = alx_cmp.h -CMP_DEPS = $(SRC_DIR)/alx_cmp.c $(patsubst %,$(INC_DIR)/%,$(CMP_INC)) -CMP_INC_DIRS = -I $(INC_DIR) +CMP_INC = alx_cmp.h +CMP_DEPS = $(SRC_DIR)/alx_cmp.c $(patsubst %,$(INC_DIR)/%,$(CMP_INC)) +CMP_INC_DIRS = -I $(INC_DIR) -FILE_INC = alx_file.h -FILE_DEPS = $(SRC_DIR)/alx_file.c $(patsubst %,$(INC_DIR)/%,$(FILE_INC)) -FILE_INC_DIRS = -I $(INC_DIR) +FILE_INC = alx_file.h +FILE_DEPS = $(SRC_DIR)/alx_file.c $(patsubst %,$(INC_DIR)/%,$(FILE_INC)) +FILE_INC_DIRS = -I $(INC_DIR) -GETN_INC = alx_getnum.h -GETN_DEPS = $(SRC_DIR)/alx_getnum.c $(patsubst %,$(INC_DIR)/%,$(GETN_INC)) -GETN_INC_DIRS = -I $(INC_DIR) +INPUT_INC = alx_input.h +INPUT_DEPS = $(SRC_DIR)/alx_input.c $(patsubst %,$(INC_DIR)/%,$(INPUT_INC)) +INPUT_INC_DIRS = -I $(INC_DIR) -MASK_INC = alx_mask.h -MASK_DEPS = $(SRC_DIR)/alx_mask.c $(patsubst %,$(INC_DIR)/%,$(MASK_INC)) -MASK_INC_DIRS = -I $(INC_DIR) +MASK_INC = alx_mask.h +MASK_DEPS = $(SRC_DIR)/alx_mask.c $(patsubst %,$(INC_DIR)/%,$(MASK_INC)) +MASK_INC_DIRS = -I $(INC_DIR) -MATH_INC = alx_math.h -MATH_DEPS = $(SRC_DIR)/alx_math.c $(patsubst %,$(INC_DIR)/%,$(MATH_INC)) -MATH_INC_DIRS = -I $(INC_DIR) +MATH_INC = alx_math.h +MATH_DEPS = $(SRC_DIR)/alx_math.c $(patsubst %,$(INC_DIR)/%,$(MATH_INC)) +MATH_INC_DIRS = -I $(INC_DIR) -NCUR_INC = alx_ncur.h -NCUR_DEPS = $(SRC_DIR)/alx_ncur.c $(patsubst %,$(INC_DIR)/%,$(NCUR_INC)) -NCUR_INC_DIRS = -I $(INC_DIR) +NCUR_INC = alx_ncur.h alx_input.h +NCUR_DEPS = $(SRC_DIR)/alx_ncur.c $(patsubst %,$(INC_DIR)/%,$(NCUR_INC)) +NCUR_INC_DIRS = -I $(INC_DIR) -SEED_INC = alx_seed.h -SEED_DEPS = $(SRC_DIR)/alx_seed.c $(patsubst %,$(INC_DIR)/%,$(SEED_INC)) -SEED_INC_DIRS = -I $(INC_DIR) +SEED_INC = alx_seed.h +SEED_DEPS = $(SRC_DIR)/alx_seed.c $(patsubst %,$(INC_DIR)/%,$(SEED_INC)) +SEED_INC_DIRS = -I $(INC_DIR) # target: dependencies # action @@ -61,8 +61,8 @@ alx_file.o: $(FILE_DEPS) $(Q)$(CC) $(CFLAGS) $(FILE_INC_DIRS) -c $< -o $@ @echo "\tCC $@" -alx_getnum.o: $(GETN_DEPS) - $(Q)$(CC) $(CFLAGS) $(GETN_INC_DIRS) -c $< -o $@ +alx_input.o: $(INPUT_DEPS) + $(Q)$(CC) $(CFLAGS) $(INPUT_INC_DIRS) -c $< -o $@ @echo "\tCC $@" alx_mask.o: $(MASK_DEPS) diff --git a/libalx/src/alx_getnum.c b/libalx/src/alx_input.c index cf46514..0c52cb9 100644 --- a/libalx/src/alx_getnum.c +++ b/libalx/src/alx_input.c @@ -11,12 +11,13 @@ * * * * * * * * * */
#include <inttypes.h>
#include <stdarg.h>
+ #include <stdbool.h>
#include <stdio.h>
/* * * * * * * * * *
* * * Other * * * * * * *
* * * * * * * * * */
- #include "alx_getnum.h"
+ #include "alx_input.h"
/******************************************************************************
@@ -26,9 +27,11 @@ # define MAX_TRIES (2)
+ # define ERR_OK (0)
# define ERR_RANGE (1)
# define ERR_SSCANF (2)
- # define ERR_FGETS (3)
+ # define ERR_FPTR (3)
+ # define ERR_FGETS (4)
# define ERR_RANGE_MSG "¡ Number is out of range !"
# define ERR_SSCANF_MSG "¡ sscanf() error !"
@@ -60,7 +63,7 @@ int alx_sscan_dbl (double *dest, double m, double def, double M, const char *str } else if ((*dest < m) || (*dest > M)) {
err = ERR_RANGE;
} else {
- err = 0;
+ err = ERR_OK;
}
if (err) {
@@ -84,7 +87,7 @@ int alx_sscan_int (int64_t *dest, double m, int64_t def, double M, const char *s } else if ((*dest < m) || (*dest > M)) {
err = ERR_RANGE;
} else {
- err = 0;
+ err = ERR_OK;
}
if (err) {
@@ -95,6 +98,49 @@ int alx_sscan_int (int64_t *dest, double m, int64_t def, double M, const char *s }
/*
+ * Scan a file name in fpath.
+ * return: 0 if correct
+ * non 0 if there is an error
+ */
+int alx_sscan_fname (const char *fpath, char *fname, bool exist, const char *str)
+{
+ char buff [FILENAME_MAX];
+ char file_path [FILENAME_MAX];
+ int err;
+ FILE *fp;
+
+ if (sscanf(str, " %s ", buff) != 1) {
+ err = ERR_SSCANF;
+ } else {
+ snprintf(file_path, FILENAME_MAX, "%s%s", fpath, buff);
+
+ fp = fopen(file_path, "r");
+
+ if (exist) {
+ if (fp == NULL) {
+ err = ERR_FPTR;
+ } else {
+ err = ERR_OK;
+ fclose(fp);
+ }
+ } else {
+ if (fp == NULL) {
+ err = ERR_OK;
+ } else {
+ err = ERR_FPTR;
+ fclose(fp);
+ }
+ }
+ }
+
+ if (!err) {
+ snprintf(fname, FILENAME_MAX, buff);
+ }
+
+ return err;
+}
+
+ /*
* Ask for a double in the range [m, M].
*
* If the user enters a non valid number, it repeats to ask for
@@ -177,7 +223,6 @@ static double loop_getdbl (double m, double def, double M) if (err) {
manage_error(err);
- R = def;
} else {
break;
}
@@ -205,7 +250,6 @@ static int64_t loop_getint (double m, int64_t def, double M) if (err) {
manage_error(err);
- Z = def;
} else {
break;
}
diff --git a/libalx/src/alx_ncur.c b/libalx/src/alx_ncur.c index 78de144..c059488 100644 --- a/libalx/src/alx_ncur.c +++ b/libalx/src/alx_ncur.c @@ -22,6 +22,8 @@ /* * * * * * * * * *
* * * Other * * * * * * *
* * * * * * * * * */
+ #include "alx_input.h"
+
#include "alx_ncur.h"
@@ -35,8 +37,9 @@ # define ERR_OK (0)
# define ERR_RANGE (1)
# define ERR_SSCANF (2)
- # define ERR_GETSTR (3)
- # define ERR_FPTR (4)
+ # define ERR_FPTR (3)
+ # define ERR_FGETS (4)
+ # define ERR_GETSTR (5)
# define ERR_RANGE_MSG "¡ Number is out of range !"
# define ERR_SSCANF_MSG "¡ sscanf() error !"
@@ -561,19 +564,17 @@ static double loop_w_getdbl (WINDOW *win, wclear(win);
wrefresh(win);
- err = ERR_OK;
if (x == ERR) {
- err = ERR_GETSTR;
- } else if (sscanf(buff, "%lf", &R) != 1) {
- err = ERR_SSCANF;
- } else if (R < m || R > M) {
- err = ERR_RANGE;
+ err = ERR_GETSTR;
} else {
- break;
+ err = alx_sscan_dbl(&R, m, def, M, buff);
}
- manage_w_error(win, err);
- R = def;
+ if (err) {
+ manage_w_error(win, err);
+ } else {
+ break;
+ }
}
return R;
@@ -595,19 +596,17 @@ static int64_t loop_w_getint (WINDOW *win, wclear(win);
wrefresh(win);
- err = ERR_OK;
if (x == ERR) {
err = ERR_GETSTR;
- } else if (sscanf(buff, "%"SCNi64, &Z) != 1) {
- err = ERR_SSCANF;
- } else if (Z < m || Z > M) {
- err = ERR_RANGE;
} else {
- break;
+ err = alx_sscan_int(&Z, m, def, M, buff);
}
- manage_w_error(win, err);
- Z = def;
+ if (err) {
+ manage_w_error(win, err);
+ } else {
+ break;
+ }
}
return Z;
@@ -634,7 +633,11 @@ static void loop_w_getstr (char *dest, int destsize, WINDOW *win) break;
}
- manage_w_error(win, err);
+ if (err) {
+ manage_w_error(win, err);
+ } else {
+ break;
+ }
}
if (!err) {
@@ -647,11 +650,8 @@ static void loop_w_getfname (const char *fpath, char *fname, bool exist, {
int i;
char buff [FILENAME_MAX];
- char buff2 [FILENAME_MAX];
- char file_path [FILENAME_MAX];
int x;
int err;
- FILE *fp;
for (i = 0; i < MAX_TRIES; i++) {
echo();
@@ -660,38 +660,17 @@ static void loop_w_getfname (const char *fpath, char *fname, bool exist, wclear(win);
wrefresh(win);
- err = ERR_OK;
if (x == ERR) {
- err = ERR_GETSTR;
- } else if (sscanf(buff, " %s ", buff2) != 1) {
- err = ERR_SSCANF;
+ err = ERR_GETSTR;
} else {
- snprintf(file_path, FILENAME_MAX, "%s%s", fpath, buff2);
-
- fp = fopen(file_path, "r");
-
- if (exist) {
- if (fp == NULL) {
- err = ERR_FPTR;
- } else {
- fclose(fp);
- break;
- }
- } else {
- if (fp != NULL) {
- err = ERR_FPTR;
- fclose(fp);
- } else {
- break;
- }
- }
+ err = alx_sscan_fname(fpath, fname, exist, buff);
}
- manage_w_error(win, err);
- }
-
- if (!err) {
- snprintf(fname, FILENAME_MAX, buff2);
+ if (err) {
+ manage_w_error(win, err);
+ } else {
+ break;
+ }
}
}
diff --git a/modules/menu/obj/Makefile b/modules/menu/obj/Makefile index 285cb35..be8ede5 100644 --- a/modules/menu/obj/Makefile +++ b/modules/menu/obj/Makefile @@ -54,7 +54,7 @@ MENUI_INC_DIRS = -I $(INC_DIR) \ -I $(CTRL_INC_DIR) \ -I $(GAME_INC_DIR) -MENUCLUI_INC_LIBALX = alx_getnum.h +MENUCLUI_INC_LIBALX = alx_input.h MENUCLUI_INC_ABOUT = about.h MENUCLUI_INC_CTRL = start.h MENUCLUI_INC_GAME = game_iface.h @@ -94,7 +94,7 @@ MENUTUI_INC_DIRS = -I $(INC_DIR) \ -I $(GAME_INC_DIR) \ -I $(SAVE_INC_DIR) -MENUGUI_INC_LIBALX = alx_getnum.h +MENUGUI_INC_LIBALX = alx_input.h MENUGUI_INC_ABOUT = about.h MENUGUI_INC_CTRL = start.h MENUGUI_INC_GAME = game_iface.h diff --git a/modules/menu/src/menu_clui.c b/modules/menu/src/menu_clui.c index 6fccbe0..dacd569 100644 --- a/modules/menu/src/menu_clui.c +++ b/modules/menu/src/menu_clui.c @@ -17,7 +17,7 @@ /* * * * * * * * * *
* * * Other * * * * * * *
* * * * * * * * * */
- #include "alx_getnum.h"
+ #include "alx_input.h"
#include "about.h"
#include "game_iface.h"
diff --git a/modules/menu/src/menu_gui.c b/modules/menu/src/menu_gui.c index 70c3e17..adff670 100644 --- a/modules/menu/src/menu_gui.c +++ b/modules/menu/src/menu_gui.c @@ -22,7 +22,7 @@ /* * * * * * * * * *
* * * Other * * * * * * *
* * * * * * * * * */
- #include "alx_getnum.h"
+ #include "alx_input.h"
#include "about.h"
#include "game_iface.h"
@@ -75,6 +75,13 @@ struct Entry_int_Data { double max;
};
+struct Entry_file_Data {
+ GtkWidget *ptr;
+ struct Label_Data lbl;
+ char *str;
+ const char *def;
+};
+
/******************************************************************************
******* variables ************************************************************
@@ -498,6 +505,7 @@ static void menu_gui_continue (void) GtkWidget *separator[4];
struct Label_Data label;
struct Button_Data button [7];
+ struct Entry_file_Data entry_file[1];
/* Text */
snprintf(label.text, LINE_SIZE, "Game menu");
|