/*
 * Copyright (C) 2007 Loren Merritt <lorenm@u.washington.edu>
 *
 * This program 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 Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "common.h"

void xlsc_param_default( xlsc_param_t *param )
{
    memset( param, 0, sizeof(xlsc_param_t) );

    param->qp = 8;
    param->deadzone = 500;
    param->keyint = 100;
    param->lambda_mult = 100;
    param->lookahead_lambda = 0.0;
    param->umv = 1;
}

static int xlsc_atobool( const char *str, int *b_error )
{
    if( !strcmp(str, "1") ||
        !strcmp(str, "true") ||
        !strcmp(str, "yes") )
        return 1;
    if( !strcmp(str, "0") ||
        !strcmp(str, "false") ||
        !strcmp(str, "no") )
        return 0;
    *b_error = 1;
    return 0;
}

static int xlsc_atoi( const char *str, int *b_error )
{
    char *end;
    int v = strtol( str, &end, 0 );
    if( end == str || *end != '\0' )
        *b_error = 1;
    return v;
}

static double xlsc_atof( const char *str, int *b_error )
{
    char *end;
    double v = strtod( str, &end );
    if( end == str || *end != '\0' )
        *b_error = 1;
    return v;
}

#define atobool(str) ( name_was_bool = 1, xlsc_atobool( str, &b_error ) )
#define atoi(str) xlsc_atoi( str, &b_error )
#define atof(str) xlsc_atof( str, &b_error )

int xlsc_param_parse( xlsc_param_t *p, const char *name, const char *value )
{
    char *name_buf = NULL;
    int b_error = 0;
    int name_was_bool;
    int value_was_null = !value;
    int i;

    if( !name )
        return XLSC_PARAM_BAD_NAME;
    if( !value )
        value = "true";

    if( value[0] == '=' )
        value++;

    if( strchr( name, '_' ) ) // s/_/-/g
    {
        char *p;
        name_buf = strdup(name);
        while( (p = strchr( name_buf, '_' )) )
            *p = '-';
        name = name_buf;
    }

    if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
        (!strncmp( name, "no", 2 ) && (i = 2)) )
    {
        name += i;
        value = atobool(value) ? "false" : "true";
    }
    name_was_bool = 0;

#define OPT(STR) else if( !strcmp( name, STR ) )
#define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
    if(0);
    OPT("deadzone")
        p->deadzone = atoi(value);
    OPT("dump-file")
        p->dump_file = strdup(value);
    OPT("keyint")
        p->keyint = atoi(value);
    OPT("lambda")
        p->lambda_mult = atoi(value);
    OPT("lookahead")
        p->lookahead = atoi(value);
    OPT("lookahead-lambda")
        p->lookahead_lambda = atof(value);
    OPT("mbd")
        p->mb_decision = atoi(value);
    OPT("qp")
        p->qp = atoi(value);
    OPT("umv")
        p->umv = atobool(value);
    OPT("verbose")
        p->verbose = atoi(value);
    else
        return XLSC_PARAM_BAD_NAME;
#undef OPT
#undef OPT2
#undef atobool
#undef atoi
#undef atof

    if( name_buf )
        free( name_buf );

    b_error |= value_was_null && !name_was_bool;
    return b_error ? XLSC_PARAM_BAD_VALUE : 0;
}

