/*
* Copyright (c) 1995-2001 Kungliga Tekniska H�gskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
static void
as_append_char (struct state *state, unsigned char c)
{
if(!as_reserve (state, 1))
*state->s++ = c;
}
/* longest integer types */
#ifdef HAVE_LONG_LONG
typedef unsigned long long u_longest;
typedef long long longest;
#else
typedef unsigned long u_longest;
typedef long longest;
#endif
/*
* is # supposed to do anything?
*/
static int
use_alternative (int flags, u_longest num, unsigned base)
{
return flags & alternate_flag && (base == 16 || base == 8) && num != 0;
}
static int
append_number(struct state *state,
u_longest num, unsigned base, char *rep,
int width, int prec, int flags, int minusp)
{
int len = 0;
int i;
u_longest n = num;
/* given precision, ignore zero flag */
if(prec != -1)
flags &= ~zero_flag;
else
prec = 1;
/* zero value with zero precision -> "" */
if(prec == 0 && n == 0)
return 0;
do{
(*state->append_char)(state, rep[n % base]);
++len;
n /= base;
} while(n);
prec -= len;
/* pad with prec zeros */
while(prec-- > 0){
(*state->append_char)(state, '0');
++len;
}
/* add length of alternate prefix (added later) to len */
if(use_alternative(flags, num, base))
len += base / 8;
/* pad with zeros */
if(flags & zero_flag){
width -= len;
if(minusp || (flags & space_flag) || (flags & plus_flag))
width--;
while(width-- > 0){
(*state->append_char)(state, '0');
len++;
}
}
/* add alternate prefix */
if(use_alternative(flags, num, base)){
if(base == 16)
(*state->append_char)(state, rep[10] + 23); /* XXX */
(*state->append_char)(state, '0');
}
/* add sign */
if(minusp){
(*state->append_char)(state, '-');
++len;
} else if(flags & plus_flag) {
(*state->append_char)(state, '+');
++len;
} else if(flags & space_flag) {
(*state->append_char)(state, ' ');
++len;
}
if(flags & minus_flag)
/* swap before padding with spaces */
for(i = 0; i < len / 2; i++){
char c = state->s[-i-1];
state->s[-i-1] = state->s[-len+i];
state->s[-len+i] = c;
}
width -= len;
while(width-- > 0){
(*state->append_char)(state, ' ');
++len;
}
if(!(flags & minus_flag))
/* swap after padding with spaces */
for(i = 0; i < len / 2; i++){
char c = state->s[-i-1];
state->s[-i-1] = state->s[-len+i];
state->s[-len+i] = c;
}
return len;
}
/*
* return length
*/
static int
append_string (struct state *state,
const unsigned char *arg,
int width,
int prec,
int flags)
{
int len = 0;