dirent wrapper. Use yaz_file_glob for XML include
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 26 Jan 2010 11:58:42 +0000 (12:58 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 26 Jan 2010 11:58:42 +0000 (12:58 +0100)
configure.ac
include/yaz/Makefile.am
include/yaz/dirent.h [new file with mode: 0644]
src/Makefile.am
src/dirent.c [new file with mode: 0644]
src/file_glob.c
src/xml_include.c
test/tst_xml_include.c
win/makefile

index 01f0835..ce9e760 100644 (file)
@@ -21,7 +21,7 @@ dnl
 YAZ_DOC
 dnl 
 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 <sys/types.h>
 AC_CHECK_HEADERS([net/if.h netinet/in.h netinet/if_ether.h],[],[],[
  #if HAVE_SYS_TYPES_H
  #include <sys/types.h>
index d580317..c2ec308 100644 (file)
@@ -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 \
  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
 
 
 EXTRA_DIST = yaz-version.h.in
 
diff --git a/include/yaz/dirent.h b/include/yaz/dirent.h
new file mode 100644 (file)
index 0000000..1f5d68f
--- /dev/null
@@ -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 <windows.h>
+
+#include <yaz/yconfig.h>
+
+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 <sys/types.h>
+#include <dirent.h>
+#endif
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+
index 2208c7f..b8fe503 100644 (file)
@@ -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 \
   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)
 
 
 libyaz_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO)
 
diff --git a/src/dirent.c b/src/dirent.c
new file mode 100644 (file)
index 0000000..7ea1df4
--- /dev/null
@@ -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 <config.h>
+#endif
+
+#include <assert.h>
+#ifdef WIN32
+#include <io.h>
+#endif
+#include <string.h>
+#include <stdlib.h>
+
+#include <yaz/dirent.h>
+
+#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
+ */
+
index 8b10664..065b5c5 100644 (file)
@@ -18,7 +18,7 @@
 #include <yaz/wrbuf.h>
 #include <yaz/tpath.h>
 #include <yaz/log.h>
 #include <yaz/wrbuf.h>
 #include <yaz/tpath.h>
 #include <yaz/log.h>
-#include <dirent.h>
+#include <yaz/dirent.h>
 #include <yaz/nmem.h>
 #include <yaz/file_glob.h>
 #include <yaz/match_glob.h>
 #include <yaz/nmem.h>
 #include <yaz/file_glob.h>
 #include <yaz/match_glob.h>
index add66d9..c0d2f0b 100644 (file)
 #include <config.h>
 #endif
 
 #include <config.h>
 #endif
 
-#if HAVE_GLOB_H
-#define USE_POSIX_GLOB 1
-#else
-#define USE_POSIX_GLOB 0
-#endif
-
-#if USE_POSIX_GLOB
-#include <glob.h>
-#endif
+#include <yaz/file_glob.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -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);
 
     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));
     wrbuf_rewind(w);
     wrbuf_printf(w, " end include src=\"%s\" ", src);
     c = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
index 235cbbe..94a0332 100644 (file)
@@ -15,7 +15,7 @@ static void tst_xml_include(void)
     const char *srcdir = getenv("srcdir");
     xmlDocPtr doc;
     xmlNodePtr node;
     const char *srcdir = getenv("srcdir");
     xmlDocPtr doc;
     xmlNodePtr node;
-    const char *xml_in = "<x><include src=\"tst_xml_include.xml\"/></x>";
+    const char *xml_in = "<x><include src=\"tst_xml_inc*.xml\"/></x>";
 
     if (srcdir == 0)
         srcdir = ".";
 
     if (srcdir == 0)
         srcdir = ".";
@@ -30,9 +30,9 @@ static void tst_xml_include(void)
     {
         const char *expect =
             "<?xml version=\"1.0\"?>\n"
     {
         const char *expect =
             "<?xml version=\"1.0\"?>\n"
-            "<x><!-- begin include src=\"tst_xml_include.xml\" -->"
+            "<x><!-- begin include src=\"tst_xml_inc*.xml\" -->"
             "<y>some</y>"
             "<y>some</y>"
-            "<!-- end include src=\"tst_xml_include.xml\" --></x>\n";
+            "<!-- end include src=\"tst_xml_inc*.xml\" --></x>\n";
 
         xmlChar *xml_out;
         int len_out;
 
         xmlChar *xml_out;
         int len_out;
index e430cb9..86c2458 100644 (file)
@@ -497,7 +497,9 @@ MISC_OBJS= \
    $(OBJDIR)\mutex.obj \
    $(OBJDIR)\json.obj \
    $(OBJDIR)\sc.obj \
    $(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\
 
 Z3950_OBJS= \
    $(OBJDIR)\z-date.obj\