[Mplayer-cvslog] CVS: main m_struct.c,NONE,1.1 m_struct.h,NONE,1.1 Makefile,1.232,1.233
Alban Bedel CVS
albeu at mplayerhq.hu
Tue Nov 12 15:16:51 CET 2002
Update of /cvsroot/mplayer/main
In directory mail:/var/tmp.root/cvs-serv26027
Modified Files:
Makefile
Added Files:
m_struct.c m_struct.h
Log Message:
A struct setter. It allow you to setup struct from some user
settings.
--- NEW FILE ---
#include "config.h"
#ifdef NEW_CONFIG
#include <stdlib.h>
#include <string.h>
#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
--- NEW FILE ---
#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
Index: Makefile
===================================================================
RCS file: /cvsroot/mplayer/main/Makefile,v
retrieving revision 1.232
retrieving revision 1.233
diff -u -r1.232 -r1.233
--- Makefile 12 Nov 2002 01:56:21 -0000 1.232
+++ Makefile 12 Nov 2002 14:16:30 -0000 1.233
@@ -26,7 +26,7 @@
# 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
More information about the MPlayer-cvslog
mailing list