changeset 8169:7c9253521f9c

A struct setter. It allow you to setup struct from some user settings.
author albeu
date Tue, 12 Nov 2002 14:16:30 +0000
parents ff6a98628e6c
children 1f6ae3d15f19
files Makefile m_struct.c m_struct.h
diffstat 3 files changed, 170 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Nov 12 12:39:05 2002 +0000
+++ b/Makefile	Tue Nov 12 14:16:30 2002 +0000
@@ -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
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m_struct.c	Tue Nov 12 14:16:30 2002 +0000
@@ -0,0 +1,119 @@
+
+#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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m_struct.h	Tue Nov 12 14:16:30 2002 +0000
@@ -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