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