#include "config.h"
#ifdef NEW_CONFIG
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <unistd.h>
#include "m_option.h"
//#include "m_config.h"
#include "mp_msg.h"
// Don't free for 'production' atm
#ifndef MP_DEBUG
#define NO_FREE
#endif
m_option_t* m_option_list_find(m_option_t* list,char* name) {
int i;
for(i = 0 ; list[i].name ; i++) {
int l = strlen(list[i].name) - 1;
if((list[i].type->flags & M_OPT_TYPE_ALLOW_WILDCARD) &&
(l > 0) && (list[i].name[l] == '*')) {
if(strncasecmp(list[i].name,name,l) == 0)
return &list[i];
} else if(strcasecmp(list[i].name,name) == 0)
return &list[i];
}
return NULL;
}
// Default function that just do a memcpy
static void copy_opt(m_option_t* opt,void* dst,void* src) {
if(dst && src)
memcpy(dst,src,opt->type->size);
}
// Helper for the print funcs (from man printf)
static char* dup_printf(const char *fmt, ...) {
/* Guess we need no more than 50 bytes. */
int n, size = 50;
char *p;
va_list ap;
if ((p = malloc (size)) == NULL)
return NULL;
while (1) {
/* Try to print in the allocated space. */
va_start(ap, fmt);
n = vsnprintf (p, size, fmt, ap);
va_end(ap);
/* If that worked, return the string. */
if (n > -1 && n < size)
return p;
/* Else try again with more space. */
if (n > -1) /* glibc 2.1 */
size = n+1; /* precisely what is needed */
else /* glibc 2.0 */
size *= 2; /* twice the old size */
if ((p = realloc (p, size)) == NULL)
return NULL;
}
}
// Flag
#define VAL(x) (*(int*)(x))
static int parse_flag(m_option_t* opt,char *name, char *param, void* dst, int src) {
if (src == M_CONFIG_FILE) {
if (!strcasecmp(param, "yes") || /* any other language? */
!strcasecmp(param, "on") ||
!strcasecmp(param, "ja") ||
!strcasecmp(param, "si") ||
!strcasecmp(param, "igen") ||
!strcasecmp(param, "y") ||
!strcasecmp(param, "j") ||
!strcasecmp(param, "i") ||
!strcmp(param, "1")) {
if(dst) VAL(dst) = opt->max;
} else if (!strcasecmp(param, "no") ||
!strcasecmp(param, "off") ||
!strcasecmp(param, "nein") ||
!strcasecmp(param, "nicht") ||
!strcasecmp(param, "nem") ||
!strcasecmp(param, "n") ||
!strcmp(param, "0")) {
if(dst) VAL(dst) = opt->min;
} else {
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "invalid parameter for %s flag: %s\n",name, param);
return M_OPT_INVALID;
}
return 1;
} else {
if(dst) VAL(dst) = opt->max;
return 0;
}
}
static char* print_flag(m_option_t* opt, void* val) {
if(VAL(val) == opt->min)
return strdup("no");
else
return strdup("yes");
}
m_option_type_t m_option_type_flag = {
"Flag",
|