1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 12:25:09 +03:00

Simplify Windows IO functions

Remove "native" non-Unicode functions which were only needed for
pre-NT systems like Windows 95/98.

Don't redefine `stat` but use `struct _stat` and `_stat()` instead.
This commit is contained in:
Nick Wellnhofer 2017-10-09 15:35:32 +02:00
parent d1505ef57a
commit 5672397477

150
xmlIO.c
View File

@ -49,38 +49,14 @@
#include <winnls.h> /* for CP_UTF8 */
#endif
/* Figure a portable way to know if a file is a directory. */
#ifndef HAVE_STAT
# ifdef HAVE__STAT
/* MS C library seems to define stat and _stat. The definition
is identical. Still, mapping them to each other causes a warning. */
# ifndef _MSC_VER
# define stat(x,y) _stat(x,y)
# endif
# define HAVE_STAT
# endif
#else
# ifdef HAVE__STAT
# if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
# define stat _stat
# endif
# endif
#endif
#ifdef HAVE_STAT
# ifndef S_ISDIR
# ifdef _S_ISDIR
# define S_ISDIR(x) _S_ISDIR(x)
# else
# ifdef S_IFDIR
# ifndef S_IFMT
# ifdef _S_IFMT
# define S_IFMT _S_IFMT
# endif
# endif
# ifdef S_IFMT
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
# endif
# endif
#ifndef S_ISDIR
# ifdef _S_ISDIR
# define S_ISDIR(x) _S_ISDIR(x)
# elif defined(S_IFDIR)
# ifdef S_IFMT
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
# elif defined(_S_IFMT)
# define S_ISDIR(m) (((m) & _S_IFMT) == S_IFDIR)
# endif
# endif
#endif
@ -658,99 +634,19 @@ xmlWrapGzOpenUtf8(const char *path, const char *mode)
*
*/
static int
xmlWrapStatUtf8(const char *path,struct stat *info)
{
#ifdef HAVE_STAT
xmlWrapStatUtf8(const char *path, struct _stat *info) {
int retval = -1;
wchar_t *wPath;
wPath = __xmlIOWin32UTF8ToWChar(path);
if (wPath)
{
retval = _wstat(wPath,info);
if (wPath) {
retval = _wstat(wPath, info);
xmlFree(wPath);
}
/* maybe path in native encoding */
if(retval < 0)
retval = stat(path,info);
retval = _stat(path, info);
return retval;
#else
return -1;
#endif
}
/**
* xmlWrapOpenNative:
* @path: the path
* @mode: type of access (0 - read, 1 - write)
*
* function opens the file specified by @path
*
*/
static FILE*
xmlWrapOpenNative(const char *path,int mode)
{
return fopen(path,mode ? "wb" : "rb");
}
/**
* xmlWrapStatNative:
* @path: the path
* @info: structure that stores results
*
* function obtains information about the file or directory
*
*/
static int
xmlWrapStatNative(const char *path,struct stat *info)
{
#ifdef HAVE_STAT
return stat(path,info);
#else
return -1;
#endif
}
typedef int (* xmlWrapStatFunc) (const char *f, struct stat *s);
static xmlWrapStatFunc xmlWrapStat = xmlWrapStatNative;
typedef FILE* (* xmlWrapOpenFunc)(const char *f,int mode);
static xmlWrapOpenFunc xmlWrapOpen = xmlWrapOpenNative;
#ifdef HAVE_ZLIB_H
typedef gzFile (* xmlWrapGzOpenFunc) (const char *f, const char *mode);
static xmlWrapGzOpenFunc xmlWrapGzOpen = gzopen;
#endif
/**
* xmlInitPlatformSpecificIo:
*
* Initialize platform specific features.
*/
static void
xmlInitPlatformSpecificIo(void)
{
static int xmlPlatformIoInitialized = 0;
OSVERSIONINFO osvi;
if(xmlPlatformIoInitialized)
return;
osvi.dwOSVersionInfoSize = sizeof(osvi);
if(GetVersionEx(&osvi) && (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)) {
xmlWrapStat = xmlWrapStatUtf8;
xmlWrapOpen = xmlWrapOpenUtf8;
#ifdef HAVE_ZLIB_H
xmlWrapGzOpen = xmlWrapGzOpenUtf8;
#endif
} else {
xmlWrapStat = xmlWrapStatNative;
xmlWrapOpen = xmlWrapOpenNative;
#ifdef HAVE_ZLIB_H
xmlWrapGzOpen = gzopen;
#endif
}
xmlPlatformIoInitialized = 1;
return;
}
#endif
@ -773,7 +669,11 @@ int
xmlCheckFilename (const char *path)
{
#ifdef HAVE_STAT
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
struct _stat stat_buffer;
#else
struct stat stat_buffer;
#endif
#endif
if (path == NULL)
return(0);
@ -788,7 +688,7 @@ xmlCheckFilename (const char *path)
(path[3] == '\\') )
return 1;
if (xmlWrapStat(path, &stat_buffer) == -1)
if (xmlWrapStatUtf8(path, &stat_buffer) == -1)
return 0;
#else
if (stat(path, &stat_buffer) == -1)
@ -935,7 +835,7 @@ xmlFileOpen_real (const char *filename) {
#endif
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
fd = xmlWrapOpen(path, 0);
fd = xmlWrapOpenUtf8(path, 0);
#else
fd = fopen(path, "r");
#endif /* WIN32 */
@ -1008,7 +908,7 @@ xmlFileOpenW (const char *filename) {
return(NULL);
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
fd = xmlWrapOpen(path, 1);
fd = xmlWrapOpenUtf8(path, 1);
#elif(__MVS__)
fd = fopen(path, "w");
#else
@ -1200,7 +1100,7 @@ xmlGzfileOpen_real (const char *filename) {
return(NULL);
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
fd = xmlWrapGzOpen(path, "rb");
fd = xmlWrapGzOpenUtf8(path, "rb");
#else
fd = gzopen(path, "rb");
#endif
@ -1277,7 +1177,7 @@ xmlGzfileOpenW (const char *filename, int compression) {
return(NULL);
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
fd = xmlWrapGzOpen(path, mode);
fd = xmlWrapGzOpenUtf8(path, mode);
#else
fd = gzopen(path, mode);
#endif
@ -2325,10 +2225,6 @@ xmlRegisterDefaultInputCallbacks(void) {
if (xmlInputCallbackInitialized)
return;
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
xmlInitPlatformSpecificIo();
#endif
xmlRegisterInputCallbacks(xmlFileMatch, xmlFileOpen,
xmlFileRead, xmlFileClose);
#ifdef HAVE_ZLIB_H
@ -2363,10 +2259,6 @@ xmlRegisterDefaultOutputCallbacks (void) {
if (xmlOutputCallbackInitialized)
return;
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
xmlInitPlatformSpecificIo();
#endif
xmlRegisterOutputCallbacks(xmlFileMatch, xmlFileOpenW,
xmlFileWrite, xmlFileClose);