From 1402692370a360a0e1d6eaddb505ce4b8ee91aa4 Mon Sep 17 00:00:00 2001 From: albeu Date: Tue, 12 Nov 2002 14:16:30 +0000 Subject: A struct setter. It allow you to setup struct from some user settings. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@8170 b3059339-0415-0410-9bf9-f77b7e298cf2 --- Makefile | 2 +- m_struct.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ m_struct.h | 50 ++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 m_struct.c create mode 100644 m_struct.h diff --git a/Makefile b/Makefile index 86f7f2e88a..68c4616e8c 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ endif # a BSD compatible 'install' program INSTALL = install -SRCS_COMMON = cpudetect.c codec-cfg.c cfgparser.c my_profile.c spudec.c playtree.c playtreeparser.c asxparser.c vobsub.c subreader.c sub_cc.c find_sub.c m_config.c m_option.c parser-cfg.c +SRCS_COMMON = cpudetect.c codec-cfg.c cfgparser.c my_profile.c spudec.c playtree.c playtreeparser.c asxparser.c vobsub.c subreader.c sub_cc.c find_sub.c m_config.c m_option.c parser-cfg.c m_struct.c SRCS_MENCODER = mencoder.c mp_msg-mencoder.c $(SRCS_COMMON) libao2/afmt.c divx4_vbr.c libvo/aclib.c libvo/osd.c libvo/sub.c libvo/font_load.c libvo/font_load_ft.c xvid_vbr.c parser-mecmd.c SRCS_MPLAYER = mplayer.c mp_msg.c $(SRCS_COMMON) mixer.c parser-mpcmd.c diff --git a/m_struct.c b/m_struct.c new file mode 100644 index 0000000000..d2c304e8b4 --- /dev/null +++ b/m_struct.c @@ -0,0 +1,119 @@ + +#include "config.h" + +#ifdef NEW_CONFIG + +#include +#include + +#include "m_option.h" +#include "m_struct.h" +#include "mp_msg.h" + +m_option_t* +m_struct_get_field(m_struct_t* st,char* f) { + int i; + + for(i = 0 ; st->fields[i].name ; i++) { + if(strcasecmp(st->fields[i].name,f) == 0) + return &st->fields[i]; + } + return NULL; +} + +void* +m_struct_alloc(m_struct_t* st) { + int i; + void* r; + + if(!st->defaults) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s need defaults\n"); + return NULL; + } + // Check the struct fields + for(i = 0 ; st->fields[i].name ; i++) { + if(st->fields[i].type->flags & M_OPT_TYPE_INDIRECT) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s->%s: option type with the indirect flag are forbiden\n",st->name,st->fields[i].name); + return NULL; + } + } + + r = calloc(1,st->size); + memcpy(r,st->defaults,st->size); + + for(i = 0 ; st->fields[i].name ; i++) { + if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC) + memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size); + m_option_copy(&st->fields[i],M_ST_MB_P(r,st->fields[i].p),M_ST_MB_P(st->defaults,st->fields[i].p)); + } + return r; +} + +int +m_struct_set(m_struct_t* st, void* obj, char* field, char* param) { + m_option_t* f = m_struct_get_field(st,field); + + if(!f) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n", + st->name,field); + return 0; + } + + if(f->type->parse(f,field,param,M_ST_MB_P(obj,f->p),M_CONFIG_FILE) < 0) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Strut %s, field %s parsing error: %s\n", + st->name,field,param); + return 0; + } + + return 1; +} + +void +m_struct_reset(m_struct_t* st, void* obj, char* field) { + m_option_t* f; + + if(!field) { // Reset all options + int i; + for(i = 0 ; st->fields[i].name ; i++) + m_option_copy(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p),M_ST_MB_P(st->defaults,st->fields[i].p)); + return; + } + + // Only one + f = m_struct_get_field(st,field); + if(!f) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n", + st->name,field); + return; + } + m_option_copy(f,M_ST_MB_P(obj,f->p),M_ST_MB_P(st->defaults,f->p)); +} + +/// Free an allocated struct +void +m_struct_free(m_struct_t* st, void* obj) { + int i; + + for(i = 0 ; st->fields[i].name ; i++) + m_option_free(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p)); + free(obj); +} + +void* +m_struct_copy(m_struct_t* st, void* obj) { + void* r = malloc(st->size); + int i; + + memcpy(r,obj,st->size); + for(i = 0 ; st->fields[i].name ; i++) { + if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC) + memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size); + m_option_copy(&st->fields[i],M_ST_MB_P(r,st->fields[i].p),M_ST_MB_P(obj,st->fields[i].p)); + } + + return r; +} + + + +#endif // NEW_CONFIG diff --git a/m_struct.h b/m_struct.h new file mode 100644 index 0000000000..02cda2a6bb --- /dev/null +++ b/m_struct.h @@ -0,0 +1,50 @@ + +#ifndef NEW_CONFIG +#warning "Including m_struct.h but NEW_CONFIG is disabled" +#else + +///////////////////// A struct setter //////////////////////// + +struct m_option; + +/// Struct definition +typedef struct m_struct_st { + char* name; // For error msg and debuging + unsigned int size; // size of the whole struct + void* defaults; // Pointer to a struct filled with the default settings + struct m_option* fields; // settable fields +} m_struct_t; + +// Note : the p field of the m_option_t struct must contain the offset +// of the member in the struct (use M_ST_OFF macro for this). + +// From glib.h (modified ;-) +#define M_ST_OFF(struct_type, member) \ + ((void*) &((struct_type*) 0)->member) +#define M_ST_MB_P(struct_p, struct_offset) \ + ((void*) (struct_p) + (unsigned long) (struct_offset)) +#define M_ST_MB(member_type, struct_p, struct_offset) \ + (*(member_type*) M_STRUCT_MEMBER_P ((struct_p), (struct_offset))) + + + +/// Allocate the struct and set it to the defaults +void* +m_struct_alloc(m_struct_t* st); +/// Set a field of the struct +int +m_struct_set(m_struct_t* st, void* obj, char* field, char* param); +/// Reset a field (or all if field == NULL) to defaults +void +m_struct_reset(m_struct_t* st, void* obj, char* field); +/// Create a copy of an existing struct +void* +m_struct_copy(m_struct_t* st, void* obj); +/// Free an allocated struct +void +m_struct_free(m_struct_t* st, void* obj); +/// Get a field description +struct m_option* +m_struct_get_field(m_struct_t* st,char* f); + +#endif -- cgit v1.2.3