Stylesheet support.
[yaz-moved-to-github.git] / src / zoom-c.c
index 9acc7c7..af940a8 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2000-2003, Index Data
  * See the file LICENSE for details.
  *
- * $Id: zoom-c.c,v 1.10 2003-12-04 12:57:30 adam Exp $
+ * $Id: zoom-c.c,v 1.16 2004-01-07 20:36:44 adam Exp $
  *
  * ZOOM layer for C, connections, result sets, queries.
  */
@@ -918,7 +918,7 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
        ZOOM_options_get(c->options, "implementationName"),
        odr_prepend(c->odr_out, "ZOOM-C", ireq->implementationName));
 
-    version = odr_strdup(c->odr_out, "$Revision: 1.10 $");
+    version = odr_strdup(c->odr_out, "$Revision: 1.16 $");
     if (strlen(version) > 10)  /* check for unexpanded CVS strings */
        version[strlen(version)-2] = '\0';
     ireq->implementationVersion = odr_prepend(c->odr_out,
@@ -1004,7 +1004,7 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr)
 {
     char ctype[50];
     Z_SOAP_Handler h[2] = {
-        {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec},
+        {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
         {0, 0, 0}
     };
     ODR o = odr_createmem(ODR_ENCODE);
@@ -1059,7 +1059,7 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr)
     ret = z_soap_codec_enc(o, &p,
                            &gdu->u.HTTP_Request->content_buf,
                            &gdu->u.HTTP_Request->content_len, h,
-                           c->charset);
+                           c->charset, 0);
 
     if (!z_GDU(c->odr_out, &gdu, 0, 0))
         return zoom_complete;
@@ -1359,6 +1359,62 @@ ZOOM_record_destroy (ZOOM_record rec)
     xfree (rec);
 }
 
+static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
+                                    int *len,
+                                    const char *buf, int sz,
+                                    const char *record_charset)
+{
+    char to[40];
+    char from[40];
+    yaz_iconv_t cd = 0;
+    yaz_marc_t mt = yaz_marc_create();
+
+    *from = '\0';
+    strcpy(to, "UTF-8");
+    if (record_charset && *record_charset)
+    {
+       /* Use "from,to" or just "from" */
+       const char *cp =strchr(record_charset, ',');
+       int clen = strlen(record_charset);
+       if (cp && cp[1])
+       {
+           strncpy( to, cp+1, sizeof(to)-1);
+           to[sizeof(to)-1] = '\0';
+           clen = cp - record_charset;
+       }
+       if (clen > sizeof(from)-1)
+           clen = sizeof(from)-1;
+       
+       if (clen)
+           strncpy(from, record_charset, clen);
+       from[clen] = '\0';
+    }
+
+    if (*from && *to)
+    {
+       cd = yaz_iconv_open(to, from);
+       yaz_marc_iconv(mt, cd);
+    }
+
+    yaz_marc_xml(mt, marc_type);
+    if (!rec->wrbuf_marc)
+       rec->wrbuf_marc = wrbuf_alloc();
+    wrbuf_rewind (rec->wrbuf_marc);
+    if (yaz_marc_decode_wrbuf (mt, buf, sz, rec->wrbuf_marc) > 0)
+    {
+       yaz_marc_destroy(mt);
+       if (cd)
+           yaz_iconv_close(cd);
+       if (len)
+           *len = wrbuf_len(rec->wrbuf_marc);
+       return wrbuf_buf(rec->wrbuf_marc);
+    }
+    yaz_marc_destroy(mt);
+    if (cd)
+       yaz_iconv_close(cd);
+    return 0;
+}
+
 static const char *record_iconv_return(ZOOM_record rec, int *len,
                                       const char *buf, int sz,
                                       const char *record_charset)
@@ -1514,7 +1570,7 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len)
                                       charset);
         else if (r->which == Z_External_octet)
         {
-            yaz_marc_t mt;
+           const char *ret_buf;
             switch (ent->value)
             {
             case VAL_SOIF:
@@ -1525,23 +1581,13 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len)
             case VAL_APPLICATION_XML:
                 break;
             default:
-                if (!rec->wrbuf_marc)
-                    rec->wrbuf_marc = wrbuf_alloc();
-
-                mt = yaz_marc_create();
-                wrbuf_rewind (rec->wrbuf_marc);
-                if (yaz_marc_decode_wrbuf (
-                        mt, (const char *) r->u.octet_aligned->buf,
-                        r->u.octet_aligned->len,
-                        rec->wrbuf_marc) > 0)
-                {
-                    yaz_marc_destroy(mt);
-                   return record_iconv_return(rec, len,
-                                              wrbuf_buf(rec->wrbuf_marc),
-                                              wrbuf_len(rec->wrbuf_marc),
-                                              charset);
-                }
-                yaz_marc_destroy(mt);
+               ret_buf = marc_iconv_return(
+                   rec, YAZ_MARC_LINE, len,
+                   (const char *) r->u.octet_aligned->buf,
+                   r->u.octet_aligned->len,
+                   charset);
+               if (ret_buf)
+                   return ret_buf;
             }
            return record_iconv_return(rec, len,
                                       (const char *) r->u.octet_aligned->buf,
@@ -1582,7 +1628,7 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len)
                                       charset);
         else if (r->which == Z_External_octet)
         {
-            yaz_marc_t mt;
+           const char *ret_buf;
             int marc_decode_type = YAZ_MARC_MARCXML;
 
             if (!strcmp(type, "oai"))
@@ -1597,24 +1643,13 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len)
             case VAL_APPLICATION_XML:
                 break;
             default:
-                if (!rec->wrbuf_marc)
-                    rec->wrbuf_marc = wrbuf_alloc();
-                wrbuf_rewind (rec->wrbuf_marc);
-                mt = yaz_marc_create();
-
-                yaz_marc_xml(mt, YAZ_MARC_MARCXML);
-                if (yaz_marc_decode_wrbuf (
-                        mt, (const char *) r->u.octet_aligned->buf,
-                        r->u.octet_aligned->len,
-                        rec->wrbuf_marc) > 0)
-                {
-                    yaz_marc_destroy(mt);
-                   return record_iconv_return(rec, len,
-                                              wrbuf_buf(rec->wrbuf_marc),
-                                              wrbuf_len(rec->wrbuf_marc),
-                                              charset);
-                }
-                yaz_marc_destroy(mt);
+               ret_buf = marc_iconv_return(
+                   rec, marc_decode_type, len,
+                   (const char *) r->u.octet_aligned->buf,
+                   r->u.octet_aligned->len,
+                   charset);
+               if (ret_buf)
+                   return ret_buf;
             }
            return record_iconv_return(rec, len,
                                       (const char *) r->u.octet_aligned->buf,
@@ -2916,9 +2951,9 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
     if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
     {
         Z_SOAP *soap_package = 0;
-        ODR o = odr_createmem(ODR_DECODE);
+        ODR o = c->odr_in;
         Z_SOAP_Handler soap_handlers[2] = {
-            {"http://www.loc.gov/zing/srw/v1.0/", 0,
+            {"http://www.loc.gov/zing/srw/", 0,
              (Z_SOAP_fun) yaz_srw_codec},
             {0, 0, 0}
         };
@@ -2943,7 +2978,6 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
         }
         else
             ret = -1;
-        odr_destroy(o);
     }
     if (ret)
     {