From 76d513fc3b7e7e267875fd965023dda71a635e1d Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 26 Jan 2010 12:58:42 +0100 Subject: [PATCH] dirent wrapper. Use yaz_file_glob for XML include --- configure.ac | 2 +- include/yaz/Makefile.am | 3 +- include/yaz/dirent.h | 66 +++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 2 +- src/dirent.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++ src/file_glob.c | 2 +- src/xml_include.c | 31 +++++++------------- test/tst_xml_include.c | 6 ++-- win/makefile | 4 ++- 9 files changed, 163 insertions(+), 28 deletions(-) create mode 100644 include/yaz/dirent.h create mode 100644 src/dirent.c diff --git a/configure.ac b/configure.ac index 01f0835..ce9e760 100644 --- a/configure.ac +++ b/configure.ac @@ -21,7 +21,7 @@ dnl YAZ_DOC dnl dnl -AC_CHECK_HEADERS([dirent.h fnmatch.h wchar.h locale.h langinfo.h pwd.h unistd.h sys/select.h sys/socket.h sys/stat.h sys/time.h sys/times.h sys/types.h glob.h sys/un.h sys/wait.h netdb.h arpa/inet.h netinet/tcp.h netinet/in_systm.h],[],[],[]) +AC_CHECK_HEADERS([dirent.h fnmatch.h wchar.h locale.h langinfo.h pwd.h unistd.h sys/select.h sys/socket.h sys/stat.h sys/time.h sys/times.h sys/types.h sys/un.h sys/wait.h netdb.h arpa/inet.h netinet/tcp.h netinet/in_systm.h],[],[],[]) AC_CHECK_HEADERS([net/if.h netinet/in.h netinet/if_ether.h],[],[],[ #if HAVE_SYS_TYPES_H #include diff --git a/include/yaz/Makefile.am b/include/yaz/Makefile.am index d580317..c2ec308 100644 --- a/include/yaz/Makefile.am +++ b/include/yaz/Makefile.am @@ -19,7 +19,8 @@ pkginclude_HEADERS= backend.h ccl.h ccl_xml.h cql.h rpn2cql.h comstack.h \ z-grs.h z-mterm2.h z-opac.h z-rrf1.h z-rrf2.h z-sum.h z-sutrs.h z-uifr1.h \ z-univ.h z-oclcui.h zes-expi.h zes-exps.h zes-order.h zes-pquery.h \ zes-psched.h zes-admin.h zes-pset.h zes-update.h zes-update0.h \ - zoom.h z-charneg.h charneg.h soap.h srw.h zgdu.h matchstr.h json.h file_glob.h + zoom.h z-charneg.h charneg.h soap.h srw.h zgdu.h matchstr.h json.h \ + file_glob.h dirent.h EXTRA_DIST = yaz-version.h.in diff --git a/include/yaz/dirent.h b/include/yaz/dirent.h new file mode 100644 index 0000000..1f5d68f --- /dev/null +++ b/include/yaz/dirent.h @@ -0,0 +1,66 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data. + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Index Data nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** \file + \brief wrapper for dirent.h or our own for Windows +*/ + +#ifdef WIN32 +/* make WIN32 version of dirent */ +#include + +#include + +YAZ_BEGIN_CDECL + +struct dirent { + char d_name[MAX_PATH]; +}; + +typedef struct DIR DIR; + +DIR *opendir (const char *path); +struct dirent *readdir (DIR *dd); +void closedir (DIR *dd); + +YAZ_END_CDECL + +#else +/* include UNIX version */ +#include +#include +#endif + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/src/Makefile.am b/src/Makefile.am index 2208c7f..b8fe503 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -103,7 +103,7 @@ libyaz_la_SOURCES=version.c options.c log.c \ copy_types.c match_glob.c poll.c daemon.c \ iconv_encode_marc8.c iconv_encode_iso_8859_1.c iconv_encode_wchar.c \ iconv_decode_marc8.c iconv_decode_iso5426.c iconv_decode_danmarc.c sc.c \ - json.c xml_include.c file_glob.c + json.c xml_include.c file_glob.c dirent.c libyaz_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO) diff --git a/src/dirent.c b/src/dirent.c new file mode 100644 index 0000000..7ea1df4 --- /dev/null +++ b/src/dirent.c @@ -0,0 +1,75 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data + * See the file LICENSE for details. + */ + +/** \file + \brief Implement opendir/readdir/closedir on Windows +*/ + +#if HAVE_CONFIG_H +#include +#endif + +#include +#ifdef WIN32 +#include +#endif +#include +#include + +#include + +#ifdef WIN32 + +struct DIR { + HANDLE handle; + WIN32_FIND_DATA find_data; + struct dirent entry; +}; + +DIR *opendir (const char *name) +{ + char fullName[MAX_PATH+1]; + DIR *dd = malloc (sizeof(*dd)); + + if (!dd) + return NULL; + strcpy (fullName, name); + strcat (fullName, "\\*.*"); + dd->handle = FindFirstFile(fullName, &dd->find_data); + return dd; +} + +struct dirent *readdir (DIR *dd) +{ + if (dd->handle == INVALID_HANDLE_VALUE) + return NULL; + strcpy (dd->entry.d_name, dd->find_data.cFileName); + if (!FindNextFile(dd->handle, &dd->find_data)) + { + FindClose (dd->handle); + dd->handle = INVALID_HANDLE_VALUE; + } + return &dd->entry; +} + +void closedir(DIR *dd) +{ + if (dd->handle != INVALID_HANDLE_VALUE) + FindClose (dd->handle); + if (dd) + free (dd); +} + +#endif + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/src/file_glob.c b/src/file_glob.c index 8b10664..065b5c5 100644 --- a/src/file_glob.c +++ b/src/file_glob.c @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/xml_include.c b/src/xml_include.c index add66d9..c0d2f0b 100644 --- a/src/xml_include.c +++ b/src/xml_include.c @@ -10,15 +10,7 @@ #include #endif -#if HAVE_GLOB_H -#define USE_POSIX_GLOB 1 -#else -#define USE_POSIX_GLOB 0 -#endif - -#if USE_POSIX_GLOB -#include -#endif +#include #include #include @@ -105,22 +97,21 @@ static int config_include_src(yaz_xml_include_t config, xmlNode **np, wrbuf_rewind(w); conf_dir_path(config, w, src); -#if USE_POSIX_GLOB { - size_t i; - glob_t glob_res; - glob(wrbuf_cstr(w), 0 /* flags */, 0 /* errfunc */, &glob_res); + int glob_ret; + yaz_glob_res_t glob_res; + + glob_ret = yaz_file_glob(wrbuf_cstr(w), &glob_res); - for (i = 0; ret == 0 && i < glob_res.gl_pathc; i++) + if (glob_ret == 0) { - const char *path = glob_res.gl_pathv[i]; - ret = config_include_one(config, &sib, path); + size_t i; + const char *path; + for (i = 0; (path = yaz_file_glob_get_file(glob_res, i)); i++) + ret = config_include_one(config, &sib, path); + yaz_file_globfree(&glob_res); } - globfree(&glob_res); } -#else - ret = config_include_one(config, &sib, wrbuf_cstr(w)); -#endif wrbuf_rewind(w); wrbuf_printf(w, " end include src=\"%s\" ", src); c = xmlNewComment((const xmlChar *) wrbuf_cstr(w)); diff --git a/test/tst_xml_include.c b/test/tst_xml_include.c index 235cbbe..94a0332 100644 --- a/test/tst_xml_include.c +++ b/test/tst_xml_include.c @@ -15,7 +15,7 @@ static void tst_xml_include(void) const char *srcdir = getenv("srcdir"); xmlDocPtr doc; xmlNodePtr node; - const char *xml_in = ""; + const char *xml_in = ""; if (srcdir == 0) srcdir = "."; @@ -30,9 +30,9 @@ static void tst_xml_include(void) { const char *expect = "\n" - "" + "" "some" - "\n"; + "\n"; xmlChar *xml_out; int len_out; diff --git a/win/makefile b/win/makefile index e430cb9..86c2458 100644 --- a/win/makefile +++ b/win/makefile @@ -497,7 +497,9 @@ MISC_OBJS= \ $(OBJDIR)\mutex.obj \ $(OBJDIR)\json.obj \ $(OBJDIR)\sc.obj \ - $(OBJDIR)\xml_include.obj + $(OBJDIR)\xml_include.obj \ + $(OBJDIR)\file_glob.obj \ + $(OBJDIR)\dirent.obj Z3950_OBJS= \ $(OBJDIR)\z-date.obj\ -- 1.7.10.4