xml_include fails if file is not found YAZ-668
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 8 Jul 2013 11:26:40 +0000 (13:26 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 8 Jul 2013 11:26:40 +0000 (13:26 +0200)
Unless glob pattern is used.

src/file_glob.c
src/xml_include.c
test/test_xml_include.c

index fd6ae0b..3a5def2 100644 (file)
@@ -36,6 +36,17 @@ struct glob_res {
     struct res_entry *entries;
 };
 
+static void add_entry(yaz_glob_res_t res, const char *str)
+{
+    struct res_entry *ent =
+        nmem_malloc(res->nmem, sizeof(*ent));
+    ent->file = nmem_strdup(res->nmem, str);
+    ent->next = 0;
+    *res->last_entry = ent;
+    res->last_entry = &ent->next;
+    res->number_of_entries++;
+}
+
 static void glob_r(yaz_glob_res_t res, const char *pattern, size_t off,
                    char *prefix)
 {
@@ -57,6 +68,11 @@ static void glob_r(yaz_glob_res_t res, const char *pattern, size_t off,
         glob_r(res, pattern, i, prefix);
         prefix[prefix_len] = '\0';
     }
+    else if (!is_pattern && !pattern[i])
+    {
+        strcpy(prefix + prefix_len, pattern + off);
+        add_entry(res, prefix);
+    }
     else
     {
         DIR * dir = opendir(*prefix ? prefix : "." );
@@ -82,13 +98,7 @@ static void glob_r(yaz_glob_res_t res, const char *pattern, size_t off,
                     }
                     else
                     {
-                        struct res_entry *ent =
-                            nmem_malloc(res->nmem, sizeof(*ent));
-                        ent->file = nmem_strdup(res->nmem, prefix);
-                        ent->next = 0;
-                        *res->last_entry = ent;
-                        res->last_entry = &ent->next;
-                        res->number_of_entries++;
+                        add_entry(res, prefix);
                     }
                     prefix[prefix_len] = '\0';
                 }
index 41005dc..00357a8 100644 (file)
@@ -102,7 +102,8 @@ static int config_include_src(yaz_xml_include_t config, xmlNode **np,
         yaz_glob_res_t glob_res;
 
         glob_ret = yaz_file_glob(wrbuf_cstr(w), &glob_res);
-
+        yaz_log(YLOG_LOG, "yaz_file_glob returned w=%s %d", wrbuf_cstr(w),
+                glob_ret);
         if (glob_ret == 0)
         {
             size_t i;
index 3e9b34d..7d45f45 100644 (file)
@@ -13,7 +13,7 @@
 #include <yaz/xml_include.h>
 #include <yaz/test.h>
 
-static void tst_xml_include(void)
+static void tst_xml_include1(void)
 {
 #if YAZ_HAVE_XML2
     const char *srcdir = getenv("srcdir");
@@ -63,12 +63,97 @@ static void tst_xml_include(void)
 #endif
 }
 
+static void tst_xml_include2(void)
+{
+#if YAZ_HAVE_XML2
+    const char *srcdir = getenv("srcdir");
+    xmlDocPtr doc;
+    xmlNodePtr node;
+    const char *xml_in = "<x><include src=\"test_xml_no.xml\"/></x>";
+
+    if (srcdir == 0)
+        srcdir = ".";
+
+    doc = xmlParseMemory(xml_in, strlen(xml_in));
+    YAZ_CHECK(doc);
+    if (!doc)
+        return;
+    node = xmlDocGetRootElement(doc);
+    YAZ_CHECK(node);
+    if (node)
+    {
+        const char *expect =
+            "<?xml version=\"1.0\"?>\n"
+            "<x><!-- begin include src=\"test_xml_no.xml\" -->"
+            "<!-- end include src=\"test_xml_no.xml\" --></x>\n";
+
+        xmlChar *xml_out;
+        int len_out;
+        int ret = yaz_xml_include_simple(node, srcdir);
+        YAZ_CHECK(ret == -1);
+    }
+    xmlFreeDoc(doc);
+#endif
+}
+
+static void tst_xml_include3(void)
+{
+#if YAZ_HAVE_XML2
+    const char *srcdir = getenv("srcdir");
+    xmlDocPtr doc;
+    xmlNodePtr node;
+    const char *xml_in = "<x><include src=\"test_xml_no*.xml\"/></x>";
+
+    if (srcdir == 0)
+        srcdir = ".";
+
+    doc = xmlParseMemory(xml_in, strlen(xml_in));
+    YAZ_CHECK(doc);
+    if (!doc)
+        return;
+    node = xmlDocGetRootElement(doc);
+    YAZ_CHECK(node);
+    if (node)
+    {
+        const char *expect =
+            "<?xml version=\"1.0\"?>\n"
+            "<x><!-- begin include src=\"test_xml_no*.xml\" -->"
+            "<!-- end include src=\"test_xml_no*.xml\" --></x>\n";
+
+        xmlChar *xml_out;
+        int len_out;
+        int ret = yaz_xml_include_simple(node, srcdir);
+        YAZ_CHECK(ret == 0);
+        xmlDocDumpMemory(doc, &xml_out, &len_out);
+        YAZ_CHECK(xml_out && len_out > 0);
+        if (xml_out && len_out > 0)
+        {
+            YAZ_CHECK(strlen(expect) == len_out);
+            if (strlen(expect) == len_out)
+            {
+                YAZ_CHECK(memcmp(expect, xml_out, len_out) == 0);
+            }
+            else
+            {
+                fwrite(xml_out, 1, len_out, stdout);
+                fflush(stdout);
+            }
+            xmlFree(xml_out);
+        }
+    }
+    xmlFreeDoc(doc);
+#endif
+}
+
+
 
 int main (int argc, char **argv)
 {
     YAZ_CHECK_INIT(argc, argv);
     YAZ_CHECK_LOG();
-    tst_xml_include();
+    tst_xml_include1();
+    tst_xml_include2();
+    tst_xml_include3();
     YAZ_CHECK_TERM;
 }