diff options
Diffstat (limited to 'generic/Exceptions.cpp')
-rw-r--r-- | generic/Exceptions.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/generic/Exceptions.cpp b/generic/Exceptions.cpp new file mode 100644 index 0000000..b541235 --- /dev/null +++ b/generic/Exceptions.cpp @@ -0,0 +1,152 @@ +#include <malloc.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <stddef.h> +#include "String.h" +#include "Exceptions.h" +#include "General.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#define _(x) x +#endif + +char GeneralException::t[BUFSIZ]; + +GeneralException::GeneralException(String emsg) : msg(emsg.strdup()) { +#ifdef DEBUG + cerr << _("Generating a General Exception error: '") << msg << "'.\n"; +#endif +} +GeneralException::GeneralException() : msg(0) { +#ifdef DEBUG + cerr << _("Generating a General Exception error: '") << msg << "'.\n"; +#endif +} +GeneralException::GeneralException(const GeneralException & e) : msg(strdup(e.msg)) { +#ifdef DEBUG + cerr << _("Generating a General Exception error: '") << msg << "'.\n"; +#endif +} + +GeneralException::~GeneralException() { + free(msg); +} + +TaskNotFound::TaskNotFound() : GeneralException("Task not found") { } + +char * GeneralException::GetMsg() { + return msg; +} + +MemoryException::MemoryException(ssize_t s) { + sprintf(t, _("Failed allocating %u bytes."), s); + msg = strdup(t); +} + +IOException::IOException(String fn, op_t op, ssize_t s) { + sprintf(t, _("An error has occured while %s %u bytes on %s: %s"), op == IO_WRITE ? _("writing") : _("reading"), + s, fn.to_charp(), strerror(errno)); + msg = strdup(t); +} + +IOGeneral::IOGeneral(String fn) : GeneralException(fn) { } + +IOGeneral::IOGeneral() { } + +IOAgain::IOAgain() : IOGeneral(_("No more bytes for reading or writing.")) { +#ifdef DEBUG + cerr << "Generating an IOAgain exception: '" << GetMsg() << "'.\n"; +#endif +} + +TaskSwitch::TaskSwitch() : GeneralException(_("Switching task in a non-tasked environnement")) { +#ifdef DEBUG + cerr << "Generating a TaskSwitch exception: '" << GetMsg() << "'.\n"; +#endif +} + +char * xstrdup(const char * s) { + char * r; + + r = (char *) xmalloc(strlen(s) + 1); + strcpy(r, s); + return r; +} + +void * xmalloc(size_t s) throw (GeneralException) { + char * r; + + if (!s) { + return 0; + } + + if (!(r = (char *) ::malloc(s + sizeof(size_t)))) { + throw MemoryException(s + sizeof(size_t)); + } + + memset(r, 0, s + sizeof(size_t)); + + *((size_t *)r) = s; + + return (void *)(r + sizeof(size_t)); +} + +void * xrealloc(void * ptr, size_t s) { + char * r; + size_t os; + + if (!ptr) { + return xmalloc(s); + } + + os = *(((size_t *) ptr) - 1); + + r = (char *) xmalloc(s); + + if (s) { + memcpy(r, ptr, MIN(s, os)); + } + + xfree(ptr); + return r; +} + +#ifdef OVER_FREE +#undef free +#endif + +void xfree(void *& p) { + if (p) { + ::free(((char *)p) - sizeof(size_t)); + p = 0; + } +} + +void xfree(char *& p) { + if (p) { + ::free(p - sizeof(size_t)); + p = 0; + } +} + +int xpipe(int * p, int flag) throw (GeneralException) { + if (pipe(p)) { + throw GeneralException(String(_("Error creating pipe: ")) + strerror(errno)); + } + + return p[flag]; +} + +pid_t xfork() throw (GeneralException) { + pid_t p; + + p = fork(); + + if (p == -1) { + throw GeneralException(_("Was not able to fork().\n")); + } + + return p; +} |