aboutsummaryrefslogtreecommitdiff
path: root/lib/user_input.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2020-03-18 22:25:27 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2020-03-18 22:25:27 -0400
commitccaeb7092b5ad40b1b3833c987ba3ec4d47f0bb8 (patch)
treeae167772a9a2343aa77bf8944b56abe853f6a2ec /lib/user_input.c
parent3731bb4679ee8716d4f579d5744c36a2d1b4a257 (diff)
downloadspmc-ccaeb7092b5ad40b1b3833c987ba3ec4d47f0bb8.tar.gz
Refactor project: build/install libspm[_static.a].so to make unit testing possible
Diffstat (limited to 'lib/user_input.c')
-rw-r--r--lib/user_input.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/user_input.c b/lib/user_input.c
new file mode 100644
index 0000000..3f358fa
--- /dev/null
+++ b/lib/user_input.c
@@ -0,0 +1,75 @@
+#include "spm.h"
+
+/**
+ * Basic case-insensitive interactive choice function
+ * @param answer
+ * @param answer_default
+ * @return
+ */
+int spm_user_yesno(int answer, int empty_input_is_yes) {
+ int result = 0;
+ answer = tolower(answer);
+
+ if (answer == 'y') {
+ result = 1;
+ } else if (answer == 'n') {
+ result = 0;
+ } else {
+ if (empty_input_is_yes) {
+ result = 1;
+ } else {
+ result = -1;
+ }
+ }
+
+ return result;
+}
+
+int spm_prompt_user(const char *msg, int empty_input_is_yes) {
+ int user_choice = 0;
+ int status_choice = 0;
+ char ch_yes = 'y';
+ char ch_no = 'n';
+
+ if (empty_input_is_yes) {
+ ch_yes = 'Y';
+ } else {
+ ch_no = 'N';
+ }
+
+ printf("\n%s [%c/%c] ", msg, ch_yes, ch_no);
+ while ((user_choice = getchar())) {
+ status_choice = spm_user_yesno(user_choice, 1);
+ if (status_choice == 0) { // No
+ break;
+ } else if (status_choice == 1) { // Yes
+ break;
+ } else { // Only triggers when spm_user_yesno's second argument is zero
+ puts("Please answer 'y' or 'n'...");
+ }
+ }
+ puts("");
+
+ return status_choice;
+}
+
+
+void spm_user_yesno_test() {
+ int choice;
+ int status;
+ while ((choice = getchar())) {
+ status = spm_user_yesno(choice, 1);
+ if (status == -1) {
+ puts("Please answer Y or N");
+ continue;
+ }
+ else if (status == 0) {
+ puts("You answered no");
+ break;
+ }
+ else if (status == 1) {
+ puts("You answered yes");
+ break;
+ }
+ }
+}