preliminary implimentation of dynamic-sized input

This commit is contained in:
Roland W-H 2022-11-08 11:39:30 +00:00
parent cb01b27501
commit 4c9614342e
5 changed files with 77 additions and 2 deletions

View File

@ -13,12 +13,14 @@
<ItemGroup>
<ClCompile Include="src\caesarcrypt.c" />
<ClCompile Include="src\crypt.c" />
<ClCompile Include="src\dyninput.c" />
</ItemGroup>
<ItemGroup>
<None Include="caesarcrypt.licenseheader" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\crypt.h" />
<ClInclude Include="src\dyninput.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>

View File

@ -21,6 +21,9 @@
<ClCompile Include="src\crypt.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\dyninput.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="caesarcrypt.licenseheader" />
@ -29,5 +32,8 @@
<ClInclude Include="src\crypt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\dyninput.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -9,6 +9,7 @@
*/
#include "crypt.h"
#include "dyninput.h"
#include <stdio.h>
#include <stdlib.h>
@ -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;
}

47
src/dyninput.c Normal file
View File

@ -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 <stdint.h>
#include <stdlib.h>
#include <stdio.h>
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;
}

16
src/dyninput.h Normal file
View File

@ -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 <stdint.h>
char* dyninput_str(int_least64_t maxsize);