summaryrefslogtreecommitdiff
path: root/lib/Exceptions.cc
blob: 585b946751eb8fdc754896cbdc01bf1adc18c9bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#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];

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);
}

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.")) {
#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) 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;
    }
}