Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions Benchmark/BenchmarkDeflateInflateTextFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ B63_BENCHMARK(Deflate_zlib_SmallTextFile, n) {
FILE *pOutCompressedFile = NULL;

B63_SUSPEND {
OpenFile(&pInFile, g_pFullPathToBenchmarkTestFiles,
"SmallBasicTextFile.txt", "r");
OpenFile(&pOutCompressedFile, g_pFullPathToBenchmarkTestFiles,
"SmallBasicTextFile.compressed.txt", "w");
RAII_STRING pathToSmallBasicTextFile =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&pathToSmallBasicTextFile,
"SmallBasicTextFile.txt");

RAII_STRING pathToSmallBasicTextFileCompressedFile =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&pathToSmallBasicTextFileCompressedFile,
"SmallBasicTextFile.compressed.txt");

OpenFile(&pInFile, &pathToSmallBasicTextFile, "r");
OpenFile(&pOutCompressedFile, &pathToSmallBasicTextFileCompressedFile,
"w");
}

const DEFLATE_RETURN_CODES statusDeflate =
Expand All @@ -40,10 +49,19 @@ B63_BENCHMARK(Inflate_zlib_SmallTextFile, n) {
FILE *pOutDecompressedFile = NULL;

B63_SUSPEND {
OpenFile(&pCompressedFile, g_pFullPathToBenchmarkTestFiles,
"SmallBasicTextFileCompressed.txt", "r");
OpenFile(&pOutDecompressedFile, g_pFullPathToBenchmarkTestFiles,
"SmallBasicTextFile.decompressed.txt", "w+");
RAII_STRING pathToSmallBasicTextFileCompressed =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&pathToSmallBasicTextFileCompressed,
"SmallBasicTextFileCompressed.txt");

RAII_STRING pathToSmallBasicTextFileDecompressedFile =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&pathToSmallBasicTextFileDecompressedFile,
"SmallBasicTextFile.decompressed.txt");

OpenFile(&pCompressedFile, &pathToSmallBasicTextFileCompressed, "r");
OpenFile(&pOutDecompressedFile,
&pathToSmallBasicTextFileDecompressedFile, "w");
}

const INFLATE_RETURN_CODES statusInflate =
Expand Down
70 changes: 56 additions & 14 deletions CoDeLib/RaiiString/src/RaiiString.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,32 @@
#include <stdlib.h>
#include <string.h>

RaiiString RaiiStringCreate(size_t lengthWithTermination) {
RaiiString RaiiStringCreateFromCString(const char *pCString) {
const size_t lengthWithoutTermination =
strnlen(pCString, MAX_CSTRING_INCLUDING_TERMINATION_LENGTH);

RaiiString newRaiistring = {NULL, 0};

if (lengthWithTermination == 0 ||
lengthWithTermination >= MAX_CSTRING_INCLUDING_TERMINATION_LENGTH) {
if (lengthWithoutTermination == 0 ||
lengthWithoutTermination == MAX_CSTRING_INCLUDING_TERMINATION_LENGTH) {
return newRaiistring;
}

const size_t lengthWithTermination = lengthWithoutTermination + 1;

newRaiistring.lengthWithTermination = lengthWithTermination;
newRaiistring.pString = (char *)calloc(lengthWithTermination, sizeof(char));

if (newRaiistring.pString == NULL) {
newRaiistring.lengthWithTermination = 0;
printf("Failed to allocate memory for RaiiString\n");
exit(1);
}
return newRaiistring;
}

RaiiString RaiiStringCreateFromCString(const char *pCString) {
const size_t length =
strnlen(pCString, MAX_CSTRING_INCLUDING_TERMINATION_LENGTH);
RaiiString newRaiistring = RaiiStringCreate(length + 1);

if (newRaiistring.pString == NULL) {
return newRaiistring;
}

strncpy(newRaiistring.pString, pCString,
newRaiistring.lengthWithTermination - 1);
newRaiistring.pString[newRaiistring.lengthWithTermination - 1] = '\0';

return newRaiistring;
}

Expand All @@ -43,3 +39,49 @@ void RaiiStringClean(RaiiString *pThis) {
}
pThis->lengthWithTermination = 0;
}

bool RaiiStringAppend_RaiiString(RaiiString *pThis,
const RaiiString *pStringToAppend) {
const bool pointersAreValid =
(pThis != NULL && pThis->pString != NULL) &&
(pStringToAppend != NULL && pStringToAppend->pString != NULL);

if (!pointersAreValid) {
return false;
}

// Subtract one to account for the redundant null-terminator
const size_t newLengthWithTermination =
pThis->lengthWithTermination - 1 +
pStringToAppend->lengthWithTermination;

if (newLengthWithTermination > MAX_CSTRING_INCLUDING_TERMINATION_LENGTH) {
return false;
}

char *pNewString = (char *)realloc(pThis->pString,
newLengthWithTermination * sizeof(char));
if (pNewString == NULL) {
return false;
}

// This method takes into account that the strings are appended to
// themselves.
memmove(&pNewString[pThis->lengthWithTermination - 1],
pStringToAppend->pString,
(pStringToAppend->lengthWithTermination - 1) * sizeof(char));
pNewString[newLengthWithTermination - 1] = '\0';

pThis->pString = pNewString;
pThis->lengthWithTermination = newLengthWithTermination;

return true;
}

bool RaiiStringAppend_cString(RaiiString *pThis, const char *pOther) {
RAII_STRING other = RaiiStringCreateFromCString(pOther);
if (other.pString == NULL) {
return false;
}
return RaiiStringAppend_RaiiString(pThis, &other);
}
30 changes: 3 additions & 27 deletions CoDeLib/Test/Utility/FileUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,10 @@
#include <stdlib.h>
#include <string.h>

void CreateFullPathToFile(const RaiiString *pFullPath,
const size_t maxFullPathStringSize,
const char *pBasePath, const char *pFileName) {
assert(pFullPath != NULL);
assert(pBasePath != NULL);
assert(pFileName != NULL);
assert(pFullPath->pString != NULL);
assert(pFullPath->lengthWithTermination >= maxFullPathStringSize);

const int charsWritten = snprintf(pFullPath->pString, maxFullPathStringSize,
"%s%s", pBasePath, pFileName);

if (charsWritten < 0) {
printf("Failed to write to string\n");
exit(1);
}
}

void OpenFile(FILE **pInFile, char *basePath, char *pFilename,
char *pOpenMode) {
const size_t lengthPath = strlen(basePath) + strlen(pFilename) + 1;
RaiiString fullPathString __attribute__((cleanup(RaiiStringClean)));
fullPathString = RaiiStringCreate(lengthPath);
CreateFullPathToFile(&fullPathString, lengthPath, basePath, pFilename);

*pInFile = fopen(fullPathString.pString, pOpenMode);
void OpenFile(FILE **pInFile, RaiiString *pFullPath, char *pOpenMode) {
*pInFile = fopen(pFullPath->pString, pOpenMode);
if (*pInFile == NULL) {
printf("Failed to open file: %s\n", fullPathString.pString);
printf("Failed to open file: %s\n", pFullPath->pString);
exit(1);
}
}
Expand Down
25 changes: 19 additions & 6 deletions CoDeLib/Test/src/TestDeflateInflateZlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,25 @@ TEST(TestDeflateInflateZlib, test_InflateZlibWorkWithDeflateZlib) {
FILE *pOutCompressedFile = NULL;
FILE *pOutDecompressedFile = NULL;

OpenFile(&pInFile, g_pFullPathToBenchmarkTestFiles,
"SmallBasicTextFile.txt", "r");
OpenFile(&pOutCompressedFile, g_pFullPathToBenchmarkTestFiles,
"SmallBasicTextFile.compressed.txt", "w+");
OpenFile(&pOutDecompressedFile, g_pFullPathToBenchmarkTestFiles,
"SmallBasicTextFile.decompressed.txt", "w+");
RAII_STRING pathToSmallBasicTextFile =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&pathToSmallBasicTextFile,
"SmallBasicTextFile.txt");

RAII_STRING pathToSmallBasicTextFileCompressed =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&pathToSmallBasicTextFileCompressed,
"SmallBasicTextFile.compressed.txt");

RAII_STRING pathToSmallBasicTextFileDecompressed =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&pathToSmallBasicTextFileDecompressed,
"SmallBasicTextFile.decompressed.txt");

OpenFile(&pInFile, &pathToSmallBasicTextFile, "r");
OpenFile(&pOutCompressedFile, &pathToSmallBasicTextFileCompressed, "w+");
OpenFile(&pOutDecompressedFile, &pathToSmallBasicTextFileDecompressed,
"w+");

const DEFLATE_RETURN_CODES statusDeflate =
deflate_zlib.Deflate(pInFile, pOutCompressedFile, NULL);
Expand Down
Loading