From f741601cc793716fdc2d957c5940b80aa775e87f Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Thu, 27 Apr 2006 08:15:20 +0000 Subject: [PATCH] applied patch from Roland Schwingel to allow UTF-8 file paths on Windows * xmlIO.c: applied patch from Roland Schwingel to allow UTF-8 file paths on Windows Daniel --- ChangeLog | 5 +++ xmlIO.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 102 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7a49f29c..dcd89288 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Apr 27 10:15:45 CEST 2006 Daniel Veillard + + * xmlIO.c: applied patch from Roland Schwingel to allow UTF-8 + file paths on Windows + Thu Apr 27 10:10:58 CEST 2006 Daniel Veillard * xmlwriter.c: patch from Jason Viers for line breaks after EndPI diff --git a/xmlIO.c b/xmlIO.c index 2610e252..12516652 100644 --- a/xmlIO.c +++ b/xmlIO.c @@ -36,6 +36,10 @@ #include #endif +#ifdef WIN32 +#include +#endif + /* Figure a portable way to know if a file is a directory. */ #ifndef HAVE_STAT # ifdef HAVE__STAT @@ -189,6 +193,39 @@ static const char *IOerr[] = { "unknown address familly", /* EAFNOSUPPORT */ }; +#if defined(WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) +/** + * __xmlIOWin32UTF8ToWChar: + * @u8String: uft-8 string + * + * Convert a string from utf-8 to wchar (WINDOWS ONLY!) + */ +static wchar_t * +__xmlIOWin32UTF8ToWChar(const char *u8String) +{ + wchar_t *wString = NULL; + + if (u8String) + { + int wLen = MultiByteToWideChar(CP_UTF8,0,u8String,-1,NULL,0); + if (wLen) + { + wString = malloc((wLen+1) * sizeof(wchar_t)); + if (wString) + { + if (MultiByteToWideChar(CP_UTF8,0,u8String,-1,wString,wLen+1) == 0) + { + free(wString); + wString = NULL; + } + } + } + } + + return wString; +} +#endif + /** * xmlIOErrMemory: * @extra: extra informations @@ -552,23 +589,44 @@ xmlCleanupOutputCallbacks(void) int xmlCheckFilename (const char *path) { + if (path == NULL) + return(0); + +#if defined(WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) + { + int retval = 0; + + wchar_t *wPath = __xmlIOWin32UTF8ToWChar(path); + if (wPath) + { + struct _stat stat_buffer; + + if (_wstat(wPath,&stat_buffer) == 0) + { + retval = 1; + + if (((stat_buffer.st_mode & S_IFDIR) == S_IFDIR)) + retval = 2; + } + + free(wPath); + } + + return retval; + } +#else #ifdef HAVE_STAT struct stat stat_buffer; - if (path == NULL) - return(0); - if (stat(path, &stat_buffer) == -1) return 0; #ifdef S_ISDIR - if (S_ISDIR(stat_buffer.st_mode)) { + if (S_ISDIR(stat_buffer.st_mode)) return 2; - } -#endif -#endif - if (path == NULL) - return(0); +#endif /* S_ISDIR */ +#endif /* HAVE_STAT */ +#endif /* WIN32 */ return 1; } @@ -692,7 +750,18 @@ xmlFileOpen_real (const char *filename) { return(NULL); #if defined(WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) - fd = fopen(path, "rb"); + { + wchar_t *wPath = __xmlIOWin32UTF8ToWChar(path); + if (wPath) + { + fd = _wfopen(wPath, L"rb"); + free(wPath); + } + else + { + fd = fopen(path, "rb"); + } + } #else fd = fopen(path, "r"); #endif /* WIN32 */ @@ -762,8 +831,24 @@ xmlFileOpenW (const char *filename) { if (path == NULL) return(NULL); - fd = fopen(path, "wb"); - if (fd == NULL) xmlIOErr(0, path); +#if defined(WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) + { + wchar_t *wPath = __xmlIOWin32UTF8ToWChar(path); + if (wPath) + { + fd = _wfopen(wPath, L"wb"); + free(wPath); + } + else + { + fd = fopen(path, "wb"); + } + } +#else + fd = fopen(path, "wb"); +#endif /* WIN32 */ + + if (fd == NULL) xmlIOErr(0, path); return((void *) fd); } #endif /* LIBXML_OUTPUT_ENABLED */