10017
|
1 |
/*
|
|
2 |
* Hedgewars, a free turn based strategy game
|
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
#include "teamlist.h"
|
|
21 |
|
|
22 |
#include "../util/util.h"
|
|
23 |
#include "../util/list.h"
|
|
24 |
#include "../util/logging.h"
|
|
25 |
|
|
26 |
#include <stdlib.h>
|
|
27 |
#include <string.h>
|
|
28 |
|
|
29 |
flib_teamlist *flib_teamlist_create() {
|
|
30 |
return flib_calloc(1, sizeof(flib_teamlist));
|
|
31 |
}
|
|
32 |
|
|
33 |
void flib_teamlist_destroy(flib_teamlist *list) {
|
|
34 |
if(list) {
|
|
35 |
for(int i=0; i<list->teamCount; i++) {
|
|
36 |
flib_team_destroy(list->teams[i]);
|
|
37 |
}
|
|
38 |
free(list->teams);
|
|
39 |
free(list);
|
|
40 |
}
|
|
41 |
}
|
|
42 |
|
|
43 |
GENERATE_STATIC_LIST_INSERT(insertTeam, flib_team*)
|
|
44 |
GENERATE_STATIC_LIST_DELETE(deleteTeam, flib_team*)
|
|
45 |
|
|
46 |
static int findTeam(const flib_teamlist *list, const char *name) {
|
|
47 |
for(int i=0; i<list->teamCount; i++) {
|
|
48 |
if(!strcmp(name, list->teams[i]->name)) {
|
|
49 |
return i;
|
|
50 |
}
|
|
51 |
}
|
|
52 |
return -1;
|
|
53 |
}
|
|
54 |
|
|
55 |
int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) {
|
|
56 |
if(!log_badargs_if2(list==NULL, team==NULL)
|
|
57 |
&& !insertTeam(&list->teams, &list->teamCount, team, pos)) {
|
|
58 |
return 0;
|
|
59 |
}
|
|
60 |
return -1;
|
|
61 |
}
|
|
62 |
|
|
63 |
int flib_teamlist_delete(flib_teamlist *list, const char *name) {
|
|
64 |
int result = -1;
|
|
65 |
if(!log_badargs_if2(list==NULL, name==NULL)) {
|
|
66 |
int itemid = findTeam(list, name);
|
|
67 |
if(itemid>=0) {
|
|
68 |
flib_team *team = list->teams[itemid];
|
|
69 |
if(!deleteTeam(&list->teams, &list->teamCount, itemid)) {
|
|
70 |
flib_team_destroy(team);
|
|
71 |
result = 0;
|
|
72 |
}
|
|
73 |
}
|
|
74 |
}
|
|
75 |
return result;
|
|
76 |
}
|
|
77 |
|
|
78 |
flib_team *flib_teamlist_find(const flib_teamlist *list, const char *name) {
|
|
79 |
flib_team *result = NULL;
|
|
80 |
if(!log_badargs_if2(list==NULL, name==NULL)) {
|
|
81 |
int itemid = findTeam(list, name);
|
|
82 |
if(itemid>=0) {
|
|
83 |
result = list->teams[itemid];
|
|
84 |
}
|
|
85 |
}
|
|
86 |
return result;
|
|
87 |
}
|
|
88 |
|
|
89 |
void flib_teamlist_clear(flib_teamlist *list) {
|
|
90 |
if(!log_badargs_if(list==NULL)) {
|
|
91 |
for(int i=0; i<list->teamCount; i++) {
|
|
92 |
flib_team_destroy(list->teams[i]);
|
|
93 |
}
|
|
94 |
free(list->teams);
|
|
95 |
list->teams = NULL;
|
|
96 |
list->teamCount = 0;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
flib_teamlist *flib_teamlist_copy(flib_teamlist *list) {
|
|
101 |
if(!list) {
|
|
102 |
return NULL;
|
|
103 |
}
|
|
104 |
flib_teamlist *result = flib_teamlist_create();
|
|
105 |
if(result) {
|
|
106 |
bool error = false;
|
|
107 |
for(int i=0; !error && i<list->teamCount; i++) {
|
|
108 |
flib_team *teamcopy = flib_team_copy(list->teams[i]);
|
|
109 |
if(!teamcopy || flib_teamlist_insert(result, teamcopy, i)) {
|
|
110 |
flib_team_destroy(teamcopy);
|
|
111 |
error = true;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
if(error) {
|
|
115 |
flib_teamlist_destroy(result);
|
|
116 |
result = NULL;
|
|
117 |
}
|
|
118 |
}
|
|
119 |
return result;
|
|
120 |
}
|