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