project_files/frontlib/model/scheme.c
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7497 7e1d72fc03c7
child 10017 de822cd3df3a
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License
       
     7  * as published by the Free Software Foundation; either version 2
       
     8  * of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18  */
       
    19 
       
    20 #include "scheme.h"
       
    21 
       
    22 #include "../util/inihelper.h"
       
    23 #include "../util/logging.h"
       
    24 #include "../util/util.h"
       
    25 
       
    26 #include <stdio.h>
       
    27 #include <stdlib.h>
       
    28 #include <limits.h>
       
    29 #include <string.h>
       
    30 
       
    31 flib_scheme *flib_scheme_create(const char *schemeName) {
       
    32 	flib_scheme *result = flib_calloc(1, sizeof(flib_scheme));
       
    33 	if(log_badargs_if(schemeName==NULL) || result==NULL) {
       
    34 		return NULL;
       
    35 	}
       
    36 
       
    37 	result->name = flib_strdupnull(schemeName);
       
    38 	result->mods = flib_calloc(flib_meta.modCount, sizeof(*result->mods));
       
    39 	result->settings = flib_calloc(flib_meta.settingCount, sizeof(*result->settings));
       
    40 
       
    41 	if(!result->mods || !result->settings || !result->name) {
       
    42 		flib_scheme_destroy(result);
       
    43 		return NULL;
       
    44 	}
       
    45 
       
    46 	for(int i=0; i<flib_meta.settingCount; i++) {
       
    47 		result->settings[i] = flib_meta.settings[i].def;
       
    48 	}
       
    49 	return result;
       
    50 }
       
    51 
       
    52 flib_scheme *flib_scheme_copy(const flib_scheme *scheme) {
       
    53 	flib_scheme *result = NULL;
       
    54 	if(scheme) {
       
    55 		result = flib_scheme_create(scheme->name);
       
    56 		if(result) {
       
    57 			memcpy(result->mods, scheme->mods, flib_meta.modCount * sizeof(*scheme->mods));
       
    58 			memcpy(result->settings, scheme->settings, flib_meta.settingCount * sizeof(*scheme->settings));
       
    59 		}
       
    60 	}
       
    61 	return result;
       
    62 }
       
    63 
       
    64 void flib_scheme_destroy(flib_scheme* scheme) {
       
    65 	if(scheme) {
       
    66 		free(scheme->mods);
       
    67 		free(scheme->settings);
       
    68 		free(scheme->name);
       
    69 		free(scheme);
       
    70 	}
       
    71 }
       
    72 
       
    73 bool flib_scheme_get_mod(const flib_scheme *scheme, const char *name) {
       
    74 	if(!log_badargs_if2(scheme==NULL, name==NULL)) {
       
    75 		for(int i=0; i<flib_meta.modCount; i++) {
       
    76 			if(!strcmp(flib_meta.mods[i].name, name)) {
       
    77 				return scheme->mods[i];
       
    78 			}
       
    79 		}
       
    80 		flib_log_e("Unable to find game mod %s", name);
       
    81 	}
       
    82 	return false;
       
    83 }
       
    84 
       
    85 int flib_scheme_get_setting(const flib_scheme *scheme, const char *name, int def) {
       
    86 	if(!log_badargs_if2(scheme==NULL, name==NULL)) {
       
    87 		for(int i=0; i<flib_meta.settingCount; i++) {
       
    88 			if(!strcmp(flib_meta.settings[i].name, name)) {
       
    89 				return scheme->settings[i];
       
    90 			}
       
    91 		}
       
    92 		flib_log_e("Unable to find game setting %s", name);
       
    93 	}
       
    94 	return def;
       
    95 }