project_files/frontlib/model/weapon.c
author alfadur
Sat, 01 Aug 2020 04:19:29 +0300
changeset 15735 caf41f1a410d
parent 10017 de822cd3df3a
permissions -rw-r--r--
add teapot grave
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    20
#include "weapon.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    21
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    22
#include "../util/inihelper.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    23
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    24
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    25
#include "../util/list.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    26
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    27
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    28
#include <ctype.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    29
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    30
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    31
static void setField(char field[WEAPONS_COUNT+1], const char *line, int lineLen, bool no9) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    32
    if(lineLen>WEAPONS_COUNT) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    33
        lineLen = WEAPONS_COUNT;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    34
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    35
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    36
    char min = '0';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    37
    char max = no9 ? '8' : '9';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    38
    for(int i=0; i<lineLen; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    39
        if(line[i] >= min && line[i] <= max) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    40
            field[i] = line[i];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    41
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    42
            flib_log_w("Invalid character in weapon config string \"%.*s\", position %i", lineLen, line, i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    43
            field[i] = '0';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    44
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    45
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    46
    for(int i=lineLen; i<WEAPONS_COUNT; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    47
        field[i] = '0';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    48
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    49
    field[WEAPONS_COUNT] = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    50
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    51
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    52
flib_weaponset *flib_weaponset_create(const char *name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    53
    flib_weaponset *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    54
    if(!log_badargs_if(name==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    55
        flib_weaponset *newSet = flib_calloc(1, sizeof(flib_weaponset));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    56
        if(newSet) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    57
            newSet->name = flib_strdupnull(name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    58
            if(newSet->name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    59
                setField(newSet->loadout, "", 0, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    60
                setField(newSet->crateprob, "", 0, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    61
                setField(newSet->crateammo, "", 0, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    62
                setField(newSet->delay, "", 0, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    63
                result = newSet;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    64
                newSet = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    65
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    66
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    67
        flib_weaponset_destroy(newSet);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    68
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    69
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    70
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    71
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    72
void flib_weaponset_destroy(flib_weaponset *cfg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    73
    if(cfg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    74
        free(cfg->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    75
        free(cfg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    76
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    77
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    78
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    79
flib_weaponset *flib_weaponset_copy(const flib_weaponset *weaponset) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    80
    if(!weaponset) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    81
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    82
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    83
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    84
    flib_weaponset *result = flib_weaponset_create(weaponset->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    85
    if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    86
        memcpy(result->loadout, weaponset->loadout, WEAPONS_COUNT+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    87
        memcpy(result->crateprob, weaponset->crateprob, WEAPONS_COUNT+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    88
        memcpy(result->delay, weaponset->delay, WEAPONS_COUNT+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    89
        memcpy(result->crateammo, weaponset->crateammo, WEAPONS_COUNT+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    90
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    91
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    92
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    93
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    94
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    95
void flib_weaponsetlist_destroy(flib_weaponsetlist *list) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    96
    if(list) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    97
        for(int i=0; i<list->weaponsetCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    98
            flib_weaponset_destroy(list->weaponsets[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    99
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   100
        free(list->weaponsets);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   101
        free(list);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   102
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   103
}
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   104
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   105
flib_weaponset *flib_weaponset_from_ammostring(const char *name, const char *ammostring) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   106
    flib_weaponset *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   107
    if(!log_badargs_if2(name==NULL, ammostring==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   108
        result = flib_weaponset_create(name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   109
        if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   110
            int fieldlen = strlen(ammostring)/4;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   111
            setField(result->loadout, ammostring, fieldlen, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   112
            setField(result->crateprob, ammostring + fieldlen, fieldlen, true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   113
            setField(result->delay, ammostring + 2*fieldlen, fieldlen, true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   114
            setField(result->crateammo, ammostring + 3*fieldlen, fieldlen, true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   115
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   116
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   117
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   118
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   119
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   120
static int fillWeaponsetFromIni(flib_weaponsetlist *list, flib_ini *ini, int index) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   121
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   122
    char *keyname = flib_ini_get_keyname(ini, index);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   123
    char *decodedKeyname = flib_urldecode(keyname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   124
    char *ammostring = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   125
    if(decodedKeyname && !flib_ini_get_str(ini, &ammostring, keyname)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   126
        flib_weaponset *set = flib_weaponset_from_ammostring(decodedKeyname, ammostring);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   127
        if(set) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   128
            result = flib_weaponsetlist_insert(list, set, list->weaponsetCount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   129
            if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   130
                flib_weaponset_destroy(set);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   131
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   132
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   133
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   134
    free(ammostring);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   135
    free(decodedKeyname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   136
    free(keyname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   137
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   138
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   139
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   140
static int fillWeaponsetsFromIni(flib_weaponsetlist *list, flib_ini *ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   141
    bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   142
    int weaponsets = flib_ini_get_keycount(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   143
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   144
    for(int i=0; i<weaponsets && !error; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   145
        error |= fillWeaponsetFromIni(list, ini, i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   146
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   147
    return error;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   148
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   149
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   150
flib_weaponsetlist *flib_weaponsetlist_from_ini(const char *filename) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   151
    flib_weaponsetlist *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   152
    if(!log_badargs_if(filename==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   153
        flib_ini *ini = flib_ini_load(filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   154
        if(!ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   155
            flib_log_e("Missing file %s.", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   156
        } else if(flib_ini_enter_section(ini, "General")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   157
            flib_log_e("Missing section \"General\" in file %s.", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   158
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   159
            flib_weaponsetlist *tmpList = flib_weaponsetlist_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   160
            if(tmpList && !fillWeaponsetsFromIni(tmpList, ini)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   161
                result = tmpList;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   162
                tmpList = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   163
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   164
            flib_weaponsetlist_destroy(tmpList);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   165
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   166
        flib_ini_destroy(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   167
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   168
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   169
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   170
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   171
static bool needsEscape(char c) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   172
    return !((c>='0' && c<='9') || (c>='a' && c <='z'));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   173
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   174
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   175
static int writeWeaponsetToIni(flib_ini *ini, flib_weaponset *set) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   176
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   177
    char weaponstring[WEAPONS_COUNT*4+1];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   178
    strcpy(weaponstring, set->loadout);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   179
    strcat(weaponstring, set->crateprob);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   180
    strcat(weaponstring, set->delay);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   181
    strcat(weaponstring, set->crateammo);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   182
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   183
    char *escapedname = flib_urlencode_pred(set->name, needsEscape);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   184
    if(escapedname) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   185
        result = flib_ini_set_str(ini, escapedname, weaponstring);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   186
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   187
    free(escapedname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   188
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   189
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   190
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   191
int flib_weaponsetlist_to_ini(const char *filename, const flib_weaponsetlist *list) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   192
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   193
    if(!log_badargs_if2(filename==NULL, list==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   194
        flib_ini *ini = flib_ini_create(NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   195
        if(ini && !flib_ini_create_section(ini, "General")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   196
            bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   197
            for(int i=0; i<list->weaponsetCount && !error; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   198
                error |= writeWeaponsetToIni(ini, list->weaponsets[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   199
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   200
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   201
            if(!error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   202
                result = flib_ini_save(ini, filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   203
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   204
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   205
        flib_ini_destroy(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   206
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   207
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   208
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   209
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   210
flib_weaponsetlist *flib_weaponsetlist_create() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   211
    return flib_calloc(1, sizeof(flib_weaponsetlist));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   212
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   213
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   214
GENERATE_STATIC_LIST_INSERT(insertWeaponset, flib_weaponset*)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   215
GENERATE_STATIC_LIST_DELETE(deleteWeaponset, flib_weaponset*)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   216
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   217
int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *set, int pos) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   218
    if(!log_badargs_if2(list==NULL, set==NULL)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   219
            && !insertWeaponset(&list->weaponsets, &list->weaponsetCount, set, pos)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   220
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   221
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   222
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   223
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   224
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   225
int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   226
    if(!log_badargs_if(list==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   227
        flib_weaponset *elem = list->weaponsets[pos];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   228
        if(!deleteWeaponset(&list->weaponsets, &list->weaponsetCount, pos)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   229
            flib_weaponset_destroy(elem);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   230
            return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   231
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   232
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   233
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   234
}