diff options
author | Pixel <Pixel> | 2001-09-20 23:27:01 +0000 |
---|---|---|
committer | Pixel <Pixel> | 2001-09-20 23:27:01 +0000 |
commit | 8346d0774d2d1e076038db27f65f1d082a460f16 (patch) | |
tree | 132f84cf1ef45d5006a2b1d52d4d40b1e8e51abc /lib/Exceptions.cc |
Initial revision
Diffstat (limited to 'lib/Exceptions.cc')
-rw-r--r-- | lib/Exceptions.cc | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/Exceptions.cc b/lib/Exceptions.cc new file mode 100644 index 0000000..b5a33bd --- /dev/null +++ b/lib/Exceptions.cc @@ -0,0 +1,88 @@ +#include <malloc.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <stddef.h> +#include "config.h" +#include "String.h" +#include "Exceptions.h" + +char GeneralException::t[BUFSIZ]; + +char * Base::strdup(const char * s) const { + return xstrdup(s); +} + +void * Base::malloc(ssize_t s) const { + return xmalloc(s); +} + +/* +void * Base::operator new(size_t s) { + return memset(xmalloc(s), 0, s); +} + +void * Base::operator new(size_t s, void * & p) { + return memset(p = xmalloc(s), 0, s); +} +*/ + +GeneralException::GeneralException(String emsg) : msg(strdup(emsg.to_charp())) { } +GeneralException::GeneralException() : msg(0) { } +GeneralException::GeneralException(const GeneralException & e) : msg(strdup(e.msg)) { } + +GeneralException::~GeneralException() { + free(msg); +} + +char * GeneralException::GetMsg() { + return msg; +} + +MemoryException::MemoryException(ssize_t s) { + sprintf(t, _("Failed allocating %lld bytes."), s); + msg = strdup(t); +} + +IOException::IOException(String fn, op_t op, ssize_t s) { + sprintf(t, _("An error has occured while %s %lld bytes from %s: %s"), op == IO_WRITE ? _("writing") : _("reading"), + s, fn.to_charp(), strerror(errno)); + msg = strdup(t); +} + +IOInternal::IOInternal(String fn, op_t op) { + sprintf(t, _("Internal error: has occured while %s from %s: open for %s."), op == IO_WRITE ? _("writing") : _("reading"), + fn.to_charp(), op == IO_WRITE ? _("reading") : _("writing")); + msg = strdup(t); +} + +IOGeneral::IOGeneral(String fn) : GeneralException(fn) { } + +char * xstrdup(const char * s) throw (MemoryException) { + char * r; + + if (!(r = ::strdup(s))) { + throw MemoryException(strlen(s + 1)); + } + + return r; +} + +void * xmalloc(ssize_t s) throw (MemoryException) { + void * r; + + if (!(r = ::malloc(s))) { + throw MemoryException(s); + } + + return r; +} + +#undef free + +void xfree(void *& p) { + if (p) { + ::free(p); + p = 0; + } +} |