⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printf.c

📁 SQLite 2.8.6 源代码,用来在Linux/Unix/Windows上编译安装.它是一个小型的数据库,但是非常好用,速度也快,一般的数据库查询之类的操作据统计比MySQL,PostgreSQL
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** 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 slightly out-of-date.**** 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"/*** Undefine COMPATIBILITY to make some slight changes in the way things** work.  I think the changes are an improvement, but they are not** backwards compatible.*//* #define COMPATIBILITY       / * Compatible with SUN OS 4.1 *//*** Conversion types fall into various categories as defined by the** following enumeration.*/enum et_type {    /* The type of the format field */   etRADIX,            /* Integer types.  %d, %x, %o, and so forth */   etFLOAT,            /* Floating point.  %f */   etEXP,              /* Exponentional notation. %e and %E */   etGENERIC,          /* Floating or exponential, depending on exponent. %g */   etSIZE,             /* Return number of characters processed so far. %n */   etSTRING,           /* Strings. %s */   etDYNSTRING,        /* Dynamically allocated strings. %z */   etPERCENT,          /* Percent symbol. %% */   etCHARX,            /* Characters. %c */   etERROR,            /* Used to indicate no such conversion type *//* The rest are extensions, not normally found in printf() */   etCHARLIT,          /* Literal characters.  %' */   etSQLESCAPE,        /* Strings with '\'' doubled.  %q */   etSQLESCAPE2,       /* Strings with '\'' doubled and enclosed in '',                          NULL pointers replaced by SQL NULL.  %Q */   etORDINAL           /* 1st, 2nd, 3rd and so forth */};/*** 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 */  int  fmttype;              /* The format field code letter */  int  base;                 /* The base for radix conversion */  char *charset;             /* The character set for conversion */  int  flag_signed;          /* Is the quantity signed? */  char *prefix;              /* Prefix on non-zero values in alt format */  enum et_type type;          /* Conversion paradigm */} et_info;/*** 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,  "0123456789",       1,    0, etRADIX,      },  { 's',   0,  0,                  0,    0, etSTRING,     },   { 'z',   0,  0,                  0,    0, etDYNSTRING,  },   { 'q',   0,  0,                  0,    0, etSQLESCAPE,  },  { 'Q',   0,  0,                  0,    0, etSQLESCAPE2, },  { 'c',   0,  0,                  0,    0, etCHARX,      },  { 'o',   8,  "01234567",         0,  "0", etRADIX,      },  { 'u',  10,  "0123456789",       0,    0, etRADIX,      },  { 'x',  16,  "0123456789abcdef", 0, "x0", etRADIX,      },  { 'X',  16,  "0123456789ABCDEF", 0, "X0", etRADIX,      },  { 'r',  10,  "0123456789",       0,    0, etORDINAL,    },  { 'f',   0,  0,                  1,    0, etFLOAT,      },  { 'e',   0,  "e",                1,    0, etEXP,        },  { 'E',   0,  "E",                1,    0, etEXP,        },  { 'g',   0,  "e",                1,    0, etGENERIC,    },  { 'G',   0,  "E",                1,    0, etGENERIC,    },  { 'i',  10,  "0123456789",       1,    0, etRADIX,      },  { 'n',   0,  0,                  0,    0, etSIZE,       },  { '%',   0,  0,                  0,    0, etPERCENT,    },  { 'b',   2,  "01",               0, "b0", etRADIX,      }, /* Binary */  { 'p',  10,  "0123456789",       0,    0, etRADIX,      }, /* Pointers */  { '\'',  0,  0,                  0,    0, etCHARLIT,    }, /* Literal char */};#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(double *val, int *cnt){  int digit;  double 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*,char*,int),  void *arg,  const char *format,  va_list ap){  register const char *fmt; /* The format string. */  register int c;           /* Next character in the format string */  register char *bufpt;     /* Pointer to the conversion buffer */  register int  precision;  /* Precision of the current field */  register int  length;     /* Length of the field */  register int  idx;        /* A general purpose loop counter */  int count;                /* Total number of characters output */  int width;                /* Width of the current field */  int flag_leftjustify;     /* True if "-" flag is present */  int flag_plussign;        /* True if "+" flag is present */  int flag_blanksign;       /* True if " " flag is present */  int flag_alternateform;   /* True if "#" flag is present */  int flag_zeropad;         /* True if field width constant starts with zero */  int flag_long;            /* True if "l" flag is present */  int flag_center;          /* True if "=" flag is present */  unsigned long longvalue;  /* Value for integer types */  double 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'. */  int  errorflag = 0;       /* True if an error is encountered */  enum et_type 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 */  int flag_dp;              /* True if decimal point should be shown */  int flag_rtz;             /* True if trailing zeros should be removed */  int flag_exp;             /* True to force display of the exponent */  int nsd;                  /* Number of significant digits returned */#endif  fmt = format;                     /* Put in a register for speed */  count = length = 0;  bufpt = 0;  for(; (c=(*fmt))!=0; ++fmt){    if( c!='%' ){      register 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 = flag_center = 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;        case '=':   flag_center = 1;          c = 0;   break;        default:                                       break;      }    }while( c==0 && (c=(*++fmt))!=0 );    if( flag_center ) flag_leftjustify = 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);#ifndef etCOMPATIBILITY        /* This is sensible, but SUN OS 4.1 doesn't do it. */        if( precision<0 ) precision = -precision;#endif        c = *++fmt;      }else{        while( c>='0' && c<='9' ){          precision = precision*10 + c - '0';          c = *++fmt;        }      }      /* Limit the precision to prevent overflowing buf[] during conversion */      if( precision>etBUFSIZE-40 ) precision = etBUFSIZE-40;    }else{      precision = -1;    }    /* Get the conversion type modifier */    if( c=='l' ){      flag_long = 1;      c = *++fmt;    }else{      flag_long = 0;    }    /* Fetch the info entry for the field */    infop = 0;    for(idx=0; idx<etNINFO; idx++){      if( c==fmtinfo[idx].fmttype ){        infop = &fmtinfo[idx];        break;      }    }    /* No info entry found.  It must be an error. */    if( infop==0 ){      xtype = etERROR;    }else{      xtype = infop->type;    }    zExtra = 0;    /*    ** At this point, variables are initialized as follows:    **    **   flag_alternateform          TRUE if a '#' is present.    **   flag_plussign               TRUE if a '+' is present.    **   flag_leftjustify            TRUE if a '-' is present or if the    **                               field width was negative.    **   flag_zeropad                TRUE if the width began with 0.    **   flag_long                   TRUE if the letter 'l' (ell) prefixed    **                               the conversion character.    **   flag_blanksign              TRUE if a ' ' is present.    **   width                       The specified field width.  This is    **                               always non-negative.  Zero is the default.    **   precision                   The specified precision.  The default    **                               is -1.    **   xtype                       The class of the conversion.    **   infop                       Pointer to the appropriate info struct.    */    switch( xtype ){      case etORDINAL:      case etRADIX:        if( flag_long )  longvalue = va_arg(ap,long);        else             longvalue = va_arg(ap,int);#ifdef etCOMPATIBILITY        /* For the format %#x, the value zero is printed "0" not "0x0".        ** I think this is stupid. */        if( longvalue==0 ) flag_alternateform = 0;#else        /* More sensible: turn off the prefix for octal (to prevent "00"),        ** but leave the prefix for hex. */        if( longvalue==0 && infop->base==8 ) flag_alternateform = 0;#endif        if( infop->flag_signed ){          if( *(long*)&longvalue<0 ){            longvalue = -*(long*)&longvalue;            prefix = '-';          }else if( flag_plussign )  prefix = '+';          else if( flag_blanksign )  prefix = ' ';          else                       prefix = 0;        }else                        prefix = 0;        if( flag_zeropad && precision<width-(prefix!=0) ){          precision = width-(prefix!=0);        }        bufpt = &buf[etBUFSIZE];        if( xtype==etORDINAL ){          long a,b;          a = longvalue%10;          b = longvalue%100;          bufpt -= 2;          if( a==0 || a>3 || (b>10 && b<14) ){            bufpt[0] = 't';            bufpt[1] = 'h';          }else if( a==1 ){            bufpt[0] = 's';            bufpt[1] = 't';          }else if( a==2 ){            bufpt[0] = 'n';            bufpt[1] = 'd';          }else if( a==3 ){            bufpt[0] = 'r';            bufpt[1] = 'd';          }        }        {          register char *cset;      /* Use registers for speed */          register int base;          cset = infop->charset;          base = infop->base;          do{                                           /* Convert to ascii */            *(--bufpt) = cset[longvalue%base];            longvalue = longvalue/base;          }while( longvalue>0 );        }        length = &buf[etBUFSIZE]-bufpt;        for(idx=precision-length; idx>0; idx--){          *(--bufpt) = '0';                             /* Zero pad */        }        if( prefix ) *(--bufpt) = prefix;               /* Add sign */        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */          char *pre, x;          pre = infop->prefix;          if( *bufpt!=pre[0] ){            for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x;          }        }        length = &buf[etBUFSIZE]-bufpt;        break;      case etFLOAT:      case etEXP:      case etGENERIC:        realvalue = va_arg(ap,double);#ifndef etNOFLOATINGPOINT        if( precision<0 ) precision = 6;         /* Set default precision */        if( precision>etBUFSIZE-10 ) precision = etBUFSIZE-10;        if( realvalue<0.0 ){          realvalue = -realvalue;          prefix = '-';        }else{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -