📄 printf.c
字号:
break; } case etSRCLIST: { SrcList *pSrc = va_arg(ap, SrcList*); int k = va_arg(ap, int); struct SrcList_item *pItem = &pSrc->a[k]; assert( k>=0 && k<pSrc->nSrc ); if( pItem->zDatabase && pItem->zDatabase[0] ){ (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase)); (*func)(arg, ".", 1); } (*func)(arg, pItem->zName, strlen(pItem->zName)); length = width = 0; 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 ){ 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 ){ 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 nTotal; /* Output size if unconstrained */ int nAlloc; /* Amount of space allocated in zText */ void *(*xRealloc)(void*,int); /* Function used to realloc memory */};/* ** 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, const char *zNewText, int nNewChar){ struct sgMprintf *pM = (struct sgMprintf*)arg; pM->nTotal += nNewChar; if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ if( pM->xRealloc==0 ){ nNewChar = pM->nAlloc - pM->nChar - 1; }else{ pM->nAlloc = pM->nChar + nNewChar*2 + 1; if( pM->zText==pM->zBase ){ pM->zText = pM->xRealloc(0, pM->nAlloc); if( pM->zText && pM->nChar ){ memcpy(pM->zText, pM->zBase, pM->nChar); } }else{ pM->zText = pM->xRealloc(pM->zText, pM->nAlloc); } } } if( pM->zText ){ if( nNewChar>0 ){ memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); pM->nChar += nNewChar; } pM->zText[pM->nChar] = 0; }}/*** This routine is a wrapper around xprintf() that invokes mout() as** the consumer. */static char *base_vprintf( void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */ int useInternal, /* Use internal %-conversions if true */ char *zInitBuf, /* Initially write here, before mallocing */ int nInitBuf, /* Size of zInitBuf[] */ const char *zFormat, /* format string */ va_list ap /* arguments */){ struct sgMprintf sM; sM.zBase = sM.zText = zInitBuf; sM.nChar = sM.nTotal = 0; sM.nAlloc = nInitBuf; sM.xRealloc = xRealloc; vxprintf(mout, &sM, useInternal, zFormat, ap); if( xRealloc ){ if( sM.zText==sM.zBase ){ sM.zText = xRealloc(0, sM.nChar+1); memcpy(sM.zText, sM.zBase, sM.nChar+1); }else if( sM.nAlloc>sM.nChar+10 ){ sM.zText = xRealloc(sM.zText, sM.nChar+1); } } return sM.zText;}/*** Realloc that is a real function, not a macro.*/static void *printf_realloc(void *old, int size){ return sqliteRealloc(old,size);}/*** Print into memory obtained from sqliteMalloc(). Use the internal** %-conversion extensions.*/char *sqliteVMPrintf(const char *zFormat, va_list ap){ char zBase[1000]; return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);}/*** Print into memory obtained from sqliteMalloc(). Use the internal** %-conversion extensions.*/char *sqliteMPrintf(const char *zFormat, ...){ va_list ap; char *z; char zBase[1000]; va_start(ap, zFormat); z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); va_end(ap); return z;}/*** Print into memory obtained from malloc(). Do not use the internal** %-conversion extensions. This routine is for use by external users.*/char *sqlite_mprintf(const char *zFormat, ...){ va_list ap; char *z; char zBuf[200]; va_start(ap,zFormat); z = base_vprintf((void*(*)(void*,int))realloc, 0, zBuf, sizeof(zBuf), zFormat, ap); va_end(ap); return z;}/* This is the varargs version of sqlite_mprintf. */char *sqlite_vmprintf(const char *zFormat, va_list ap){ char zBuf[200]; return base_vprintf((void*(*)(void*,int))realloc, 0, zBuf, sizeof(zBuf), zFormat, ap);}/*** sqlite_snprintf() works like snprintf() except that it ignores the** current locale settings. This is important for SQLite because we** are not able to use a "," as the decimal point in place of "." as** specified by some locales.*/char *sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){ char *z; va_list ap; va_start(ap,zFormat); z = base_vprintf(0, 0, zBuf, n, zFormat, ap); va_end(ap); return z;}/*** 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 + -