From eef850ed11fb73f1d69b07a12e2140ab773cc4bd Mon Sep 17 00:00:00 2001 From: TheUltimateOptimist Date: Sun, 28 Jan 2024 00:35:54 +0100 Subject: [PATCH] implemented readInput --- src/userinput.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/userinput.c b/src/userinput.c index e161a63..8fe6660 100644 --- a/src/userinput.c +++ b/src/userinput.c @@ -2,6 +2,7 @@ #include #include +#include void trimLeft(char *input) { size_t length = strlen(input); @@ -21,4 +22,28 @@ void trimRight(char *input) { input[index] = '\0'; index--; } +} + +char *readInput() { + size_t bufferSize = 100; + char *buffer = malloc(bufferSize*sizeof(char)); + char c; + int index = 0; + do { + c = fgetc(stdin); + if (!(bufferSize > index)) { + char *newBuffer = malloc(2*bufferSize*sizeof(char)); + for (int i = 0; i < bufferSize; i++) { + newBuffer[i] = buffer[i]; + } + bufferSize *= 2; + } + buffer[index] = c; + index++; + } + while(c != '\n' && c != EOF); + buffer[index - 1] = '\0'; + trimLeft(buffer); + trimRight(buffer); + return buffer; } \ No newline at end of file