aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/str.c41
-rw-r--r--lib/user_input.c73
2 files changed, 60 insertions, 54 deletions
diff --git a/lib/str.c b/lib/str.c
index 5bf95ec..9b5b201 100644
--- a/lib/str.c
+++ b/lib/str.c
@@ -776,3 +776,44 @@ char **strdup_array(char **array) {
return result;
}
+
+/**
+ * Compare two arrays of strings
+ *
+ * `a` and/or `b` may be `NULL`. You should test for `NULL` in advance if _your_ program considers this an error condition.
+ *
+ * @param a array of strings
+ * @param b array of strings
+ * @return 0 = identical
+ */
+int strcmp_array(const char **a, const char **b) {
+ size_t a_len = 0;
+ size_t b_len = 0;
+
+ // This could lead to false-positives depending on what the caller plans to achieve
+ if (a == NULL && b == NULL) {
+ return 0;
+ } else if (a == NULL) {
+ return -1;
+ } else if (b == NULL) {
+ return 1;
+ }
+
+ // Get length of arrays
+ for (a_len = 0; a[a_len] != NULL; a_len++);
+ for (b_len = 0; b[b_len] != NULL; b_len++);
+
+ // Check lengths are equal
+ if (a_len < b_len) return (int)(b_len - a_len);
+ else if (a_len > b_len) return (int)(a_len - b_len);
+
+ // Compare strings in the arrays returning the total difference in bytes
+ int result = 0;
+ for (size_t ai = 0, bi = 0 ;a[ai] != NULL || b[bi] != NULL; ai++, bi++) {
+ int status = 0;
+ if ((status = strcmp(a[ai], b[bi]) != 0)) {
+ result += status;
+ }
+ }
+ return result;
+}
diff --git a/lib/user_input.c b/lib/user_input.c
index 3f358fa..416b59f 100644
--- a/lib/user_input.c
+++ b/lib/user_input.c
@@ -1,32 +1,6 @@
#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';
@@ -38,38 +12,29 @@ int spm_prompt_user(const char *msg, int empty_input_is_yes) {
}
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;
-}
+ char ch[2] = {0,};
+ int input_count = 0;
+ while (scanf("%c", ch) == 1) {
+ ch[1] = '\0';
-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");
+ if (input_count != 0) {
+ input_count = 0;
+ ch[0] = '\0';
continue;
}
- else if (status == 0) {
- puts("You answered no");
- break;
- }
- else if (status == 1) {
- puts("You answered yes");
- break;
+
+ if ((isempty(ch) && empty_input_is_yes != 0)) {
+ return 1;
+ } else if ((isempty(ch) && empty_input_is_yes == 0)) {
+ return 0;
+ } else if (tolower(ch[0]) == tolower(ch_yes)) { // Yes
+ return 1;
+ } else if (tolower(ch[0]) == tolower(ch_no)) { // No
+ return 0;
+ } else {
+ printf("Please answer '%c' or '%c'...\n", tolower(ch_yes), tolower(ch_no));
}
+ input_count++;
}
}