Skip to content

Commit

Permalink
Code readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cfnptr committed Feb 18, 2024
1 parent b90d906 commit 26afb01
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 32 deletions.
3 changes: 2 additions & 1 deletion include/pack/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static const char* const packResultStrings[PACK_RESULT_COUNT] = {
*/
inline static const char* packResultToString(PackResult result)
{
if (result >= PACK_RESULT_COUNT) return "Unknown PACK result";
if (result >= PACK_RESULT_COUNT)
return "Unknown PACK result";
return packResultStrings[result];
}
2 changes: 1 addition & 1 deletion libraries/mpio
9 changes: 6 additions & 3 deletions source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ PackResult readPackHeader(const char* filePath, PackHeader* _header)
assert(_header);

FILE* file = openFile(filePath, "rb");
if (!file) return FAILED_TO_OPEN_FILE_PACK_RESULT;
if (!file)
return FAILED_TO_OPEN_FILE_PACK_RESULT;

PackHeader header;
size_t result = fread(&header, sizeof(PackHeader), 1, file);
closeFile(file);

if (result != 1) return FAILED_TO_READ_FILE_PACK_RESULT;
if (header.magic != PACK_HEADER_MAGIC) return BAD_FILE_TYPE_PACK_RESULT;
if (result != 1)
return FAILED_TO_READ_FILE_PACK_RESULT;
if (header.magic != PACK_HEADER_MAGIC)
return BAD_FILE_TYPE_PACK_RESULT;

*_header = header;
return SUCCESS_PACK_RESULT;
Expand Down
55 changes: 37 additions & 18 deletions source/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ static PackResult createPackItems(FILE* packFile,
assert(_items != NULL);

PackItem* items = malloc(itemCount * sizeof(PackItem));
if (!items) return FAILED_TO_ALLOCATE_PACK_RESULT;
if (!items)
return FAILED_TO_ALLOCATE_PACK_RESULT;

for (uint64_t i = 0; i < itemCount; i++)
{
Expand Down Expand Up @@ -112,7 +113,8 @@ PackResult createFilePackReader(const char* filePath,
assert(packReader != NULL);

PackReader packReaderInstance = calloc(1, sizeof(PackReader_T));
if (!packReaderInstance) return FAILED_TO_ALLOCATE_PACK_RESULT;
if (!packReaderInstance)
return FAILED_TO_ALLOCATE_PACK_RESULT;

char* path;

Expand Down Expand Up @@ -155,7 +157,8 @@ PackResult createFilePackReader(const char* filePath,
if (!files)
{
#if __APPLE__
if (isResourcesDirectory) free(path);
if (isResourcesDirectory)
free(path);
#endif
destroyPackReader(packReaderInstance);
return FAILED_TO_ALLOCATE_PACK_RESULT;
Expand All @@ -167,7 +170,8 @@ PackResult createFilePackReader(const char* filePath,
if (!zstdContexts)
{
#if __APPLE__
if (isResourcesDirectory) free(path);
if (isResourcesDirectory)
free(path);
#endif
destroyPackReader(packReaderInstance);
return FAILED_TO_ALLOCATE_PACK_RESULT;
Expand All @@ -181,7 +185,8 @@ PackResult createFilePackReader(const char* filePath,
if (!file)
{
#if __APPLE__
if (isResourcesDirectory) free(path);
if (isResourcesDirectory)
free(path);
#endif
destroyPackReader(packReaderInstance);
return FAILED_TO_OPEN_FILE_PACK_RESULT;
Expand All @@ -200,7 +205,8 @@ PackResult createFilePackReader(const char* filePath,
}

#if __APPLE__
if (isResourcesDirectory) free(path);
if (isResourcesDirectory)
free(path);
#endif

PackHeader header;
Expand Down Expand Up @@ -255,7 +261,9 @@ PackResult createFilePackReader(const char* filePath,
}
void destroyPackReader(PackReader packReader)
{
if (!packReader) return;
if (!packReader)
return;

destroyPackItems(packReader->itemCount, packReader->items);

FILE** files = packReader->files;
Expand All @@ -265,7 +273,8 @@ void destroyPackReader(PackReader packReader)
for (uint32_t i = 0; i < threadCount; i++)
{
if (ZSTD_freeDCtx(zstdContexts[i]) != 0) abort();
if (files[i]) closeFile(files[i]);
if (files[i])
closeFile(files[i]);
}

free(packReader->zstdContexts);
Expand All @@ -288,7 +297,8 @@ static int comparePackItems(const void* _a, const void* _b)
const PackItem* a = _a;
const PackItem* b = _b;
int difference = (int)a->header.pathSize - (int)b->header.pathSize;
if (difference != 0) return difference;
if (difference != 0)
return difference;
return memcmp(a->path, b->path, a->header.pathSize);
}
bool getPackItemIndex(PackReader packReader, const char* path, uint64_t* index)
Expand All @@ -305,7 +315,8 @@ bool getPackItemIndex(PackReader packReader, const char* path, uint64_t* index)
PackItem* item = bsearch(&searchItem,
packReader->items, packReader->itemCount,
sizeof(PackItem), comparePackItems);
if (!item) return false;
if (!item)
return false;

*index = item - packReader->items;
return true;
Expand Down Expand Up @@ -336,12 +347,14 @@ PackResult readPackItemData(PackReader packReader,
PackItemHeader header = packReader->items[itemIndex].header;
FILE* file = packReader->files[threadIndex];
int seekResult = seekFile(file, header.dataOffset, SEEK_SET);
if (seekResult != 0) return FAILED_TO_SEEK_FILE_PACK_RESULT;
if (seekResult != 0)
return FAILED_TO_SEEK_FILE_PACK_RESULT;

if (header.zipSize > 0)
{
uint8_t* zipBuffer = malloc(header.zipSize);
if (!zipBuffer) return FAILED_TO_ALLOCATE_PACK_RESULT;
if (!zipBuffer)
return FAILED_TO_ALLOCATE_PACK_RESULT;

if (fread(zipBuffer, sizeof(uint8_t), header.zipSize, file) != header.zipSize)
{
Expand All @@ -359,7 +372,8 @@ PackResult readPackItemData(PackReader packReader,
else
{
size_t result = fread(buffer, sizeof(uint8_t), header.dataSize, file);
if (result != header.dataSize) return FAILED_TO_READ_FILE_PACK_RESULT;
if (result != header.dataSize)
return FAILED_TO_READ_FILE_PACK_RESULT;
}

return SUCCESS_PACK_RESULT;
Expand Down Expand Up @@ -405,7 +419,8 @@ PackResult unpackFiles(const char* filePath, bool printProgress)

PackReader packReader; // TODO: do this from multiple threads.
PackResult packResult = createFilePackReader(filePath, false, 1, &packReader);
if (packResult != SUCCESS_PACK_RESULT) return packResult;
if (packResult != SUCCESS_PACK_RESULT)
return packResult;

uint64_t rawFileSize = 0;
uint64_t fileOffset = sizeof(PackHeader);
Expand All @@ -419,9 +434,12 @@ PackResult unpackFiles(const char* filePath, bool printProgress)
{
int progress = (int)(((float)(i + 1) / (float)itemCount) * 100.0f);
const char* spacing;
if (progress < 10) spacing = " ";
else if (progress < 100) spacing = " ";
else spacing = "";
if (progress < 10)
spacing = " ";
else if (progress < 100)
spacing = " ";
else
spacing = "";
printf("[%s%d%%] Unpacking file %s ", spacing, progress, item->path);
fflush(stdout);
}
Expand Down Expand Up @@ -451,7 +469,8 @@ PackResult unpackFiles(const char* filePath, bool printProgress)

for (uint8_t j = 0; j < pathSize; j++)
{
if (itemPath[j] == '/' || itemPath[j] == '\\') itemPath[j] = '-';
if (itemPath[j] == '/' || itemPath[j] == '\\')
itemPath[j] = '-';
}

FILE* itemFile = openFile(itemPath, "wb");
Expand Down
24 changes: 16 additions & 8 deletions source/writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ static PackResult writePackItems(FILE* packFile, uint64_t itemCount,

uint32_t bufferSize = 1;
uint8_t* itemData = malloc(sizeof(uint8_t));
if (!itemData) return FAILED_TO_ALLOCATE_PACK_RESULT;
if (!itemData)
return FAILED_TO_ALLOCATE_PACK_RESULT;

uint8_t* zipData = malloc(sizeof(uint8_t));
if (!zipData)
Expand All @@ -64,14 +65,18 @@ static PackResult writePackItems(FILE* packFile, uint64_t itemCount,
{
int progress = (int)(((float)(i + 1) / (float)itemCount) * 100.0f);
const char* spacing;
if (progress < 10) spacing = " ";
else if (progress < 100) spacing = " ";
else spacing = "";
if (progress < 10)
spacing = " ";
else if (progress < 100)
spacing = " ";
else
spacing = "";
printf("[%s%d%%] Packing file %s ", spacing, progress, itemPath);
fflush(stdout);
}

if (onPackFile) onPackFile(i, argument);
if (onPackFile)
onPackFile(i, argument);

size_t pathSize = strlen(itemPath);
if (pathSize > UINT8_MAX)
Expand Down Expand Up @@ -162,7 +167,8 @@ static PackResult writePackItems(FILE* packFile, uint64_t itemCount,
for (size_t j = 0; j < i; j++)
{
PackItemHeader* header = &itemHeaders[j];
if (header->zipSize != zipSize || header->dataSize != itemSize) continue;
if (header->zipSize != zipSize || header->dataSize != itemSize)
continue;

if (seekFile(packFile, header->dataOffset, SEEK_SET) != 0)
{
Expand Down Expand Up @@ -276,7 +282,8 @@ static int comparePackPathPairs(const void* _a, const void* _b)
uint8_t al = (uint8_t)strlen(a->itemPath);
uint8_t bl = (uint8_t)strlen(b->itemPath);
int difference = al - bl;
if (difference != 0) return difference;
if (difference != 0)
return difference;
return memcmp(a->itemPath, b->itemPath, al);
}

Expand All @@ -290,7 +297,8 @@ PackResult packFiles(const char* filePath, uint64_t fileCount,
assert(fileItemPaths != NULL);

FileItemPath* pathPairs = malloc(fileCount * sizeof(FileItemPath));
if (!pathPairs) return FAILED_TO_ALLOCATE_PACK_RESULT;
if (!pathPairs)
return FAILED_TO_ALLOCATE_PACK_RESULT;

uint64_t itemCount = 0;
for (uint64_t i = 0; i < fileCount; i++)
Expand Down
3 changes: 2 additions & 1 deletion wrappers/cpp/pack/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ class Reader final
uint64_t index;
auto _path = path.generic_string();
auto result = getPackItemIndex(instance, _path.c_str(), &index);
if (!result) throw runtime_error("Item is not exist");
if (!result)
throw runtime_error("Item is not exist");
return index;
}

Expand Down

0 comments on commit 26afb01

Please sign in to comment.