Create config_file_userdata.c

This commit is contained in:
twinaphex 2014-09-15 21:25:40 +02:00
parent 49f9f4b5fd
commit 07d86c7ac3
5 changed files with 178 additions and 115 deletions

View File

@ -114,6 +114,7 @@ OBJ += frontend/frontend.o \
cheats.o \
core_info.o \
conf/config_file.o \
conf/config_file_userdata.o \
screenshot.o \
gfx/scaler/scaler.o \
gfx/shader_common.o \

View File

@ -18,7 +18,7 @@
#include "dsp_filter.h"
#include "../dynamic.h"
#include "../conf/config_file.h"
#include "../conf/config_file_userdata.h"
#include "filters/dspfilter.h"
#include "../file_path.h"
#include "../file_ext.h"
@ -64,120 +64,13 @@ static const struct dspfilter_implementation *find_implementation(
return NULL;
}
struct dsp_userdata
{
config_file_t *conf;
const char *prefix[2];
};
static int get_float(void *userdata, const char *key_str,
float *value, float default_value)
{
struct dsp_userdata *dsp = (struct dsp_userdata*)userdata;
char key[2][256];
snprintf(key[0], sizeof(key[0]), "%s_%s", dsp->prefix[0], key_str);
snprintf(key[1], sizeof(key[1]), "%s_%s", dsp->prefix[1], key_str);
bool got = config_get_float(dsp->conf, key[0], value);
got = got || config_get_float(dsp->conf, key[1], value);
if (!got)
*value = default_value;
return got;
}
static int get_int(void *userdata, const char *key_str,
int *value, int default_value)
{
struct dsp_userdata *dsp = (struct dsp_userdata*)userdata;
char key[2][256];
snprintf(key[0], sizeof(key[0]), "%s_%s", dsp->prefix[0], key_str);
snprintf(key[1], sizeof(key[1]), "%s_%s", dsp->prefix[1], key_str);
bool got = config_get_int(dsp->conf, key[0], value);
got = got || config_get_int(dsp->conf, key[1], value);
if (!got)
*value = default_value;
return got;
}
#define get_array_setup() \
struct dsp_userdata *dsp = (struct dsp_userdata*)userdata; \
\
char key[2][256]; \
snprintf(key[0], sizeof(key[0]), "%s_%s", dsp->prefix[0], key_str); \
snprintf(key[1], sizeof(key[1]), "%s_%s", dsp->prefix[1], key_str); \
\
char *str = NULL; \
bool got = config_get_string(dsp->conf, key[0], &str); \
got = got || config_get_string(dsp->conf, key[1], &str);
#define get_array_body(T) \
if (got) \
{ \
unsigned i; \
struct string_list *list = string_split(str, " "); \
*values = (T*)calloc(list->size, sizeof(T)); \
for (i = 0; i < list->size; i++) \
(*values)[i] = (T)strtod(list->elems[i].data, NULL); \
*out_num_values = list->size; \
string_list_free(list); \
return true; \
} \
else \
{ \
*values = (T*)calloc(num_default_values, sizeof(T)); \
memcpy(*values, default_values, sizeof(T) * num_default_values); \
*out_num_values = num_default_values; \
return false; \
}
static int get_float_array(void *userdata, const char *key_str,
float **values, unsigned *out_num_values,
const float *default_values, unsigned num_default_values)
{
get_array_setup()
get_array_body(float)
}
static int get_int_array(void *userdata, const char *key_str,
int **values, unsigned *out_num_values,
const int *default_values, unsigned num_default_values)
{
get_array_setup()
get_array_body(int)
}
static int get_string(void *userdata, const char *key_str,
char **output, const char *default_output)
{
get_array_setup()
if (got)
{
*output = str;
return true;
}
*output = strdup(default_output);
return false;
}
static void dspfilter_free(void *ptr)
{
free(ptr);
}
static const struct dspfilter_config dspfilter_config = {
get_float,
get_int,
get_float_array,
get_int_array,
get_string,
dspfilter_free,
config_userdata_get_float,
config_userdata_get_int,
config_userdata_get_float_array,
config_userdata_get_int_array,
config_userdata_get_string,
config_userdata_free,
};
static bool create_filter_graph(rarch_dsp_filter_t *dsp, float sample_rate)
@ -208,7 +101,7 @@ static bool create_filter_graph(rarch_dsp_filter_t *dsp, float sample_rate)
if (!dsp->instances[i].impl)
return false;
struct dsp_userdata userdata;
struct config_file_userdata userdata;
userdata.conf = dsp->conf;
/* Index-specific configs take priority over ident-specific. */
userdata.prefix[0] = key;

120
conf/config_file_userdata.c Normal file
View File

@ -0,0 +1,120 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2014 - Daniel De Matteis
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "config_file_userdata.h"
#include "../string_list.h"
#define get_array_setup() \
struct config_file_userdata *dsp = (struct config_file_userdata*)userdata; \
\
char key[2][256]; \
snprintf(key[0], sizeof(key[0]), "%s_%s", dsp->prefix[0], key_str); \
snprintf(key[1], sizeof(key[1]), "%s_%s", dsp->prefix[1], key_str); \
\
char *str = NULL; \
bool got = config_get_string(dsp->conf, key[0], &str); \
got = got || config_get_string(dsp->conf, key[1], &str);
#define get_array_body(T) \
if (got) \
{ \
unsigned i; \
struct string_list *list = string_split(str, " "); \
*values = (T*)calloc(list->size, sizeof(T)); \
for (i = 0; i < list->size; i++) \
(*values)[i] = (T)strtod(list->elems[i].data, NULL); \
*out_num_values = list->size; \
string_list_free(list); \
return true; \
} \
else \
{ \
*values = (T*)calloc(num_default_values, sizeof(T)); \
memcpy(*values, default_values, sizeof(T) * num_default_values); \
*out_num_values = num_default_values; \
return false; \
}
int config_userdata_get_float(void *userdata, const char *key_str,
float *value, float default_value)
{
struct config_file_userdata *dsp = (struct config_file_userdata*)userdata;
char key[2][256];
snprintf(key[0], sizeof(key[0]), "%s_%s", dsp->prefix[0], key_str);
snprintf(key[1], sizeof(key[1]), "%s_%s", dsp->prefix[1], key_str);
bool got = config_get_float(dsp->conf, key[0], value);
got = got || config_get_float(dsp->conf, key[1], value);
if (!got)
*value = default_value;
return got;
}
int config_userdata_get_int(void *userdata, const char *key_str,
int *value, int default_value)
{
struct config_file_userdata *dsp = (struct config_file_userdata*)userdata;
char key[2][256];
snprintf(key[0], sizeof(key[0]), "%s_%s", dsp->prefix[0], key_str);
snprintf(key[1], sizeof(key[1]), "%s_%s", dsp->prefix[1], key_str);
bool got = config_get_int(dsp->conf, key[0], value);
got = got || config_get_int(dsp->conf, key[1], value);
if (!got)
*value = default_value;
return got;
}
int config_userdata_get_float_array(void *userdata, const char *key_str,
float **values, unsigned *out_num_values,
const float *default_values, unsigned num_default_values)
{
get_array_setup()
get_array_body(float)
}
int config_userdata_get_int_array(void *userdata, const char *key_str,
int **values, unsigned *out_num_values,
const int *default_values, unsigned num_default_values)
{
get_array_setup()
get_array_body(int)
}
int config_userdata_get_string(void *userdata, const char *key_str,
char **output, const char *default_output)
{
get_array_setup()
if (got)
{
*output = str;
return true;
}
*output = strdup(default_output);
return false;
}
void config_userdata_free(void *ptr)
{
if (ptr)
free(ptr);
}

View File

@ -0,0 +1,48 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2014 - Daniel De Matteis
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _CONFIG_FILE_USERDATA_H
#define _CONFIG_FILE_USERDATA_H
#include <string.h>
#include "config_file.h"
struct config_file_userdata
{
config_file_t *conf;
const char *prefix[2];
};
int config_userdata_get_float(void *userdata, const char *key_str,
float *value, float default_value);
int config_userdata_get_int(void *userdata, const char *key_str,
int *value, int default_value);
int config_userdata_get_float_array(void *userdata, const char *key_str,
float **values, unsigned *out_num_values,
const float *default_values, unsigned num_default_values);
int config_userdata_get_int_array(void *userdata, const char *key_str,
int **values, unsigned *out_num_values,
const int *default_values, unsigned num_default_values);
int config_userdata_get_string(void *userdata, const char *key_str,
char **output, const char *default_output);
void config_userdata_free(void *ptr);
#endif

View File

@ -92,6 +92,7 @@ CONFIG FILE
#endif
#include "../conf/config_file.c"
#include "../conf/config_file_userdata.c"
#include "../core_options.c"
/*============================================================