From d13e3280c039bca31fbef2670fbe2e848fbf3112 Mon Sep 17 00:00:00 2001 From: TheUltimateOptimist Date: Mon, 29 Jan 2024 01:40:06 +0100 Subject: [PATCH] refactoring: replaced printf with printText --- src/userinput.c | 77 +++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/src/userinput.c b/src/userinput.c index 73611a0..41bf30d 100644 --- a/src/userinput.c +++ b/src/userinput.c @@ -20,6 +20,12 @@ char **fakeInput = NULL; name##ld = &name##ldvalue;\ } +void printText(char *text) { + if (fakeInput == NULL) { + printf("%s", text); + fflush(stdout); + } +} void trimLeft(char *input) { size_t length = strlen(input); @@ -44,7 +50,7 @@ void trimRight(char *input) { char *readInput() { if (fakeInput != NULL) { if (fakeInput[0] == NULL) { - printf("The given list of inputs in the test was exceeded!\n"); + printText("The given list of inputs in the test was exceeded!\n"); exit(1); } char *result = malloc(strlen(fakeInput[0])*sizeof(char) + 1); @@ -75,54 +81,58 @@ char *readInput() { return buffer; } +void printlf(long double value) { + if (fakeInput != NULL) { + return; + } + if (roundl(value) == value) { + printf("%.0Lf", value); + } + else { + printf("%Lf", value); + } +} + char *gets(char *message, unsigned long *minLength, unsigned long *maxLength) { - printf("%s", message); - fflush(stdout); + printText(message); char *result = readInput(); while (minLength != NULL && strlen(result) < *minLength || maxLength != NULL && strlen(result) > *maxLength) { - printf("%s", "Ungueltige Eingabe! "); + printText("Ungueltige Eingabe! "); if (minLength != NULL && maxLength == NULL) { - printf("Die Eingabe muss mind. %lu Zeichen lang sein.\n", *minLength); + printText("Die Eingabe muss mind. "); + printlf(*minLength); + printText(" Zeichen lang sein.\n"); } else if (maxLength != NULL && minLength == NULL) { - printf("Die Eingabe darf maximal %lu Zeichen lang sein.\n", *maxLength); + printf("Die Eingabe darf maximal "); + printlf(*maxLength); + printText(" Zeichen lang sein.\n"); } else { - printf("Die Eingabe muss %lu bis %lu Zeichen lang sein.\n", *minLength, *maxLength); + printText("Die Eingabe muss "); + printlf(*minLength); + printText(" bis "); + printlf(*maxLength); + printText(" Zeichen lang sein.\n"); } - printf("%s", message); - fflush(stdout); + printText(message); result = readInput(); } return result; } -void printlf(long double value) { - if (fakeInput != NULL) { - return; - } - if (roundl(value) == value) { - printf("%.0Lf", value); - } - else { - printf("%Lf", value); - } -} - long double getNumber(char *message, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) { while (1) { char *input = gets(message, NULL, NULL); if (strlen(input) == 0) { - printf("%s\n", "Fehlende Eingabe!"); - fflush(stdout); + printText("Fehlende Eingabe!\n"); free(input); continue; } char *endptr; long double number = strtold(input, &endptr); if (strlen(endptr) > 0) { - printf("%s\n", "Ungueltige Eingabe! Gib eine gueltige Zahl ein."); - fflush(stdout); + printText("Ungueltige Eingabe! Gib eine gueltige Zahl ein.\n"); free(input); continue; } @@ -149,33 +159,32 @@ long double getNumber(char *message, long double *min, long double *max, long do if (isValid) { return number; } - printf("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:"); + printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:"); if (min != NULL) { - printf(" >="); + printText(" >="); printlf(*min); } if (max != NULL) { - printf(" <="); + printText(" <="); printlf(*max); } if (greaterThan != NULL) { - printf(" >"); + printText(" >"); printlf(*greaterThan); } if (smallerThan != NULL) { - printf(" <"); + printText(" <"); printlf(*smallerThan); } if (min == NULL && greaterThan == NULL) { - printf(" >="); + printText(" >="); printlf(leftBorder); } if (max == NULL && smallerThan == NULL) { - printf(" <="); + printText(" <="); printlf(rightBorder); } - printf("\n"); - fflush(stdout); + printText("\n"); } }