#ifdef FLAC__STRINGS_IN_UTF8 #include #include #include #include #include #include #include #include /* for WideCharToMultiByte and MultiByteToWideChar */ #include "share/win_utf8_io.h" /* convert WCHAR stored Unicode string to UTF-8. Caller is responsible for freeing memory */ char *utf8_from_wchar(const wchar_t *wstr) { char *utf8str; int len; if (!wstr) return NULL; if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0) return NULL; if ((utf8str = (char *)malloc(++len)) == NULL) return NULL; if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) { free(utf8str); utf8str = NULL; } return utf8str; } /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */ wchar_t *wchar_from_utf8(const char *str) { wchar_t *widestr; int len; if (!str) return NULL; len=(int)strlen(str)+1; if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) != NULL) { if (MultiByteToWideChar(CP_UTF8, 0, str, len, widestr, len) == 0) { if (MultiByteToWideChar(CP_ACP, 0, str, len, widestr, len) == 0) { /* try conversion from Ansi in case the initial UTF-8 conversion had failed */ free(widestr); widestr = NULL; } } } return widestr; } /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */ int get_utf8_argv(int *argc, char ***argv) { typedef int (__cdecl *__wgetmainargs_)(int*, wchar_t***, wchar_t***, int, int*); __wgetmainargs_ __wgetmainargs; HMODULE handle; int wargc; wchar_t **wargv; wchar_t **wenv; char **utf8argv; int ret, i; if ((handle = LoadLibrary("msvcrt.dll")) == NULL) return 1; if ((__wgetmainargs = (__wgetmainargs_)GetProcAddress(handle, "__wgetmainargs")) == NULL) return 1; i = 0; if (__wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) return 1; if ((utf8argv = (char **)malloc(wargc*sizeof(char*))) == NULL) return 1; ret = 0; for (i=0; iactime = ut.actime; times->modtime = ut.modtime; } } return ret; } int unlink_utf8(const char *filename) { wchar_t *wname; int ret; if (!(wname = wchar_from_utf8(filename))) return -1; ret = _wunlink(wname); free(wname); return ret; } int rename_utf8(const char *oldname, const char *newname) { wchar_t *wold = NULL; wchar_t *wnew = NULL; int ret = -1; while (1) { if (!(wold = wchar_from_utf8(oldname))) break; if (!(wnew = wchar_from_utf8(newname))) break; ret = _wrename(wold, wnew); break; } if (wold) free(wold); if (wnew) free(wnew); return ret; } #endif