Handle UTF-8 output on Windows in openal-info

This commit is contained in:
Chris Robinson 2016-09-09 09:52:01 -07:00
parent 45dfdca6f9
commit ba449ccce5

View File

@ -24,6 +24,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "AL/alc.h"
#include "AL/al.h"
@ -41,6 +42,70 @@
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static WCHAR *FromUTF8(const char *str)
{
WCHAR *out = NULL;
int len;
if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
{
out = calloc(sizeof(WCHAR), len);
MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
}
return out;
}
/* Override printf, fprintf, and fwrite so we can print UTF-8 strings. */
static void al_fprintf(FILE *file, const char *fmt, ...)
{
char str[1024];
WCHAR *wstr;
va_list ap;
va_start(ap, fmt);
vsnprintf(str, sizeof(str), fmt, ap);
va_end(ap);
str[sizeof(str)-1] = 0;
wstr = FromUTF8(str);
if(!wstr)
fprintf(file, "<UTF-8 error> %s", str);
else
fprintf(file, "%ls", wstr);
free(wstr);
}
#define fprintf al_fprintf
#define printf(...) al_fprintf(stdout, __VA_ARGS__)
static int al_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *file)
{
char str[1024];
WCHAR *wstr;
size_t len;
len = size * nmemb;
if(len > sizeof(str)-1)
len = sizeof(str)-1;
memcpy(str, ptr, len);
str[len] = 0;
wstr = FromUTF8(str);
if(!wstr)
fprintf(file, "<UTF-8 error> %s", str);
else
fprintf(file, "%ls", wstr);
free(wstr);
return len / size;
}
#define fwrite al_fwrite
#endif
#define MAX_WIDTH 80
static void printList(const char *list, char separator)