//-----------------------------------------------------------------------------
// 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/string
// 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 "string_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	str_metric_to_sl
//
// purpose	Convert a string containing a number followed by an optional
//		metric suffix into an signed long integer.
//
// arguments	1 (const char *) pointer to string to convert
//		2 (char * *) pointer to string to store end pointer
//		3 (int) numeric base value, 2 to 36
//		4 (signed long) metric scale for lower case suffixes
//		5 (signed long) metric scale for upper case suffixes
//
// returns	(signed long) converted result
//
// note		Numeric values that exceed the maximums for this data type
//		will result in undefined values being returned.
//
// suffixes	'k' or 'K' multiplies the preceeding number by scale**1
//		'm' or 'M' multiplies the preceeding number by scale**2
//		'g' or 'G' multiplies the preceeding number by scale**3
//		't' or 'T' multiplies the preceeding number by scale**4
//		'p' or 'P' multiplies the preceeding number by scale**5
//		'e' or 'E' multiplies the preceeding number by scale**6
//
// warning	Metric suffixes are not recognized if the base setting
//		uses that character for its symbol set.
//-----------------------------------------------------------------------------
signed long
str_metric_to_sl (
    const char *	arg_str
    ,
    char * *		arg_end_ptr
    ,
    int			arg_base
    ,
    signed long		arg_scale_lower
    ,
    signed long		arg_scale_upper
    )
__PROTO_END__
{
    signed long		result	;
    char *		end_ptr	;

    end_ptr = NULL;
    result = strtol( arg_str, & end_ptr, arg_base );
    if ( end_ptr ) {
	switch ( * end_ptr ) {
	case 'e':
	    result *= arg_scale_lower;
	case 'p':
	    result *= arg_scale_lower;
	case 't':
	    result *= arg_scale_lower;
	case 'g':
	    result *= arg_scale_lower;
	case 'm':
	    result *= arg_scale_lower;
	case 'k':
	    result *= arg_scale_lower;
	    ++ end_ptr;
	    break;
	case 'E':
	    result *= arg_scale_upper;
	case 'P':
	    result *= arg_scale_upper;
	case 'T':
	    result *= arg_scale_upper;
	case 'G':
	    result *= arg_scale_upper;
	case 'M':
	    result *= arg_scale_upper;
	case 'K':
	    result *= arg_scale_upper;
	    ++ end_ptr;
	    break;
	}
	if ( arg_end_ptr ) * arg_end_ptr = end_ptr;
    }
    return result;
}

