From 4c9614342e0cc6e509324f21b5c2a097e991442a Mon Sep 17 00:00:00 2001 From: Roland W-H Date: Tue, 8 Nov 2022 11:39:30 +0000 Subject: [PATCH] preliminary implimentation of dynamic-sized input --- caesarcrypt.vcxproj | 2 ++ caesarcrypt.vcxproj.filters | 6 +++++ src/caesarcrypt.c | 8 +++++-- src/dyninput.c | 47 +++++++++++++++++++++++++++++++++++++ src/dyninput.h | 16 +++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/dyninput.c create mode 100644 src/dyninput.h diff --git a/caesarcrypt.vcxproj b/caesarcrypt.vcxproj index 17290f0..aaf6fc9 100644 --- a/caesarcrypt.vcxproj +++ b/caesarcrypt.vcxproj @@ -13,12 +13,14 @@ + + 16.0 diff --git a/caesarcrypt.vcxproj.filters b/caesarcrypt.vcxproj.filters index c52380a..64627fc 100644 --- a/caesarcrypt.vcxproj.filters +++ b/caesarcrypt.vcxproj.filters @@ -21,6 +21,9 @@ Source Files + + Source Files + @@ -29,5 +32,8 @@ Header Files + + Source Files + \ No newline at end of file diff --git a/src/caesarcrypt.c b/src/caesarcrypt.c index b2e6c65..1fb2713 100644 --- a/src/caesarcrypt.c +++ b/src/caesarcrypt.c @@ -9,6 +9,7 @@ */ #include "crypt.h" +#include "dyninput.h" #include #include @@ -28,10 +29,11 @@ int main() fgets(buf, 3, stdin); int choice = strtol(buf, NULL, 10); - char* msg = calloc(128, sizeof(char)); + //char* msg = calloc(128, sizeof(char)); fputs("Enter your message text: ", stdout); - if (msg) fgets(msg, 128, stdin); + //if (msg) fgets(msg, 128, stdin); + char* msg = dyninput_str(INT64_MAX); fputs("Enter your key (shift value): ", stdout); fgets(buf, 4, stdin); @@ -56,5 +58,7 @@ int main() } } + free(msg); + msg = NULL; return 0; } diff --git a/src/dyninput.c b/src/dyninput.c new file mode 100644 index 0000000..bcb8b5e --- /dev/null +++ b/src/dyninput.c @@ -0,0 +1,47 @@ +/* + * Filename: dyninput.c + * Authors(s): Roland (Roland@example.com) + * Description: Short description here + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ + +#include +#include +#include + + +char* dyninput_str(int_least64_t maxsize) +{ + char* str = malloc(3 * sizeof(char)); + /*if (!str) + puts("Error allocating memory!"); + return NULL;*/ + + int_least64_t size = 3, i = 0; + char c = 0; + + do + { + c = getchar(); + if (i + 2 == size) + { + char* tmp = realloc(str, size + 1); + if (!tmp) + { + puts("Error (re)allocating memory!"); + return NULL; + } + str = tmp; + size++; + } + + str[i] = c; + i++; + } while (c && c != '\n'); + str[i] = '\0'; + + return str; +} diff --git a/src/dyninput.h b/src/dyninput.h new file mode 100644 index 0000000..efdabc4 --- /dev/null +++ b/src/dyninput.h @@ -0,0 +1,16 @@ +/* + * Filename: dyninput.h + * Authors(s): Roland (r.weirhowell@gmail.com) + * Description: Header file for dyninput.c + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include + + +char* dyninput_str(int_least64_t maxsize);