From f720562471a064dba92a465d999aa833e59d69b1 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 10 Sep 2013 11:34:31 +0200 Subject: [PATCH] Re-use match functions a little more --- src/sru-p.h | 2 ++ src/xml_match.c | 11 ++++++++--- src/xml_to_opac.c | 55 ++++++++++++++--------------------------------------- 3 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/sru-p.h b/src/sru-p.h index 8a644c1..fb6b290 100644 --- a/src/sru-p.h +++ b/src/sru-p.h @@ -51,6 +51,8 @@ int yaz_match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, Odr_int **val); int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o, char **val, int *len); +int yaz_match_xsd_string_n_nmem(xmlNodePtr ptr, const char *elem, NMEM nmem, + char **val, int *len); int yaz_match_xsd_element(xmlNodePtr ptr, const char *elem); int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o, diff --git a/src/xml_match.c b/src/xml_match.c index b99247e..dd804db 100644 --- a/src/xml_match.c +++ b/src/xml_match.c @@ -25,8 +25,8 @@ int yaz_match_xsd_element(xmlNodePtr ptr, const char *elem) #define CHECK_TYPE 0 -int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o, - char **val, int *len) +int yaz_match_xsd_string_n_nmem(xmlNodePtr ptr, const char *elem, NMEM nmem, + char **val, int *len) { #if CHECK_TYPE struct _xmlAttr *attr; @@ -55,12 +55,17 @@ int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o, *val = ""; return 1; } - *val = odr_strdup(o, (const char *) ptr->content); + *val = nmem_strdup(nmem, (const char *) ptr->content); if (len) *len = xmlStrlen(ptr->content); return 1; } +int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o, + char **val, int *len) +{ + return yaz_match_xsd_string_n_nmem(ptr, elem, o->mem, val, len); +} int yaz_match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val) { diff --git a/src/xml_to_opac.c b/src/xml_to_opac.c index ead47bd..236bf8a 100644 --- a/src/xml_to_opac.c +++ b/src/xml_to_opac.c @@ -20,41 +20,14 @@ #include #if YAZ_HAVE_XML2 -#include -#include - -static int match_element(xmlNode *ptr, const char *elem) -{ - if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem)) - { - return 1; - } - return 0; -} - -static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, NMEM nmem, - char **val, int *len) -{ - if (!match_element(ptr, elem)) - return 0; - ptr = ptr->children; - if (!ptr || ptr->type != XML_TEXT_NODE) - { - *val = ""; - return 1; - } - *val = nmem_strdup(nmem, (const char *) ptr->content); - if (len) - *len = xmlStrlen(ptr->content); - return 1; -} +#include "sru-p.h" static int match_element_next(xmlNode **ptr, const char *elem, NMEM nmem, char **val) { while (*ptr && (*ptr)->type != XML_ELEMENT_NODE) (*ptr) = (*ptr)->next; - if (*ptr && match_xsd_string_n(*ptr, elem, nmem, val, 0)) + if (*ptr && yaz_match_xsd_string_n_nmem(*ptr, elem, nmem, val, 0)) { *ptr = (*ptr)->next; return 1; @@ -69,7 +42,7 @@ static int match_v_next(xmlNode **ptr, const char *elem, NMEM nmem, while (*ptr && (*ptr)->type != XML_ELEMENT_NODE) (*ptr) = (*ptr)->next; *val = nmem_booldup(nmem, 0); - if (*ptr && match_element(*ptr, elem)) + if (*ptr && yaz_match_xsd_element(*ptr, elem)) { struct _xmlAttr *attr = (*ptr)->properties; @@ -134,7 +107,7 @@ static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem) ptr = ptr->next; if (!ptr) break; - if (!match_element(ptr, "volume")) + if (!yaz_match_xsd_element(ptr, "volume")) return 0; ptr = ptr->next; } @@ -147,7 +120,7 @@ static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem) ptr = ptr->next; if (!ptr) break; - if (!match_element(ptr, "volume")) + if (!yaz_match_xsd_element(ptr, "volume")) return 0; volume(ptr->children, (*volp) + i, nmem); ptr = ptr->next; @@ -187,7 +160,7 @@ static int circulations(xmlNode *ptr, Z_CircRecord ***circp, ptr = ptr->next; if (!ptr) break; - if (!match_element(ptr, "circulation")) + if (!yaz_match_xsd_element(ptr, "circulation")) return 0; ptr = ptr->next; } @@ -200,7 +173,7 @@ static int circulations(xmlNode *ptr, Z_CircRecord ***circp, ptr = ptr->next; if (!ptr) break; - if (!match_element(ptr, "circulation")) + if (!yaz_match_xsd_element(ptr, "circulation")) return 0; circulation(ptr->children, (*circp) + i, nmem); ptr = ptr->next; @@ -240,7 +213,7 @@ static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem) h->volumes = 0; while (ptr && ptr->type != XML_ELEMENT_NODE) ptr = ptr->next; - if (match_element(ptr, "volumes")) + if (yaz_match_xsd_element(ptr, "volumes")) { volumes(ptr->children, &h->volumes, &h->num_volumes, nmem); ptr = ptr->next; @@ -250,7 +223,7 @@ static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem) h->circulationData = 0; while (ptr && ptr->type != XML_ELEMENT_NODE) ptr = ptr->next; - if (match_element(ptr, "circulations")) + if (yaz_match_xsd_element(ptr, "circulations")) { circulations(ptr->children, &h->circulationData, &h->num_circulationData, nmem); @@ -271,12 +244,12 @@ static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr, if (!nmem) nmem = yaz_marc_get_nmem(mt); - if (!match_element(ptr, "opacRecord")) + if (!yaz_match_xsd_element(ptr, "opacRecord")) return 0; ptr = ptr->children; while (ptr && ptr->type != XML_ELEMENT_NODE) ptr = ptr->next; - if (!match_element(ptr, "bibliographicRecord")) + if (!yaz_match_xsd_element(ptr, "bibliographicRecord")) return 0; if (!bibliographicRecord(mt, ptr->children, &ext, cd, nmem, syntax)) return 0; @@ -288,7 +261,7 @@ static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr, ptr = ptr->next; while (ptr && ptr->type != XML_ELEMENT_NODE) ptr = ptr->next; - if (!match_element(ptr, "holdings")) + if (!yaz_match_xsd_element(ptr, "holdings")) return 0; ptr = ptr->children; @@ -300,7 +273,7 @@ static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr, ptr = ptr->next; if (!ptr) break; - if (!match_element(ptr, "holding")) + if (!yaz_match_xsd_element(ptr, "holding")) return 0; ptr = ptr->next; } @@ -314,7 +287,7 @@ static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr, ptr = ptr->next; if (!ptr) break; - if (!match_element(ptr, "holding")) + if (!yaz_match_xsd_element(ptr, "holding")) return 0; if (!holdingsRecord(ptr->children, opac->holdingsData + i, nmem)) return 0; -- 1.7.10.4