a012c5d79435ad4d8a6733daa51682cf8dff246e
[yazpp-moved-to-github.git] / zoom / zexcept.cpp
1 // $Header: /home/cvsroot/yaz++/zoom/zexcept.cpp,v 1.5 2002-10-30 09:13:12 adam Exp $
2
3 // Z39.50 Exception classes
4
5 #include <errno.h>
6 #include <string.h>             // for strerror(), strlen(), strcpy()
7 #include <stdio.h>              // for sprintf()
8 #include <yaz/diagbib1.h>
9 #include "zoom.h"
10
11
12 namespace ZOOM {
13     exception::exception(int errcode) {
14         code = errcode;
15     }
16
17     int exception::errcode() const {
18         return code;
19     }
20
21     const char *exception::errmsg() const {
22         static char buf[40];
23         sprintf(buf, "error #%d", code);
24         return buf;
25     }
26
27
28
29     systemException::systemException() : exception(errno){
30         code = errno;
31     }
32
33     int systemException::errcode() const {
34         return code;
35     }
36
37     const char *systemException::errmsg() const {
38         return strerror(code);
39     }
40
41
42     
43     bib1Exception::bib1Exception(int errcode, const char *addinfo) :
44         exception(errcode) {
45         info = new char[strlen(addinfo)+1];
46         strcpy((char*) info, addinfo);
47         //fprintf(stderr, "made new bib1Exception 0x%lx (%d, 0x%lx=%s)\n",
48                 //(long) this, code, (long) info, info);
49     }
50
51 #if 0
52     bib1Exception::bib1Exception(bib1Exception& src) :
53         exception(src) {
54         code = src.code;
55         info = new char[strlen(src.info)+1];
56         strcpy((char*) info, src.info);
57         //fprintf(stderr, "copied bib1Exception 0x%lx to 0x%lx (%d, 0x%lx=%s)\n",
58                 //(long) &src, (long) this, code, (long) info, info);
59     }
60 #endif
61
62     bib1Exception::~bib1Exception() {
63         //fprintf(stderr, "deleting bib1Exception 0x%lx (%d, 0x%lx=%s)\n",
64                 //(long) this, code, (long) info, info);
65         //delete info;
66         //  ### Don't actually do the deletion for now.  Exception
67         //  reference semantics are too weird for me to grok so I'm
68         //  doing The Wrong Thing in the knowledge that it will more
69         //  or less work -- it just leaks memory.
70     }
71
72     int bib1Exception::errcode() const {
73         return code;
74     }
75
76     const char *bib1Exception::errmsg() const {
77         return diagbib1_str(code);
78     }
79
80     const char *bib1Exception::addinfo() const {
81         return info;
82     }
83
84
85
86     queryException::queryException(int qtype, const char *source) :
87         exception(qtype) {
88         q = new char[strlen(source)+1];
89         strcpy((char*) q, source);
90     }
91
92     queryException::~queryException() {
93         //delete q; // ### see comment on bib1Exception destructor
94     }
95
96     int queryException::errcode() const {
97         return code;
98     }
99
100     const char *queryException::errmsg() const {
101         switch (code) {
102         case PREFIX: return "bad prefix search";
103         case CCL: return "bad CCL search";
104         default: break;
105         }
106         return "bad search (unknown type)";
107     }
108
109     const char *queryException::addinfo() const {
110         return q;
111     }
112 }