Skip to content

Commit

Permalink
Prefix symbols to prevent potential conflicts when linking to the lib…
Browse files Browse the repository at this point in the history
…rary
  • Loading branch information
davidebeatrici committed May 26, 2021
1 parent e745c1d commit 2951ae5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
14 changes: 7 additions & 7 deletions FileSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <sys/stat.h>

FILE *FileOpen(const char *path, const bool write)
FILE *Ham_FileOpen(const char *path, const bool write)
{
if (!path)
{
Expand All @@ -14,7 +14,7 @@ FILE *FileOpen(const char *path, const bool write)
return fopen(path, write ? "wb" : "rb");
}

bool FileClose(FILE *file)
bool Ham_FileClose(FILE *file)
{
if (!file)
{
Expand All @@ -24,7 +24,7 @@ bool FileClose(FILE *file)
return fclose(file) == 0;
}

bool FileRead(FILE *file, void *dst, const size_t size)
bool Ham_FileRead(FILE *file, void *dst, const size_t size)
{
if (!file || !dst || size == 0)
{
Expand All @@ -34,7 +34,7 @@ bool FileRead(FILE *file, void *dst, const size_t size)
return fread(dst, 1, size, file) == size;
}

bool FileWrite(FILE *file, const void *src, const size_t size)
bool Ham_FileWrite(FILE *file, const void *src, const size_t size)
{
if (!file || !src || size == 0)
{
Expand All @@ -44,7 +44,7 @@ bool FileWrite(FILE *file, const void *src, const size_t size)
return fwrite(src, 1, size, file) == size;
}

bool FileSeek(FILE *file, const size_t offset)
bool Ham_FileSeek(FILE *file, const size_t offset)
{
if (!file)
{
Expand All @@ -54,7 +54,7 @@ bool FileSeek(FILE *file, const size_t offset)
return fseek(file, (long)offset, SEEK_SET) == 0;
}

size_t FileSize(const char *path)
size_t Ham_FileSize(const char *path)
{
if (!path)
{
Expand All @@ -70,7 +70,7 @@ size_t FileSize(const char *path)
return st.st_size;
}

const char *PathRelativeToBase(const char *full, const char *base)
const char *Ham_PathRelativeToBase(const char *full, const char *base)
{
if (!full || !base)
{
Expand Down
14 changes: 7 additions & 7 deletions FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
#include <stdbool.h>
#include <stdio.h>

FILE *FileOpen(const char *path, const bool write);
bool FileClose(FILE *file);
FILE *Ham_FileOpen(const char *path, const bool write);
bool Ham_FileClose(FILE *file);

bool FileRead(FILE *file, void *dst, const size_t size);
bool FileWrite(FILE *file, const void *src, const size_t size);
bool Ham_FileRead(FILE *file, void *dst, const size_t size);
bool Ham_FileWrite(FILE *file, const void *src, const size_t size);

bool FileSeek(FILE *file, const size_t offset);
bool Ham_FileSeek(FILE *file, const size_t offset);

size_t FileSize(const char *path);
size_t Ham_FileSize(const char *path);

const char *PathRelativeToBase(const char *full, const char *base);
const char *Ham_PathRelativeToBase(const char *full, const char *base);

#endif
54 changes: 27 additions & 27 deletions Hamcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ HAMCORE *HamcoreOpen(const char *path)
}
memset(hamcore, 0, sizeof(HAMCORE));

hamcore->File = FileOpen(path, false);
hamcore->File = Ham_FileOpen(path, false);
if (!hamcore->File)
{
free(hamcore);
Expand All @@ -38,7 +38,7 @@ HAMCORE *HamcoreOpen(const char *path)
bool ok = false;

uint8_t header[HAMCORE_HEADER_SIZE];
if (!FileRead(hamcore->File, header, sizeof(header)))
if (!Ham_FileRead(hamcore->File, header, sizeof(header)))
{
goto FINAL;
}
Expand All @@ -49,7 +49,7 @@ HAMCORE *HamcoreOpen(const char *path)
}

uint32_t tmp;
if (!FileRead(hamcore->File, &tmp, sizeof(tmp)))
if (!Ham_FileRead(hamcore->File, &tmp, sizeof(tmp)))
{
goto FINAL;
}
Expand All @@ -66,7 +66,7 @@ HAMCORE *HamcoreOpen(const char *path)

for (size_t i = 0; i < files->Num; ++i)
{
if (!FileRead(hamcore->File, &tmp, sizeof(tmp)))
if (!Ham_FileRead(hamcore->File, &tmp, sizeof(tmp)))
{
goto FINAL;
}
Expand All @@ -81,26 +81,26 @@ HAMCORE *HamcoreOpen(const char *path)
--tmp;
}

if (!FileRead(hamcore->File, file->Path, tmp))
if (!Ham_FileRead(hamcore->File, file->Path, tmp))
{
goto FINAL;
}

if (!FileRead(hamcore->File, &tmp, sizeof(tmp)))
if (!Ham_FileRead(hamcore->File, &tmp, sizeof(tmp)))
{
goto FINAL;
}

file->OriginalSize = BigEndian32(tmp);

if (!FileRead(hamcore->File, &tmp, sizeof(tmp)))
if (!Ham_FileRead(hamcore->File, &tmp, sizeof(tmp)))
{
goto FINAL;
}

file->Size = BigEndian32(tmp);

if (!FileRead(hamcore->File, &tmp, sizeof(tmp)))
if (!Ham_FileRead(hamcore->File, &tmp, sizeof(tmp)))
{
goto FINAL;
}
Expand All @@ -126,7 +126,7 @@ void HamcoreClose(HAMCORE *hamcore)
return;
}

FileClose(hamcore->File);
Ham_FileClose(hamcore->File);

HAMCORE_FILES *files = &hamcore->Files;
if (!files->List)
Expand Down Expand Up @@ -175,15 +175,15 @@ bool HamcoreRead(HAMCORE *hamcore, void *dst, const HAMCORE_FILE *hamcore_file)
return false;
}

if (!FileSeek(hamcore->File, hamcore_file->Offset))
if (!Ham_FileSeek(hamcore->File, hamcore_file->Offset))
{
return false;
}

bool ok = false;

void *buf = malloc(hamcore_file->Size);
if (!FileRead(hamcore->File, buf, hamcore_file->Size))
if (!Ham_FileRead(hamcore->File, buf, hamcore_file->Size))
{
goto FINAL;
}
Expand Down Expand Up @@ -229,7 +229,7 @@ bool HamcoreBuild(const char *dst_path, const char *base_path, const char **src_
continue;
}

FILE *handle = FileOpen(path, false);
FILE *handle = Ham_FileOpen(path, false);
if (!handle)
{
fprintf(stderr, "HamcoreBuild(): Failed to open \"%s\", skipping...\n", path);
Expand All @@ -239,10 +239,10 @@ bool HamcoreBuild(const char *dst_path, const char *base_path, const char **src_
COMPRESSED_FILE *compressed_file = &compressed_files[i];
HAMCORE_FILE *file = &compressed_file->File;

file->OriginalSize = FileSize(path);
file->OriginalSize = Ham_FileSize(path);
void *content = malloc(file->OriginalSize);
int ret = FileRead(handle, content, file->OriginalSize);
FileClose(handle);
int ret = Ham_FileRead(handle, content, file->OriginalSize);
Ham_FileClose(handle);

if (!ret)
{
Expand Down Expand Up @@ -271,7 +271,7 @@ bool HamcoreBuild(const char *dst_path, const char *base_path, const char **src_
continue;
}

const char *relative_path = base_path ? PathRelativeToBase(path, base_path) : path;
const char *relative_path = base_path ? Ham_PathRelativeToBase(path, base_path) : path;
if (!relative_path)
{
fprintf(stderr, "HamcoreBuild(): Failed to get relative path for \"%s\", skipping...\n", path);
Expand Down Expand Up @@ -343,9 +343,9 @@ bool HamcoreBuild(const char *dst_path, const char *base_path, const char **src_
}

void *ptr = buffer;
WriteAndSeek(&ptr, HAMCORE_HEADER_DATA, HAMCORE_HEADER_SIZE);
Ham_WriteAndSeek(&ptr, HAMCORE_HEADER_DATA, HAMCORE_HEADER_SIZE);
uint32_t tmp = BigEndian32((uint32_t)num);
WriteAndSeek(&ptr, &tmp, sizeof(tmp));
Ham_WriteAndSeek(&ptr, &tmp, sizeof(tmp));

for (size_t i = 0; i < num; ++i)
{
Expand All @@ -357,47 +357,47 @@ bool HamcoreBuild(const char *dst_path, const char *base_path, const char **src_

const size_t path_length = strlen(file->Path);
tmp = BigEndian32((uint32_t)path_length + 1);
WriteAndSeek(&ptr, &tmp, sizeof(tmp));
WriteAndSeek(&ptr, file->Path, path_length);
Ham_WriteAndSeek(&ptr, &tmp, sizeof(tmp));
Ham_WriteAndSeek(&ptr, file->Path, path_length);
free(file->Path);

tmp = BigEndian32((uint32_t)file->OriginalSize);
WriteAndSeek(&ptr, &tmp, sizeof(tmp));
Ham_WriteAndSeek(&ptr, &tmp, sizeof(tmp));

tmp = BigEndian32((uint32_t)file->Size);
WriteAndSeek(&ptr, &tmp, sizeof(tmp));
Ham_WriteAndSeek(&ptr, &tmp, sizeof(tmp));

tmp = BigEndian32((uint32_t)file->Offset);
WriteAndSeek(&ptr, &tmp, sizeof(tmp));
Ham_WriteAndSeek(&ptr, &tmp, sizeof(tmp));
}

for (size_t i = 0; i < num; ++i)
{
COMPRESSED_FILE *compressed_file = &compressed_files[i];
WriteAndSeek(&ptr, compressed_file->Data, compressed_file->File.Size);
Ham_WriteAndSeek(&ptr, compressed_file->Data, compressed_file->File.Size);
free(compressed_file->Data);
}

free(compressed_files);

bool ok = false;

FILE *handle = FileOpen(dst_path, true);
FILE *handle = Ham_FileOpen(dst_path, true);
if (!handle)
{
fprintf(stderr, "HamcoreBuild(): Failed to open \"%s\"!\n", dst_path);
goto FINAL;
}

if (!FileWrite(handle, buffer, buffer_size))
if (!Ham_FileWrite(handle, buffer, buffer_size))
{
fprintf(stderr, "HamcoreBuild(): Failed to write \"%s\"!\n", dst_path);
goto FINAL;
}

ok = true;
FINAL:
FileClose(handle);
Ham_FileClose(handle);
free(buffer);
return ok;
}
6 changes: 2 additions & 4 deletions Memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

#include <string.h>

size_t CompressionBufferSize(const size_t original_size) { return original_size * 2 + 256; }

uint32_t Swap32(const uint32_t value)
uint32_t Ham_Swap32(const uint32_t value)
{
uint32_t swapped;
((uint8_t *)&swapped)[0] = ((uint8_t *)&value)[3];
Expand All @@ -14,7 +12,7 @@ uint32_t Swap32(const uint32_t value)
return swapped;
}

void WriteAndSeek(void **dst, const void *src, const size_t size)
void Ham_WriteAndSeek(void **dst, const void *src, const size_t size)
{
if (!dst || !*dst)
{
Expand Down
8 changes: 4 additions & 4 deletions Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#ifdef BYTE_ORDER_BIG_ENDIAN
#define BigEndian32
#else
#define BigEndian32 Swap32
#define BigEndian32 Ham_Swap32
#endif

size_t CompressionBufferSize(const size_t original_size);
#define CompressionBufferSize(original_size) (original_size * 2 + 256)

uint32_t Swap32(const uint32_t value);
uint32_t Ham_Swap32(const uint32_t value);

void WriteAndSeek(void **dst, const void *src, const size_t size);
void Ham_WriteAndSeek(void **dst, const void *src, const size_t size);

#endif

0 comments on commit 2951ae5

Please sign in to comment.