From 50655fbc1a9c37319079fe55f52ae549706046e1 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sun, 3 Jul 2022 11:56:24 -0500 Subject: [PATCH] Added an option to read input from a file. --- futil.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ futil.h | 15 +++++++++++++++ main.c | 50 +++++++++++++++++--------------------------------- 3 files changed, 88 insertions(+), 33 deletions(-) create mode 100644 futil.c create mode 100644 futil.h diff --git a/futil.c b/futil.c new file mode 100644 index 0000000..890cf76 --- /dev/null +++ b/futil.c @@ -0,0 +1,56 @@ +#include "futil.h" + +int ReadAllString(const char* path, char** content_ptr, size_t* bytes_read) { + if (!path || !content_ptr || !bytes_read) return 0; + + FILE *file = fopen(path, "rb"); //Always open the file in binary mode. + + if (!file) { + fprintf(stderr, "Failed to open '%s'. %s.\n", path, strerror(errno)); + return 0; + } + + char *content = NULL, *temp; + size_t used = 0, capacity = 0, read = 0; //Allows initialize stack allocated values to zero! + + while(1) { + if (used + FUTIL_READ_SIZE + 1 > capacity) { + capacity = used + FUTIL_READ_SIZE + 1; + + temp = realloc(content, capacity); + + if (!temp) { + fprintf(stderr, "Failed to realloc space for file contents. %s.\n", strerror(errno)); + free(content); + fclose(file); + return 0; + } + + content = temp; + } + + read = fread(content + used, 1, FUTIL_READ_SIZE, file); + + if (read == 0) break; + + used += read; + } + + fclose(file); + //Shrink the buffer to fit just the contents of the buffer. + temp = realloc(content, used + 1); + + if (!temp) { + fprintf(stderr, "Failed to realloc to only hold the file contents. %s.\n", strerror(errno)); + free(content); + return 0; + } + + content = temp; + content[used] = '\0'; + + *content_ptr = content; + *bytes_read = used; + + return 1; +} \ No newline at end of file diff --git a/futil.h b/futil.h new file mode 100644 index 0000000..d062d45 --- /dev/null +++ b/futil.h @@ -0,0 +1,15 @@ +#ifndef FUTIL_H +#define FUTIL_H + +#include +#include +#include +#include + +#ifndef FUTIL_READ_SIZE +#define FUTIL_READ_SIZE 2097152 //Default here is 2MiB +#endif + +int ReadAllString(const char*, char**, size_t*); + +#endif \ No newline at end of file diff --git a/main.c b/main.c index 56736ca..788d5e7 100644 --- a/main.c +++ b/main.c @@ -2,43 +2,13 @@ #include #include #include "tstring.h" - -// const unsigned char First = 0x70; -// const unsigned char Teo = 0xE0; -// const unsigned char Three = 0xF0; -// const unsigned char Four = 0xF7; +#include "futil.h" int main(int argc, char** argv) { char input[1024] = { 0 }; char glyph[8] = { 0 }; -// char characters[8] = { 42 }; - -// int index = 2; -// TString* string = TStringCreate(); -// //0xF0 0xA0 0xBA 0x8C -// char* test = u8"\xE5\x87\x84"; -// char* test2 = u8"\xE3\x81\x84"; -// char* test4 = u8"\xF0\xA0\xBA\x8C"; -// char* test5 = "B"; -// //char* test3 = " Just some extra texts"; - -// TStringAppendText(test, string); -// TStringAppendText(test2, string); -// TStringAppendText(test4, string); -// //TStringAppendText(test5, string); -// //TStringAppendText(test3, string); - -// //TStringGetGlyph(index, characters, string); -// TStringGetGlyph(3, glyph, string); -// //TStringPrintDetails(string); -// TStringInsertText("END", 2, string); -// TStringPrintDetails(string); -// TStringPop(glyph, string); -// printf("%s\n", glyph); -// TStringPrintDetails(string); -// TStringPop(glyph, string); -// TStringPrintDetails(string); -// printf("%s\n", glyph); + char* file; + size_t bytesRead; TString* string = TStringCreate(); @@ -72,6 +42,20 @@ int main(int argc, char** argv) { continue; } + if (input[0] == 'f') { + printf("Enter file path to open: "); + fgets(input, sizeof input, stdin); + input[strlen(input) - 1] = '\0'; + + if (ReadAllString(input, &file, &bytesRead)) { + TStringAppendText(file, string); + printf("Read %ld byte(s)\n", bytesRead); + free(file); + } + + continue; + } + if (input[0] == 'r') { TStringPop(glyph, string); printf("Popped '%s'\n", glyph);