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