25 |
25 |
26 #include <stdlib.h> |
26 #include <stdlib.h> |
27 #include <string.h> |
27 #include <string.h> |
28 |
28 |
29 flib_teamlist *flib_teamlist_create() { |
29 flib_teamlist *flib_teamlist_create() { |
30 return flib_calloc(1, sizeof(flib_teamlist)); |
30 return flib_calloc(1, sizeof(flib_teamlist)); |
31 } |
31 } |
32 |
32 |
33 void flib_teamlist_destroy(flib_teamlist *list) { |
33 void flib_teamlist_destroy(flib_teamlist *list) { |
34 if(list) { |
34 if(list) { |
35 for(int i=0; i<list->teamCount; i++) { |
35 for(int i=0; i<list->teamCount; i++) { |
36 flib_team_destroy(list->teams[i]); |
36 flib_team_destroy(list->teams[i]); |
37 } |
37 } |
38 free(list->teams); |
38 free(list->teams); |
39 free(list); |
39 free(list); |
40 } |
40 } |
41 } |
41 } |
42 |
42 |
43 GENERATE_STATIC_LIST_INSERT(insertTeam, flib_team*) |
43 GENERATE_STATIC_LIST_INSERT(insertTeam, flib_team*) |
44 GENERATE_STATIC_LIST_DELETE(deleteTeam, flib_team*) |
44 GENERATE_STATIC_LIST_DELETE(deleteTeam, flib_team*) |
45 |
45 |
46 static int findTeam(const flib_teamlist *list, const char *name) { |
46 static int findTeam(const flib_teamlist *list, const char *name) { |
47 for(int i=0; i<list->teamCount; i++) { |
47 for(int i=0; i<list->teamCount; i++) { |
48 if(!strcmp(name, list->teams[i]->name)) { |
48 if(!strcmp(name, list->teams[i]->name)) { |
49 return i; |
49 return i; |
50 } |
50 } |
51 } |
51 } |
52 return -1; |
52 return -1; |
53 } |
53 } |
54 |
54 |
55 int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) { |
55 int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) { |
56 if(!log_badargs_if2(list==NULL, team==NULL) |
56 if(!log_badargs_if2(list==NULL, team==NULL) |
57 && !insertTeam(&list->teams, &list->teamCount, team, pos)) { |
57 && !insertTeam(&list->teams, &list->teamCount, team, pos)) { |
58 return 0; |
58 return 0; |
59 } |
59 } |
60 return -1; |
60 return -1; |
61 } |
61 } |
62 |
62 |
63 int flib_teamlist_delete(flib_teamlist *list, const char *name) { |
63 int flib_teamlist_delete(flib_teamlist *list, const char *name) { |
64 int result = -1; |
64 int result = -1; |
65 if(!log_badargs_if2(list==NULL, name==NULL)) { |
65 if(!log_badargs_if2(list==NULL, name==NULL)) { |
66 int itemid = findTeam(list, name); |
66 int itemid = findTeam(list, name); |
67 if(itemid>=0) { |
67 if(itemid>=0) { |
68 flib_team *team = list->teams[itemid]; |
68 flib_team *team = list->teams[itemid]; |
69 if(!deleteTeam(&list->teams, &list->teamCount, itemid)) { |
69 if(!deleteTeam(&list->teams, &list->teamCount, itemid)) { |
70 flib_team_destroy(team); |
70 flib_team_destroy(team); |
71 result = 0; |
71 result = 0; |
72 } |
72 } |
73 } |
73 } |
74 } |
74 } |
75 return result; |
75 return result; |
76 } |
76 } |
77 |
77 |
78 flib_team *flib_teamlist_find(const flib_teamlist *list, const char *name) { |
78 flib_team *flib_teamlist_find(const flib_teamlist *list, const char *name) { |
79 flib_team *result = NULL; |
79 flib_team *result = NULL; |
80 if(!log_badargs_if2(list==NULL, name==NULL)) { |
80 if(!log_badargs_if2(list==NULL, name==NULL)) { |
81 int itemid = findTeam(list, name); |
81 int itemid = findTeam(list, name); |
82 if(itemid>=0) { |
82 if(itemid>=0) { |
83 result = list->teams[itemid]; |
83 result = list->teams[itemid]; |
84 } |
84 } |
85 } |
85 } |
86 return result; |
86 return result; |
87 } |
87 } |
88 |
88 |
89 void flib_teamlist_clear(flib_teamlist *list) { |
89 void flib_teamlist_clear(flib_teamlist *list) { |
90 if(!log_badargs_if(list==NULL)) { |
90 if(!log_badargs_if(list==NULL)) { |
91 for(int i=0; i<list->teamCount; i++) { |
91 for(int i=0; i<list->teamCount; i++) { |
92 flib_team_destroy(list->teams[i]); |
92 flib_team_destroy(list->teams[i]); |
93 } |
93 } |
94 free(list->teams); |
94 free(list->teams); |
95 list->teams = NULL; |
95 list->teams = NULL; |
96 list->teamCount = 0; |
96 list->teamCount = 0; |
97 } |
97 } |
98 } |
98 } |
99 |
99 |
100 flib_teamlist *flib_teamlist_copy(flib_teamlist *list) { |
100 flib_teamlist *flib_teamlist_copy(flib_teamlist *list) { |
101 if(!list) { |
101 if(!list) { |
102 return NULL; |
102 return NULL; |
103 } |
103 } |
104 flib_teamlist *result = flib_teamlist_create(); |
104 flib_teamlist *result = flib_teamlist_create(); |
105 if(result) { |
105 if(result) { |
106 bool error = false; |
106 bool error = false; |
107 for(int i=0; !error && i<list->teamCount; i++) { |
107 for(int i=0; !error && i<list->teamCount; i++) { |
108 flib_team *teamcopy = flib_team_copy(list->teams[i]); |
108 flib_team *teamcopy = flib_team_copy(list->teams[i]); |
109 if(!teamcopy || flib_teamlist_insert(result, teamcopy, i)) { |
109 if(!teamcopy || flib_teamlist_insert(result, teamcopy, i)) { |
110 flib_team_destroy(teamcopy); |
110 flib_team_destroy(teamcopy); |
111 error = true; |
111 error = true; |
112 } |
112 } |
113 } |
113 } |
114 if(error) { |
114 if(error) { |
115 flib_teamlist_destroy(result); |
115 flib_teamlist_destroy(result); |
116 result = NULL; |
116 result = NULL; |
117 } |
117 } |
118 } |
118 } |
119 return result; |
119 return result; |
120 } |
120 } |