Skip to content

Commit

Permalink
fix(build): switch from char16_t to uint16_t and convert using wcrtomb
Browse files Browse the repository at this point in the history
This patch is supposed to improve compatibility with Windows and MacOS
  • Loading branch information
lmichaelis committed May 3, 2024
1 parent 86f168f commit 1a84b55
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/Riff.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ char const* DmRiff_readString(DmRiff* slf) {
return str;
}

char16_t* DmRiff_readStringUtf(DmRiff* slf) {
uint16_t* DmRiff_readStringUtf(DmRiff* slf) {
if (slf == NULL) {
return u"";
return NULL;
}

char16_t* mem = (char16_t*) slf->mem;
char16_t* str = (char16_t*) slf->mem + slf->pos;
uint16_t* mem = (uint16_t*) slf->mem;
uint16_t* str = (uint16_t*) slf->mem + slf->pos;

while (mem[slf->pos] != 0) {
slf->pos += 1;
Expand Down
1 change: 0 additions & 1 deletion src/_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <stdatomic.h>
#include <stdbool.h>
#include <uchar.h>

typedef enum DmResolveFlags {
DmResolve_AFTER_PREPARE_TIME = 1 << 10,
Expand Down
5 changes: 2 additions & 3 deletions src/_Riff.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: MIT-Modern-Variant
#pragma once
#include <stdbool.h>
#include <uchar.h>

#define DM_FOURCC(a, b, c, d) (((d) << 24U) | ((c) << 16U) | ((b) << 8U) | (a))
#define DM_FOURCC_RIFF DM_FOURCC('R', 'I', 'F', 'F')
Expand Down Expand Up @@ -118,7 +117,7 @@ DMINT void DmInfo_parse(DmInfo* slf, DmRiff* rif);
DMINT void DmVersion_parse(DmVersion* slf, DmRiff* rif);
DMINT void DmReference_parse(DmReference* slf, DmRiff* rif);

DMINT char* Dm_utf16ToUtf8Inline(char* out, char16_t const* u16);
DMINT char* Dm_utf16ToUtf8Inline(char* out, uint16_t const* u16);

DMINT bool DmRiff_init(DmRiff* slf, void const* buf, size_t len);
DMINT bool DmRiff_is(DmRiff const* slf, uint32_t id, uint32_t typ);
Expand All @@ -132,5 +131,5 @@ DMINT void DmRiff_readInt(DmRiff* slf, int32_t* buf);
DMINT void DmRiff_readDword(DmRiff* slf, uint32_t* buf);
DMINT void DmRiff_readDouble(DmRiff* slf, double* buf);
DMINT char const* DmRiff_readString(DmRiff* slf);
DMINT char16_t* DmRiff_readStringUtf(DmRiff* slf);
DMINT uint16_t* DmRiff_readStringUtf(DmRiff* slf);
DMINT void DmRiff_reportDone(DmRiff* slf);
17 changes: 5 additions & 12 deletions src/io/Common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// SPDX-License-Identifier: MIT-Modern-Variant
#include "_Internal.h"

#include <uchar.h>
#include <wchar.h>

void DmGuid_parse(DmGuid* slf, DmRiff* rif) {
DmRiff_read(rif, slf->data, sizeof slf->data);
}

char* Dm_utf16ToUtf8Inline(char* out, char16_t const* u16) {
char* Dm_utf16ToUtf8Inline(char* out, uint16_t const* u16) {
size_t len = 0;
while (u16[len] != 0) {
len += 1;
Expand All @@ -21,13 +20,7 @@ char* Dm_utf16ToUtf8Inline(char* out, char16_t const* u16) {
size_t i = 0;
size_t j = 0;
for (; i < len; ++i) {
#ifndef _WIN32
j += c16rtomb(out + j, u16[i], &state);
#else
// NOTE: MinGW on Windows does not correctly implement `c16romb`, thus we just use `wcrtomb`.
// According to MinGW, `char16_t` should be equivalent to `wchar_t` on Windows.
j += wcrtomb(out + j, (wchar_t) u16[i], &state);
#endif
j += wcrtomb(out + j, u16[i], &state);
}

out[j] = '\0';
Expand All @@ -38,7 +31,7 @@ void DmUnfo_parse(DmUnfo* slf, DmRiff* rif) {
DmRiff cnk;
while (DmRiff_readChunk(rif, &cnk)) {
if (DmRiff_is(&cnk, DM_FOURCC_UNAM, 0)) {
char16_t* raw = DmRiff_readStringUtf(&cnk);
uint16_t* raw = DmRiff_readStringUtf(&cnk);
slf->unam = Dm_utf16ToUtf8Inline((char*) raw, raw);
} else {
DmRiff_reportDone(&cnk);
Expand Down Expand Up @@ -83,11 +76,11 @@ void DmReference_parse(DmReference* slf, DmRiff* rif) {
} else if (DmRiff_is(&cnk, DM_FOURCC_GUID, 0)) {
DmGuid_parse(&slf->guid, &cnk);
} else if (DmRiff_is(&cnk, DM_FOURCC_NAME, 0)) {
char16_t* raw = DmRiff_readStringUtf(&cnk);
uint16_t* raw = DmRiff_readStringUtf(&cnk);
slf->name = Dm_utf16ToUtf8Inline((char*) raw, raw);
continue; // Ignore following bytes
} else if (DmRiff_is(&cnk, DM_FOURCC_FILE, 0)) {
char16_t* raw = DmRiff_readStringUtf(&cnk);
uint16_t* raw = DmRiff_readStringUtf(&cnk);
slf->file = Dm_utf16ToUtf8Inline((char*) raw, raw);
continue; // Ignore following bytes
} else if (DmRiff_is(&cnk, DM_FOURCC_VERS, 0)) {
Expand Down
4 changes: 2 additions & 2 deletions src/io/Segment.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ static void DmSegment_parseChordItem(DmMessage_Chord* slf, DmRiff* rif) {
{
uint32_t end_position = rif->pos + item_size;

char16_t name[16];
DmRiff_read(rif, name, sizeof name);
uint16_t name[16];
DmRiff_read(rif, name, 16 * sizeof(int16_t));
(void) Dm_utf16ToUtf8Inline(slf->name, name);

DmRiff_readDword(rif, &slf->time);
Expand Down

0 comments on commit 1a84b55

Please sign in to comment.