From 58de344698f856900eb58dafaa813a7753c31b21 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 18 Jul 2022 16:27:44 -0500 Subject: [PATCH] Added a string truncate function to clear the string and reset it to a default state. --- src/main.c | 9 +++++++++ src/tstring.c | 18 ++++++++++++++++++ src/tstring.h | 1 + 3 files changed, 28 insertions(+) diff --git a/src/main.c b/src/main.c index 53dd14c..08f31c8 100644 --- a/src/main.c +++ b/src/main.c @@ -102,6 +102,15 @@ int main(int argc, char** argv) { } } + if (input[0] == 't') { + TStringPrintDetails(string, 0); + + TStringTruncate(string); + + printf("TString truncated\n"); + continue; + } + TStringAppendText(input, string); } diff --git a/src/tstring.c b/src/tstring.c index cd8a482..328f9b5 100644 --- a/src/tstring.c +++ b/src/tstring.c @@ -41,6 +41,24 @@ TString* TStringCreate(void) { return string; } +void TStringTruncate(TString* string) { + if (!string) return; + + char* newBuffer = calloc(DEFAULT_TSTRING_BYTE_BUFFER, sizeof(char)); + + if (!newBuffer) { + fprintf(stderr, "Failed to calloc space during TString truncation. %s\n", strerror(errno)); + return; + } + + free(string->Characters); + + string->Characters = newBuffer; + string->Capacity = DEFAULT_TSTRING_BYTE_BUFFER; + string->ByteCount = 0; + string->Length = 0; +} + int TStringLength(const TString* string) { if (!string) return -1; diff --git a/src/tstring.h b/src/tstring.h index 25bcbcb..3733efc 100644 --- a/src/tstring.h +++ b/src/tstring.h @@ -8,6 +8,7 @@ typedef struct __tstring TString; TString* TStringCreate(void); +void TStringTruncate(TString* string); void TStringAppendText(const char* text, TString* string); void TStringGetGlyph(int index, char buffer[8], const TString* string); void TStringFree(TString* string);