vfprintf.c File Reference


Detailed Description

The bulk of this file was pulled in from the FreeBSD 6.1 distribution, in /usr/src/lib/libc/stdio/vfprintf.c since that's the function that performs the real work of format string processing.

Changes to the file include several modifications to make it compile, changes to implement snprintf / vsnprintf, and of course, the changes to implement the Formatter::format() callback hook.

Wherever possible, the original implementation has been retained to simplify merging in future changes from the FreeBSD camp, using the __FORMATTER__ macro to distinguish the adaptations.

Definition in file vfprintf.c.

#include <stdarg.h>
#include <memory.h>
#include "config.h"
#include "compat/fpclassify.h"
#include <sys/cdefs.h>
#include <sys/types.h>
#include <ctype.h>
#include <limits.h>
#include <locale.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <float.h>
#include <math.h>
#include "floatio.h"
#include "gdtoa.h"

Go to the source code of this file.

Classes

struct  __siov
struct  __suio
union  arg

Defines

#define __FORMATTER__
#define __FBSDID(x)
#define INTMAX_MAX   INT_MAX
#define reallocf   realloc
#define to_digit(c)   ((c) - '0')
#define is_digit(c)   ((unsigned)to_digit(c) <= 9)
#define to_char(n)   ((n) + '0')
#define DEFPREC   6
#define BUF   100
#define STATIC_ARG_TBL_SIZE   8
#define ALT   0x001
#define LADJUST   0x004
#define LONGDBL   0x008
#define LONGINT   0x010
#define LLONGINT   0x020
#define SHORTINT   0x040
#define ZEROPAD   0x080
#define FPT   0x100
#define GROUPING   0x200
#define SIZET   0x400
#define PTRDIFFT   0x800
#define INTMAXT   0x1000
#define CHARINT   0x2000
#define PADSIZE   16
#define PRINT(ptr, len)
#define PAD(howmany, with)
#define PRINTANDPAD(p, ep, len, with)
#define FLUSH()   {}
#define GETARG(type)
#define SARG()
#define UARG()
#define INTMAX_SIZE   (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
#define SJARG()
#define UJARG()
#define GETASTER(val)
#define ADDTYPE(type)
#define ADDSARG()
#define ADDUARG()
#define ADDASTER()

Enumerations

enum  typeid {
  T_UNUSED, TP_SHORT, T_INT, T_U_INT,
  TP_INT, T_LONG, T_U_LONG, TP_LONG,
  T_LLONG, T_U_LLONG, TP_LLONG, T_PTRDIFFT,
  TP_PTRDIFFT, T_SIZET, TP_SIZET, T_INTMAXT,
  T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR,
  TP_SCHAR, T_DOUBLE, T_LONG_DOUBLE, T_WINT,
  TP_WCHAR
}

Functions

 __FBSDID ("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.69 2005/04/16 22:36:51 das Exp $")
size_t formatter_format (void *fmtobj, char *str, size_t strsz)
static char * __ujtoa (uintmax_t, char *, int, int, const char *, int, char, const char *)
static char * __ultoa (u_long, char *, int, int, const char *, int, char, const char *)
static char * __wcsconv (wchar_t *, int)
static void __find_arguments (const char *, va_list, union arg **)
static void __grow_type_table (int, enum typeid **, int *)
static int exponent (char *, int, int)
int vsnprintf (char *str, size_t strsz, const char *fmt0, va_list ap)
int snprintf (char *str, size_t strsz, const char *fmt,...)


Define Documentation

#define __FBSDID (  ) 

Definition at line 85 of file vfprintf.c.

#define __FORMATTER__

Definition at line 16 of file vfprintf.c.

 
#define ADDASTER (  ) 

Value:

n2 = 0; \
        cp = fmt; \
        while (is_digit(*cp)) { \
                n2 = 10 * n2 + to_digit(*cp); \
                cp++; \
        } \
        if (*cp == '$') { \
                int hold = nextarg; \
                nextarg = n2; \
                ADDTYPE (T_INT); \
                nextarg = hold; \
                fmt = ++cp; \
        } else { \
                ADDTYPE (T_INT); \
        }

Referenced by __find_arguments().

 
#define ADDSARG (  ) 

Value:

((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
                ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
                ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
                ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
                ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))

Referenced by __find_arguments().

#define ADDTYPE ( type   ) 

Value:

((nextarg >= tablesize) ? \
                __grow_type_table(nextarg, &typetable, &tablesize) : (void)0, \
        (nextarg > tablemax) ? tablemax = nextarg : 0, \
        typetable[nextarg++] = type)

Referenced by __find_arguments().

 
#define ADDUARG (  ) 

Value:

((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
                ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
                ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
                ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
                ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))

Referenced by __find_arguments().

#define ALT   0x001

Definition at line 562 of file vfprintf.c.

Referenced by vsnprintf().

#define BUF   100

Definition at line 555 of file vfprintf.c.

Referenced by vsnprintf().

#define CHARINT   0x2000

Definition at line 575 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

#define DEFPREC   6

Definition at line 542 of file vfprintf.c.

Referenced by vsnprintf().

 
#define FLUSH (  )     {}

Referenced by vsnprintf().

#define FPT   0x100

Definition at line 569 of file vfprintf.c.

Referenced by vsnprintf().

#define GETARG ( type   ) 

Value:

((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
            (nextarg++, va_arg(ap, type)))

Referenced by vsnprintf().

#define GETASTER ( val   ) 

Value:

n2 = 0; \
        cp = fmt; \
        while (is_digit(*cp)) { \
                n2 = 10 * n2 + to_digit(*cp); \
                cp++; \
        } \
        if (*cp == '$') { \
                int hold = nextarg; \
                if (argtable == NULL) { \
                        argtable = statargtable; \
                        __find_arguments (fmt0, orgap, &argtable); \
                } \
                nextarg = n2; \
                val = GETARG (int); \
                nextarg = hold; \
                fmt = ++cp; \
        } else { \
                val = GETARG (int); \
        }

Referenced by vsnprintf().

#define GROUPING   0x200

Definition at line 570 of file vfprintf.c.

Referenced by vsnprintf().

#define INTMAX_MAX   INT_MAX

Definition at line 148 of file vfprintf.c.

Referenced by __ujtoa().

#define INTMAX_SIZE   (INTMAXT|SIZET|PTRDIFFT|LLONGINT)

Referenced by vsnprintf().

#define INTMAXT   0x1000

Definition at line 574 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

#define is_digit (  )     ((unsigned)to_digit(c) <= 9)

Definition at line 273 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

#define LADJUST   0x004

Definition at line 563 of file vfprintf.c.

Referenced by vsnprintf().

#define LLONGINT   0x020

Definition at line 566 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

#define LONGDBL   0x008

Definition at line 564 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

#define LONGINT   0x010

Definition at line 565 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

#define PAD ( howmany,
with   ) 

Value:

{ \
        if ((n = (howmany)) > 0) { \
                while (n > PADSIZE) { \
                        PRINT(with, PADSIZE); \
                        n -= PADSIZE; \
                } \
                PRINT(with, n); \
        } \
}

Referenced by vsnprintf().

#define PADSIZE   16

Referenced by vsnprintf().

#define PRINT ( ptr,
len   ) 

Value:

{                               \
        sz = (((size_t)len) <= strsz) ? len : strsz;    \
        memcpy(str, ptr, sz);                           \
        str += sz;                                      \
        strsz -= sz;                                    \
}

Referenced by vsnprintf().

#define PRINTANDPAD ( p,
ep,
len,
with   ) 

Value:

do {    \
        n2 = (ep) - (p);                        \
        if (n2 > (len))                         \
                n2 = (len);                     \
        if (n2 > 0)                             \
                PRINT((p), n2);                 \
        PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
} while(0)

Referenced by vsnprintf().

#define PTRDIFFT   0x800

Definition at line 573 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

#define reallocf   realloc

Definition at line 154 of file vfprintf.c.

Referenced by __grow_type_table().

 
#define SARG (  ) 

Value:

(flags&LONGINT ? GETARG(long) : \
            flags&SHORTINT ? (long)(short)GETARG(int) : \
            flags&CHARINT ? (long)(signed char)GETARG(int) : \
            (long)GETARG(int))

Referenced by vsnprintf().

#define SHORTINT   0x040

Definition at line 567 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

#define SIZET   0x400

Definition at line 572 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

 
#define SJARG (  ) 

Value:

(flags&INTMAXT ? GETARG(intmax_t) : \
            flags&SIZET ? (intmax_t)GETARG(size_t) : \
            flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
            (intmax_t)GETARG(long long))

Referenced by vsnprintf().

#define STATIC_ARG_TBL_SIZE   8

Definition at line 557 of file vfprintf.c.

Referenced by __find_arguments(), __grow_type_table(), and vsnprintf().

#define to_char (  )     ((n) + '0')

Definition at line 274 of file vfprintf.c.

Referenced by __ujtoa(), __ultoa(), exponent(), and oasys::fast_ultoa().

#define to_digit (  )     ((c) - '0')

Definition at line 272 of file vfprintf.c.

Referenced by __find_arguments(), and vsnprintf().

 
#define UARG (  ) 

Value:

(flags&LONGINT ? GETARG(u_long) : \
            flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
            flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
            (u_long)GETARG(u_int))

Referenced by vsnprintf().

 
#define UJARG (  ) 

Value:

(flags&INTMAXT ? GETARG(uintmax_t) : \
            flags&SIZET ? (uintmax_t)GETARG(size_t) : \
            flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
            (uintmax_t)GETARG(unsigned long long))

Referenced by vsnprintf().

#define ZEROPAD   0x080

Definition at line 568 of file vfprintf.c.

Referenced by vsnprintf().


Enumeration Type Documentation

enum typeid

Enumerator:
T_UNUSED 
TP_SHORT 
T_INT 
T_U_INT 
TP_INT 
T_LONG 
T_U_LONG 
TP_LONG 
T_LLONG 
T_U_LLONG 
TP_LLONG 
T_PTRDIFFT 
TP_PTRDIFFT 
T_SIZET 
TP_SIZET 
T_INTMAXT 
T_UINTMAXT 
TP_INTMAXT 
TP_VOID 
TP_CHAR 
TP_SCHAR 
T_DOUBLE 
T_LONG_DOUBLE 
T_WINT 
TP_WCHAR 

Definition at line 193 of file vfprintf.c.


Function Documentation

__FBSDID ( "$FreeBSD: src/lib/libc/stdio/vfprintf.  c,
v 1.69 2005/04/16 22:36:51 das Exp $"   
)

static void __find_arguments ( const char *  ,
va_list  ,
union arg **   
) [static]

Definition at line 1478 of file vfprintf.c.

References ADDASTER, ADDSARG, ADDTYPE, ADDUARG, CHARINT, arg::intarg, INTMAXT, is_digit, LLONGINT, LONGDBL, LONGINT, PTRDIFFT, SHORTINT, SIZET, STATIC_ARG_TBL_SIZE, T_DOUBLE, T_INT, T_INTMAXT, T_LLONG, T_LONG, T_LONG_DOUBLE, T_PTRDIFFT, T_SIZET, T_U_INT, T_U_LLONG, T_U_LONG, T_UINTMAXT, T_UNUSED, T_WINT, to_digit, TP_CHAR, TP_INT, TP_INTMAXT, TP_LLONG, TP_LONG, TP_PTRDIFFT, TP_SCHAR, TP_SHORT, TP_SIZET, TP_VOID, and TP_WCHAR.

Referenced by vsnprintf().

static void __grow_type_table ( int  ,
enum typeid **  ,
int *   
) [static]

Definition at line 1804 of file vfprintf.c.

References reallocf, STATIC_ARG_TBL_SIZE, and T_UNUSED.

static char * __ujtoa ( uintmax_t  ,
char *  ,
int  ,
int  ,
const char *  ,
int  ,
char  ,
const char *   
) [static]

Definition at line 360 of file vfprintf.c.

References __ultoa(), INTMAX_MAX, and to_char.

Referenced by vsnprintf().

static char * __ultoa ( u_long  ,
char *  ,
int  ,
int  ,
const char *  ,
int  ,
char  ,
const char *   
) [static]

Definition at line 283 of file vfprintf.c.

References to_char.

Referenced by __ujtoa(), and vsnprintf().

static char * __wcsconv ( wchar_t *  ,
int   
) [static]

Definition at line 437 of file vfprintf.c.

Referenced by vsnprintf().

static int exponent ( char *  ,
int  ,
int   
) [static]

Definition at line 1833 of file vfprintf.c.

References MAXEXPDIG, and to_char.

Referenced by vsnprintf().

size_t formatter_format ( void *  fmtobj,
char *  str,
size_t  strsz 
)

Referenced by vsnprintf().

int snprintf ( char *  str,
size_t  strsz,
const char *  fmt,
  ... 
)

Definition at line 1459 of file vfprintf.c.

References vsnprintf().

Referenced by oasys::Bluetooth::_batostr(), dtn_strerror(), oasys::ByteBufShim::format(), oasys::NullStringShim::format(), oasys::StringShim::format(), oasys::UIntShim::format(), oasys::IntShim::format(), dtn::RouteEntry::format(), oasys::RefCountedObject::format(), dtn::Link::format(), dtn::Contact::format(), dtn::Bundle::format(), oasys::Log::gen_prefix(), dtn::FragmentManager::get_hash_key(), dtn::APIClient::handle_recv(), oasys::FatalSignals::handler(), oasys::Log::log_multiline(), main(), dtn::ContactManager::new_opportunistic_link(), oasys::Log::parse_debug_file(), oasys::KeyMarshal::process_int(), oasys::KeyMarshal::process_int64(), dtn::CustodySignal::reason_to_str(), oasys::FileSystemStore::tidy_database(), dtn::DTNServer::tidy_dir(), oasys::Getopt::usage(), and oasys::Log::vlogf().

int vsnprintf ( char *  str,
size_t  strsz,
const char *  fmt0,
va_list  ap 
)

Definition at line 582 of file vfprintf.c.

References __find_arguments(), __hdtoa(), __hldtoa(), __ldtoa(), __ujtoa(), __ultoa(), __wcsconv(), ALT, BUF, CHARINT, DEFPREC, exponent(), FLUSH, formatter_format(), FPT, GETARG, GETASTER, GROUPING, INTMAX_SIZE, INTMAXT, is_digit, LADJUST, LLONGINT, LONGDBL, LONGINT, MAXEXPDIG, PAD, PADSIZE, PRINT, PRINTANDPAD, PTRDIFFT, SARG, SHORTINT, SIZET, SJARG, STATIC_ARG_TBL_SIZE, to_digit, UARG, __suio::uio_iov, __suio::uio_iovcnt, __suio::uio_resid, UJARG, and ZEROPAD.

Referenced by oasys::Logger::Logger(), oasys::Logger::logpath_appendf(), oasys::Logger::logpathf(), snprintf(), oasys::StringBuffer::vappendf(), oasys::StringAppender::vappendf(), oasys::BufferedOutput::vformat_buf(), and oasys::Log::vlogf().


Generated on Thu Jun 7 12:54:31 2007 for DTN Reference Implementation by  doxygen 1.5.1