Release 1.6.4
[yazpp-moved-to-github.git] / zoom / zrec.cpp
1 // Z39.50 Record class
2
3 #if HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 #include "zoom.h"
7 #include <yaz/yaz-util.h>       // for yaz_matchstr()
8
9
10 namespace ZOOM {
11     record::syntax::syntax (value rs): val(rs) {}
12
13     record::syntax::operator std::string() const {
14         switch (val) {
15         case GRS1:   return "grs1";
16         case SUTRS:  return "sutrs";
17         case USMARC: return "usmarc";
18         case UKMARC: return "ukmarc";
19         case XML:    return "xml";
20         default: break;
21         }
22         return "unknown";
23     }
24
25     bool record::syntax::operator==(const record::syntax &s) const {
26         return s.val == val;
27     }
28
29     bool record::syntax::operator==(record::syntax::value rs) const {
30         return rs == val;
31     }
32
33     record::syntax::operator record::syntax::value() const {
34         return val;
35     }
36
37
38     record::record(resultSet &rs, size_t i): owner(rs) {
39         if ((r = ZOOM_resultset_record(rs._getYazResultSet(), i)) == 0) {
40             const char *errmsg; // unused: carries same info as `errcode'
41             const char *addinfo;
42             int errcode = ZOOM_connection_error(rs._getYazConnection(),
43                                                 &errmsg, &addinfo);
44             throw bib1Exception(errcode, addinfo);
45         }
46
47         // Memory management is odd here.  The ZOOM-C record we've
48         // just fetched (`r') is owned by the ZOOM-C result-set we
49         // fetched it from (`rs.rs'), so the underlying (ZOOM-C)
50         // record is _not_ destroyed when this object is destroyed:
51         // it's done when the underlying result-set is deleted.
52     }
53
54     record::~record() {
55         // Nothing to do -- see comment in constructor
56     }
57
58     // It's tempting to modify this method just to return either the
59     // string that ZOOM_record_get("syntax") gives us, or the VAL_*
60     // value from Yaz's OID database, but we'd break the nominal
61     // plug-compatibility of competing C++ binding implementations
62     // if we did that.
63     //
64     record::syntax record::recsyn() const {
65         const char *syn = ZOOM_record_get(r, "syntax", 0);
66
67         // These string constants are from yaz/util/oid.c
68         if (!yaz_matchstr(syn, "xml"))
69             return syntax::XML;
70         else if (!yaz_matchstr(syn, "GRS-1"))
71             return syntax::GRS1;
72         else if (!yaz_matchstr(syn, "SUTRS"))
73             return syntax::SUTRS;
74         else if (!yaz_matchstr(syn, "USmarc"))
75             return syntax::USMARC;
76         else if (!yaz_matchstr(syn, "UKmarc"))
77             return syntax::UKMARC;
78         else if (!yaz_matchstr(syn, "XML") ||
79                  !yaz_matchstr(syn, "text-XML") ||
80                  !yaz_matchstr(syn, "application-XML"))
81             return syntax::XML;
82
83         return syntax::UNKNOWN;
84     }
85
86     std::string record::render() const {
87         int len;
88         const char* data = ZOOM_record_get(r, "render", &len);
89         return std::string(data, len);
90     }
91
92     std::string record::rawdata() const {
93         int len;
94         const char* data = ZOOM_record_get(r, "raw", &len);
95         return std::string(data, len);
96     }
97 }
98 /*
99  * Local variables:
100  * c-basic-offset: 4
101  * c-file-style: "Stroustrup"
102  * indent-tabs-mode: nil
103  * End:
104  * vim: shiftwidth=4 tabstop=8 expandtab
105  */
106