前些时候,贴了一段微软的printf代码,园子里的大侠们说要看看output的代码,这不最近有幸看了微软的
output代码,所以贴上来给园子里大侠看看。
tip:
源代码版权归微软所有,这里仅贴出来供大家看看。
?/***
*output.c – printf style output to a file
*
* copyright (c) 1989-1991, microsoft corporation. all rights reserved.
*
*purpose:
* this file contains the code that does all the work for the
* printf family of functions. it should not be called directly, only
* by the *printf functions. we don't make any assumtions about the
* sizes of ints, longs, shorts, or long doubles, but if types do overlap,
* we also try to be efficient. we do assume that pointers are the same
* size as either ints or longs.
* if cprflag is defined, defines _cprintf instead.
* **** doesn't currently do mthread locking ****
*
*revision history:
* 06-01-89 phg module created
* 08-28-89 jcr added cast to get rid of warning (no object changes)
* 02-15-90 gjf fixed copyright
* 03-19-90 gjf made calling type _calltype1 and added #include
* <cruntime.h>.
* 03-26-90 gjf changed local macro to incorporate _calltype4. placed
* prototype for _output() in internal.h and #include-d
* it.
* 08-01-90 sbm compiles cleanly with -w3, moved _cfltcvt_tab and
* typedefs double and longdouble to new header
* <fltintrn.h>, formerly named <struct.h>
* 09-05-90 sbm first attempt at adding cprflag and code to generate
* cprintf. anything in #ifdef cprflag untested.
* still needs to have locking added for mthread case.
* 10-03-90 gjf new-style function declarators.
* 01-02-91 srw added _win32_ conditional for 'c' and 's' format chars.
* 01-16-91 gjf ansi naming.
* 01-16-91 srw added #include of maketabc.out (_win32_)
* 04-09-91 pnt use the _cruiser_ mapping for _mac_
* 04-16-91 srw fixed #include of maketabc.out (_win32_)
* 04-25-91 srw made nullstring static
* 05-20-91 gjf moved state table for win32 inline (_win32_).
* 09-12-91 jcr bumped conversion buffer size to be ansi-compliant
* 09-17-91 ihj add partial unicode (%ws, %wc) support
* 09-28-91 gjf merged with crt32 and crtdll versions. for now, 9-17-91
* change is built only for win32, not dosx32 (_win32_).
* 10-22-91 etc complete wchar_t/mb support under _intl. for now,
* 9-28-91 change is additionally under !_intl. bug fix:
* ints and pointers are longs.
* 11-19-91 etc added support for _wsprintf, _vwsprintf with wprflag;
* added %tc %ts (generic string handling).
* 12-05-91 gdp bug fix: va_arg was used inconsistently for double
* 12-19-91 etc added some comments on wsprintf optimization, undones;
* check return on malloc.
* 03-25-92 djm posix support
* 04-16-92 krs support new iso {s|f}wprintf with unicode format string.
* 06-08-92 srw modified to not use free and malloc for mbtowc conversion.
* 06-10-92 krs fix glitch in previous change.
* 07-14-92 tvb added alpha support (quad stuff).
* 07-17-92 krs fix typo which broke wprflag support.
* 04-16-93 sks fix bug in 's' option logic.
* 07-16-93 srw alpha merge
* 08-17-93 cfw avoid mapping tchar macros incorrectly if _mbcs defined.
*
*******************************************************************************/
/* temporary hack to minimize changes. this should go into fltintrn.h */
#if defined(_m_mrx000) || defined(_m_alpha) || defined(_m_ppc)
#define double double
#endif
#include <cruntime.h>
#include <limits.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdarg.h>
#include <cvt.h>
#include <conio.h>
#include <internal.h>
#include <fltintrn.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
/* inline keyword is non-ansi c7 extension */
/* consider: move to cruntime.h! */
#if !defined(_msc_ver) || defined(__stdc__)
#define __inline static
#else
/* undone: compiler is broken */
#define __inline static
#endif
#if defined(wprflag) && !defined(_unicode)
#define _unicode 1
#endif
#ifdef _mbcs /* always want either unicode or sbcs for tchar.h */
#undef _mbcs
#endif
#include <tchar.h>
/* this macro defines a function which is private and as fast as possible: */
/* for example, in c 6.0, it might be static _fastcall <type> near. */
#define local(x) static x _calltype4
/* int/long/short/pointer sizes */
/* the following should be set depending on the sizes of various types */
#define long_is_int 1 /* 1 means long is same size as int */
#define short_is_int 0 /* 1 means short is same size as int */
#define longdouble_is_double 1 /* 1 means long double is same as double */
#define ptr_is_int 1 /* 1 means ptr is same size as int */
#define ptr_is_long 1 /* 1 means ptr is same size as long */
#if long_is_int
#define get_long_arg(x) (long)get_int_arg(x)
#endif
#ifndef wprflag
#if short_is_int
#define get_short_arg(x) (short)get_int_arg(x)
#endif
#endif
#if ptr_is_int
#define get_ptr_arg(x) (void *)get_int_arg(x)
#elif ptr_is_long
#define get_ptr_arg(x) (void *)get_long_arg(x)
#else
#error size of pointer must be same as size of int or long
#endif
/* constants */
/* size of conversion buffer (ansi-specified minimum is 509) */
#define buffersize 512
#if (buffersize < cvtbufsize)
#error conversion buffer too small for max double.
#endif
/* flag definitions */
#define fl_sign 0x0001 /* put plus or minus in front */
#define fl_signsp 0x0002 /* put space or minus in front */
#define fl_left 0x0004 /* left justify */
#define fl_leadzero 0x0008 /* pad with leading zeros */
#define fl_long 0x0010 /* long value given */
#define fl_short 0x0020 /* short value given */
#define fl_signed 0x0040 /* signed data given */
#define fl_alternate 0x0080 /* alternate form requested */
#define fl_negative 0x0100 /* value is negative */
#define fl_forceoctal 0x0200 /* force leading '0' for octals */
#define fl_longdouble 0x0400 /* long double value given */
#define fl_widechar 0x0800 /* wide characters */
/* state definitions */
enum state {
st_normal, /* normal state; outputting literal chars */
st_percent, /* just read '%' */
st_flag, /* just read flag character */
st_width, /* just read width specifier */
st_dot, /* just read '.' */
st_precis, /* just read precision specifier */
st_size, /* just read size specifier */
st_type /* just read type specifier */
};
#define numstates (st_type + 1)
/* character type values */
enum chartype {
ch_other, /* character with no special meaning */
ch_percent, /* '%' */
ch_dot, /* '.' */
ch_star, /* '*' */
ch_zero, /* '0' */
ch_digit, /* '1'..'9' */
ch_flag, /* ' ', '+', '-', '#' */
ch_size, /* 'h', 'l', 'l', 'n', 'f', 'w' */
ch_type /* type specifying character */
};
/* static data (read only, since we are re-entrant) */
#if defined(wprflag) || defined(cprflag)
extern char *__nullstring; /* string to print on null ptr */
extern wchar_t *__wnullstring; /* string to print on null ptr */
#else /* wprflag || cprflag */
char *__nullstring = "(null)"; /* string to print on null ptr */
wchar_t *__wnullstring = l"(null)";/* string to print on null ptr */
#endif /* wprflag || cprflag */
/* the state table. this table is actually two tables combined into one. */
/* the lower nybble of each byte gives the character class of any */
/* character; while the uper nybble of the byte gives the next state */
/* to enter. see the macros below the table for details. */
/* */
/* the table is generated by maketabc.c — use this program to make */
/* changes. */
#if defined(wprflag) || defined(cprflag)
extern const char __lookuptable[];
#else /* wprflag/cprflag */
#if defined(_cruiser_) || defined(_mac_)
/* table generated by maketabc.c built with -d_cruiser_. */
const char __lookuptable[] = {
0x06, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
0x10, 0x00, 0x03, 0x06, 0x00, 0x06, 0x02, 0x10,
0x04, 0x45, 0x45, 0x45, 0x05, 0x05, 0x05, 0x05,
0x05, 0x35, 0x30, 0x00, 0x50, 0x00, 0x00, 0x00,
0x00, 0x20, 0x20, 0x38, 0x50, 0x58, 0x07, 0x08,
0x00, 0x30, 0x30, 0x30, 0x57, 0x50, 0x07, 0x00,
0x00, 0x20, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00,
0x08, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x00,
0x00, 0x70, 0x70, 0x78, 0x78, 0x78, 0x78, 0x08,
0x07, 0x08, 0x00, 0x00, 0x07, 0x00, 0x08, 0x08,
0x08, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x07,
0x08
};
#else /* ndef _cruiser_ || _mac_ */
#if defined(_win32_) || defined(_posix_)
/* table generated by maketabc.c built with -d_win32_. defines additional */
/* format code %z for counted string. */
const char __lookuptable[] = {
0x06, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
0x10, 0x00, 0x03, 0x06, 0x00, 0x06, 0x02, 0x10,
0x04, 0x45, 0x45, 0x45, 0x05, 0x05, 0x05, 0x05,
0x05, 0x35, 0x30, 0x00, 0x50, 0x00, 0x00, 0x00,
0x00, 0x20, 0x20, 0x38, 0x50, 0x58, 0x07, 0x08,
0x00, 0x30, 0x30, 0x30, 0x57, 0x50, 0x07, 0x00,
0x00, 0x20, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00,
0x08, 0x60, 0x68, 0x60, 0x60, 0x60, 0x60, 0x00,
0x00, 0x70, 0x70, 0x78, 0x78, 0x78, 0x78, 0x08,
0x07, 0x08, 0x00, 0x00, 0x07, 0x00, 0x08, 0x08,
0x08, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x07,
0x08
};
#else /* ndef _win32_ */
#error error – only cruiser, win32, posix, or mac target supported!
#endif /* _win32_ || _posix_ */
#endif /* _cruiser_ || _mac_ */
#endif /* wprflag || cprflag */
#define find_char_class(c)
((c) < _t(' ') || (c) > _t('x') ?
ch_other
:
__lookuptable[(c)-_t(' ')] & 0xf)
#define find_next_state(class, state)
(__lookuptable[(class) * numstates + (state)] >> 4)
/*
* note: cprflag and wprflag cases are currently mutually exclusive.
*/
/* prototypes */
#ifdef cprflag
#define write_char(ch, pnw) write_char(ch, pnw)
#define write_multi_char(ch, num, pnw) write_multi_char(ch, num, pnw)
#define write_string(s, len, pnw) write_string(s, len, pnw)
#define write_wstring(s, len, pnw) write_wstring(s, len, pnw)
local(void) write_char(int ch, int *pnumwritten);
local(void) write_multi_char(int ch, int num, int *pnumwritten);
local(void) write_string(char *string, int len, int *numwritten);
local(void) write_wstring(wchar_t *string, int len, int *numwritten);
#elif wprflag
#define write_char(ch, pnw) write_char(ch, stream, pnw)
#define write_multi_char(ch, num, pnw) write_multi_char(ch, num, stream, pnw)
#define write_string(s, len, pnw) write_string(s, len, stream, pnw)
local(void) write_char(int ch, file *f, int *pnumwritten);
local(void) write_multi_char(wchar_t ch, int num, file *f, int *pnumwritten);
local(void) write_string(wchar_t *string, int len, file *f, int *numwritten);
#else
#define write_char(ch, pnw) write_char(ch, stream, pnw)
#define write_multi_char(ch, num, pnw) write_multi_char(ch, num, stream, pnw)
#define write_string(s, len, pnw) write_string(s, len, stream, pnw)
#define write_wstring(s, len, pnw) write_wstring(s, len, stream, pnw)
local(void) write_char(int ch, file *f, int *pnumwritten);
local(void) write_multi_char(int ch, int num, file *f, int *pnumwritten);
local(void) write_string(char *string, int len, file *f, int *numwritten);
local(void) write_wstring(wchar_t *string, int len, file *f, int *numwritten);
#endif
__inline int _calltype4 get_int_arg(va_list *pargptr);
#ifndef wprflag
#if !short_is_int
__inline short _calltype4 get_short_arg(va_list *pargptr);
#endif
#endif
#if !long_is_int
__inline long _calltype4 get_long_arg(va_list *pargptr);
#endif
#ifdef _alpha_
__inline __int64 _calltype4 get_quad_arg(va_list *pargptr);
#endif
#ifdef cprflag
local(int) output(const char *, va_list);
/***
*int _cprintf(format, arglist) – write formatted output directly to console
*
*purpose:
* writes formatted data like printf, but uses console i/o functions.
*
*entry:
* char *format – format string to determine data formats
* arglist – list of pointers to where to put data
*
*exit:
* returns number of characters written
*
*exceptions:
*
*******************************************************************************/
int _calltype2 _cprintf (
const char * format,
…
)
{
va_list arglist;
va_start(arglist, format);
return output(format, arglist);
}
#endif /* cprflag */
/***
*int _output(stream, format, argptr), static int output(format, argptr)
*
*purpose:
* output performs printf style output onto a stream. it is called by
* printf/fprintf/sprintf/vprintf/vfprintf/vsprintf to so the dirty
* work. in multi-thread situations, _output assumes that the given
* stream is already locked.
*
* algorithm:
* the format string is parsed by using a finite state automaton
* based on the current state and the current character read from
* the format string. thus, looping is on a per-character basis,
* not a per conversion specifier basis. once the format specififying
* character is read, output is performed.
*
*entry:
* file *stream – stream for output
* char *format – printf style format string
* va_list argptr – pointer to list of subsidiary arguments
*
*exit:
* returns the number of characters written, or -1 if an output error
* occurs.
#ifdef wprflag
* the wide-character flavour returns the number of wide-characters written.
#endif
*
*exceptions:
*
*******************************************************************************/
#ifdef cprflag
local(int) output (
#elif wprflag
int _calltype1 _woutput (
file *stream,
#else
int _calltype1 _output (
file *stream,
#endif
const tchar *format,
va_list argptr
)
{
int hexadd; /* offset to add to number to get 'a'..'f' */
tchar ch; /* character just read */
int flags; /* flag word — see #defines above for flag values */
enum state state; /* current state */
enum chartype chclass; /* class of current character */
int radix; /* current conversion radix */
int charsout; /* characters currently written so far, -1 = io error */
int fldwidth; /* selected field width — 0 means default */
int precision; /* selected precision — -1 means default */
tchar prefix[2]; /* numeric prefix — up to two characters */
int prefixlen; /* length of prefix — 0 means no prefix */
int capexp; /* non-zero = 'e' exponent signifient, zero = 'e' */
int no_output; /* non-zero = prodcue no output for this specifier */
union {
char *sz; /* pointer text to be printed, not zero terminated */
wchar_t *wz;
} text;
int textlen; /* length of the text in bytes/wchars to be printed.
textlen is in multibyte or wide chars if wprflag */
union {
char sz[buffersize];
#ifdef wprflag
wchar_t wz[buffersize];
#endif
} buffer;
wchar_t wchar; /* temp wchar_t */
int bufferiswide; /* non-zero = buffer contains wide chars already */
charsout = 0; /* no characters written yet */
state = st_normal; /* starting state */
/* main loop — loop while format character exist and no i/o errors */
while ((ch = *format++) != _t('