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
|
#include "Error.h"
#include "SimpleTypeSystem.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct _SErrorPrivate {
char * message;
SErrorType error_type;
};
char * error_to_string_method (SError * self);
void error_deinit_method (SError * self);
SError * s_error_new (SErrorType error, char * message) {
SError * self = malloc (sizeof (SError));
SErrorClass * klass = malloc (sizeof (SErrorClass));
s_base_object_set_class ((SBaseObjectInstance *) self, (SBaseObjectClass *) klass);
s_base_object_initize ((SBaseObjectInstance *) self);
s_base_object_set_to_string_method ((SBaseObjectInstance *) self, error_to_string_method);
s_base_object_set_deinit_method ((SBaseObjectInstance *) self, error_deinit_method);
self->priv->message = s_string_new (message);
return self;
}
void s_error_free (SError * self) {
s_base_object_free ((SBaseObjectInstance *) self);
}
char * error_to_string_method (SError * self) {
char * ret_val = malloc (sizeof(char) * 32);
char * error_type_str = NULL;
switch (self->priv->error_type) {
case (S_ERROR_NONE):
error_type_str = s_string_new ("NONE");
break;
case (S_ERROR_INPUT_OUTPUT):
error_type_str = s_string_new ("INPUT/OUTPUT");
break;
case (S_ERROR_OVERFLOW):
error_type_str = s_string_new ("OVERFLOW");
break;
case (S_ERROR_OTHER):
error_type_str = s_string_new ("OTHER");
break;
case (S_ERROR_NULL):
error_type_str = s_string_new ("NULL");
break;
}
sprintf (ret_val, "Error: %s, Message: %s", error_type_str, self->priv->message);
free (error_type_str);
return ret_val;
}
void error_deinit_method (SError * self) {
free (self->priv->message);
free ((SErrorClass *) s_base_object_get_class ((SBaseObjectInstance *) self));
free (self);
}
|