project_files/frontlib/model/schemelist.c
author sheepluva
Sat, 29 Jul 2017 00:25:38 +0200
changeset 12443 350a4871b2c1
parent 10017 de822cd3df3a
permissions -rw-r--r--
cleanup/fix .hgignore
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 "schemelist.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 <stdio.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    28
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    29
#include <limits.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    30
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    31
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    32
static char *makePrefixedName(int schemeIndex, const char *settingName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    33
    return flib_asprintf("%i\\%s", schemeIndex, settingName);
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
static int readSettingsFromIni(flib_ini *ini, flib_scheme *scheme, int index) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    37
    bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    38
    for(int i=0; i<flib_meta.settingCount && !error; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    39
        char *key = makePrefixedName(index, flib_meta.settings[i].name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    40
        if(!key) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    41
            error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    42
        } else if(flib_ini_get_int_opt(ini, &scheme->settings[i], key, flib_meta.settings[i].def)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    43
            flib_log_e("Error reading setting %s in schemes file.", key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    44
            error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    45
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    46
        free(key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    47
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    48
    return error;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    49
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    50
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    51
static int readModsFromIni(flib_ini *ini, flib_scheme *scheme, int index) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    52
    bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    53
    for(int i=0; i<flib_meta.modCount && !error; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    54
        char *key = makePrefixedName(index, flib_meta.mods[i].name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    55
        if(!key) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    56
            error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    57
        } else if(flib_ini_get_bool_opt(ini, &scheme->mods[i], key, false)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    58
            flib_log_e("Error reading mod %s in schemes file.", key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    59
            error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    60
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    61
        free(key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    62
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    63
    return error;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    64
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    65
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    66
static flib_scheme *readSchemeFromIni(flib_ini *ini, int index) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    67
    flib_scheme *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    68
    char *schemeNameKey = makePrefixedName(index+1, "name");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    69
    if(schemeNameKey) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    70
        char *schemeName = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    71
        if(!flib_ini_get_str_opt(ini, &schemeName, schemeNameKey, "Unnamed")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    72
            flib_scheme *tmpScheme = flib_scheme_create(schemeName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    73
            if(tmpScheme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    74
                if(!readSettingsFromIni(ini, tmpScheme, index) && !readModsFromIni(ini, tmpScheme, index)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    75
                    result = tmpScheme;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    76
                    tmpScheme = NULL;
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_scheme_destroy(tmpScheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    80
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    81
        free(schemeName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    82
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    83
    free(schemeNameKey);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    84
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    85
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    86
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    87
static flib_schemelist *fromIniHandleError(flib_schemelist *result, flib_ini *ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    88
    flib_ini_destroy(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    89
    flib_schemelist_destroy(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    90
    return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    91
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    92
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    93
flib_schemelist *flib_schemelist_from_ini(const char *filename) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    94
    if(log_badargs_if(filename==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    95
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    96
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    97
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    98
    flib_schemelist *list = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    99
    flib_ini *ini = flib_ini_load(filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   100
    if(!ini || flib_ini_enter_section(ini, "schemes")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   101
        flib_log_e("Missing file or missing section \"schemes\" in file %s.", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   102
        return fromIniHandleError(list, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   103
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   104
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   105
    list = flib_schemelist_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   106
    if(!list) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   107
        return fromIniHandleError(list, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   108
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   109
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   110
    int schemeCount = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   111
    if(flib_ini_get_int(ini, &schemeCount, "size")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   112
        flib_log_e("Missing or malformed scheme count in config file %s.", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   113
        return fromIniHandleError(list, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   114
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   115
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   116
    for(int i=0; i<schemeCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   117
        flib_scheme *scheme = readSchemeFromIni(ini, i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   118
        if(!scheme || flib_schemelist_insert(list, scheme, i)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   119
            flib_scheme_destroy(scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   120
            flib_log_e("Error reading scheme %i from config file %s.", i, filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   121
            return fromIniHandleError(list, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   122
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   123
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   124
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   125
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   126
    flib_ini_destroy(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   127
    return list;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   128
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   129
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   130
static int writeSchemeToIni(const flib_scheme *scheme, flib_ini *ini, int index) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   131
    bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   132
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   133
    char *key = makePrefixedName(index+1, "name");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   134
    error |= !key || flib_ini_set_str(ini, key, scheme->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   135
    free(key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   136
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   137
    for(int i=0; i<flib_meta.modCount && !error; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   138
        char *key = makePrefixedName(index+1, flib_meta.mods[i].name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   139
        error |= !key || flib_ini_set_bool(ini, key, scheme->mods[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   140
        free(key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   141
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   142
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   143
    for(int i=0; i<flib_meta.settingCount && !error; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   144
        char *key = makePrefixedName(index+1, flib_meta.settings[i].name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   145
        error |= !key || flib_ini_set_int(ini, key, scheme->settings[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   146
        free(key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   147
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   148
    return error;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   149
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   150
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   151
int flib_schemelist_to_ini(const char *filename, const flib_schemelist *schemes) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   152
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   153
    if(!log_badargs_if2(filename==NULL, schemes==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   154
        flib_ini *ini = flib_ini_create(NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   155
        if(ini && !flib_ini_create_section(ini, "schemes")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   156
            bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   157
            error |= flib_ini_set_int(ini, "size", schemes->schemeCount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   158
            for(int i=0; i<schemes->schemeCount && !error; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   159
                error |= writeSchemeToIni(schemes->schemes[i], ini, i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   160
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   161
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   162
            if(!error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   163
                result = flib_ini_save(ini, filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   164
            }
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
flib_schemelist *flib_schemelist_create() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   172
    return flib_calloc(1, sizeof(flib_schemelist));
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
void flib_schemelist_destroy(flib_schemelist *list) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   176
    if(list) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   177
        for(int i=0; i<list->schemeCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   178
            flib_scheme_destroy(list->schemes[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   179
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   180
        free(list->schemes);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   181
        free(list);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   182
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   183
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   184
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   185
flib_scheme *flib_schemelist_find(flib_schemelist *list, const char *name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   186
    if(!log_badargs_if2(list==NULL, name==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   187
        for(int i=0; i<list->schemeCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   188
            if(!strcmp(name, list->schemes[i]->name)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   189
                return list->schemes[i];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   190
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   191
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   192
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   193
    return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   194
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   195
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   196
GENERATE_STATIC_LIST_INSERT(insertScheme, flib_scheme*)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   197
GENERATE_STATIC_LIST_DELETE(deleteScheme, flib_scheme*)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   198
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   199
int flib_schemelist_insert(flib_schemelist *list, flib_scheme *cfg, int pos) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   200
    if(!log_badargs_if2(list==NULL, cfg==NULL)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   201
            && !insertScheme(&list->schemes, &list->schemeCount, cfg, pos)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   202
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   203
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   204
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   205
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   206
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   207
int flib_schemelist_delete(flib_schemelist *list, int pos) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   208
    if(!log_badargs_if(list==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   209
        flib_scheme *elem = list->schemes[pos];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   210
        if(!deleteScheme(&list->schemes, &list->schemeCount, pos)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   211
            flib_scheme_destroy(elem);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   212
            return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   213
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   214
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   215
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   216
}