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.

/*
 * Hedgewars, a free turn based strategy game
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "teamlist.h"

#include "../util/util.h"
#include "../util/list.h"
#include "../util/logging.h"

#include <stdlib.h>
#include <string.h>

flib_teamlist *flib_teamlist_create() {
	return flib_calloc(1, sizeof(flib_teamlist));
}

void flib_teamlist_destroy(flib_teamlist *list) {
	if(list) {
		for(int i=0; i<list->teamCount; i++) {
			flib_team_release(list->teams[i]);
		}
		free(list->teams);
		free(list);
	}
}

GENERATE_STATIC_LIST_INSERT(insertTeam, flib_team*)
GENERATE_STATIC_LIST_DELETE(deleteTeam, flib_team*)

static int findTeam(const flib_teamlist *list, const char *name) {
	for(int i=0; i<list->teamCount; i++) {
		if(!strcmp(name, list->teams[i]->name)) {
			return i;
		}
	}
	return -1;
}

int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) {
	if(!list || !team) {
		flib_log_e("null parameter in flib_teamlist_insert");
	} else if(!insertTeam(&list->teams, &list->teamCount, team, pos)) {
		flib_team_retain(team);
		return 0;
	}
	return -1;
}

int flib_teamlist_delete(flib_teamlist *list, const char *name) {
	int result = -1;
	if(!list || !name) {
		flib_log_e("null parameter in flib_teamlist_delete");
	} else {
		int itemid = findTeam(list, name);
		if(itemid>=0) {
			flib_team *team = list->teams[itemid];
			if(!deleteTeam(&list->teams, &list->teamCount, itemid)) {
				flib_team_release(team);
				result = 0;
			}
		}
	}
	return result;
}

flib_team *flib_teamlist_find(const flib_teamlist *list, const char *name) {
	flib_team *result = NULL;
	if(!list || !name) {
		flib_log_e("null parameter in flib_teamlist_find");
	} else {
		int itemid = findTeam(list, name);
		if(itemid>=0) {
			result = list->teams[itemid];
		}
	}
	return result;
}

void flib_teamlist_clear(flib_teamlist *list) {
	if(!list) {
		flib_log_e("null parameter in flib_teamlist_clear");
	} else {
		for(int i=0; i<list->teamCount; i++) {
			flib_team_release(list->teams[i]);
		}
		free(list->teams);
		list->teams = NULL;
		list->teamCount = 0;
	}
}