|
1 #include "teamlist.h" |
|
2 |
|
3 #include "../util/util.h" |
|
4 #include "../util/list.h" |
|
5 #include "../util/logging.h" |
|
6 |
|
7 #include <stdlib.h> |
|
8 #include <string.h> |
|
9 |
|
10 flib_teamlist *flib_teamlist_create() { |
|
11 return flib_calloc(1, sizeof(flib_teamlist)); |
|
12 } |
|
13 |
|
14 void flib_teamlist_destroy(flib_teamlist *list) { |
|
15 if(list) { |
|
16 for(int i=0; i<list->teamCount; i++) { |
|
17 flib_team_release(list->teams[i]); |
|
18 } |
|
19 free(list->teams); |
|
20 free(list); |
|
21 } |
|
22 } |
|
23 |
|
24 GENERATE_STATIC_LIST_INSERT(insertTeam, flib_team*) |
|
25 GENERATE_STATIC_LIST_DELETE(deleteTeam, flib_team*) |
|
26 |
|
27 static int findTeam(const flib_teamlist *list, const char *name) { |
|
28 for(int i=0; i<list->teamCount; i++) { |
|
29 if(!strcmp(name, list->teams[i]->name)) { |
|
30 return i; |
|
31 } |
|
32 } |
|
33 return -1; |
|
34 } |
|
35 |
|
36 int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) { |
|
37 if(!list || !team) { |
|
38 flib_log_e("null parameter in flib_teamlist_insert"); |
|
39 } else if(!insertTeam(&list->teams, &list->teamCount, team, pos)) { |
|
40 flib_team_retain(team); |
|
41 return 0; |
|
42 } |
|
43 return -1; |
|
44 } |
|
45 |
|
46 int flib_teamlist_delete(flib_teamlist *list, const char *name) { |
|
47 int result = -1; |
|
48 if(!list || !name) { |
|
49 flib_log_e("null parameter in flib_teamlist_delete"); |
|
50 } else { |
|
51 int itemid = findTeam(list, name); |
|
52 if(itemid>=0) { |
|
53 flib_team *team = list->teams[itemid]; |
|
54 if(!deleteTeam(&list->teams, &list->teamCount, itemid)) { |
|
55 flib_team_release(team); |
|
56 result = 0; |
|
57 } |
|
58 } |
|
59 } |
|
60 return result; |
|
61 } |
|
62 |
|
63 flib_team *flib_teamlist_find(const flib_teamlist *list, const char *name) { |
|
64 flib_team *result = NULL; |
|
65 if(!list || !name) { |
|
66 flib_log_e("null parameter in flib_teamlist_find"); |
|
67 } else { |
|
68 int itemid = findTeam(list, name); |
|
69 if(itemid>=0) { |
|
70 result = list->teams[itemid]; |
|
71 } |
|
72 } |
|
73 return result; |
|
74 } |
|
75 |
|
76 void flib_teamlist_clear(flib_teamlist *list) { |
|
77 if(!list) { |
|
78 flib_log_e("null parameter in flib_teamlist_clear"); |
|
79 } else { |
|
80 for(int i=0; i<list->teamCount; i++) { |
|
81 flib_team_release(list->teams[i]); |
|
82 } |
|
83 free(list->teams); |
|
84 list->teams = NULL; |
|
85 list->teamCount = 0; |
|
86 } |
|
87 } |