//-----------------------------------------------------------------------------
// 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_app_sll_text
//
// purpose	Append to the first string (given with maximum available space)
//		a string of text representing the signed long long value.
//
//		Do append only if all the text can be appended.
//
// arguments	1 (char *) pointer to target string
//		2 (size_t) maximum space in target string
//		3 (signed long long) the number to convert
//		4 (const char *) language id string, default "eng"
//
// returns	(size_t) new length of string, or ~0 for error
//
// note		Contents of available space after the termination character
//		are destroyed if the append fails due to insufficient space.
//		Nothing should be kept there, anyway.
//-----------------------------------------------------------------------------
size_t
str_app_sll_text (
    char *		arg_target
    ,
    size_t		arg_length
    ,
    signed long long	arg_value
    ,
    const char *	arg_lang
    )
__PROTO_END__
{
    char *		reset_ptr	;
    unsigned long long	value		;
    size_t		len		;

    //-------------------------------------------------
    // Check language ID code string (SIL or ISO code).
    //-------------------------------------------------
    if ( tolower( arg_lang[0] ) != 'e' ||
	 tolower( arg_lang[1] ) != 'n' ||
	 tolower( arg_lang[2] ) != 'g' ||
	 tolower( arg_lang[3] ) != 'a' ||
	 arg_lang[4] != 0 ) {
	return ~0;
    }

    //-----------------------------------------
    // Find and save the current end of string.
    //-----------------------------------------
    reset_ptr = arg_target;
    while ( * reset_ptr ) ++ reset_ptr;
    len = reset_ptr - arg_target;

    //--------------------------------
    // Check for negative or positive.
    //--------------------------------
    if ( arg_value < 0 ) {
	value = - arg_value;
	len = str_app_str( arg_target, arg_length, "minus " );
	if ( len == ~0 ) {
	    * reset_ptr = 0;
	    return ~0;
	}
    } else {
	value = arg_value;
    }

    //-------------------------
    // Do the rest as unsigned.
    //-------------------------
    len = str_app_ull_text( arg_target, arg_length, value, arg_lang );
    if ( len == ~0 ) {
	* reset_ptr = 0;
    }
    return len;
}

