#include "input.h"
#include "clog.h"
#include "util.h"
#include <cstring>
#include <cstdlib>

/******************************************************************************/

void REQ_STRING( CVars &in, const char * name, int min_len, int max_len, const char *regex ) throw (bad_parameter)
{
        int len = in[name].length();
        char ret[2048]; ret[0] = 0;

        logfmt( FLOG_MAX, "REQ_STRING(%s)", name );

        bool match = reg_match( in[name].c_str(), regex, ret, sizeof(ret) );
        len = strlen(ret);
        if ( !match || len < min_len || len > max_len )
        {
                logfmt( FLOG_MAX, "Error in parameter '%s', len= %d <= %d <= %d", name, min_len, match, max_len );
                in[name].set_valid( false );
                throw bad_parameter( in, name );
        }

        in[name] = ret;

        CVars::iterator i = in.find( name );
        i->second.set_valid();

        //logfmt( FLOG_MAX, "i->second.valid()=%d", i->second.valid() );

        // if not match regexp

}

/******************************************************************************/

long REQ_LONG( CVars &in, const char *name, long min, long max )
{
        char *end;
        long r = strtol( in[name].c_str(), &end, 10 );

        if ( in[name].length() && !*end )
        {
                in[name].set_valid();
                return r;
        }
        else
        {
                in[name].set_valid( false );
                throw bad_parameter( in, name );
        }

        return r;
}

/******************************************************************************/

bool OptionalSpecified( CVars &in, const char * name )
{
        if ( in.find(name)==in.end() ) return false;    // parameter omitted, not even passed from frontend

        return ! reg_match( in[name].c_str(), "^[[:space:]]*$", NULL, 0 );
}

/******************************************************************************/

/*
vi:ts=2
*/

