📄 printf.c
字号:
/*** The "printf" code that follows dates from the 1980's. It is in** the public domain. The original comments are included here for** completeness. They are very out-of-date but might be useful as** an historical reference. Most of the "enhancements" have been backed** out so that the functionality is now the same as standard printf().******************************************************************************** The following modules is an enhanced replacement for the "printf" subroutines** found in the standard C library. The following enhancements are** supported:**** + Additional functions. The standard set of "printf" functions** includes printf, fprintf, sprintf, vprintf, vfprintf, and** vsprintf. This module adds the following:**** * snprintf -- Works like sprintf, but has an extra argument** which is the size of the buffer written to.**** * mprintf -- Similar to sprintf. Writes output to memory** obtained from malloc.**** * xprintf -- Calls a function to dispose of output.**** * nprintf -- No output, but returns the number of characters** that would have been output by printf.**** * A v- version (ex: vsnprintf) of every function is also** supplied.**** + A few extensions to the formatting notation are supported:**** * The "=" flag (similar to "-") causes the output to be** be centered in the appropriately sized field.**** * The %b field outputs an integer in binary notation.**** * The %c field now accepts a precision. The character output** is repeated by the number of times the precision specifies.**** * The %' field works like %c, but takes as its character the** next character of the format string, instead of the next** argument. For example, printf("%.78'-") prints 78 minus** signs, the same as printf("%.78c",'-').**** + When compiled using GCC on a SPARC, this version of printf is** faster than the library printf for SUN OS 4.1.**** + All functions are fully reentrant.***/#include "sqliteInt.h"/*** Conversion types fall into various categories as defined by the** following enumeration.*/#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */#define etFLOAT 2 /* Floating point. %f */#define etEXP 3 /* Exponentional notation. %e and %E */#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */#define etSIZE 5 /* Return number of characters processed so far. %n */#define etSTRING 6 /* Strings. %s */#define etDYNSTRING 7 /* Dynamically allocated strings. %z */#define etPERCENT 8 /* Percent symbol. %% */#define etCHARX 9 /* Characters. %c */#define etERROR 10 /* Used to indicate no such conversion type *//* The rest are extensions, not normally found in printf() */#define etCHARLIT 11 /* Literal characters. %' */#define etSQLESCAPE 12 /* Strings with '\'' doubled. %q */#define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '', NULL pointers replaced by SQL NULL. %Q */#define etTOKEN 14 /* a pointer to a Token structure */#define etSRCLIST 15 /* a pointer to a SrcList *//*** An "etByte" is an 8-bit unsigned value.*/typedef unsigned char etByte;/*** Each builtin conversion character (ex: the 'd' in "%d") is described** by an instance of the following structure*/typedef struct et_info { /* Information about each format field */ char fmttype; /* The format field code letter */ etByte base; /* The base for radix conversion */ etByte flags; /* One or more of FLAG_ constants below */ etByte type; /* Conversion paradigm */ char *charset; /* The character set for conversion */ char *prefix; /* Prefix on non-zero values in alt format */} et_info;/*** Allowed values for et_info.flags*/#define FLAG_SIGNED 1 /* True if the value to convert is signed */#define FLAG_INTERN 2 /* True if for internal use only *//*** The following table is searched linearly, so it is good to put the** most frequently used conversion types first.*/static et_info fmtinfo[] = { { 'd', 10, 1, etRADIX, "0123456789", 0 }, { 's', 0, 0, etSTRING, 0, 0 }, { 'z', 0, 2, etDYNSTRING, 0, 0 }, { 'q', 0, 0, etSQLESCAPE, 0, 0 }, { 'Q', 0, 0, etSQLESCAPE2, 0, 0 }, { 'c', 0, 0, etCHARX, 0, 0 }, { 'o', 8, 0, etRADIX, "01234567", "0" }, { 'u', 10, 0, etRADIX, "0123456789", 0 }, { 'x', 16, 0, etRADIX, "0123456789abcdef", "x0" }, { 'X', 16, 0, etRADIX, "0123456789ABCDEF", "X0" }, { 'f', 0, 1, etFLOAT, 0, 0 }, { 'e', 0, 1, etEXP, "e", 0 }, { 'E', 0, 1, etEXP, "E", 0 }, { 'g', 0, 1, etGENERIC, "e", 0 }, { 'G', 0, 1, etGENERIC, "E", 0 }, { 'i', 10, 1, etRADIX, "0123456789", 0 }, { 'n', 0, 0, etSIZE, 0, 0 }, { '%', 0, 0, etPERCENT, 0, 0 }, { 'p', 10, 0, etRADIX, "0123456789", 0 }, { 'T', 0, 2, etTOKEN, 0, 0 }, { 'S', 0, 2, etSRCLIST, 0, 0 },};#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))/*** If NOFLOATINGPOINT is defined, then none of the floating point** conversions will work.*/#ifndef etNOFLOATINGPOINT/*** "*val" is a double such that 0.1 <= *val < 10.0** Return the ascii code for the leading digit of *val, then** multiply "*val" by 10.0 to renormalize.**** Example:** input: *val = 3.14159** output: *val = 1.4159 function return = '3'**** The counter *cnt is incremented each time. After counter exceeds** 16 (the number of significant digits in a 64-bit float) '0' is** always returned.*/static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ int digit; LONGDOUBLE_TYPE d; if( (*cnt)++ >= 16 ) return '0'; digit = (int)*val; d = digit; digit += '0'; *val = (*val - d)*10.0; return digit;}#endif#define etBUFSIZE 1000 /* Size of the output buffer *//*** The root program. All variations call this core.**** INPUTS:** func This is a pointer to a function taking three arguments** 1. A pointer to anything. Same as the "arg" parameter.** 2. A pointer to the list of characters to be output** (Note, this list is NOT null terminated.)** 3. An integer number of characters to be output.** (Note: This number might be zero.)**** arg This is the pointer to anything which will be passed as the** first argument to "func". Use it for whatever you like.**** fmt This is the format string, as in the usual print.**** ap This is a pointer to a list of arguments. Same as in** vfprint.**** OUTPUTS:** The return value is the total number of characters sent to** the function "func". Returns -1 on a error.**** Note that the order in which automatic variables are declared below** seems to make a big difference in determining how fast this beast** will run.*/static int vxprintf( void (*func)(void*,const char*,int), /* Consumer of text */ void *arg, /* First argument to the consumer */ int useExtended, /* Allow extended %-conversions */ const char *fmt, /* Format string */ va_list ap /* arguments */){ int c; /* Next character in the format string */ char *bufpt; /* Pointer to the conversion buffer */ int precision; /* Precision of the current field */ int length; /* Length of the field */ int idx; /* A general purpose loop counter */ int count; /* Total number of characters output */ int width; /* Width of the current field */ etByte flag_leftjustify; /* True if "-" flag is present */ etByte flag_plussign; /* True if "+" flag is present */ etByte flag_blanksign; /* True if " " flag is present */ etByte flag_alternateform; /* True if "#" flag is present */ etByte flag_zeropad; /* True if field width constant starts with zero */ etByte flag_long; /* True if "l" flag is present */ unsigned long longvalue; /* Value for integer types */ LONGDOUBLE_TYPE realvalue; /* Value for real types */ et_info *infop; /* Pointer to the appropriate info structure */ char buf[etBUFSIZE]; /* Conversion buffer */ char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ etByte errorflag = 0; /* True if an error is encountered */ etByte xtype; /* Conversion paradigm */ char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ static char spaces[] = " ";#define etSPACESIZE (sizeof(spaces)-1)#ifndef etNOFLOATINGPOINT int exp; /* exponent of real numbers */ double rounder; /* Used for rounding floating point values */ etByte flag_dp; /* True if decimal point should be shown */ etByte flag_rtz; /* True if trailing zeros should be removed */ etByte flag_exp; /* True to force display of the exponent */ int nsd; /* Number of significant digits returned */#endif func(arg,"",0); count = length = 0; bufpt = 0; for(; (c=(*fmt))!=0; ++fmt){ if( c!='%' ){ int amt; bufpt = (char *)fmt; amt = 1; while( (c=(*++fmt))!='%' && c!=0 ) amt++; (*func)(arg,bufpt,amt); count += amt; if( c==0 ) break; } if( (c=(*++fmt))==0 ){ errorflag = 1; (*func)(arg,"%",1); count++; break; } /* Find out what flags are present */ flag_leftjustify = flag_plussign = flag_blanksign = flag_alternateform = flag_zeropad = 0; do{ switch( c ){ case '-': flag_leftjustify = 1; c = 0; break; case '+': flag_plussign = 1; c = 0; break; case ' ': flag_blanksign = 1; c = 0; break; case '#': flag_alternateform = 1; c = 0; break; case '0': flag_zeropad = 1; c = 0; break; default: break; } }while( c==0 && (c=(*++fmt))!=0 ); /* Get the field width */ width = 0; if( c=='*' ){ width = va_arg(ap,int); if( width<0 ){ flag_leftjustify = 1; width = -width; } c = *++fmt; }else{ while( c>='0' && c<='9' ){ width = width*10 + c - '0'; c = *++fmt; } } if( width > etBUFSIZE-10 ){ width = etBUFSIZE-10; } /* Get the precision */ if( c=='.' ){ precision = 0; c = *++fmt; if( c=='*' ){ precision = va_arg(ap,int); if( precision<0 ) precision = -precision; c = *++fmt;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -