X-Git-Url: http://git.indexdata.com/?p=yazpp-moved-to-github.git;a=blobdiff_plain;f=zoom%2Fzexcept.cpp;h=aeca4bc08df1ddbf437a71d9feff25c3e82ee792;hp=bb8743ae6f70e577d5b86a51a8cc2b730f76f106;hb=HEAD;hpb=b8248fc2b2eb866a23a677135deb4633cb1f3322 diff --git a/zoom/zexcept.cpp b/zoom/zexcept.cpp index bb8743a..aeca4bc 100644 --- a/zoom/zexcept.cpp +++ b/zoom/zexcept.cpp @@ -1,112 +1,115 @@ -// $Header: /home/cvsroot/yaz++/zoom/zexcept.cpp,v 1.3 2002-10-09 09:07:10 mike Exp $ - // Z39.50 Exception classes +#if HAVE_CONFIG_H +#include +#endif +#include #include -#include // for strerror(), strlen(), strcpy() -#include // for sprintf() -#include +#include // for strerror(), strlen(), strcpy() +#include #include "zoom.h" namespace ZOOM { exception::exception(int errcode) { - code = errcode; + code = errcode; } - int exception::errcode() const { - return code; + exception::~exception() { + // Nothing to do, but G++ requires this to be explicit anyway } - const char *exception::errmsg() const { - static char buf[40]; - sprintf(buf, "error #%d", code); - return buf; + int exception::errcode() const { + return code; } + std::string exception::errmsg() const { + char buf[40]; + sprintf(buf, "error #%d", code); + return buf; + } - systemException::systemException() : exception::exception(errno){ - code = errno; - } - int systemException::errcode() const { - return code; + systemException::systemException() : exception(errno){ + code = errno; } - const char *systemException::errmsg() const { - return strerror(code); + std::string systemException::errmsg() const { + // For thread safety on linux (and most unix systems), we need + // to use the reentrant version of the error translation + // function. Microsoft's strerror() is thread safe, since it + // returns a pointer to thread local storage. Unfortunately + // there several different reentrant versions. Here, we check + // for glibc, since we are using gcc. It appears at least the + // current version of gcc has strerror_r() available by + // default. + #ifdef __GLIBC__ + char buf[1024]; + // PJD: result not necessarily equal to buf + const char* result = strerror_r(code, buf, sizeof(buf)); + if (result != 0) + return result; + return exception::errmsg(); + #else + return strerror(code); + #endif } - - bib1Exception::bib1Exception(int errcode, const char *addinfo) : - exception::exception(errcode) { - info = new char[strlen(addinfo)+1]; - strcpy((char*) info, addinfo); - //fprintf(stderr, "made new bib1Exception 0x%lx (%d, 0x%lx=%s)\n", - //(long) this, code, (long) info, info); - } -#if 0 - bib1Exception::bib1Exception(bib1Exception& src) : - exception::exception(src) { - code = src.code; - info = new char[strlen(src.info)+1]; - strcpy((char*) info, src.info); - //fprintf(stderr, "copied bib1Exception 0x%lx to 0x%lx (%d, 0x%lx=%s)\n", - //(long) &src, (long) this, code, (long) info, info); + bib1Exception::bib1Exception(int errcode, const std::string &addinfo) : + exception(errcode), info(addinfo) { + // std::cerr << "WARNING: made bib1Exception(" << errcode << "=" << + // ZOOM_diag_str(errcode) << ", '" << addinfo << "')\n"; } -#endif bib1Exception::~bib1Exception() { - //fprintf(stderr, "deleting bib1Exception 0x%lx (%d, 0x%lx=%s)\n", - //(long) this, code, (long) info, info); - //delete info; - // ### Don't actually do the deletion for now. Exception - // reference semantics are too weird for me to grok so I'm - // doing The Wrong Thing in the knowledge that it will more - // or less work -- it just leaks memory. - } - - int bib1Exception::errcode() const { - return code; + //fprintf(stderr, "deleting bib1Exception 0x%lx (%d, 0x%lx=%s)\n", + //(long) this, code, (long) info, info); + //delete info; + // ### Don't actually do the deletion for now. Exception + // reference semantics are too weird for me to grok so I'm + // doing The Wrong Thing in the knowledge that it will more + // or less work -- it just leaks memory. (Or does it?) } - const char *bib1Exception::errmsg() const { - return diagbib1_str(code); + std::string bib1Exception::errmsg() const { + return ZOOM_diag_str(code); } - const char *bib1Exception::addinfo() const { - return info; + std::string bib1Exception::addinfo() const { + return info; } - queryException::queryException(int qtype, const char *source) : - exception::exception(qtype) { - q = new char[strlen(source)+1]; - strcpy((char*) q, source); - } + queryException::queryException(int qtype, const std::string &source) : + exception(qtype), q(source) {} queryException::~queryException() { - delete q; + //delete q; // ### see comment on bib1Exception destructor } - int queryException::errcode() const { - return code; + std::string queryException::errmsg() const { + switch (code) { + case PREFIX: return "bad prefix search"; + case CCL: return "bad CCL search"; + default: break; + } + return "bad search (unknown type)"; } - const char *queryException::errmsg() const { - switch (code) { - case PREFIX: return "bad prefix search"; - case CCL: return "bad CCL search"; - default: break; - } - return "bad search (unknown type)"; - } - - const char *queryException::addinfo() const { - return q; + std::string queryException::addinfo() const { + return q; } } +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +