#include #include #include #include #include #include "config.h" #include "String.h" #include "Exceptions.h" char GeneralException::t[BUFSIZ]; GeneralException::GeneralException(String emsg) : msg(emsg.strdup()) { } 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 %ld bytes."), s); msg = strdup(t); } IOException::IOException(String fn, op_t op, ssize_t s) { sprintf(t, _("An error has occured while %s %ld bytes from %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.")) { } TaskSwitch::TaskSwitch() : GeneralException(_("Switching task in a non-tasked environnement")) { } char * xstrdup(const char * s) throw (GeneralException) { char * r; if (!(r = ::strdup(s))) { throw MemoryException(strlen(s + 1)); } return r; } void * xmalloc(ssize_t s) throw (GeneralException) { void * r; if (!(r = ::malloc(s))) { throw MemoryException(s); } memset(r, 0, s); return r; } void * xrealloc(void * ptr, size_t s) throw (GeneralException) { void * r; if (!(r = ::realloc(ptr, s))) { throw MemoryException(s); } return r; } #ifdef OVER_FREE #undef free #endif void xfree(void *& p) { if (p) { ::free(p); p = 0; } }