project_files/frontlib/model/teamlist.c
author Medo <smaxein@googlemail.com>
Wed, 27 Jun 2012 22:52:19 +0200
changeset 7314 6171f0bad318
parent 7275 15f722e0b96f
child 7316 f7b49b2c5d84
permissions -rw-r--r--
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib files that did not have their license fixed already.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7314
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     1
/*
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     2
 * Hedgewars, a free turn based strategy game
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     4
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     5
 * This program is free software; you can redistribute it and/or
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     6
 * modify it under the terms of the GNU General Public License
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     8
 * of the License, or (at your option) any later version.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     9
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    13
 * GNU General Public License for more details.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    14
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    15
 * You should have received a copy of the GNU General Public License
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    16
 * along with this program; if not, write to the Free Software
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    18
 */
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    19
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
#include "teamlist.h"
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
#include "../util/util.h"
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
#include "../util/list.h"
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
#include "../util/logging.h"
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
#include <stdlib.h>
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
#include <string.h>
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
flib_teamlist *flib_teamlist_create() {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
	return flib_calloc(1, sizeof(flib_teamlist));
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
void flib_teamlist_destroy(flib_teamlist *list) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	if(list) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
		for(int i=0; i<list->teamCount; i++) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
			flib_team_release(list->teams[i]);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
		}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
		free(list->teams);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		free(list);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
GENERATE_STATIC_LIST_INSERT(insertTeam, flib_team*)
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
GENERATE_STATIC_LIST_DELETE(deleteTeam, flib_team*)
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
static int findTeam(const flib_teamlist *list, const char *name) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
	for(int i=0; i<list->teamCount; i++) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
		if(!strcmp(name, list->teams[i]->name)) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
			return i;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
	return -1;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
	if(!list || !team) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
		flib_log_e("null parameter in flib_teamlist_insert");
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	} else if(!insertTeam(&list->teams, &list->teamCount, team, pos)) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
		flib_team_retain(team);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
		return 0;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
	return -1;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
int flib_teamlist_delete(flib_teamlist *list, const char *name) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
	int result = -1;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	if(!list || !name) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
		flib_log_e("null parameter in flib_teamlist_delete");
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
	} else {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
		int itemid = findTeam(list, name);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
		if(itemid>=0) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
			flib_team *team = list->teams[itemid];
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
			if(!deleteTeam(&list->teams, &list->teamCount, itemid)) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
				flib_team_release(team);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
				result = 0;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
			}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
		}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
	return result;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
flib_team *flib_teamlist_find(const flib_teamlist *list, const char *name) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
	flib_team *result = NULL;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
	if(!list || !name) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
		flib_log_e("null parameter in flib_teamlist_find");
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
	} else {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
		int itemid = findTeam(list, name);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
		if(itemid>=0) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
			result = list->teams[itemid];
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
		}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
	return result;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
void flib_teamlist_clear(flib_teamlist *list) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
	if(!list) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
		flib_log_e("null parameter in flib_teamlist_clear");
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
	} else {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
		for(int i=0; i<list->teamCount; i++) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
			flib_team_release(list->teams[i]);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
		}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
		free(list->teams);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
		list->teams = NULL;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
		list->teamCount = 0;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
}