Skip to content

Commit

Permalink
lone/types: rename endian readers/writers
Browse files Browse the repository at this point in the history
Make the be/le suffix a part of the type name.
This is more consistent all things considered
and also simplifies code generation with macros
since not passing the endianness omits the type
prefix entirely with no need to hack in underscores.
  • Loading branch information
matheusmoreira committed Sep 11, 2024
1 parent ee719ee commit 6d858b3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 88 deletions.
108 changes: 54 additions & 54 deletions include/lone/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,33 +224,33 @@ void lone_s32_write(void *address, lone_s32 s32);
void lone_u64_write(void *address, lone_u64 u64);
void lone_s64_write(void *address, lone_s64 s64);

lone_u16 lone_u16_read_le(void *address);
lone_s16 lone_s16_read_le(void *address);
lone_u32 lone_u32_read_le(void *address);
lone_s32 lone_s32_read_le(void *address);
lone_u64 lone_u64_read_le(void *address);
lone_s64 lone_s64_read_le(void *address);

lone_u16 lone_u16_read_be(void *address);
lone_s16 lone_s16_read_be(void *address);
lone_u32 lone_u32_read_be(void *address);
lone_s32 lone_s32_read_be(void *address);
lone_u64 lone_u64_read_be(void *address);
lone_s64 lone_s64_read_be(void *address);

void lone_u16_write_le(void *address, lone_u16 u16);
void lone_s16_write_le(void *address, lone_s16 s16);
void lone_u32_write_le(void *address, lone_u32 u32);
void lone_s32_write_le(void *address, lone_s32 s32);
void lone_u64_write_le(void *address, lone_u64 u64);
void lone_s64_write_le(void *address, lone_s64 s64);

void lone_u16_write_be(void *address, lone_u16 u16);
void lone_s16_write_be(void *address, lone_s16 s16);
void lone_u32_write_be(void *address, lone_u32 u32);
void lone_s32_write_be(void *address, lone_s32 s32);
void lone_u64_write_be(void *address, lone_u64 u64);
void lone_s64_write_be(void *address, lone_s64 s64);
lone_u16 lone_u16le_read(void *address);
lone_s16 lone_s16le_read(void *address);
lone_u32 lone_u32le_read(void *address);
lone_s32 lone_s32le_read(void *address);
lone_u64 lone_u64le_read(void *address);
lone_s64 lone_s64le_read(void *address);

lone_u16 lone_u16be_read(void *address);
lone_s16 lone_s16be_read(void *address);
lone_u32 lone_u32be_read(void *address);
lone_s32 lone_s32be_read(void *address);
lone_u64 lone_u64be_read(void *address);
lone_s64 lone_s64be_read(void *address);

void lone_u16le_write(void *address, lone_u16 u16);
void lone_s16le_write(void *address, lone_s16 s16);
void lone_u32le_write(void *address, lone_u32 u32);
void lone_s32le_write(void *address, lone_s32 s32);
void lone_u64le_write(void *address, lone_u64 u64);
void lone_s64le_write(void *address, lone_s64 s64);

void lone_u16be_write(void *address, lone_u16 u16);
void lone_s16be_write(void *address, lone_s16 s16);
void lone_u32be_write(void *address, lone_u32 u32);
void lone_s32be_write(void *address, lone_s32 s32);
void lone_u64be_write(void *address, lone_u64 u64);
void lone_s64be_write(void *address, lone_s64 s64);

/* ╭────────────────────────────────────────────────────────────────────────╮
│ │
Expand Down Expand Up @@ -282,33 +282,33 @@ bool lone_bytes_write_s32(struct lone_bytes bytes, lone_size offset, lone_s32 s3
bool lone_bytes_write_u64(struct lone_bytes bytes, lone_size offset, lone_u64 u64);
bool lone_bytes_write_s64(struct lone_bytes bytes, lone_size offset, lone_s64 s64);

struct lone_optional_u16 lone_bytes_read_u16_le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s16 lone_bytes_read_s16_le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_u32 lone_bytes_read_u32_le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s32 lone_bytes_read_s32_le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_u64 lone_bytes_read_u64_le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s64 lone_bytes_read_s64_le(struct lone_bytes bytes, lone_size offset);

bool lone_bytes_write_u16_le(struct lone_bytes bytes, lone_size offset, lone_u16 u16);
bool lone_bytes_write_s16_le(struct lone_bytes bytes, lone_size offset, lone_s16 s16);
bool lone_bytes_write_u32_le(struct lone_bytes bytes, lone_size offset, lone_u32 u32);
bool lone_bytes_write_s32_le(struct lone_bytes bytes, lone_size offset, lone_s32 s32);
bool lone_bytes_write_u64_le(struct lone_bytes bytes, lone_size offset, lone_u64 u64);
bool lone_bytes_write_s64_le(struct lone_bytes bytes, lone_size offset, lone_s64 s64);

struct lone_optional_u16 lone_bytes_read_u16_be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s16 lone_bytes_read_s16_be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_u32 lone_bytes_read_u32_be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s32 lone_bytes_read_s32_be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_u64 lone_bytes_read_u64_be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s64 lone_bytes_read_s64_be(struct lone_bytes bytes, lone_size offset);

bool lone_bytes_write_u16_be(struct lone_bytes bytes, lone_size offset, lone_u16 u16);
bool lone_bytes_write_s16_be(struct lone_bytes bytes, lone_size offset, lone_s16 s16);
bool lone_bytes_write_u32_be(struct lone_bytes bytes, lone_size offset, lone_u32 u32);
bool lone_bytes_write_s32_be(struct lone_bytes bytes, lone_size offset, lone_s32 s32);
bool lone_bytes_write_u64_be(struct lone_bytes bytes, lone_size offset, lone_u64 u64);
bool lone_bytes_write_s64_be(struct lone_bytes bytes, lone_size offset, lone_s64 s64);
struct lone_optional_u16 lone_bytes_read_u16le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s16 lone_bytes_read_s16le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_u32 lone_bytes_read_u32le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s32 lone_bytes_read_s32le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_u64 lone_bytes_read_u64le(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s64 lone_bytes_read_s64le(struct lone_bytes bytes, lone_size offset);

bool lone_bytes_write_u16le(struct lone_bytes bytes, lone_size offset, lone_u16 u16);
bool lone_bytes_write_s16le(struct lone_bytes bytes, lone_size offset, lone_s16 s16);
bool lone_bytes_write_u32le(struct lone_bytes bytes, lone_size offset, lone_u32 u32);
bool lone_bytes_write_s32le(struct lone_bytes bytes, lone_size offset, lone_s32 s32);
bool lone_bytes_write_u64le(struct lone_bytes bytes, lone_size offset, lone_u64 u64);
bool lone_bytes_write_s64le(struct lone_bytes bytes, lone_size offset, lone_s64 s64);

struct lone_optional_u16 lone_bytes_read_u16be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s16 lone_bytes_read_s16be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_u32 lone_bytes_read_u32be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s32 lone_bytes_read_s32be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_u64 lone_bytes_read_u64be(struct lone_bytes bytes, lone_size offset);
struct lone_optional_s64 lone_bytes_read_s64be(struct lone_bytes bytes, lone_size offset);

bool lone_bytes_write_u16be(struct lone_bytes bytes, lone_size offset, lone_u16 u16);
bool lone_bytes_write_s16be(struct lone_bytes bytes, lone_size offset, lone_s16 s16);
bool lone_bytes_write_u32be(struct lone_bytes bytes, lone_size offset, lone_u32 u32);
bool lone_bytes_write_s32be(struct lone_bytes bytes, lone_size offset, lone_s32 s32);
bool lone_bytes_write_u64be(struct lone_bytes bytes, lone_size offset, lone_u64 u64);
bool lone_bytes_write_s64be(struct lone_bytes bytes, lone_size offset, lone_s64 s64);

/* ╭────────────────────────────────────────────────────────────────────────╮
│ │
Expand Down
8 changes: 4 additions & 4 deletions source/lone/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ lone_elf_read_##sign##bits(struct lone_elf_header *header, void *address)
\
switch (header->ident[LONE_ELF_IDENT_INDEX_DATA_ENCODING]) { \
case LONE_ELF_IDENT_DATA_ENCODING_LITTLE_ENDIAN: \
value = lone_##sign##bits##_read_le(address); \
value = lone_##sign##bits##le_read(address); \
break; \
case LONE_ELF_IDENT_DATA_ENCODING_BIG_ENDIAN: \
value = lone_##sign##bits##_read_be(address); \
value = lone_##sign##bits##be_read(address); \
break; \
default: \
return LONE_OPTIONAL_ABSENT_VALUE(sign##bits); \
Expand Down Expand Up @@ -293,10 +293,10 @@ lone_elf_write_##sign##bits(struct lone_elf_header *header,
\
switch (header->ident[LONE_ELF_IDENT_INDEX_DATA_ENCODING]) { \
case LONE_ELF_IDENT_DATA_ENCODING_LITTLE_ENDIAN: \
lone_##sign##bits##_write_le(address, value); \
lone_##sign##bits##le_write(address, value); \
break; \
case LONE_ELF_IDENT_DATA_ENCODING_BIG_ENDIAN: \
lone_##sign##bits##_write_be(address, value); \
lone_##sign##bits##be_write(address, value); \
break; \
case LONE_ELF_IDENT_DATA_ENCODING_INVALID: \
default: \
Expand Down
40 changes: 20 additions & 20 deletions source/lone/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ LONE_BYTES_WRITER(s64)
#undef LONE_BYTES_READER
#undef LONE_BYTES_WRITER

lone_u16 lone_u16_read_le(void *address)
lone_u16 lone_u16le_read(void *address)
{
unsigned char *bytes = address;
lone_u16 value = 0;
Expand All @@ -151,7 +151,7 @@ lone_u16 lone_u16_read_le(void *address)
return value;
}

lone_u16 lone_u16_read_be(void *address)
lone_u16 lone_u16be_read(void *address)
{
unsigned char *bytes = address;
lone_u16 value = 0;
Expand All @@ -162,7 +162,7 @@ lone_u16 lone_u16_read_be(void *address)
return value;
}

lone_u32 lone_u32_read_le(void *address)
lone_u32 lone_u32le_read(void *address)
{
unsigned char *bytes = address;
lone_u32 value = 0;
Expand All @@ -175,7 +175,7 @@ lone_u32 lone_u32_read_le(void *address)
return value;
}

lone_u32 lone_u32_read_be(void *address)
lone_u32 lone_u32be_read(void *address)
{
unsigned char *bytes = address;
lone_u32 value = 0;
Expand All @@ -188,7 +188,7 @@ lone_u32 lone_u32_read_be(void *address)
return value;
}

lone_u64 lone_u64_read_le(void *address)
lone_u64 lone_u64le_read(void *address)
{
unsigned char *bytes = address;
lone_u64 value = 0;
Expand All @@ -205,7 +205,7 @@ lone_u64 lone_u64_read_le(void *address)
return value;
}

lone_u64 lone_u64_read_be(void *address)
lone_u64 lone_u64be_read(void *address)
{
unsigned char *bytes = address;
lone_u64 value = 0;
Expand All @@ -222,23 +222,23 @@ lone_u64 lone_u64_read_be(void *address)
return value;
}

void lone_u16_write_le(void *address, lone_u16 value)
void lone_u16le_write(void *address, lone_u16 value)
{
unsigned char *bytes = address;

bytes[0] = ((unsigned char) (value >> 0));
bytes[1] = ((unsigned char) (value >> 8));
}

void lone_u16_write_be(void *address, lone_u16 value)
void lone_u16be_write(void *address, lone_u16 value)
{
unsigned char *bytes = address;

bytes[0] = ((unsigned char) (value >> 8));
bytes[1] = ((unsigned char) (value >> 0));
}

void lone_u32_write_le(void *address, lone_u32 value)
void lone_u32le_write(void *address, lone_u32 value)
{
unsigned char *bytes = address;

Expand All @@ -248,7 +248,7 @@ void lone_u32_write_le(void *address, lone_u32 value)
bytes[3] = ((unsigned char) (value >> 24));
}

void lone_u32_write_be(void *address, lone_u32 value)
void lone_u32be_write(void *address, lone_u32 value)
{
unsigned char *bytes = address;

Expand All @@ -258,7 +258,7 @@ void lone_u32_write_be(void *address, lone_u32 value)
bytes[3] = ((unsigned char) (value >> 0));
}

void lone_u64_write_le(void *address, lone_u64 value)
void lone_u64le_write(void *address, lone_u64 value)
{
unsigned char *bytes = address;

Expand All @@ -272,7 +272,7 @@ void lone_u64_write_le(void *address, lone_u64 value)
bytes[7] = ((unsigned char) (value >> 56));
}

void lone_u64_write_be(void *address, lone_u64 value)
void lone_u64be_write(void *address, lone_u64 value)
{
unsigned char *bytes = address;

Expand All @@ -287,15 +287,15 @@ void lone_u64_write_be(void *address, lone_u64 value)
}

#define LONE_ENDIAN_SIGNED_READER(bits, endian) \
lone_s##bits lone_s##bits##_read_##endian(void *address) \
lone_s##bits lone_s##bits##endian##_read(void *address) \
{ \
return (lone_s##bits) lone_u##bits##_read_##endian(address); \
return (lone_s##bits) lone_u##bits##endian##_read(address); \
}

#define LONE_ENDIAN_SIGNED_WRITER(bits, endian) \
void lone_s##bits##_write_##endian(void *address, lone_s##bits value) \
void lone_s##bits##endian##_write(void *address, lone_s##bits value) \
{ \
lone_u##bits##_write_##endian(address, (lone_u##bits) value); \
lone_u##bits##endian##_write(address, (lone_u##bits) value); \
}

#define LONE_ENDIAN_SIGNED_READERS_FOR_BITS(bits) \
Expand Down Expand Up @@ -328,26 +328,26 @@ LONE_ENDIAN_SIGNED_WRITERS()

#define LONE_BYTES_ENDIAN_READER_2(type, endian) \
struct lone_optional_##type \
lone_bytes_read_##type##_##endian(struct lone_bytes bytes, lone_size offset) \
lone_bytes_read_##type##endian(struct lone_bytes bytes, lone_size offset) \
{ \
struct lone_optional_##type result = { .present = false, .value = 0 }; \
\
if (lone_bytes_contains_block(bytes, offset, sizeof(lone_##type))) { \
result.value = lone_##type##_read_##endian(bytes.pointer + offset); \
result.value = lone_##type##endian##_read(bytes.pointer + offset); \
result.present = true; \
} \
\
return result; \
}

#define LONE_BYTES_ENDIAN_WRITER_2(type, endian) \
bool lone_bytes_write_##type##_##endian(struct lone_bytes bytes, \
bool lone_bytes_write_##type##endian(struct lone_bytes bytes, \
lone_size offset, lone_##type value) \
{ \
bool written = false; \
\
if (lone_bytes_contains_block(bytes, offset, sizeof(lone_##type))) { \
lone_##type##_write_##endian(bytes.pointer + offset, value); \
lone_##type##endian##_write(bytes.pointer + offset, value); \
written = true; \
} \
\
Expand Down
Loading

0 comments on commit 6d858b3

Please sign in to comment.