/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
5.2.7 by Gustav Hartvigsson
* Switched licence to a more permisive one.
1
/*
2
Copyright (c) 2013-2014 Gustav Hartvigsson
3
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
10
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
13
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
THE SOFTWARE.
21
*/
3 by Gustav Hartvigsson
Fixed a few things...
22
23
#include "Error.h"
24
#include <stdlib.h>
25
#include <string.h>
26
#include <stdio.h>
22 by Gustav Hartvigsson
* Made code compile
27
#include <assert.h>
39 by Gustav Hartvigsson
* Added "check" target for testing.
28
#include "utils.h"
29
30
struct SErrorPrivate {
81 by Gustav Hartvigsson
* Re arranged members of structs to prevent struct fragmentation and padding.
31
  schar * message;
5.2.2 by Gustav Hartvigsson
Made sure -- using valgrind -- that the SBaseObjectInstance and
32
  SErrorType error_type;
3 by Gustav Hartvigsson
Fixed a few things...
33
};
34
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
35
schar *
61 by Gustav Hartvigsson
* Made the code more easy to read.
36
error_to_string_method (SObject * self);
37
void
38
error_free_method (SError * self);
3 by Gustav Hartvigsson
Fixed a few things...
39
61 by Gustav Hartvigsson
* Made the code more easy to read.
40
SError *
41
s_error_new (SErrorType error, char * message) {
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
42
  SError * self = malloc (sizeof (SError));
43
  SErrorClass * klass = malloc (sizeof (SErrorClass));
5.2.2 by Gustav Hartvigsson
Made sure -- using valgrind -- that the SBaseObjectInstance and
44
  self->priv = malloc (sizeof (SErrorPrivate));
45
  
48 by Gustav Hartvigsson
* Finnished SLinkedList.
46
  s_object_set_class (S_OBJECT (self), S_OBJECT_CLASS (klass));
47
  s_object_initialize (S_OBJECT (self), "Error");
48
  s_object_set_to_string_method (S_OBJECT (self), error_to_string_method);
49
  s_object_set_free_method (S_OBJECT (self), METHOD(error_free_method));
50
  
3 by Gustav Hartvigsson
Fixed a few things...
51
  self->priv->message = s_string_new (message);
5.2.2 by Gustav Hartvigsson
Made sure -- using valgrind -- that the SBaseObjectInstance and
52
  self->priv->error_type = error;
3 by Gustav Hartvigsson
Fixed a few things...
53
  return self;
54
}
55
61 by Gustav Hartvigsson
* Made the code more easy to read.
56
void
57
s_error_free (SError * self) {
48 by Gustav Hartvigsson
* Finnished SLinkedList.
58
  s_object_free (S_OBJECT (self));
3 by Gustav Hartvigsson
Fixed a few things...
59
}
60
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
61
schar *
61 by Gustav Hartvigsson
* Made the code more easy to read.
62
error_to_string_method (SObject * obj) {
49 by Gustav Hartvigsson
* started work SBox (Untested).
63
  SError * self = S_ERROR (obj);
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
64
  schar * ret_val = NULL;
65
  schar * error_type_str = NULL;
39 by Gustav Hartvigsson
* Added "check" target for testing.
66
  error_type_str = s_string_new (SErrorTypeName[self->priv->error_type]);
67
  
68
  ret_val = s_string_new_fmt ("Error: %s, Message: %s", error_type_str, self->priv->message);
3 by Gustav Hartvigsson
Fixed a few things...
69
  free (error_type_str);
70
  return ret_val;
71
}
72
61 by Gustav Hartvigsson
* Made the code more easy to read.
73
void
74
error_free_method (SError * self) {
3 by Gustav Hartvigsson
Fixed a few things...
75
  free (self->priv->message);
5.2.2 by Gustav Hartvigsson
Made sure -- using valgrind -- that the SBaseObjectInstance and
76
  free (self->priv);
49 by Gustav Hartvigsson
* started work SBox (Untested).
77
  SErrorClass * klass = S_ERROR_CLASS (s_object_get_class (S_OBJECT (self)));
5.2.2 by Gustav Hartvigsson
Made sure -- using valgrind -- that the SBaseObjectInstance and
78
  free (klass);
3 by Gustav Hartvigsson
Fixed a few things...
79
  free (self);
80
}
81
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
82
schar *
61 by Gustav Hartvigsson
* Made the code more easy to read.
83
s_error_get_name (SErrorType k) {
49 by Gustav Hartvigsson
* started work SBox (Untested).
84
  return SErrorTypeName[k];
85
}
86