New WRBUF wrapper mp::wrbuf
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 4 Apr 2012 08:33:53 +0000 (10:33 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 4 Apr 2012 08:33:53 +0000 (10:33 +0200)
include/metaproxy/util.hpp
src/filter_record_transform.cpp
src/filter_sru_to_z3950.cpp
src/filter_zoom.cpp
src/gduutil.cpp
src/metaproxy_prog.cpp
src/util.cpp

index e13027f..ea43910 100644 (file)
@@ -152,6 +152,18 @@ namespace metaproxy_1 {
         ODR m_odr;
     };
 
+    class wrbuf : public boost::noncopyable
+    {
+    public:
+        wrbuf();
+        ~wrbuf();
+        operator WRBUF() const;
+        size_t len();
+        const char *buf();
+    private:
+        WRBUF m_wrbuf;
+    };
+    
     class PlainFile {
         class Rep;
         boost::scoped_ptr<Rep> m_p;
index 5241d6e..a7cb28c 100644 (file)
@@ -461,7 +461,7 @@ void yf::RecordTransform::Impl::process(mp::Package &package) const
             Z_NamePlusRecord *npr = records->records[i];
             if (npr->which == Z_NamePlusRecord_databaseRecord)
             {
-                WRBUF output_record = wrbuf_alloc();
+                mp::wrbuf output_record;
                 Z_External *r = npr->u.databaseRecord;
                 int ret_trans = 0;
                 if (r->which == Z_External_OPAC)
@@ -482,8 +482,8 @@ void yf::RecordTransform::Impl::process(mp::Package &package) const
                 {
                     npr->u.databaseRecord =
                         z_ext_record_oid(odr_en, match_syntax,
-                                         wrbuf_buf(output_record),
-                                         wrbuf_len(output_record));
+                                         output_record.buf(),
+                                         output_record.len());
                 }
                 else
                 {
@@ -493,7 +493,6 @@ void yf::RecordTransform::Impl::process(mp::Package &package) const
                             YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
                             yaz_record_conv_get_error(rc));
                 }
-                wrbuf_destroy(output_record);
             }
         }
         package.response() = gdu_res;
index 2b62986..93196e1 100644 (file)
@@ -303,16 +303,15 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
         package.log_reset(l);
         if (l.length())
         {
-            WRBUF w = wrbuf_alloc();
+            mp::wrbuf w;
             
             wrbuf_puts(w, "<log>\n");
             wrbuf_xmlputs(w, l.c_str());
             wrbuf_puts(w, "</log>");
             
-            sru_pdu_res->extraResponseData_len = wrbuf_len(w);
+            sru_pdu_res->extraResponseData_len = w.len();
             sru_pdu_res->extraResponseData_buf =
                 odr_strdup(odr_en, wrbuf_cstr(w));
-            wrbuf_destroy(w);
         }
     }
     
index e8e1681..76eb388 100644 (file)
@@ -85,7 +85,7 @@ namespace metaproxy_1 {
             friend class Impl;
             friend class Frontend;
             std::string zurl;
-            WRBUF m_apdu_wrbuf;
+            mp::wrbuf m_apdu_wrbuf;
             ZOOM_connection m_connection;
             ZOOM_resultset m_resultset;
             std::string m_frontend_database;
@@ -263,7 +263,6 @@ void yf::Zoom::process(mp::Package &package) const
 
 yf::Zoom::Backend::Backend()
 {
-    m_apdu_wrbuf = wrbuf_alloc();
     m_connection = ZOOM_connection_create(0);
     ZOOM_connection_save_apdu_wrbuf(m_connection, m_apdu_wrbuf);
     m_resultset = 0;
@@ -281,7 +280,6 @@ yf::Zoom::Backend::~Backend()
         xmlFreeDoc(explain_doc);
     ZOOM_connection_destroy(m_connection);
     ZOOM_resultset_destroy(m_resultset);
-    wrbuf_destroy(m_apdu_wrbuf);
 }
 
 
@@ -845,7 +843,7 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package,
             return false;
         }
         b->content_session_id.assign(xx, 6);
-        WRBUF w = wrbuf_alloc();
+        mp::wrbuf w;
         wrbuf_puts(w, "#content_proxy\n");
         wrbuf_printf(w, "connector: %s\n", b->sptr->contentConnector.c_str());
         if (authentication.length())
@@ -855,11 +853,10 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package,
         if (realm.length())
             wrbuf_printf(w, "realm: %s\n", realm.c_str());
 
-        fwrite(wrbuf_buf(w), 1, wrbuf_len(w), file);
+        fwrite(w.buf(), 1, w.len(), file);
         fclose(file);
         package.log("zoom", YLOG_LOG, "content file: %s", fname);
         xfree(fname);
-        wrbuf_destroy(w);
     }
     return true;
 }
@@ -1791,20 +1788,18 @@ next_proxy:
 
     Odr_int hits = 0;
     Z_Query *query = sr->query;
-    WRBUF ccl_wrbuf = 0;
-    WRBUF pqf_wrbuf = 0;
+    mp::wrbuf ccl_wrbuf;
+    mp::wrbuf pqf_wrbuf;
     std::string sortkeys;
 
     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
     {
         // RPN
-        pqf_wrbuf = wrbuf_alloc();
         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
     }
     else if (query->which == Z_Query_type_2)
     {
         // CCL
-        ccl_wrbuf = wrbuf_alloc();
         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
                     query->u.type_2->len);
     }
@@ -1857,7 +1852,7 @@ next_proxy:
             return;
         }
 
-        WRBUF sru_sortkeys_wrbuf = wrbuf_alloc();
+        mp::wrbuf sru_sortkeys_wrbuf;
         if (cql_sortby_to_sortkeys(cn, wrbuf_vp_puts, sru_sortkeys_wrbuf))
         {
             error = YAZ_BIB1_ILLEGAL_SORT_RELATION;
@@ -1866,16 +1861,12 @@ next_proxy:
             log_diagnostic(package, error, addinfo);
             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
             package.response() = apdu_res;
-            wrbuf_destroy(sru_sortkeys_wrbuf);
             cql_parser_destroy(cp);
             return;
         }
-        WRBUF sort_spec_wrbuf = wrbuf_alloc();
+        mp::wrbuf sort_spec_wrbuf;
         yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf),
                                       sort_spec_wrbuf);
-        wrbuf_destroy(sru_sortkeys_wrbuf);
-
-        ccl_wrbuf = wrbuf_alloc();
         wrbuf_puts(ccl_wrbuf, ccl_buf);
         
         yaz_tok_cfg_t tc = yaz_tok_cfg_create();
@@ -1910,8 +1901,6 @@ next_proxy:
             }
         }
         yaz_tok_parse_destroy(tp);
-        wrbuf_destroy(sort_spec_wrbuf);
-
         cql_parser_destroy(cp);
     }
     else
@@ -1924,16 +1913,15 @@ next_proxy:
         return;
     }
 
-    if (ccl_wrbuf)
+    if (ccl_wrbuf.len())
     {
         // CCL to PQF
-        assert(pqf_wrbuf == 0);
+        assert(pqf_wrbuf.len() == 0);
         int cerror, cpos;
         struct ccl_rpn_node *cn;
         package.log("zoom", YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf));
         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
                           &cerror, &cpos);
-        wrbuf_destroy(ccl_wrbuf);
         if (!cn)
         {
             char *addinfo = odr_strdup_null(odr, ccl_err_msg(cerror));
@@ -1959,13 +1947,12 @@ next_proxy:
             package.response() = apdu_res;
             return;
         }
-        pqf_wrbuf = wrbuf_alloc();
         ccl_pquery(pqf_wrbuf, cn);
         package.log("zoom", YLOG_LOG, "RPN: %s", wrbuf_cstr(pqf_wrbuf));
         ccl_rpn_delete(cn);
     }
     
-    assert(pqf_wrbuf);
+    assert(pqf_wrbuf.len());
 
     ZOOM_query q = ZOOM_query_create();
     ZOOM_query_sortby2(q, b->sptr->sortStrategy.c_str(), sortkeys.c_str());
@@ -1975,7 +1962,7 @@ next_proxy:
         int status = 0;
         Z_RPNQuery *zquery;
         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
-        WRBUF wrb = wrbuf_alloc();
+        mp::wrbuf wrb;
             
         if (!strcmp(b->get_option("sru"), "solr"))
         {
@@ -2001,8 +1988,6 @@ next_proxy:
         }
         ZOOM_query_destroy(q);
         
-        wrbuf_destroy(wrb);
-        wrbuf_destroy(pqf_wrbuf);
         if (status)
         {
             error = YAZ_BIB1_MALFORMED_QUERY;
@@ -2019,7 +2004,6 @@ next_proxy:
         package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
         b->search(q, &hits, &error, &addinfo, odr);
         ZOOM_query_destroy(q);
-        wrbuf_destroy(pqf_wrbuf);
     }
 
     if (error && proxy_step)
index 47b4eb1..0f1ebd8 100644 (file)
@@ -226,10 +226,9 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
                 os << "-";
 
             os << " ";
-            WRBUF wr = wrbuf_alloc();
+            mp::wrbuf wr;
             yaz_query_to_wrbuf(wr, sr->query);
             os << wrbuf_cstr(wr);
-            wrbuf_destroy(wr);
         }
         break;
     case Z_APDU_searchResponse:
@@ -360,11 +359,10 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
                 os << " ";
                 if (sr->termListAndStartPoint)
                 {
-                    WRBUF wr = wrbuf_alloc();
+                    mp::wrbuf wr;
                     yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint, 
                                       sr->attributeSet);
                     os << wrbuf_cstr(wr);
-                    wrbuf_destroy(wr);
                 }
                 else
                     os << "-";
index 3784907..4f28e4f 100644 (file)
@@ -29,6 +29,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include <metaproxy/filter.hpp>
 #include <metaproxy/package.hpp>
+#include <metaproxy/util.hpp>
 #include "router_flexml.hpp"
 #include "factory_static.hpp"
 
@@ -190,7 +191,7 @@ static int sc_main(
         yaz_log(YLOG_FATAL, "XInclude processing failed");
         return 1;
     }
-    WRBUF base_path = wrbuf_alloc();
+    mp::wrbuf base_path;
     const char *last_p = strrchr(fname,
 #ifdef WIN32
                                  '\\'
@@ -228,7 +229,6 @@ static int sc_main(
         yaz_log(YLOG_FATAL, "Unknown Exception");
         ret = 1;
     }
-    wrbuf_destroy(base_path);
     xmlFreeDoc(doc);
     return ret;
 }
index ea6c931..3ab3033 100644 (file)
@@ -262,22 +262,19 @@ std::string mp_util::zQueryToString(Z_Query *query)
 {
     std::string query_str = "";
 
-    if (query && query->which == Z_Query_type_1){
+    if (query && query->which == Z_Query_type_1)
+    {
         Z_RPNQuery *rpn = query->u.type_1;
         
-        if (rpn){
-            
-            // allocate wrbuf (strings in YAZ!)
-            WRBUF w = wrbuf_alloc();
+        if (rpn)
+        {
+            mp::wrbuf w;
             
             // put query in w
             yaz_rpnquery_to_wrbuf(w, rpn);
             
             // from w to std::string
-            query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
-            
-            // destroy wrbuf
-            wrbuf_destroy(w);
+            query_str = std::string(w.buf(), w.len());
         }
     }
 
@@ -704,6 +701,31 @@ std::string mp_util::uri_decode(std::string s)
     return result;
 }
 
+mp::wrbuf::wrbuf()
+{
+    m_wrbuf = wrbuf_alloc();
+}
+
+mp::wrbuf::~wrbuf()
+{
+    wrbuf_destroy(m_wrbuf);
+}
+
+mp::wrbuf::operator WRBUF() const
+{
+    return m_wrbuf;
+}
+
+size_t mp::wrbuf::len()
+{
+    return wrbuf_len(m_wrbuf);
+}
+
+const char *mp::wrbuf::buf()
+{
+    return wrbuf_buf(m_wrbuf);
+}
+
 
 /*
  * Local variables: