Skip to content

Commit

Permalink
fix contrib (nsf2wav, nsfmeta) build under macos
Browse files Browse the repository at this point in the history
  • Loading branch information
tsone committed Jan 24, 2025
1 parent 1a884a5 commit 1e43f31
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
20 changes: 11 additions & 9 deletions contrib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ DYNLIB_EXT=.so
EXE_EXT=
INSTALL=install

LIBS_ICONV = -liconv

DESTDIR=
PREFIX=/usr/local
LIBDIR=$(DESTDIR)$(PREFIX)/lib
Expand All @@ -19,14 +17,16 @@ OBJDIR := .objs
LIB_STATIC=$(STATIC_PREFIX)nsfplay$(STATIC_EXT)
LIB_DYNLIB=$(DYNLIB_PREFIX)nsfplay$(DYNLIB_EXT)

CXXFLAGS_DEBUG := -g -O0 -Wall -fPIC -std=c++17
CFLAGS_DEBUG := -g -O0 -Wall -fPIC
CFLAGS_WARN := -Wall -Wno-deprecated-declarations

CXXFLAGS_DEBUG := -g -O0 $(CFLAGS_WARN) -fPIC -std=c++17
CFLAGS_DEBUG := -g -O0 $(CFLAGS_WARN) -fPIC

CXXFLAGS_RELEASE := -g0 -O2 -Wall -fPIC -std=c++17 -DNDEBUG
CFLAGS_RELEASE := -g0 -O2 -Wall -fPIC -DNDEBUG
CXXFLAGS_RELEASE := -g0 -O2 $(CFLAGS_WARN) -fPIC -std=c++17 -DNDEBUG
CFLAGS_RELEASE := -g0 -O2 $(CFLAGS_WARN) -fPIC -DNDEBUG

CXXFLAGS_RELEASE_DEBUG := -g -O2 -Wall -fPIC -std=c++17 -DNDEBUG
CFLAGS_RELEASE_DEBUG := -g -O2 -Wall -fPIC -DNDEBUG
CXXFLAGS_RELEASE_DEBUG := -g -O2 $(CFLAGS_WARN) -fPIC -std=c++17 -DNDEBUG
CFLAGS_RELEASE_DEBUG := -g -O2 $(CFLAGS_WARN) -fPIC -DNDEBUG

ifeq ($(CFLAGS),)
CFLAGS := $(CFLAGS_DEBUG)
Expand All @@ -36,6 +36,8 @@ ifeq ($(CXXFLAGS),)
CXXFLAGS := $(CXXFLAGS_DEBUG)
endif

LDFLAGS_EXTRA = -liconv

LIBXGM_CPP_SRCS = \
../xgm/devices/Audio/MedianFilter.cpp \
../xgm/devices/Audio/echo.cpp \
Expand Down Expand Up @@ -175,7 +177,7 @@ release_debug:
demo: nsf2wav$(EXE_EXT)

nsfmeta$(EXE_EXT): $(OBJDIR)/nsfmeta.o $(LIB_STATIC)
$(CXX) -o $@ $^ $(LDFLAGS_EXTRA) $(LIBS_ICONV)
$(CXX) -o $@ $^ $(LDFLAGS_EXTRA)

nsf2wav$(EXE_EXT): $(OBJDIR)/nsf2wav.o $(LIB_STATIC)
$(CXX) -o $@ $^ $(LDFLAGS_EXTRA)
Expand Down
35 changes: 34 additions & 1 deletion xgm/fileutil.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include "fileutil.h"

#if defined(_WIN32)
#include <windows.h>
#else
#include <iconv.h>
#endif

FILE* fopen_utf8(const char* filename, const char* mode)
{
#if defined(_WIN32)
const int MAX_WPATH = 2048;
const int MAX_WMODE = 16;
wchar_t wfilename[MAX_WPATH];
Expand All @@ -12,4 +16,33 @@ FILE* fopen_utf8(const char* filename, const char* mode)
utf8_file(filename,wfilename,MAX_WPATH);
utf8_file(mode,wmode,MAX_WMODE);
return _wfopen(wfilename,wmode);
#else
return fopen(filename, mode); // assume non-Windows OS has UTF-8 path names
#endif
}

#if !defined(_WIN32)
int mbs_to_utf8(char* outbuf, size_t outsize, const char* incharset, const char* inbuf, size_t insize)
{
char *inptr = (char *) inbuf;
char *outptr = outbuf;
iconv_t cd = iconv_open("UTF-8", incharset);

if (cd == (iconv_t) -1)
{
outbuf[0] = '\0';
return -1;
}

while (insize > 0)
{
if (iconv(cd, &inptr, &insize, &outptr, &outsize) == (size_t) -1)
break;
}
if (outsize >= 1)
*outptr = '\0';

iconv_close(cd);
return outptr - outbuf;
}
#endif
6 changes: 6 additions & 0 deletions xgm/fileutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ extern FILE* fopen_utf8(const char* filename, const char* mode);
// convert a utf8 filename to wchar_t, returns length
// use wfile=NULL, wfile_len=0 to query length without writing the conversion to wfile
#define utf8_file(utf8,wfile,wfile_len) MultiByteToWideChar(CP_UTF8,0,(utf8),-1,(wfile),(wfile_len))

#ifndef _WIN32
// convert multibyte charset string to utf-8 string using iconv
// returns number converted characters or -1 on error
extern int mbs_to_utf8(char* outbuf, size_t outsize, const char* incharset, const char* inbuf, size_t insize);
#endif
6 changes: 5 additions & 1 deletion xgm/player/nsf/nsf.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if defined(_MSC_VER) || defined(__MINGW32__)
#if defined(_WIN32)
#include <windows.h>
#else
#define stricmp strcasecmp
Expand Down Expand Up @@ -62,10 +62,14 @@ void sjis_legacy(char* s, const unsigned int length)
if (utf8) return; // valid UTF8, don't convert

// if not valid UTF8 assume legacy shift-JIS
#if defined(_WIN32)
// (convenient conversion back and forth using windows wide character)
wchar_t w[1024];
MultiByteToWideChar(932,0,s,-1,w,1024);
WideCharToMultiByte(CP_UTF8,0,w,-1,s,length,NULL,NULL);
#else
mbs_to_utf8(s, length, "SHIFT_JIS", s, length);
#endif
}


Expand Down

0 comments on commit 1e43f31

Please sign in to comment.