📄 printf.c
字号:
if( flag_plussign ) prefix = '+'; else if( flag_blanksign ) prefix = ' '; else prefix = 0; } if( infop->type==etGENERIC && precision>0 ) precision--; rounder = 0.0;#ifdef COMPATIBILITY /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);#else /* It makes more sense to use 0.5 */ for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1);#endif if( infop->type==etFLOAT ) realvalue += rounder; /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ exp = 0; if( realvalue>0.0 ){ int k = 0; while( realvalue>=1e8 && k++<100 ){ realvalue *= 1e-8; exp+=8; } while( realvalue>=10.0 && k++<100 ){ realvalue *= 0.1; exp++; } while( realvalue<1e-8 && k++<100 ){ realvalue *= 1e8; exp-=8; } while( realvalue<1.0 && k++<100 ){ realvalue *= 10.0; exp--; } if( k>=100 ){ bufpt = "NaN"; length = 3; break; } } bufpt = buf; /* ** If the field type is etGENERIC, then convert to either etEXP ** or etFLOAT, as appropriate. */ flag_exp = xtype==etEXP; if( xtype!=etFLOAT ){ realvalue += rounder; if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } } if( xtype==etGENERIC ){ flag_rtz = !flag_alternateform; if( exp<-4 || exp>precision ){ xtype = etEXP; }else{ precision = precision - exp; xtype = etFLOAT; } }else{ flag_rtz = 0; } /* ** The "exp+precision" test causes output to be of type etEXP if ** the precision is too large to fit in buf[]. */ nsd = 0; if( xtype==etFLOAT && exp+precision<etBUFSIZE-30 ){ flag_dp = (precision>0 || flag_alternateform); if( prefix ) *(bufpt++) = prefix; /* Sign */ if( exp<0 ) *(bufpt++) = '0'; /* Digits before "." */ else for(; exp>=0; exp--) *(bufpt++) = et_getdigit(&realvalue,&nsd); if( flag_dp ) *(bufpt++) = '.'; /* The decimal point */ for(exp++; exp<0 && precision>0; precision--, exp++){ *(bufpt++) = '0'; } while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd); *(bufpt--) = 0; /* Null terminate */ if( flag_rtz && flag_dp ){ /* Remove trailing zeros and "." */ while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0; if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0; } bufpt++; /* point to next free slot */ }else{ /* etEXP or etGENERIC */ flag_dp = (precision>0 || flag_alternateform); if( prefix ) *(bufpt++) = prefix; /* Sign */ *(bufpt++) = et_getdigit(&realvalue,&nsd); /* First digit */ if( flag_dp ) *(bufpt++) = '.'; /* Decimal point */ while( (precision--)>0 ) *(bufpt++) = et_getdigit(&realvalue,&nsd); bufpt--; /* point to last digit */ if( flag_rtz && flag_dp ){ /* Remove tail zeros */ while( bufpt>=buf && *bufpt=='0' ) *(bufpt--) = 0; if( bufpt>=buf && *bufpt=='.' ) *(bufpt--) = 0; } bufpt++; /* point to next free slot */ if( exp || flag_exp ){ *(bufpt++) = infop->charset[0]; if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; } /* sign of exp */ else { *(bufpt++) = '+'; } if( exp>=100 ){ *(bufpt++) = (exp/100)+'0'; /* 100's digit */ exp %= 100; } *(bufpt++) = exp/10+'0'; /* 10's digit */ *(bufpt++) = exp%10+'0'; /* 1's digit */ } } /* The converted number is in buf[] and zero terminated. Output it. ** Note that the number is in the usual order, not reversed as with ** integer conversions. */ length = bufpt-buf; bufpt = buf; /* Special case: Add leading zeros if the flag_zeropad flag is ** set and we are not left justified */ if( flag_zeropad && !flag_leftjustify && length < width){ int i; int nPad = width - length; for(i=width; i>=nPad; i--){ bufpt[i] = bufpt[i-nPad]; } i = prefix!=0; while( nPad-- ) bufpt[i++] = '0'; length = width; }#endif break; case etSIZE: *(va_arg(ap,int*)) = count; length = width = 0; break; case etPERCENT: buf[0] = '%'; bufpt = buf; length = 1; break; case etCHARLIT: case etCHARX: c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt); if( precision>=0 ){ for(idx=1; idx<precision; idx++) buf[idx] = c; length = precision; }else{ length =1; } bufpt = buf; break; case etSTRING: case etDYNSTRING: bufpt = va_arg(ap,char*); if( bufpt==0 ){ bufpt = ""; }else if( xtype==etDYNSTRING ){ zExtra = bufpt; } length = strlen(bufpt); if( precision>=0 && precision<length ) length = precision; break; case etSQLESCAPE: case etSQLESCAPE2: { int i, j, n, c, isnull; char *arg = va_arg(ap,char*); isnull = arg==0; if( isnull ) arg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); for(i=n=0; (c=arg[i])!=0; i++){ if( c=='\'' ) n++; } n += i + 1 + ((!isnull && xtype==etSQLESCAPE2) ? 2 : 0); if( n>etBUFSIZE ){ bufpt = zExtra = sqliteMalloc( n ); if( bufpt==0 ) return -1; }else{ bufpt = buf; } j = 0; if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\''; for(i=0; (c=arg[i])!=0; i++){ bufpt[j++] = c; if( c=='\'' ) bufpt[j++] = c; } if( !isnull && xtype==etSQLESCAPE2 ) bufpt[j++] = '\''; bufpt[j] = 0; length = j; if( precision>=0 && precision<length ) length = precision; } break; case etERROR: buf[0] = '%'; buf[1] = c; errorflag = 0; idx = 1+(c!=0); (*func)(arg,"%",idx); count += idx; if( c==0 ) fmt--; break; }/* End switch over the format type */ /* ** The text of the conversion is pointed to by "bufpt" and is ** "length" characters long. The field width is "width". Do ** the output. */ if( !flag_leftjustify ){ register int nspace; nspace = width-length; if( nspace>0 ){ if( flag_center ){ nspace = nspace/2; width -= nspace; flag_leftjustify = 1; } count += nspace; while( nspace>=etSPACESIZE ){ (*func)(arg,spaces,etSPACESIZE); nspace -= etSPACESIZE; } if( nspace>0 ) (*func)(arg,spaces,nspace); } } if( length>0 ){ (*func)(arg,bufpt,length); count += length; } if( flag_leftjustify ){ register int nspace; nspace = width-length; if( nspace>0 ){ count += nspace; while( nspace>=etSPACESIZE ){ (*func)(arg,spaces,etSPACESIZE); nspace -= etSPACESIZE; } if( nspace>0 ) (*func)(arg,spaces,nspace); } } if( zExtra ){ if( xtype==etDYNSTRING ){ free(zExtra); }else{ sqliteFree(zExtra); } } }/* End for loop over the format string */ return errorflag ? -1 : count;} /* End of function *//* This structure is used to store state information about the** write to memory that is currently in progress.*/struct sgMprintf { char *zBase; /* A base allocation */ char *zText; /* The string collected so far */ int nChar; /* Length of the string so far */ int nAlloc; /* Amount of space allocated in zText */};/* ** This function implements the callback from vxprintf. **** This routine add nNewChar characters of text in zNewText to** the sgMprintf structure pointed to by "arg".*/static void mout(void *arg, char *zNewText, int nNewChar){ struct sgMprintf *pM = (struct sgMprintf*)arg; if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ pM->nAlloc = pM->nChar + nNewChar*2 + 1; if( pM->zText==pM->zBase ){ pM->zText = sqliteMalloc(pM->nAlloc); if( pM->zText && pM->nChar ) memcpy(pM->zText,pM->zBase,pM->nChar); }else{ char *z = sqliteRealloc(pM->zText, pM->nAlloc); if( z==0 ){ sqliteFree(pM->zText); pM->nChar = 0; pM->nAlloc = 0; } pM->zText = z; } } if( pM->zText ){ memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); pM->nChar += nNewChar; pM->zText[pM->nChar] = 0; }}/*** sqlite_mprintf() works like printf(), but allocations memory to hold the** resulting string and returns a pointer to the allocated memory. Use** sqliteFree() to release the memory allocated.*/char *sqliteMPrintf(const char *zFormat, ...){ va_list ap; struct sgMprintf sMprintf; char zBuf[200]; sMprintf.nChar = 0; sMprintf.nAlloc = sizeof(zBuf); sMprintf.zText = zBuf; sMprintf.zBase = zBuf; va_start(ap,zFormat); vxprintf(mout,&sMprintf,zFormat,ap); va_end(ap); sMprintf.zText[sMprintf.nChar] = 0; return sqliteRealloc(sMprintf.zText, sMprintf.nChar+1);}/*** sqlite_mprintf() works like printf(), but allocations memory to hold the** resulting string and returns a pointer to the allocated memory. Use** sqliteFree() to release the memory allocated.*/char *sqlite_mprintf(const char *zFormat, ...){ va_list ap; struct sgMprintf sMprintf; char *zNew; char zBuf[200]; sMprintf.nChar = 0; sMprintf.nAlloc = sizeof(zBuf); sMprintf.zText = zBuf; sMprintf.zBase = zBuf; va_start(ap,zFormat); vxprintf(mout,&sMprintf,zFormat,ap); va_end(ap); sMprintf.zText[sMprintf.nChar] = 0; zNew = malloc( sMprintf.nChar+1 ); if( zNew ) strcpy(zNew,sMprintf.zText); if( sMprintf.zText!=sMprintf.zBase ){ sqliteFree(sMprintf.zText); } return zNew;}/* This is the varargs version of sqlite_mprintf. */char *sqlite_vmprintf(const char *zFormat, va_list ap){ struct sgMprintf sMprintf; char *zNew; char zBuf[200]; sMprintf.nChar = 0; sMprintf.zText = zBuf; sMprintf.nAlloc = sizeof(zBuf); sMprintf.zBase = zBuf; vxprintf(mout,&sMprintf,zFormat,ap); sMprintf.zText[sMprintf.nChar] = 0; zNew = malloc( sMprintf.nChar+1 ); if( zNew ) strcpy(zNew,sMprintf.zText); if( sMprintf.zText!=sMprintf.zBase ){ sqliteFree(sMprintf.zText); } return zNew;}/*** The following four routines implement the varargs versions of the** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h** header files for a more detailed description of how these interfaces** work.**** These routines are all just simple wrappers.*/int sqlite_exec_printf( sqlite *db, /* An open database */ const char *sqlFormat, /* printf-style format string for the SQL */ sqlite_callback xCallback, /* Callback function */ void *pArg, /* 1st argument to callback function */ char **errmsg, /* Error msg written here */ ... /* Arguments to the format string. */){ va_list ap; int rc; va_start(ap, errmsg); rc = sqlite_exec_vprintf(db, sqlFormat, xCallback, pArg, errmsg, ap); va_end(ap); return rc;}int sqlite_exec_vprintf( sqlite *db, /* An open database */ const char *sqlFormat, /* printf-style format string for the SQL */ sqlite_callback xCallback, /* Callback function */ void *pArg, /* 1st argument to callback function */ char **errmsg, /* Error msg written here */ va_list ap /* Arguments to the format string. */){ char *zSql; int rc; zSql = sqlite_vmprintf(sqlFormat, ap); rc = sqlite_exec(db, zSql, xCallback, pArg, errmsg); free(zSql); return rc;}int sqlite_get_table_printf( sqlite *db, /* An open database */ const char *sqlFormat, /* printf-style format string for the SQL */ char ***resultp, /* Result written to a char *[] that this points to */ int *nrow, /* Number of result rows written here */ int *ncol, /* Number of result columns written here */ char **errmsg, /* Error msg written here */ ... /* Arguments to the format string */){ va_list ap; int rc; va_start(ap, errmsg); rc = sqlite_get_table_vprintf(db, sqlFormat, resultp, nrow, ncol, errmsg, ap); va_end(ap); return rc;}int sqlite_get_table_vprintf( sqlite *db, /* An open database */ const char *sqlFormat, /* printf-style format string for the SQL */ char ***resultp, /* Result written to a char *[] that this points to */ int *nrow, /* Number of result rows written here */ int *ncolumn, /* Number of result columns written here */ char **errmsg, /* Error msg written here */ va_list ap /* Arguments to the format string */){ char *zSql; int rc; zSql = sqlite_vmprintf(sqlFormat, ap); rc = sqlite_get_table(db, zSql, resultp, nrow, ncolumn, errmsg); free(zSql); return rc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -