Back to index

php5  5.3.10
Defines | Functions | Variables
util.c File Reference
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

Go to the source code of this file.

Defines

#define _64e3   (64.0 * 64.0 * 64.0)
#define _64e4   (64.0 * 64.0 * 64.0 * 64.0)
#define _64e15   (_64e3 * _64e4 * _64e4 * _64e4)
#define _64e16   (_64e4 * _64e4 * _64e4 * _64e4)
#define _64e63   (_64e15 * _64e16 * _64e16 * _64e16)
#define _64e64   (_64e16 * _64e16 * _64e16 * _64e16)
#define sqliteNextChar(X)   (++(X));
#define sqliteCharVal(X)   ((int)*(X))

Functions

void * sqliteMalloc (int n)
void * sqliteMallocRaw (int n)
void sqliteFree (void *p)
void * sqliteRealloc (void *p, int n)
char * sqliteStrDup (const char *z)
char * sqliteStrNDup (const char *z, int n)
void sqliteSetString (char **pz,...)
void sqliteSetNString (char **pz,...)
void sqliteErrorMsg (Parse *pParse, const char *zFormat,...)
void sqliteDequote (char *z)
int sqliteHashNoCase (const char *z, int n)
int sqliteStrICmp (const char *zLeft, const char *zRight)
int sqliteStrNICmp (const char *zLeft, const char *zRight, int N)
int sqliteIsNumber (const char *z)
double sqliteAtoF (const char *z, const char **pzEnd)
int sqliteFitsIn32Bits (const char *zNum)
int sqliteCompare (const char *atext, const char *btext)
int sqliteSortCompare (const char *a, const char *b)
void sqliteRealToSortable (double r, char *z)
int sqliteGlobCompare (const unsigned char *zPattern, const unsigned char *zString)
int sqliteLikeCompare (const unsigned char *zPattern, const unsigned char *zString)
int sqliteSafetyOn (sqlite *db)
int sqliteSafetyOff (sqlite *db)
int sqliteSafetyCheck (sqlite *db)

Variables

int sqlite_malloc_failed = 0
static unsigned char UpperToLower []

Define Documentation

#define _64e15   (_64e3 * _64e4 * _64e4 * _64e4)

Definition at line 777 of file util.c.

#define _64e16   (_64e4 * _64e4 * _64e4 * _64e4)

Definition at line 778 of file util.c.

#define _64e3   (64.0 * 64.0 * 64.0)

Definition at line 775 of file util.c.

#define _64e4   (64.0 * 64.0 * 64.0 * 64.0)

Definition at line 776 of file util.c.

#define _64e63   (_64e15 * _64e16 * _64e16 * _64e16)

Definition at line 779 of file util.c.

#define _64e64   (_64e16 * _64e16 * _64e16 * _64e16)

Definition at line 780 of file util.c.

#define sqliteCharVal (   X)    ((int)*(X))

Definition at line 872 of file util.c.

#define sqliteNextChar (   X)    (++(X));

Definition at line 871 of file util.c.


Function Documentation

double sqliteAtoF ( const char *  z,
const char **  pzEnd 
)

Definition at line 555 of file util.c.

                                                    {
  int sign = 1;
  LONGDOUBLE_TYPE v1 = 0.0;
  if( *z=='-' ){
    sign = -1;
    z++;
  }else if( *z=='+' ){
    z++;
  }
  while( isdigit(*z) ){
    v1 = v1*10.0 + (*z - '0');
    z++;
  }
  if( *z=='.' ){
    LONGDOUBLE_TYPE divisor = 1.0;
    z++;
    while( isdigit(*z) ){
      v1 = v1*10.0 + (*z - '0');
      divisor *= 10.0;
      z++;
    }
    v1 /= divisor;
  }
  if( *z=='e' || *z=='E' ){
    int esign = 1;
    int eval = 0;
    LONGDOUBLE_TYPE scale = 1.0;
    z++;
    if( *z=='-' ){
      esign = -1;
      z++;
    }else if( *z=='+' ){
      z++;
    }
    while( isdigit(*z) ){
      eval = eval*10 + *z - '0';
      z++;
    }
    while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
    while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
    while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
    while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
    if( esign<0 ){
      v1 /= scale;
    }else{
      v1 *= scale;
    }
  }
  if( pzEnd ) *pzEnd = z;
  return sign<0 ? -v1 : v1;
}

Here is the caller graph for this function:

int sqliteCompare ( const char *  atext,
const char *  btext 
)

Definition at line 638 of file util.c.

                                                       {
  int result;
  int isNumA, isNumB;
  if( atext==0 ){
    return -1;
  }else if( btext==0 ){
    return 1;
  }
  isNumA = sqliteIsNumber(atext);
  isNumB = sqliteIsNumber(btext);
  if( isNumA ){
    if( !isNumB ){
      result = -1;
    }else{
      double rA, rB;
      rA = sqliteAtoF(atext, 0);
      rB = sqliteAtoF(btext, 0);
      if( rA<rB ){
        result = -1;
      }else if( rA>rB ){
        result = +1;
      }else{
        result = 0;
      }
    }
  }else if( isNumB ){
    result = +1;
  }else {
    result = strcmp(atext, btext);
  }
  return result; 
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqliteDequote ( char *  z)

Definition at line 436 of file util.c.

                           {
  int quote;
  int i, j;
  if( z==0 ) return;
  quote = z[0];
  switch( quote ){
    case '\'':  break;
    case '"':   break;
    case '[':   quote = ']';  break;
    default:    return;
  }
  for(i=1, j=0; z[i]; i++){
    if( z[i]==quote ){
      if( z[i+1]==quote ){
        z[j++] = quote;
        i++;
      }else{
        z[j++] = 0;
        break;
      }
    }else{
      z[j++] = z[i];
    }
  }
}

Here is the caller graph for this function:

void sqliteErrorMsg ( Parse pParse,
const char *  zFormat,
  ... 
)

Definition at line 417 of file util.c.

                                                            {
  va_list ap;
  pParse->nErr++;
  sqliteFree(pParse->zErrMsg);
  va_start(ap, zFormat);
  pParse->zErrMsg = sqliteVMPrintf(zFormat, ap);
  va_end(ap);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqliteFitsIn32Bits ( const char *  zNum)

Definition at line 618 of file util.c.

                                        {
  int i, c;
  if( *zNum=='-' || *zNum=='+' ) zNum++;
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
  return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
}

Here is the caller graph for this function:

void sqliteFree ( void *  p)

Definition at line 277 of file util.c.

                        {
  if( p ){
    free(p);
  }
}
int sqliteGlobCompare ( const unsigned char *  zPattern,
const unsigned char *  zString 
)

Definition at line 942 of file util.c.

                                                                              {
  register int c;
  int invert;
  int seen;
  int c2;

  while( (c = *zPattern)!=0 ){
    switch( c ){
      case '*':
        while( (c=zPattern[1]) == '*' || c == '?' ){
          if( c=='?' ){
            if( *zString==0 ) return 0;
            sqliteNextChar(zString);
          }
          zPattern++;
        }
        if( c==0 ) return 1;
        if( c=='[' ){
          while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
            sqliteNextChar(zString);
          }
          return *zString!=0;
        }else{
          while( (c2 = *zString)!=0 ){
            while( c2 != 0 && c2 != c ){ c2 = *++zString; }
            if( c2==0 ) return 0;
            if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
            sqliteNextChar(zString);
          }
          return 0;
        }
      case '?': {
        if( *zString==0 ) return 0;
        sqliteNextChar(zString);
        zPattern++;
        break;
      }
      case '[': {
        int prior_c = 0;
        seen = 0;
        invert = 0;
        c = sqliteCharVal(zString);
        if( c==0 ) return 0;
        c2 = *++zPattern;
        if( c2=='^' ){ invert = 1; c2 = *++zPattern; }
        if( c2==']' ){
          if( c==']' ) seen = 1;
          c2 = *++zPattern;
        }
        while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){
          if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){
            zPattern++;
            c2 = sqliteCharVal(zPattern);
            if( c>=prior_c && c<=c2 ) seen = 1;
            prior_c = 0;
          }else if( c==c2 ){
            seen = 1;
            prior_c = c2;
          }else{
            prior_c = c2;
          }
          sqliteNextChar(zPattern);
        }
        if( c2==0 || (seen ^ invert)==0 ) return 0;
        sqliteNextChar(zString);
        zPattern++;
        break;
      }
      default: {
        if( c != *zString ) return 0;
        zPattern++;
        zString++;
        break;
      }
    }
  }
  return *zString==0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqliteHashNoCase ( const char *  z,
int  n 
)

Definition at line 487 of file util.c.

                                          {
  int h = 0;
  if( n<=0 ) n = strlen(z);
  while( n > 0  ){
    h = (h<<3) ^ h ^ UpperToLower[(unsigned char)*z++];
    n--;
  }
  return h & 0x7fffffff;
}

Here is the caller graph for this function:

int sqliteIsNumber ( const char *  z)

Definition at line 522 of file util.c.

                                 {
  if( *z=='-' || *z=='+' ) z++;
  if( !isdigit(*z) ){
    return 0;
  }
  z++;
  while( isdigit(*z) ){ z++; }
  if( *z=='.' ){
    z++;
    if( !isdigit(*z) ) return 0;
    while( isdigit(*z) ){ z++; }
  }
  if( *z=='e' || *z=='E' ){
    z++;
    if( *z=='+' || *z=='-' ) z++;
    if( !isdigit(*z) ) return 0;
    while( isdigit(*z) ){ z++; }
  }
  return *z==0;
}

Here is the caller graph for this function:

int sqliteLikeCompare ( const unsigned char *  zPattern,
const unsigned char *  zString 
)

Definition at line 1031 of file util.c.

                                                                              {
  register int c;
  int c2;

  while( (c = UpperToLower[*zPattern])!=0 ){
    switch( c ){
      case '%': {
        while( (c=zPattern[1]) == '%' || c == '_' ){
          if( c=='_' ){
            if( *zString==0 ) return 0;
            sqliteNextChar(zString);
          }
          zPattern++;
        }
        if( c==0 ) return 1;
        c = UpperToLower[c];
        while( (c2=UpperToLower[*zString])!=0 ){
          while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
          if( c2==0 ) return 0;
          if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
          sqliteNextChar(zString);
        }
        return 0;
      }
      case '_': {
        if( *zString==0 ) return 0;
        sqliteNextChar(zString);
        zPattern++;
        break;
      }
      default: {
        if( c != UpperToLower[*zString] ) return 0;
        zPattern++;
        zString++;
        break;
      }
    }
  }
  return *zString==0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* sqliteMalloc ( int  n)

Definition at line 252 of file util.c.

                         {
  void *p;
  if( (p = malloc(n))==0 ){
    if( n>0 ) sqlite_malloc_failed++;
  }else{
    memset(p, 0, n);
  }
  return p;
}
void* sqliteMallocRaw ( int  n)

Definition at line 266 of file util.c.

                            {
  void *p;
  if( (p = malloc(n))==0 ){
    if( n>0 ) sqlite_malloc_failed++;
  }
  return p;
}

Here is the caller graph for this function:

void* sqliteRealloc ( void *  p,
int  n 
)

Definition at line 288 of file util.c.

                                   {
  void *p2;
  if( p==0 ){
    return sqliteMalloc(n);
  }
  if( n==0 ){
    sqliteFree(p);
    return 0;
  }
  p2 = realloc(p, n);
  if( p2==0 ){
    sqlite_malloc_failed++;
  }
  return p2;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqliteRealToSortable ( double  r,
char *  z 
)

Definition at line 797 of file util.c.

                                            {
  int neg;
  int exp;
  int cnt = 0;

  /* This array maps integers between 0 and 63 into base-64 digits.
  ** The digits must be chosen such at their ASCII codes are increasing.
  ** This means we can not use the traditional base-64 digit set. */
  static const char zDigit[] = 
     "0123456789"
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     "abcdefghijklmnopqrstuvwxyz"
     "|~";
  if( r<0.0 ){
    neg = 1;
    r = -r;
    *z++ = '-';
  } else {
    neg = 0;
    *z++ = '0';
  }
  exp = 0;

  if( r==0.0 ){
    exp = -1024;
  }else if( r<(0.5/64.0) ){
    while( r < 0.5/_64e64 && exp > -961  ){ r *= _64e64;  exp -= 64; }
    while( r < 0.5/_64e16 && exp > -1009 ){ r *= _64e16;  exp -= 16; }
    while( r < 0.5/_64e4  && exp > -1021 ){ r *= _64e4;   exp -= 4; }
    while( r < 0.5/64.0   && exp > -1024 ){ r *= 64.0;    exp -= 1; }
  }else if( r>=0.5 ){
    while( r >= 0.5*_64e63 && exp < 960  ){ r *= 1.0/_64e64; exp += 64; }
    while( r >= 0.5*_64e15 && exp < 1008 ){ r *= 1.0/_64e16; exp += 16; }
    while( r >= 0.5*_64e3  && exp < 1020 ){ r *= 1.0/_64e4;  exp += 4; }
    while( r >= 0.5        && exp < 1023 ){ r *= 1.0/64.0;   exp += 1; }
  }
  if( neg ){
    exp = -exp;
    r = -r;
  }
  exp += 1024;
  r += 0.5;
  if( exp<0 ) return;
  if( exp>=2048 || r>=1.0 ){
    strcpy(z, "~~~~~~~~~~~~");
    return;
  }
  *z++ = zDigit[(exp>>6)&0x3f];
  *z++ = zDigit[exp & 0x3f];
  while( r>0.0 && cnt<10 ){
    int digit;
    r *= 64.0;
    digit = (int)r;
    assert( digit>=0 && digit<64 );
    *z++ = zDigit[digit & 0x3f];
    r -= digit;
    cnt++;
  }
  *z = 0;
}

Here is the caller graph for this function:

Definition at line 1128 of file util.c.

                                 {
  if( db->pVdbe!=0 ){
    db->magic = SQLITE_MAGIC_ERROR;
    return 1;
  }
  return 0;
}

Here is the caller graph for this function:

Definition at line 1107 of file util.c.

                               {
  if( db->magic==SQLITE_MAGIC_BUSY ){
    db->magic = SQLITE_MAGIC_OPEN;
    return 0;
  }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR
             || db->want_to_close ){
    db->magic = SQLITE_MAGIC_ERROR;
    db->flags |= SQLITE_Interrupt;
  }
  return 1;
}

Here is the caller graph for this function:

Definition at line 1090 of file util.c.

                              {
  if( db->magic==SQLITE_MAGIC_OPEN ){
    db->magic = SQLITE_MAGIC_BUSY;
    return 0;
  }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR
             || db->want_to_close ){
    db->magic = SQLITE_MAGIC_ERROR;
    db->flags |= SQLITE_Interrupt;
  }
  return 1;
}

Here is the caller graph for this function:

void sqliteSetNString ( char **  pz,
  ... 
)

Definition at line 372 of file util.c.

                                     {
  va_list ap;
  int nByte;
  const char *z;
  char *zResult;
  int n;

  if( pz==0 ) return;
  nByte = 0;
  va_start(ap, pz);
  while( (z = va_arg(ap, const char*))!=0 ){
    n = va_arg(ap, int);
    if( n<=0 ) n = strlen(z);
    nByte += n;
  }
  va_end(ap);
  sqliteFree(*pz);
  *pz = zResult = sqliteMallocRaw( nByte + 1 );
  if( zResult==0 ) return;
  va_start(ap, pz);
  while( (z = va_arg(ap, const char*))!=0 ){
    n = va_arg(ap, int);
    if( n<=0 ) n = strlen(z);
    strncpy(zResult, z, n);
    zResult += n;
  }
  *zResult = 0;
#ifdef MEMORY_DEBUG
#if MEMORY_DEBUG>1
  fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
#endif
#endif
  va_end(ap);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqliteSetString ( char **  pz,
  ... 
)

Definition at line 333 of file util.c.

                                    {
  va_list ap;
  int nByte;
  const char *z;
  char *zResult;

  if( pz==0 ) return;
  nByte = 1;
  va_start(ap, pz);
  while( (z = va_arg(ap, const char*))!=0 ){
    nByte += strlen(z);
  }
  va_end(ap);
  sqliteFree(*pz);
  *pz = zResult = sqliteMallocRaw( nByte );
  if( zResult==0 ){
    return;
  }
  *zResult = 0;
  va_start(ap, pz);
  while( (z = va_arg(ap, const char*))!=0 ){
    strcpy(zResult, z);
    zResult += strlen(zResult);
  }
  va_end(ap);
#ifdef MEMORY_DEBUG
#if MEMORY_DEBUG>1
  fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
#endif
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqliteSortCompare ( const char *  a,
const char *  b 
)

Definition at line 712 of file util.c.

                                                   {
  int res = 0;
  int isNumA, isNumB;
  int dir = 0;

  while( res==0 && *a && *b ){
    if( a[0]=='N' || b[0]=='N' ){
      if( a[0]==b[0] ){
        a += 2;
        b += 2;
        continue;
      }
      if( a[0]=='N' ){
        dir = b[0];
        res = -1;
      }else{
        dir = a[0];
        res = +1;
      }
      break;
    }
    assert( a[0]==b[0] );
    if( (dir=a[0])=='A' || a[0]=='D' ){
      res = strcmp(&a[1],&b[1]);
      if( res ) break;
    }else{
      isNumA = sqliteIsNumber(&a[1]);
      isNumB = sqliteIsNumber(&b[1]);
      if( isNumA ){
        double rA, rB;
        if( !isNumB ){
          res = -1;
          break;
        }
        rA = sqliteAtoF(&a[1], 0);
        rB = sqliteAtoF(&b[1], 0);
        if( rA<rB ){
          res = -1;
          break;
        }
        if( rA>rB ){
          res = +1;
          break;
        }
      }else if( isNumB ){
        res = +1;
        break;
      }else{
        res = strcmp(&a[1],&b[1]);
        if( res ) break;
      }
    }
    a += strlen(&a[1]) + 2;
    b += strlen(&b[1]) + 2;
  }
  if( dir=='-' || dir=='D' ) res = -res;
  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* sqliteStrDup ( const char *  z)

Definition at line 307 of file util.c.

                                 {
  char *zNew;
  if( z==0 ) return 0;
  zNew = sqliteMallocRaw(strlen(z)+1);
  if( zNew ) strcpy(zNew, z);
  return zNew;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqliteStrICmp ( const char *  zLeft,
const char *  zRight 
)

Definition at line 501 of file util.c.

                                                        {
  register unsigned char *a, *b;
  a = (unsigned char *)zLeft;
  b = (unsigned char *)zRight;
  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
  return UpperToLower[*a] - UpperToLower[*b];
}

Here is the caller graph for this function:

char* sqliteStrNDup ( const char *  z,
int  n 
)

Definition at line 314 of file util.c.

                                         {
  char *zNew;
  if( z==0 ) return 0;
  zNew = sqliteMallocRaw(n+1);
  if( zNew ){
    memcpy(zNew, z, n);
    zNew[n] = 0;
  }
  return zNew;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqliteStrNICmp ( const char *  zLeft,
const char *  zRight,
int  N 
)

Definition at line 508 of file util.c.

                                                                {
  register unsigned char *a, *b;
  a = (unsigned char *)zLeft;
  b = (unsigned char *)zRight;
  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 27 of file util.c.

unsigned char UpperToLower[] [static]
Initial value:
 {
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
    252,253,254,255
}

Definition at line 465 of file util.c.