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

📄 printf.c

📁 这是一个开源的数据库系统,值得学习啊, 里面用了SQL语句,与微软的SQL SERVIER,差不了多少
💻 C
📖 第 1 页 / 共 3 页
字号:
        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:      case etSQLESCAPE3: {        int i, j, n, ch, isnull;        int needQuote;        char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */        char *escarg = va_arg(ap,char*);        isnull = escarg==0;        if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");        for(i=n=0; (ch=escarg[i])!=0; i++){          if( ch==q )  n++;        }        needQuote = !isnull && xtype==etSQLESCAPE2;        n += i + 1 + needQuote*2;        if( n>etBUFSIZE ){          bufpt = zExtra = sqlite3_malloc( n );          if( bufpt==0 ) return -1;        }else{          bufpt = buf;        }        j = 0;        if( needQuote ) bufpt[j++] = q;        for(i=0; (ch=escarg[i])!=0; i++){          bufpt[j++] = ch;          if( ch==q ) bufpt[j++] = ch;        }        if( needQuote ) bufpt[j++] = q;        bufpt[j] = 0;        length = j;        /* The precision is ignored on %q and %Q */        /* if( precision>=0 && precision<length ) length = precision; */        break;      }      case etTOKEN: {        Token *pToken = va_arg(ap, Token*);        if( pToken && pToken->z ){          (*func)(arg, (char*)pToken->z, pToken->n);        }        length = width = 0;        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;      }    }/* 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 ){      sqlite3_free(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 */  int  iMallocFailed;            /* True if xRealloc() has failed */};/* ** 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;  if( pM->iMallocFailed ) return;  pM->nTotal += nNewChar;  if( pM->zText ){    if( pM->nChar + nNewChar + 1 > pM->nAlloc ){      if( pM->xRealloc==0 ){        nNewChar =  pM->nAlloc - pM->nChar - 1;      }else{        int nAlloc = pM->nChar + nNewChar*2 + 1;        if( pM->zText==pM->zBase ){          pM->zText = pM->xRealloc(0, nAlloc);          if( pM->zText==0 ){            pM->nAlloc = 0;            pM->iMallocFailed = 1;            return;          }else if( pM->nChar ){            memcpy(pM->zText, pM->zBase, pM->nChar);          }        }else{          char *zNew;          zNew = pM->xRealloc(pM->zText, nAlloc);          if( zNew ){            pM->zText = zNew;          }else{            pM->iMallocFailed = 1;            pM->xRealloc(pM->zText, 0);            pM->zText = 0;            pM->nAlloc = 0;            return;          }        }        pM->nAlloc = nAlloc;      }    }    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),  /* realloc() function. 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;  sM.iMallocFailed = 0;  vxprintf(mout, &sM, useInternal, zFormat, ap);  assert(sM.iMallocFailed==0 || sM.zText==0);  if( xRealloc && !sM.iMallocFailed ){    if( sM.zText==sM.zBase ){      sM.zText = xRealloc(0, sM.nChar+1);      if( sM.zText ){        memcpy(sM.zText, sM.zBase, sM.nChar+1);      }    }else if( sM.nAlloc>sM.nChar+10 ){      char *zNew;      sqlite3MallocBenignFailure(1);      zNew = xRealloc(sM.zText, sM.nChar+1);      if( zNew ){        sM.zText = zNew;      }    }  }  return sM.zText;}/*** Realloc that is a real function, not a macro.*/static void *printf_realloc(void *old, int size){  return sqlite3_realloc(old, size);}/*** Print into memory obtained from sqliteMalloc().  Use the internal** %-conversion extensions.*/char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){  char *z;  char zBase[SQLITE_PRINT_BUF_SIZE];  z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);  if( z==0 && db!=0 ){    db->mallocFailed = 1;  }  return z;}/*** Print into memory obtained from sqliteMalloc().  Use the internal** %-conversion extensions.*/char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){  va_list ap;  char *z;  char zBase[SQLITE_PRINT_BUF_SIZE];  va_start(ap, zFormat);  z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);  va_end(ap);  if( z==0 && db!=0 ){    db->mallocFailed = 1;  }  return z;}/*** Print into memory obtained from sqlite3_malloc().  Omit the internal** %-conversion extensions.*/char *sqlite3_vmprintf(const char *zFormat, va_list ap){  char zBase[SQLITE_PRINT_BUF_SIZE];  return base_vprintf(sqlite3_realloc, 0, zBase, sizeof(zBase), zFormat, ap);}/*** Print into memory obtained from sqlite3_malloc()().  Omit the internal** %-conversion extensions.*/char *sqlite3_mprintf(const char *zFormat, ...){  va_list ap;  char *z;  va_start(ap, zFormat);  z = sqlite3_vmprintf(zFormat, ap);  va_end(ap);  return z;}/*** sqlite3_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 *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){  char *z;  va_list ap;  if( n<=0 ){    return zBuf;  }  zBuf[0] = 0;  va_start(ap,zFormat);  z = base_vprintf(0, 0, zBuf, n, zFormat, ap);  va_end(ap);  return z;}#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG)/*** A version of printf() that understands %lld.  Used for debugging.** The printf() built into some versions of windows does not understand %lld** and segfaults if you give it a long long int.*/void sqlite3DebugPrintf(const char *zFormat, ...){  extern int getpid(void);  va_list ap;  char zBuf[500];  va_start(ap, zFormat);  base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);  va_end(ap);  fprintf(stdout,"%s", zBuf);  fflush(stdout);}#endif

⌨️ 快捷键说明

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