//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/option
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include "option_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	option_string
//
// purpose	Check if an option matches, and if so, store a pointer to
//		the string value for that option.
//
// arguments	1 (int *) pointer to number of options      (as in: & argc)
//		2 (char * * *) pointer to argv for options  (as in: & argv)
//		3 (char * *) pointer where to store result pointer
//		4..N (const char *) pointers to possible options
//
// returns	(int) 0 : option did not match, pointers unchanged
//		(int) 1 : option did match, result pointer stored, and if
//			a 2nd arguments is used, argc and argv are updated
//-----------------------------------------------------------------------------
int
option_string (
    int *		arg_argc_ptr
    ,
    char * * *		arg_argv_ptr
    ,
    char * *		arg_result_ptr
    ,
    ...
    )
__PROTO_END__
{
    va_list		var_args	;
    const char *	opt_ptr		;
    char * *		argv		;
    char *		args		;
    int			argc		;

    if ( ! arg_argc_ptr || ! ( argc = * arg_argc_ptr ) ) return 0;
    if ( ! arg_argv_ptr || ! ( argv = * arg_argv_ptr ) ) return 0;
    if ( ! ( args = * argv ) ) return 0;
    va_start( var_args, arg_result_ptr );
    while ( ( opt_ptr = va_arg( var_args, const char * ) ) ) {
	size_t opt_len;

	opt_len = strlen( opt_ptr );
	if ( memcmp( opt_ptr, args, opt_len ) == 0 &&
	     ( args[opt_len] == 0 || args[opt_len] == '=' ) ) {
	    if ( args[opt_len] == 0 ) {
		* arg_argc_ptr = -- argc;
		* arg_argv_ptr = ++ argv;
		args = * argv;
	    } else {
		args += opt_len;
		++ args;
	    }
	    if ( arg_result_ptr ) * arg_result_ptr = args;
	    va_end( var_args );
	    return 1;
	}
    }
    va_end( var_args );
    return 0;
}

