Bump copyright year
[yazpp-moved-to-github.git] / zoom / zexcept.cpp
1 // Z39.50 Exception classes
2
3 #include <iostream>
4 #include <errno.h>
5 #include <string.h>             // for strerror(), strlen(), strcpy()
6 #include <stdio.h>
7 #include "zoom.h"
8
9
10 namespace ZOOM {
11     exception::exception(int errcode) {
12         code = errcode;
13     }
14
15     exception::~exception() {
16         // Nothing to do, but G++ requires this to be explicit anyway
17     }
18
19     int exception::errcode() const {
20         return code;
21     }
22
23     std::string exception::errmsg() const {
24         char buf[40];
25         sprintf(buf, "error #%d", code);
26         return buf;
27     }
28
29
30
31     systemException::systemException() : exception(errno){
32         code = errno;
33     }
34
35     std::string systemException::errmsg() const {
36         // For thread safety on linux (and most unix systems), we need
37         // to use the reentrant version of the error translation
38         // function.  Microsoft's strerror() is thread safe, since it
39         // returns a pointer to thread local storage.  Unfortunately
40         // there several different reentrant versions.  Here, we check
41         // for glibc, since we are using gcc.  It appears at least the
42         // current version of gcc has strerror_r() available by
43         // default.
44         #ifdef __GLIBC__
45             char buf[1024];
46             // PJD: result not necessarily equal to buf
47             const char* result = strerror_r(code, buf, sizeof(buf));
48             if (result != 0)
49                 return result;
50             return exception::errmsg();
51         #else
52             return strerror(code);
53         #endif
54     }
55
56
57     
58     bib1Exception::bib1Exception(int errcode, const std::string &addinfo) :
59         exception(errcode), info(addinfo) {
60         // std::cerr << "WARNING: made bib1Exception(" << errcode << "=" <<
61         //   ZOOM_diag_str(errcode) << ", '" << addinfo << "')\n";
62     }
63
64     bib1Exception::~bib1Exception() {
65         //fprintf(stderr, "deleting bib1Exception 0x%lx (%d, 0x%lx=%s)\n",
66                 //(long) this, code, (long) info, info);
67         //delete info;
68         //  ### Don't actually do the deletion for now.  Exception
69         //  reference semantics are too weird for me to grok so I'm
70         //  doing The Wrong Thing in the knowledge that it will more
71         //  or less work -- it just leaks memory.  (Or does it?)
72     }
73
74     std::string bib1Exception::errmsg() const {
75         return ZOOM_diag_str(code);
76     }
77
78     std::string bib1Exception::addinfo() const {
79         return info;
80     }
81
82
83
84     queryException::queryException(int qtype, const std::string &source) :
85         exception(qtype), q(source) {}
86
87     queryException::~queryException() {
88         //delete q; // ### see comment on bib1Exception destructor
89     }
90
91     std::string queryException::errmsg() const {
92         switch (code) {
93         case PREFIX: return "bad prefix search";
94         case CCL: return "bad CCL search";
95         default: break;
96         }
97         return "bad search (unknown type)";
98     }
99
100     std::string queryException::addinfo() const {
101         return q;
102     }
103 }
104 /*
105  * Local variables:
106  * c-basic-offset: 4
107  * c-file-style: "Stroustrup"
108  * indent-tabs-mode: nil
109  * End:
110  * vim: shiftwidth=4 tabstop=8 expandtab
111  */
112