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