bzr branch
http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad_ducktape
21
by Gustav Hatvigsson
* added Modeline to (almost) all files. |
1 |
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
|
2 |
* vi: set shiftwidth=2 tabstop=2 expandtab:
|
|
3 |
* :indentSize=2:tabSize=2:noTabs=true:
|
|
4 |
*/
|
|
5 |
||
16
by Gustav Hartvigsson
* made the code compile. |
6 |
#include "DynamicArray.h" |
7 |
||
17
by Gustav Hartvigsson
* fixed a few Doxygen problems. |
8 |
/*
|
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
9 |
* This file, as the rest of the project is under MIT license.
|
10 |
* see http://opensource.org/licenses/MIT
|
|
11 |
*
|
|
12 |
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
|
|
13 |
*/
|
|
7
by Gustav Hartvigsson
* Added licensing information to the files. |
14 |
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
15 |
struct DynamicArray { |
7
by Gustav Hartvigsson
* Added licensing information to the files. |
16 |
size_t max_size; |
17 |
size_t len; |
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
18 |
FreeFunc free_func; |
29
by Gustav Hartvigsson
* Fixed Makefile |
19 |
_pointer * array; |
7
by Gustav Hartvigsson
* Added licensing information to the files. |
20 |
};
|
21 |
||
29
by Gustav Hartvigsson
* Fixed Makefile |
22 |
void _private_for_each_item_free (DynamicArray * self, _pointer item, |
23 |
_pointer data); |
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
24 |
|
25 |
DynamicArray * dynamic_array_new (size_t len, FreeFunc free_func) { |
|
7
by Gustav Hartvigsson
* Added licensing information to the files. |
26 |
DynamicArray * self = malloc (sizeof(DynamicArray)); |
27 |
|
|
28 |
self->max_size = len; |
|
29 |
self->len = 0; |
|
30 |
|
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
31 |
self->free_func = free_func; |
32 |
|
|
33 |
self->array = malloc (len * sizeof (* self->array)); |
|
7
by Gustav Hartvigsson
* Added licensing information to the files. |
34 |
|
35 |
return self; |
|
36 |
}
|
|
37 |
||
38 |
void dynamic_array_free (DynamicArray * self) { |
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
39 |
if (self->free_func != NULL) { |
40 |
dynamic_array_for_each (self, (ForEachFunc) _private_for_each_item_free, |
|
41 |
NULL); |
|
42 |
} else { |
|
43 |
for (int i = 0; i < self->len; i++) { |
|
44 |
free (self->array[i]); |
|
45 |
} |
|
46 |
} |
|
7
by Gustav Hartvigsson
* Added licensing information to the files. |
47 |
free (self->array); |
48 |
free (self); |
|
49 |
}
|
|
50 |
||
29
by Gustav Hartvigsson
* Fixed Makefile |
51 |
_pointer dynamic_array_get (DynamicArray * self, size_t index) { |
7
by Gustav Hartvigsson
* Added licensing information to the files. |
52 |
return self->array[index]; |
53 |
}
|
|
54 |
||
55 |
size_t dynamic_array_len (DynamicArray * self) { |
|
56 |
return self->len; |
|
57 |
}
|
|
58 |
||
19
by Gustav Hatvigsson
* Started working on some tests, they do not work... yet. |
59 |
size_t dynamic_array_size (DynamicArray * self) { |
60 |
return self->max_size; |
|
61 |
}
|
|
62 |
||
29
by Gustav Hartvigsson
* Fixed Makefile |
63 |
void dynamic_array_add (DynamicArray * self, _pointer data) { |
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
64 |
if (self->len >= self->max_size - 1) { |
65 |
self->array = realloc (self->array, |
|
66 |
(sizeof (* self->array) * |
|
67 |
(self->max_size + ARRAY_PADDING))); |
|
7
by Gustav Hartvigsson
* Added licensing information to the files. |
68 |
self->max_size = self->max_size + ARRAY_PADDING; |
69 |
} |
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
70 |
self->array[self->len] = data; |
7
by Gustav Hartvigsson
* Added licensing information to the files. |
71 |
self->len++; |
72 |
}
|
|
73 |
||
29
by Gustav Hartvigsson
* Fixed Makefile |
74 |
_pointer * dynamic_array_dump_array (DynamicArray * self) { |
75 |
_pointer * ret_val = malloc (sizeof (* self->array) * (self->len + 1)); |
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
76 |
for (int i = 0; i < self->len; i++) { |
7
by Gustav Hartvigsson
* Added licensing information to the files. |
77 |
ret_val[i] = self->array[i]; |
78 |
} |
|
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
79 |
ret_val[self->len] = NULL; |
7
by Gustav Hartvigsson
* Added licensing information to the files. |
80 |
return ret_val; |
81 |
}
|
|
82 |
||
16
by Gustav Hartvigsson
* made the code compile. |
83 |
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func, |
29
by Gustav Hartvigsson
* Fixed Makefile |
84 |
_pointer data) { |
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
85 |
for (int i = 0; i < self->len; i++) { |
16
by Gustav Hartvigsson
* made the code compile. |
86 |
func (self, self->array[i], data); |
7
by Gustav Hartvigsson
* Added licensing information to the files. |
87 |
} |
88 |
}
|
|
89 |
||
90 |
||
31
by Gustav Hartvigsson
* Woops! |
91 |
void _private_for_each_item_free (DynamicArray * self, _pointer item, |
29
by Gustav Hartvigsson
* Fixed Makefile |
92 |
_pointer data) { |
26
by Gustav Hartvigsson
* Fixed the DynamicArray |
93 |
FreeFunc func = self->free_func; |
94 |
func (item); |
|
95 |
}
|
|
96 |