Update to use Odr_int
[metaproxy-moved-to-github.git] / src / gduutil.cpp
index 333e605..976b091 100644 (file)
@@ -1,13 +1,26 @@
-/* $Id: gduutil.cpp,v 1.11 2006-09-21 11:45:00 marc Exp $
-   Copyright (c) 2005-2006, Index Data.
-
-   See the LICENSE file for details
+/* This file is part of Metaproxy.
+   Copyright (C) 2005-2009 Index Data
+
+Metaproxy is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
 #include "gduutil.hpp"
 #include "util.hpp"
 
 #include <yaz/wrbuf.h>
+#include <yaz/oid_db.h>
 #include <yaz/querytowrbuf.h>
 
 #include <iostream>
 namespace mp = metaproxy_1;
 
 // Doxygen doesn't like mp::gdu, so we use this instead
-namespace mp_gdu = metaproxy_1::gdu;
+namespace mp_util = metaproxy_1::util;
+
+
+
 
 std::ostream& std::operator<<(std::ostream& os,  Z_GDU& zgdu)
 {
@@ -114,52 +130,74 @@ std::ostream& std::operator<<(std::ostream& os, Z_DefaultDiagFormat& ddf)
     return os;
 }
 
+static void dump_opt_string(std::ostream& os, const char *s)
+{
+    os << " ";
+    if (s)
+        os << s;
+    else
+        os << "-";
+}
+
+static void dump_opt_int(std::ostream& os, const Odr_int *i)
+{
+    os << " ";
+    if (i)
+        os << *i;
+    else
+        os << "-";
+}
+
 std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
 {
     switch(zapdu.which) {
 
     case Z_APDU_initRequest:
-        os << " " << "initRequest" << " ";
+        os << " " << "initRequest";
                         
         {
             Z_InitRequest *ir 
                 = zapdu.u.initRequest;
 
             Z_IdAuthentication *a = ir->idAuthentication;
-            if (a && a->which == Z_IdAuthentication_idPass )
-                os << a->u.idPass->userId << " ";
-            //<< ":" << a->u.idPass->groupId << " ";
+            if (a && a->which == Z_IdAuthentication_idPass)
+                dump_opt_string(os, a->u.idPass->userId);
+            else if (a && a->which == Z_IdAuthentication_open)
+                dump_opt_string(os, a->u.open);
             else
-                os << "-" << " ";
-
+                dump_opt_string(os, 0);
+            
+            os << " ";
             std::list<std::string> vhosts;
             mp::util::get_vhost_otherinfo(ir->otherInfo, vhosts);
             if (vhosts.size()){
                 copy(vhosts.begin(), vhosts.end(), 
                      ostream_iterator<string>(os, " "));
             }
-                else
-                    os << "-" << " " ;
-
-            os << (ir->implementationId) << " "
-                //<< ir->referenceId << " "
-               << (ir->implementationName) << " "
-               << (ir->implementationVersion);
+            else
+                os << "-" ;
+            
+            dump_opt_string(os, ir->implementationId);
+            dump_opt_string(os, ir->implementationName);
+            dump_opt_string(os, ir->implementationVersion);
         }
         break;
     case Z_APDU_initResponse:
-        os << " " << "initResponse" << " ";
+        os << " " << "initResponse ";
         {
             Z_InitResponse *ir 
                 = zapdu.u.initResponse;
             if (ir->result && *(ir->result))
-                os << "OK" << " "
-                   << (ir->implementationId) << " "
-                    //<< ir->referenceId << " "
-                   << (ir->implementationName) << " "
-                   << (ir->implementationVersion) << " ";
+            {
+                os << "OK";
+            }
             else
-                os << "DIAG";
+            {
+                os << "FAIL";
+            }
+            dump_opt_string(os, ir->implementationId);
+            dump_opt_string(os, ir->implementationName);
+            dump_opt_string(os, ir->implementationVersion);
         }
         break;
     case Z_APDU_searchRequest:
@@ -171,39 +209,42 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
             for (int i = 0; i < sr->num_databaseNames; i++)
             {
                 os << sr->databaseNames[i];
-                if (i+1 ==  sr->num_databaseNames)
-                    os << " ";
-                else
+                if (i+1 !=  sr->num_databaseNames)
                     os << "+";
             }
-                         
+
+            dump_opt_string(os, sr->resultSetName);
+
+            os << " ";
+            if (sr->preferredRecordSyntax)
+            {
+                char oid_name_str[OID_STR_MAX];
+                const char *oid_name = yaz_oid_to_string_buf(
+                    sr->preferredRecordSyntax, 0, oid_name_str);
+                
+                os << oid_name;
+            }
+            else
+                os << "-";
+
+            os << " ";
             WRBUF wr = wrbuf_alloc();
             yaz_query_to_wrbuf(wr, sr->query);
-            os << wrbuf_buf(wr);
-            wrbuf_free(wr, 1);
+            os << wrbuf_cstr(wr);
+            wrbuf_destroy(wr);
         }
         break;
     case Z_APDU_searchResponse:
-        os << " " << "searchResponse" << " ";
+        os << " " << "searchResponse ";
         {
             Z_SearchResponse *sr 
                 = zapdu.u.searchResponse;
             if (sr->searchStatus && *(sr->searchStatus))
             {
                 os << "OK";
-                if (sr->resultCount)
-                    os << " " << *(sr->resultCount);
-                else
-                    os << " -";
-                //<< sr->referenceId << " "
-                if (sr->numberOfRecordsReturned)
-                    os << " " << *(sr->numberOfRecordsReturned);
-                else
-                    os << " -";
-                if (sr->nextResultSetPosition)
-                    os << " " << *(sr->nextResultSetPosition);
-                else
-                    os << " -";
+                dump_opt_int(os, sr->resultCount);
+                dump_opt_int(os, sr->numberOfRecordsReturned);
+                dump_opt_int(os, sr->nextResultSetPosition);
             }
             else 
                 if (sr->records)
@@ -213,13 +254,26 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
         }
         break;
     case Z_APDU_presentRequest:
-        os << " " << "presentRequest" << " ";
+        os << " " << "presentRequest";
         {
             Z_PresentRequest *pr = zapdu.u.presentRequest;
-            os << pr->resultSetId << " "
-                //<< pr->referenceId << " "
-               << *(pr->resultSetStartPoint) << " "
-               << *(pr->numberOfRecordsRequested);
+            dump_opt_string(os, pr->resultSetId);
+            dump_opt_int(os, pr->resultSetStartPoint);
+            dump_opt_int(os, pr->numberOfRecordsRequested);
+            if (pr->preferredRecordSyntax)
+            {
+                char oid_name_str[OID_STR_MAX];
+                const char *oid_name = yaz_oid_to_string_buf(
+                    pr->preferredRecordSyntax, 0, oid_name_str);
+                    
+                os << " " << oid_name;
+            }
+            else
+                os << " -";
+            const char * msg = 0;
+            if (pr->recordComposition)
+                msg = mp_util::record_composition_to_esn(pr->recordComposition);
+            dump_opt_string(os, msg);
         }
         break;
     case Z_APDU_presentResponse:
@@ -291,33 +345,24 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
                 for (int i = 0; i < sr->num_databaseNames; i++)
                 {
                     os << sr->databaseNames[i];
-                    if (i+1 ==  sr->num_databaseNames)
-                        os << " ";
-                    else
-                    os << "+";
+                    if (i+1 !=  sr->num_databaseNames)
+                        os << "+";
                 }
-                if (sr->numberOfTermsRequested)
-                    os << " " << *(sr->numberOfTermsRequested);
-                else
-                     os << " -";
-                if (sr->preferredPositionInResponse)
-                    os << " " << *(sr->preferredPositionInResponse);
-                else
-                    os << " -";
-                if (sr->stepSize)
-                    os << " " << *(sr->stepSize);
-                else
-                    os << " -";
-                
+                dump_opt_int(os, sr->numberOfTermsRequested);
+                dump_opt_int(os, sr->preferredPositionInResponse);
+                dump_opt_int(os, sr->stepSize);
+
+                os << " ";
                 if (sr->termListAndStartPoint)
                 {
                     WRBUF wr = wrbuf_alloc();
-                    yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint, VAL_NONE);
-                    os << wrbuf_buf(wr);
-                    wrbuf_free(wr, 1);
+                    yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint, 
+                                      sr->attributeSet);
+                    os << wrbuf_cstr(wr);
+                    wrbuf_destroy(wr);
                 }
                 else
-                    os << " -";
+                    os << "-";
             }
         }
         break;
@@ -328,61 +373,41 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
                 = zapdu.u.scanResponse;
             if (sr)
             {
-                if ((sr->scanStatus) && !*(sr->scanStatus))
+                if (!sr->scanStatus)
                 {
                     os << "OK";
-                    //<< *(sr->scanStatus) << " "
-                    if (sr->numberOfEntriesReturned)
-                        os << " " << *(sr->numberOfEntriesReturned);
-                    else
-                        os << " -";
-                    //<< sr->referenceId << " "
-                    if (sr->positionOfTerm)
-                        os << " " << *(sr->positionOfTerm);
-                    else
-                        os << " -";
-                    if (sr->stepSize)
-                        os << " " << *(sr->stepSize);
-                     else
-                        os << " -";                  
                 }
-                else {
-                    os << "ERROR";
-                    if (sr->scanStatus)
-                    {
-                        os << " " << *(sr->scanStatus) << " ";
-                    
-                        switch (*(sr->scanStatus)){
-                        case Z_Scan_success:
-                            os << "success ";
-                            break;
-                        case Z_Scan_partial_1:
-                            os << "partial_1";
-                            break;
-                        case Z_Scan_partial_2:
-                            os << "partial_2";
-                            break;
-                        case Z_Scan_partial_3:
-                            os << "partial_3";
-                            break;
-                        case Z_Scan_partial_4:
-                            os << "partial_4";
-                            break;
-                        case Z_Scan_partial_5:
-                            os << "partial_5";
-                            break;
-                        case Z_Scan_failure:
-                            os << "failure";
-                            break;
-                        default:
-                            os << "unknown";
-                        }
+                else
+                {
+                    switch (*(sr->scanStatus)){
+                    case Z_Scan_success:
+                        os << "OK";
+                        break;
+                    case Z_Scan_partial_1:
+                        os << "partial_1";
+                        break;
+                    case Z_Scan_partial_2:
+                        os << "partial_2";
+                        break;
+                    case Z_Scan_partial_3:
+                        os << "partial_3";
+                        break;
+                    case Z_Scan_partial_4:
+                        os << "partial_4";
+                        break;
+                    case Z_Scan_partial_5:
+                        os << "partial_5";
+                        break;
+                    case Z_Scan_failure:
+                        os << "failure";
+                        break;
+                    default:
+                        os << "unknown";
                     }
-                    if (sr->numberOfEntriesReturned)
-                        os << " " << *(sr->numberOfEntriesReturned);
-                    else
-                        os << " -";
                 }
+                dump_opt_int(os, sr->numberOfEntriesReturned);
+                dump_opt_int(os, sr->positionOfTerm);
+                dump_opt_int(os, sr->stepSize);
             }
         }
         break;
@@ -539,187 +564,14 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
 }
 
 
-std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu) 
-{
-    os << "SRU";
-    
-    switch(srw_pdu.which) {
-    case  Z_SRW_searchRetrieve_request:
-        os << " " << "searchRetrieveRequest";
-        {
-            Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
-            if (sr)
-            {
-                if (sr->database)
-                    os << " " << (sr->database);
-                else
-                    os << " -";
-                if (sr->startRecord)
-                    os << " " << *(sr->startRecord);
-                else
-                    os << " -";
-                if (sr->maximumRecords)
-                    os << " " << *(sr->maximumRecords);
-                else
-                    os << " -";
-                if (sr->recordPacking)
-                    os << " " << (sr->recordPacking);
-                else
-                    os << " -";
-                
-                switch (sr->query_type){
-                case Z_SRW_query_type_cql:
-                    os << " CQL " << sr->query.cql;
-                    break;
-                case Z_SRW_query_type_xcql:
-                    os << " XCQL";
-                    break;
-                case Z_SRW_query_type_pqf:
-                    os << " PQF " << sr->query.pqf;
-                    break;
-                }
-            }
-        }
-        break;
-    case  Z_SRW_searchRetrieve_response:
-        os << " " << "searchRetrieveResponse";
-        {
-            Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
-            if (sr)
-            {
-                if (! (sr->num_diagnostics))
-                {
-                    os << " OK";
-                    if (sr->numberOfRecords)
-                        os << " " << *(sr->numberOfRecords);
-                    else
-                        os << " -";
-                    //if (sr->num_records)
-                    os << " " << (sr->num_records);
-                    //else
-                    //os << " -";
-                    if (sr->nextRecordPosition)
-                        os << " " << *(sr->nextRecordPosition);
-                    else
-                        os << " -";
-                } 
-                else
-                {
-                    os << " DIAG";
-                    if (sr->diagnostics && sr->diagnostics->uri)
-                        os << " " << (sr->diagnostics->uri);
-                    else
-                        os << " -";
-                    if (sr->diagnostics && sr->diagnostics->message)
-                        os << " " << (sr->diagnostics->message);
-                    else
-                        os << " -";
-                    if (sr->diagnostics && sr->diagnostics->details)
-                        os << " " << (sr->diagnostics->details);
-                    else
-                        os << " -";
-                }
-                
-                    
-            }
-        }
-        break;
-    case  Z_SRW_explain_request:
-        os << " " << "explainRequest";
-        break;
-    case  Z_SRW_explain_response:
-        os << " " << "explainResponse";
-        break;
-    case  Z_SRW_scan_request:
-        os << " " << "scanRequest";
-        break;
-    case  Z_SRW_scan_response:
-        os << " " << "scanResponse";
-        break;
-    case  Z_SRW_update_request:
-        os << " " << "updateRequest";
-        break;
-    case  Z_SRW_update_response:
-        os << " " << "updateResponse";
-        break;
-    default: 
-        os << " " << "UNKNOWN";    
-    }
-
-    return os;    
-}
-
-    
-//         {
-//             Z_InitRequest *ir 
-//                 = zapdu.u.initRequest;
-
-//             Z_IdAuthentication *a = ir->idAuthentication;
-//             if (a && a->which == Z_IdAuthentication_idPass )
-//                 os << a->u.idPass->userId << " ";
-//             //<< ":" << a->u.idPass->groupId << " ";
-//             else
-//                 os << "-" << " ";
-
-//             std::list<std::string> vhosts;
-//             mp::util::get_vhost_otherinfo(ir->otherInfo, vhosts);
-//             if (vhosts.size()){
-//                 copy(vhosts.begin(), vhosts.end(), 
-//                      ostream_iterator<string>(os, " "));
-//             }
-//                 else
-//                     os << "-" << " " ;
-
-//             os << (ir->implementationId) << " "
-//                 //<< ir->referenceId << " "
-//                << (ir->implementationName) << " "
-//                << (ir->implementationVersion);
-//         }
-//         break;
-//     case Z_APDU_initResponse:
-//         os << " " << "initResponse" << " ";
-//         {
-//             Z_InitResponse *ir 
-//                 = zapdu.u.initResponse;
-//             if (ir->result && *(ir->result))
-//                 os << "OK" << " "
-//                    << (ir->implementationId) << " "
-//                     //<< ir->referenceId << " "
-//                    << (ir->implementationName) << " "
-//                    << (ir->implementationVersion) << " ";
-//             else
-//                 os << "DIAG";
-//         }
-//         break;
-//     case Z_APDU_searchRequest:
-//         os << " " << "searchRequest" << " ";
-//         { 
-//             Z_SearchRequest *sr 
-//                 = zapdu.u.searchRequest;
-                            
-//             for (int i = 0; i < sr->num_databaseNames; i++)
-//             {
-//                 os << sr->databaseNames[i];
-//                 if (i+1 ==  sr->num_databaseNames)
-//                     os << " ";
-//                 else
-//                     os << "+";
-//             }
-                         
-//             WRBUF wr = wrbuf_alloc();
-//             yaz_query_to_wrbuf(wr, sr->query);
-//             os << wrbuf_buf(wr);
-//             wrbuf_free(wr, 1);
-//         }
-//         break;
-
 
 
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
- * c-file-style: "stroustrup"
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+