1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-10 17:42:29 +03:00

Massive commit to run PGINDENT on all *.c and *.h files.

This commit is contained in:
Bruce Momjian
1997-09-07 05:04:48 +00:00
parent 8fecd4febf
commit 1ccd423235
687 changed files with 150775 additions and 136888 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* arrayutils.c--
* This file contains some support routines required for array functions.
* This file contains some support routines required for array functions.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayutils.c,v 1.3 1996/11/10 03:03:03 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayutils.c,v 1.4 1997/09/07 04:49:57 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -20,94 +20,109 @@
int
GetOffset(int n, int dim[], int lb[], int indx[])
{
int i, scale, offset;
for (i = n-1, scale = 1, offset = 0; i >= 0; scale*=dim[i--])
offset += (indx[i] - lb[i])*scale;
return offset ;
{
int i,
scale,
offset;
for (i = n - 1, scale = 1, offset = 0; i >= 0; scale *= dim[i--])
offset += (indx[i] - lb[i]) * scale;
return offset;
}
int
getNitems(int n, int a[])
{
int i, ret;
for (i = 0, ret = 1; i < n; ret *= a[i++]);
if (n == 0) ret = 0;
return ret;
{
int i,
ret;
for (i = 0, ret = 1; i < n; ret *= a[i++]);
if (n == 0)
ret = 0;
return ret;
}
int
compute_size(int st[], int endp[], int n, int base)
{
int i, ret;
for (i = 0, ret = base; i < n; i++)
ret *= (endp[i] - st[i] + 1);
return ret;
int i,
ret;
for (i = 0, ret = base; i < n; i++)
ret *= (endp[i] - st[i] + 1);
return ret;
}
void
mda_get_offset_values(int n, int dist[], int PC[], int span[])
{
int i, j;
for (j = n-2, dist[n-1]=0; j >= 0; j--)
for (i = j+1, dist[j] = PC[j]-1; i < n;
dist[j] -= (span[i] - 1)*PC[i], i++);
}
{
int i,
j;
for (j = n - 2, dist[n - 1] = 0; j >= 0; j--)
for (i = j + 1, dist[j] = PC[j] - 1; i < n;
dist[j] -= (span[i] - 1) * PC[i], i++);
}
void
mda_get_range(int n, int span[], int st[], int endp[])
{
int i;
for (i= 0; i < n; i++)
span[i] = endp[i] - st[i] + 1;
}
{
int i;
for (i = 0; i < n; i++)
span[i] = endp[i] - st[i] + 1;
}
void
mda_get_prod(int n, int range[], int P[])
{
int i;
for (i= n-2, P[n-1] = 1; i >= 0; i--)
P[i] = P[i+1] * range[i + 1];
}
{
int i;
for (i = n - 2, P[n - 1] = 1; i >= 0; i--)
P[i] = P[i + 1] * range[i + 1];
}
int
tuple2linear(int n, int tup[], int scale[])
{
int i, lin;
for (i= lin = 0; i < n; i++)
lin += tup[i]*scale[i];
return lin;
}
int i,
lin;
for (i = lin = 0; i < n; i++)
lin += tup[i] * scale[i];
return lin;
}
void
array2chunk_coord(int n, int C[], int a_coord[], int c_coord[])
{
int i;
for (i= 0; i < n; i++)
c_coord[i] = a_coord[i]/C[i];
int i;
for (i = 0; i < n; i++)
c_coord[i] = a_coord[i] / C[i];
}
/*-----------------------------------------------------------------------------
generates the tuple that is lexicographically one greater than the current
n-tuple in "curr", with the restriction that the i-th element of "curr" is
less than the i-th element of "span".
RETURNS 0 if no next tuple exists
RETURNS 0 if no next tuple exists
1 otherwise
-----------------------------------------------------------------------------*/
int
next_tuple(int n, int curr[], int span[])
{
int i;
if (!n) return(-1);
curr[n-1] = (curr[n-1]+1)%span[n-1];
for (i = n-1; i*(!curr[i]); i--)
curr[i-1] = (curr[i-1]+1)%span[i-1];
if (i)
return(i);
if (curr[0])
return(0);
return(-1);
}
int i;
if (!n)
return (-1);
curr[n - 1] = (curr[n - 1] + 1) % span[n - 1];
for (i = n - 1; i * (!curr[i]); i--)
curr[i - 1] = (curr[i - 1] + 1) % span[i - 1];
if (i)
return (i);
if (curr[0])
return (0);
return (-1);
}

View File

@@ -1,74 +1,74 @@
/*-------------------------------------------------------------------------
*
* bool.c--
* Functions for the built-in type "bool".
* Functions for the built-in type "bool".
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/bool.c,v 1.4 1997/04/27 19:20:07 thomas Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/bool.c,v 1.5 1997/09/07 04:49:58 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "utils/builtins.h" /* where the declarations go */
#include "utils/builtins.h" /* where the declarations go */
#include "utils/palloc.h"
/*****************************************************************************
* USER I/O ROUTINES *
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
/*
* boolin - converts "t" or "f" to 1 or 0
* boolin - converts "t" or "f" to 1 or 0
*/
bool
boolin(char *b)
{
if (b == NULL)
elog(WARN, "Bad input string for type bool");
return((bool) (*b == 't') || (*b == 'T'));
if (b == NULL)
elog(WARN, "Bad input string for type bool");
return ((bool) (*b == 't') || (*b == 'T'));
}
/*
* boolout - converts 1 or 0 to "t" or "f"
* boolout - converts 1 or 0 to "t" or "f"
*/
char *
char *
boolout(long b)
{
char *result = (char *) palloc(2);
*result = (b) ? 't' : 'f';
result[1] = '\0';
return(result);
char *result = (char *) palloc(2);
*result = (b) ? 't' : 'f';
result[1] = '\0';
return (result);
}
/*****************************************************************************
* PUBLIC ROUTINES *
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
bool
booleq(int8 arg1, int8 arg2)
{
return(arg1 == arg2);
}
bool
boolne(int8 arg1, int8 arg2)
booleq(int8 arg1, int8 arg2)
{
return(arg1 != arg2);
return (arg1 == arg2);
}
bool
boollt(int8 arg1, int8 arg2)
{
return(arg1 < arg2);
boolne(int8 arg1, int8 arg2)
{
return (arg1 != arg2);
}
bool
boolgt(int8 arg1, int8 arg2)
{
return(arg1 > arg2);
boollt(int8 arg1, int8 arg2)
{
return (arg1 < arg2);
}
bool
boolgt(int8 arg1, int8 arg2)
{
return (arg1 > arg2);
}

View File

@@ -8,8 +8,8 @@
* A slightly modified version of this file and a discussion of the
* workings can be found in the book "Software Solutions in C" by
* Dale Schumacher, Academic Press, ISBN: 0-12-632360-7.
*
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.9 1997/08/22 07:12:52 momjian Exp $
*
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.10 1997/09/07 04:49:59 momjian Exp $
*/
#include <stdio.h>
@@ -26,332 +26,363 @@
static const char *num_word(Cash value);
/* when we go to 64 bit values we will have to modify this */
#define CASH_BUFSZ 24
#define CASH_BUFSZ 24
#define TERMINATOR (CASH_BUFSZ - 1)
#define LAST_PAREN (TERMINATOR - 1)
#define LAST_DIGIT (LAST_PAREN - 1)
#define TERMINATOR (CASH_BUFSZ - 1)
#define LAST_PAREN (TERMINATOR - 1)
#define LAST_DIGIT (LAST_PAREN - 1)
#ifdef USE_LOCALE
static struct lconv *lconv = NULL;
#endif
/* cash_in()
* Convert a string to a cash data type.
* Format is [$]###[,]###[.##]
* Examples: 123.45 $123.45 $123,456.78
*
*
* This is currently implemented as a 32-bit integer.
* XXX HACK It looks as though some of the symbols for
* monetary values returned by localeconv() can be multiple
* bytes/characters. This code assumes one byte only. - tgl 97/04/14
* monetary values returned by localeconv() can be multiple
* bytes/characters. This code assumes one byte only. - tgl 97/04/14
*/
Cash *
Cash *
cash_in(const char *str)
{
Cash *result;
Cash *result;
Cash value = 0;
Cash dec = 0;
Cash sgn = 1;
int seen_dot = 0;
const char *s = str;
int fpoint;
char dsymbol, ssymbol, psymbol, nsymbol, csymbol;
Cash value = 0;
Cash dec = 0;
Cash sgn = 1;
int seen_dot = 0;
const char *s = str;
int fpoint;
char dsymbol,
ssymbol,
psymbol,
nsymbol,
csymbol;
#ifdef USE_LOCALE
if (lconv == NULL) lconv = localeconv();
if (lconv == NULL)
lconv = localeconv();
/* frac_digits in the C locale seems to return CHAR_MAX */
/* best guess is 2 in this case I think */
fpoint = ((lconv->frac_digits != CHAR_MAX)? lconv->frac_digits: 2); /* int_frac_digits? */
/* frac_digits in the C locale seems to return CHAR_MAX */
/* best guess is 2 in this case I think */
fpoint = ((lconv->frac_digits != CHAR_MAX) ? lconv->frac_digits : 2); /* int_frac_digits? */
dsymbol = *lconv->mon_decimal_point;
ssymbol = *lconv->mon_thousands_sep;
csymbol = *lconv->currency_symbol;
psymbol = *lconv->positive_sign;
nsymbol = *lconv->negative_sign;
dsymbol = *lconv->mon_decimal_point;
ssymbol = *lconv->mon_thousands_sep;
csymbol = *lconv->currency_symbol;
psymbol = *lconv->positive_sign;
nsymbol = *lconv->negative_sign;
#else
fpoint = 2;
dsymbol = '.';
ssymbol = ',';
csymbol = '$';
psymbol = '+';
nsymbol = '-';
fpoint = 2;
dsymbol = '.';
ssymbol = ',';
csymbol = '$';
psymbol = '+';
nsymbol = '-';
#endif
/* we need to add all sorts of checking here. For now just */
/* strip all leading whitespace and any leading dollar sign */
while (isspace(*s) || *s == csymbol) s++;
/* we need to add all sorts of checking here. For now just */
/* strip all leading whitespace and any leading dollar sign */
while (isspace(*s) || *s == csymbol)
s++;
/* a leading minus or paren signifies a negative number */
/* again, better heuristics needed */
if (*s == nsymbol || *s == '(') {
sgn = -1;
s++;
/* a leading minus or paren signifies a negative number */
/* again, better heuristics needed */
if (*s == nsymbol || *s == '(')
{
sgn = -1;
s++;
} else if (*s == psymbol) {
s++;
}
while (isspace(*s) || *s == csymbol) s++;
for (; ; s++) {
/* we look for digits as int4 as we have less */
/* than the required number of decimal places */
if (isdigit(*s) && dec < fpoint) {
value = (value * 10) + *s - '0';
if (seen_dot)
dec++;
/* decimal point? then start counting fractions... */
} else if (*s == dsymbol && !seen_dot) {
seen_dot = 1;
/* "thousands" separator? then skip... */
} else if (*s == ssymbol) {
} else {
/* round off */
if (isdigit(*s) && *s >= '5')
value++;
/* adjust for less than required decimal places */
for (; dec < fpoint; dec++)
value *= 10;
break;
}
}
else if (*s == psymbol)
{
s++;
}
while (isspace(*s) || *s == '0' || *s == ')') s++;
while (isspace(*s) || *s == csymbol)
s++;
if (*s != '\0')
elog(WARN,"Bad money external representation %s",str);
for (;; s++)
{
/* we look for digits as int4 as we have less */
/* than the required number of decimal places */
if (isdigit(*s) && dec < fpoint)
{
value = (value * 10) + *s - '0';
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN,"Memory allocation failed, can't input cash '%s'",str);
if (seen_dot)
dec++;
*result = (value * sgn);
/* decimal point? then start counting fractions... */
}
else if (*s == dsymbol && !seen_dot)
{
seen_dot = 1;
return(result);
} /* cash_in() */
/* "thousands" separator? then skip... */
}
else if (*s == ssymbol)
{
}
else
{
/* round off */
if (isdigit(*s) && *s >= '5')
value++;
/* adjust for less than required decimal places */
for (; dec < fpoint; dec++)
value *= 10;
break;
}
}
while (isspace(*s) || *s == '0' || *s == ')')
s++;
if (*s != '\0')
elog(WARN, "Bad money external representation %s", str);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN, "Memory allocation failed, can't input cash '%s'", str);
*result = (value * sgn);
return (result);
} /* cash_in() */
/* cash_out()
* Function to convert cash to a dollars and cents representation.
* XXX HACK This code appears to assume US conventions for
* positive-valued amounts. - tgl 97/04/14
* positive-valued amounts. - tgl 97/04/14
*/
const char *
cash_out(Cash *value)
const char *
cash_out(Cash * value)
{
char *result;
char buf[CASH_BUFSZ];
int minus = 0;
int count = LAST_DIGIT;
int point_pos;
int comma_position = 0;
char mon_group, comma, points;
char csymbol, dsymbol, *nsymbol;
char convention;
char *result;
char buf[CASH_BUFSZ];
int minus = 0;
int count = LAST_DIGIT;
int point_pos;
int comma_position = 0;
char mon_group,
comma,
points;
char csymbol,
dsymbol,
*nsymbol;
char convention;
#ifdef USE_LOCALE
if (lconv == NULL) lconv = localeconv();
if (lconv == NULL)
lconv = localeconv();
mon_group = *lconv->mon_grouping;
comma = *lconv->mon_thousands_sep;
csymbol = *lconv->currency_symbol;
dsymbol = *lconv->mon_decimal_point;
nsymbol = lconv->negative_sign;
/* frac_digits in the C locale seems to return CHAR_MAX */
/* best guess is 2 in this case I think */
points = ((lconv->frac_digits != CHAR_MAX)? lconv->frac_digits: 2); /* int_frac_digits? */
convention = lconv->n_sign_posn;
mon_group = *lconv->mon_grouping;
comma = *lconv->mon_thousands_sep;
csymbol = *lconv->currency_symbol;
dsymbol = *lconv->mon_decimal_point;
nsymbol = lconv->negative_sign;
/* frac_digits in the C locale seems to return CHAR_MAX */
/* best guess is 2 in this case I think */
points = ((lconv->frac_digits != CHAR_MAX) ? lconv->frac_digits : 2); /* int_frac_digits? */
convention = lconv->n_sign_posn;
#else
mon_group = 3;
comma = ',';
csymbol = '$';
dsymbol = '.';
nsymbol = "-";
points = 2;
convention = 0;
mon_group = 3;
comma = ',';
csymbol = '$';
dsymbol = '.';
nsymbol = "-";
points = 2;
convention = 0;
#endif
point_pos = LAST_DIGIT - points;
point_pos = LAST_DIGIT - points;
/* We're playing a little fast and loose with this. Shoot me. */
if (!mon_group || mon_group == CHAR_MAX)
mon_group = 3;
/* We're playing a little fast and loose with this. Shoot me. */
if (!mon_group || mon_group == CHAR_MAX)
mon_group = 3;
/* allow more than three decimal points and separate them */
if (comma) {
point_pos -= (points - 1)/mon_group;
comma_position = point_pos % (mon_group + 1);
}
/* allow more than three decimal points and separate them */
if (comma)
{
point_pos -= (points - 1) / mon_group;
comma_position = point_pos % (mon_group + 1);
}
/* we work with positive amounts and add the minus sign at the end */
if (*value < 0) {
minus = 1;
*value *= -1;
}
/* we work with positive amounts and add the minus sign at the end */
if (*value < 0)
{
minus = 1;
*value *= -1;
}
/* allow for trailing negative strings */
memset(buf, ' ', CASH_BUFSZ);
buf[TERMINATOR] = buf[LAST_PAREN] = '\0';
/* allow for trailing negative strings */
memset(buf, ' ', CASH_BUFSZ);
buf[TERMINATOR] = buf[LAST_PAREN] = '\0';
while (*value || count > (point_pos - 2)) {
if (points && count == point_pos)
buf[count--] = dsymbol;
else if (comma && count % (mon_group + 1) == comma_position)
buf[count--] = comma;
while (*value || count > (point_pos - 2))
{
if (points && count == point_pos)
buf[count--] = dsymbol;
else if (comma && count % (mon_group + 1) == comma_position)
buf[count--] = comma;
buf[count--] = (*value % 10) + '0';
*value /= 10;
}
buf[count--] = (*value % 10) + '0';
*value /= 10;
}
buf[count] = csymbol;
buf[count] = csymbol;
if (buf[LAST_DIGIT] == ',')
buf[LAST_DIGIT] = buf[LAST_PAREN];
if (buf[LAST_DIGIT] == ',')
buf[LAST_DIGIT] = buf[LAST_PAREN];
/* see if we need to signify negative amount */
if (minus) {
if (!PointerIsValid(result = PALLOC(CASH_BUFSZ + 2 - count + strlen(nsymbol))))
elog(WARN,"Memory allocation failed, can't output cash",NULL);
/* see if we need to signify negative amount */
if (minus)
{
if (!PointerIsValid(result = PALLOC(CASH_BUFSZ + 2 - count + strlen(nsymbol))))
elog(WARN, "Memory allocation failed, can't output cash", NULL);
/* Position code of 0 means use parens */
if (convention == 0)
sprintf(result, "(%s)", buf + count);
else if (convention == 2)
sprintf(result, "%s%s", buf + count, nsymbol);
/* Position code of 0 means use parens */
if (convention == 0)
sprintf(result, "(%s)", buf + count);
else if (convention == 2)
sprintf(result, "%s%s", buf + count, nsymbol);
else
sprintf(result, "%s%s", nsymbol, buf + count);
}
else
sprintf(result, "%s%s", nsymbol, buf + count);
} else {
if (!PointerIsValid(result = PALLOC(CASH_BUFSZ + 2 - count)))
elog(WARN,"Memory allocation failed, can't output cash",NULL);
{
if (!PointerIsValid(result = PALLOC(CASH_BUFSZ + 2 - count)))
elog(WARN, "Memory allocation failed, can't output cash", NULL);
strcpy(result, buf + count);
}
strcpy(result, buf + count);
}
return(result);
} /* cash_out() */
return (result);
} /* cash_out() */
bool
cash_eq(Cash *c1, Cash *c2)
cash_eq(Cash * c1, Cash * c2)
{
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(FALSE);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (FALSE);
return(*c1 == *c2);
} /* cash_eq() */
return (*c1 == *c2);
} /* cash_eq() */
bool
cash_ne(Cash *c1, Cash *c2)
cash_ne(Cash * c1, Cash * c2)
{
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(FALSE);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (FALSE);
return(*c1 != *c2);
} /* cash_ne() */
return (*c1 != *c2);
} /* cash_ne() */
bool
cash_lt(Cash *c1, Cash *c2)
cash_lt(Cash * c1, Cash * c2)
{
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(FALSE);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (FALSE);
return(*c1 < *c2);
} /* cash_lt() */
return (*c1 < *c2);
} /* cash_lt() */
bool
cash_le(Cash *c1, Cash *c2)
cash_le(Cash * c1, Cash * c2)
{
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(FALSE);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (FALSE);
return(*c1 <= *c2);
} /* cash_le() */
return (*c1 <= *c2);
} /* cash_le() */
bool
cash_gt(Cash *c1, Cash *c2)
cash_gt(Cash * c1, Cash * c2)
{
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(FALSE);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (FALSE);
return(*c1 > *c2);
} /* cash_gt() */
return (*c1 > *c2);
} /* cash_gt() */
bool
cash_ge(Cash *c1, Cash *c2)
cash_ge(Cash * c1, Cash * c2)
{
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(FALSE);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (FALSE);
return(*c1 >= *c2);
} /* cash_ge() */
return (*c1 >= *c2);
} /* cash_ge() */
/* cash_pl()
* Add two cash values.
*/
Cash *
cash_pl(Cash *c1, Cash *c2)
Cash *
cash_pl(Cash * c1, Cash * c2)
{
Cash *result;
Cash *result;
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(NULL);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN,"Memory allocation failed, can't add cash",NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN, "Memory allocation failed, can't add cash", NULL);
*result = (*c1 + *c2);
*result = (*c1 + *c2);
return(result);
} /* cash_pl() */
return (result);
} /* cash_pl() */
/* cash_mi()
* Subtract two cash values.
*/
Cash *
cash_mi(Cash *c1, Cash *c2)
Cash *
cash_mi(Cash * c1, Cash * c2)
{
Cash *result;
Cash *result;
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(NULL);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN,"Memory allocation failed, can't subtract cash",NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN, "Memory allocation failed, can't subtract cash", NULL);
*result = (*c1 - *c2);
*result = (*c1 - *c2);
return(result);
} /* cash_mi() */
return (result);
} /* cash_mi() */
/* cash_mul()
* Multiply cash by floating point number.
*/
Cash *
cash_mul(Cash *c, float8 *f)
Cash *
cash_mul(Cash * c, float8 * f)
{
Cash *result;
Cash *result;
if (!PointerIsValid(f) || !PointerIsValid(c))
return(NULL);
if (!PointerIsValid(f) || !PointerIsValid(c))
return (NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN,"Memory allocation failed, can't multiply cash",NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN, "Memory allocation failed, can't multiply cash", NULL);
*result = ((*f) * (*c));
*result = ((*f) * (*c));
return(result);
} /* cash_mul() */
return (result);
} /* cash_mul() */
/* cash_div()
@@ -360,116 +391,121 @@ cash_mul(Cash *c, float8 *f)
* XXX Don't know if rounding or truncating is correct behavior.
* Round for now. - tgl 97/04/15
*/
Cash *
cash_div(Cash *c, float8 *f)
Cash *
cash_div(Cash * c, float8 * f)
{
Cash *result;
Cash *result;
if (!PointerIsValid(f) || !PointerIsValid(c))
return(NULL);
if (!PointerIsValid(f) || !PointerIsValid(c))
return (NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN,"Memory allocation failed, can't divide cash",NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN, "Memory allocation failed, can't divide cash", NULL);
if (*f == 0.0)
elog(WARN,"cash_div: divide by 0.0 error");
if (*f == 0.0)
elog(WARN, "cash_div: divide by 0.0 error");
*result = rint(*c / *f);
*result = rint(*c / *f);
return(result);
} /* cash_div() */
return (result);
} /* cash_div() */
/* cashlarger()
* Return larger of two cash values.
*/
Cash *
cashlarger(Cash *c1, Cash *c2)
Cash *
cashlarger(Cash * c1, Cash * c2)
{
Cash *result;
Cash *result;
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(NULL);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN,"Memory allocation failed, can't return larger cash",NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN, "Memory allocation failed, can't return larger cash", NULL);
*result = ((*c1 > *c2)? *c1: *c2);
*result = ((*c1 > *c2) ? *c1 : *c2);
return(result);
} /* cashlarger() */
return (result);
} /* cashlarger() */
/* cashsmaller()
* Return smaller of two cash values.
*/
Cash *
cashsmaller(Cash *c1, Cash *c2)
Cash *
cashsmaller(Cash * c1, Cash * c2)
{
Cash *result;
Cash *result;
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return(NULL);
if (!PointerIsValid(c1) || !PointerIsValid(c2))
return (NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN,"Memory allocation failed, can't return smaller cash",NULL);
if (!PointerIsValid(result = PALLOCTYPE(Cash)))
elog(WARN, "Memory allocation failed, can't return smaller cash", NULL);
*result = ((*c1 < *c2)? *c1: *c2);
*result = ((*c1 < *c2) ? *c1 : *c2);
return(result);
} /* cashsmaller() */
return (result);
} /* cashsmaller() */
/* cash_words_out()
* This converts a int4 as well but to a representation using words
* Obviously way North American centric - sorry
*/
const char *
cash_words_out(Cash *value)
const char *
cash_words_out(Cash * value)
{
static char buf[128];
char *p = buf;
Cash m0;
Cash m1;
Cash m2;
Cash m3;
static char buf[128];
char *p = buf;
Cash m0;
Cash m1;
Cash m2;
Cash m3;
/* work with positive numbers */
if (*value < 0) {
*value *= -1;
strcpy(buf, "minus ");
p += 6;
} else {
*buf = 0;
}
/* work with positive numbers */
if (*value < 0)
{
*value *= -1;
strcpy(buf, "minus ");
p += 6;
}
else
{
*buf = 0;
}
m0 = *value % 100; /* cents */
m1 = (*value/100) % 1000; /* hundreds */
m2 = (*value/100000) % 1000; /* thousands */
m3 = *value/100000000 % 1000; /* millions */
m0 = *value % 100; /* cents */
m1 = (*value / 100) % 1000; /* hundreds */
m2 = (*value / 100000) % 1000; /* thousands */
m3 = *value / 100000000 % 1000; /* millions */
if (m3) {
strcat(buf, num_word(m3));
strcat(buf, " million ");
}
if (m3)
{
strcat(buf, num_word(m3));
strcat(buf, " million ");
}
if (m2) {
strcat(buf, num_word(m2));
strcat(buf, " thousand ");
}
if (m2)
{
strcat(buf, num_word(m2));
strcat(buf, " thousand ");
}
if (m1)
strcat(buf, num_word(m1));
if (m1)
strcat(buf, num_word(m1));
if (!*p)
strcat(buf, "zero");
if (!*p)
strcat(buf, "zero");
strcat(buf, (int)(*value/100) == 1 ? " dollar and " : " dollars and ");
strcat(buf, num_word(m0));
strcat(buf, m0 == 1 ? " cent" : " cents");
*buf = toupper(*buf);
return(buf);
} /* cash_words_out() */
strcat(buf, (int) (*value / 100) == 1 ? " dollar and " : " dollars and ");
strcat(buf, num_word(m0));
strcat(buf, m0 == 1 ? " cent" : " cents");
*buf = toupper(*buf);
return (buf);
} /* cash_words_out() */
/*************************************************************************
@@ -479,48 +515,52 @@ cash_words_out(Cash *value)
static const char *
num_word(Cash value)
{
static char buf[128];
static const char *small[] = {
"zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
"thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
const char **big = small + 18;
int tu = value % 100;
static char buf[128];
static const char *small[] = {
"zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
"thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
const char **big = small + 18;
int tu = value % 100;
/* deal with the simple cases first */
if (value <= 20)
return(small[value]);
/* deal with the simple cases first */
if (value <= 20)
return (small[value]);
/* is it an even multiple of 100? */
if (!tu) {
sprintf(buf, "%s hundred", small[value/100]);
return(buf);
}
/* is it an even multiple of 100? */
if (!tu)
{
sprintf(buf, "%s hundred", small[value / 100]);
return (buf);
}
/* more than 99? */
if (value > 99) {
/* is it an even multiple of 10 other than 10? */
if (value % 10 == 0 && tu > 10)
sprintf(buf, "%s hundred %s",
small[value/100], big[tu/10]);
else if (tu < 20)
sprintf(buf, "%s hundred and %s",
small[value/100], small[tu]);
/* more than 99? */
if (value > 99)
{
/* is it an even multiple of 10 other than 10? */
if (value % 10 == 0 && tu > 10)
sprintf(buf, "%s hundred %s",
small[value / 100], big[tu / 10]);
else if (tu < 20)
sprintf(buf, "%s hundred and %s",
small[value / 100], small[tu]);
else
sprintf(buf, "%s hundred %s %s",
small[value / 100], big[tu / 10], small[tu % 10]);
}
else
sprintf(buf, "%s hundred %s %s",
small[value/100], big[tu/10], small[tu % 10]);
{
/* is it an even multiple of 10 other than 10? */
if (value % 10 == 0 && tu > 10)
sprintf(buf, "%s", big[tu / 10]);
else if (tu < 20)
sprintf(buf, "%s", small[tu]);
else
sprintf(buf, "%s %s", big[tu / 10], small[tu % 10]);
}
} else {
/* is it an even multiple of 10 other than 10? */
if (value % 10 == 0 && tu > 10)
sprintf(buf, "%s", big[tu/10]);
else if (tu < 20)
sprintf(buf, "%s", small[tu]);
else
sprintf(buf, "%s %s", big[tu/10], small[tu % 10]);
}
return(buf);
} /* num_word() */
return (buf);
} /* num_word() */

View File

@@ -1,384 +1,460 @@
/*-------------------------------------------------------------------------
*
* char.c--
* Functions for the built-in type "char".
* Functions for the built-in type "cid".
* Functions for the built-in type "char2".
* Functions for the built-in type "char4".
* Functions for the built-in type "char8".
* Functions for the built-in type "char16".
* Functions for the built-in type "char".
* Functions for the built-in type "cid".
* Functions for the built-in type "char2".
* Functions for the built-in type "char4".
* Functions for the built-in type "char8".
* Functions for the built-in type "char16".
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.7 1997/08/12 20:39:16 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.8 1997/09/07 04:50:02 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h> /* for sprintf() */
#include <stdio.h> /* for sprintf() */
#include <string.h>
#include "postgres.h"
#include "utils/palloc.h"
#include "utils/builtins.h" /* where the declarations go */
/*****************************************************************************
* USER I/O ROUTINES *
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
/*
* charin - converts "x" to 'x'
* charin - converts "x" to 'x'
*/
int32 charin(char *ch)
int32
charin(char *ch)
{
if (ch == NULL)
return((int32) NULL);
return((int32) *ch);
if (ch == NULL)
return ((int32) NULL);
return ((int32) * ch);
}
/*
* charout - converts 'x' to "x"
* charout - converts 'x' to "x"
*/
char *charout(int32 ch)
char *
charout(int32 ch)
{
char *result = (char *) palloc(2);
result[0] = (char) ch;
result[1] = '\0';
return(result);
}
char *result = (char *) palloc(2);
/*
* cidin - converts "..." to internal representation.
*
* NOTE: we must not use 'charin' because cid might be a non
* printable character...
*/
int32 cidin(char *s)
{
CommandId c;
if (s==NULL)
c = 0;
else
c = atoi(s);
return((int32)c);
}
/*
* cidout - converts a cid to "..."
*
* NOTE: we must no use 'charout' because cid might be a non
* printable character...
*/
char *cidout(int32 c)
{
char *result;
CommandId c2;
/*
* cid is a number between 0 .. 2^16-1, therefore we need at most
* 6 chars for the string (5 digits + '\0')
* NOTE: print it as an UNSIGNED int!
*/
result = palloc(6);
c2 = (CommandId)c;
sprintf(result, "%u", (unsigned)(c2));
return(result);
}
/*
* char16in - converts "..." to internal reprsentation
*
* Note:
* Currently if strlen(s) < 14, the extra chars are nulls
*/
char *char16in(char *s)
{
char *result;
if (s == NULL)
return(NULL);
result = (char *) palloc(16);
strncpy(result, s, 16);
return(result);
}
/*
* char16out - converts internal reprsentation to "..."
*/
char *char16out(char *s)
{
char *result = (char *) palloc(17);
if (s == NULL) {
result[0] = '-';
result[0] = (char) ch;
result[1] = '\0';
} else
strNcpy(result, s, 16);
return(result);
return (result);
}
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
bool chareq(int8 arg1, int8 arg2) { return(arg1 == arg2); }
bool charne(int8 arg1, int8 arg2) { return(arg1 != arg2); }
bool charlt(int8 arg1, int8 arg2) { return((uint8)arg1 < (uint8)arg2); }
bool charle(int8 arg1, int8 arg2) { return((uint8)arg1 <= (uint8)arg2); }
bool chargt(int8 arg1, int8 arg2) { return((uint8)arg1 > (uint8)arg2); }
bool charge(int8 arg1, int8 arg2) { return((uint8)arg1 >= (uint8)arg2); }
int8 charpl(int8 arg1, int8 arg2) { return(arg1 + arg2); }
int8 charmi(int8 arg1, int8 arg2) { return(arg1 - arg2); }
int8 charmul(int8 arg1, int8 arg2) { return(arg1 * arg2); }
int8 chardiv(int8 arg1, int8 arg2) { return(arg1 / arg2); }
bool cideq(int8 arg1, int8 arg2) { return(arg1 == arg2); }
/*
* char16eq - returns 1 iff arguments are equal
* char16ne - returns 1 iff arguments are not equal
* cidin - converts "..." to internal representation.
*
* BUGS:
* Assumes that "xy\0\0a" should be equal to "xy\0b".
* If not, can do the comparison backwards for efficiency.
* NOTE: we must not use 'charin' because cid might be a non
* printable character...
*/
int32
cidin(char *s)
{
CommandId c;
if (s == NULL)
c = 0;
else
c = atoi(s);
return ((int32) c);
}
/*
* cidout - converts a cid to "..."
*
* char16lt - returns 1 iff a < b
* char16le - returns 1 iff a <= b
* char16gt - returns 1 iff a < b
* char16ge - returns 1 iff a <= b
* NOTE: we must no use 'charout' because cid might be a non
* printable character...
*/
char *
cidout(int32 c)
{
char *result;
CommandId c2;
/*
* cid is a number between 0 .. 2^16-1, therefore we need at most 6
* chars for the string (5 digits + '\0') NOTE: print it as an
* UNSIGNED int!
*/
result = palloc(6);
c2 = (CommandId) c;
sprintf(result, "%u", (unsigned) (c2));
return (result);
}
/*
* char16in - converts "..." to internal reprsentation
*
* Note:
* Currently if strlen(s) < 14, the extra chars are nulls
*/
char *
char16in(char *s)
{
char *result;
if (s == NULL)
return (NULL);
result = (char *) palloc(16);
strncpy(result, s, 16);
return (result);
}
/*
* char16out - converts internal reprsentation to "..."
*/
char *
char16out(char *s)
{
char *result = (char *) palloc(17);
if (s == NULL)
{
result[0] = '-';
result[1] = '\0';
}
else
strNcpy(result, s, 16);
return (result);
}
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
bool chareq(int8 arg1, int8 arg2)
{
return (arg1 == arg2);
}
bool charne(int8 arg1, int8 arg2)
{
return (arg1 != arg2);
}
bool charlt(int8 arg1, int8 arg2)
{
return ((uint8) arg1 < (uint8) arg2);
}
bool charle(int8 arg1, int8 arg2)
{
return ((uint8) arg1 <= (uint8) arg2);
}
bool chargt(int8 arg1, int8 arg2)
{
return ((uint8) arg1 > (uint8) arg2);
}
bool charge(int8 arg1, int8 arg2)
{
return ((uint8) arg1 >= (uint8) arg2);
}
int8 charpl(int8 arg1, int8 arg2)
{
return (arg1 + arg2);
}
int8 charmi(int8 arg1, int8 arg2)
{
return (arg1 - arg2);
}
int8 charmul(int8 arg1, int8 arg2)
{
return (arg1 * arg2);
}
int8 chardiv(int8 arg1, int8 arg2)
{
return (arg1 / arg2);
}
bool cideq(int8 arg1, int8 arg2)
{
return (arg1 == arg2);
}
/*
* char16eq - returns 1 iff arguments are equal
* char16ne - returns 1 iff arguments are not equal
*
* BUGS:
* Assumes that "xy\0\0a" should be equal to "xy\0b".
* If not, can do the comparison backwards for efficiency.
*
* char16lt - returns 1 iff a < b
* char16le - returns 1 iff a <= b
* char16gt - returns 1 iff a < b
* char16ge - returns 1 iff a <= b
*
*/
bool char16eq(char *arg1, char *arg2)
bool
char16eq(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 16) == 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 16) == 0);
}
bool char16ne(char *arg1, char *arg2)
bool
char16ne(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 16) != 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 16) != 0);
}
bool char16lt(char *arg1, char *arg2)
bool
char16lt(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 16) < 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 16) < 0);
}
bool char16le(char *arg1, char *arg2)
bool
char16le(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 16) <= 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 16) <= 0);
}
bool char16gt(char *arg1, char *arg2)
bool
char16gt(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 16) > 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 16) > 0);
}
bool char16ge(char *arg1, char *arg2)
bool
char16ge(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 16) >= 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 16) >= 0);
}
/* ============================== char2 ============================== */
uint16 char2in(char *s)
uint16
char2in(char *s)
{
uint16 res;
if (s == NULL)
return(0);
strncpy((char *) &res, s, 2);
return(res);
uint16 res;
if (s == NULL)
return (0);
strncpy((char *) &res, s, 2);
return (res);
}
char *char2out(uint16 s)
char *
char2out(uint16 s)
{
char *result = (char *) palloc(3);
strNcpy(result, (char *) &s, 2);
return(result);
char *result = (char *) palloc(3);
strNcpy(result, (char *) &s, 2);
return (result);
}
bool char2eq(uint16 a, uint16 b)
bool
char2eq(uint16 a, uint16 b)
{
return(strncmp((char *) &a, (char *) &b, 2) == 0);
return (strncmp((char *) &a, (char *) &b, 2) == 0);
}
bool char2ne(uint16 a, uint16 b)
bool
char2ne(uint16 a, uint16 b)
{
return(strncmp((char *) &a, (char *) &b, 2) != 0);
return (strncmp((char *) &a, (char *) &b, 2) != 0);
}
bool char2lt(uint16 a, uint16 b)
bool
char2lt(uint16 a, uint16 b)
{
return(strncmp((char *) &a, (char *) &b, 2) < 0);
return (strncmp((char *) &a, (char *) &b, 2) < 0);
}
bool char2le(uint16 a, uint16 b)
bool
char2le(uint16 a, uint16 b)
{
return(strncmp((char *) &a, (char *) &b, 2) <= 0);
return (strncmp((char *) &a, (char *) &b, 2) <= 0);
}
bool char2gt(uint16 a, uint16 b)
bool
char2gt(uint16 a, uint16 b)
{
return(strncmp((char *) &a, (char *) &b, 2) > 0);
return (strncmp((char *) &a, (char *) &b, 2) > 0);
}
bool char2ge(uint16 a, uint16 b)
bool
char2ge(uint16 a, uint16 b)
{
return(strncmp((char *) &a, (char *) &b, 2) >= 0);
return (strncmp((char *) &a, (char *) &b, 2) >= 0);
}
int32 char2cmp(uint16 a, uint16 b)
int32
char2cmp(uint16 a, uint16 b)
{
return (strncmp((char *) &a, (char *) &b, 2));
return (strncmp((char *) &a, (char *) &b, 2));
}
/* ============================== char4 ============================== */
uint32 char4in(char *s)
uint32
char4in(char *s)
{
uint32 res;
if (s == NULL)
return(0);
strncpy((char *) &res, s, 4);
return(res);
uint32 res;
if (s == NULL)
return (0);
strncpy((char *) &res, s, 4);
return (res);
}
char *char4out(s)
uint32 s;
char *
char4out(s)
uint32 s;
{
char *result = (char *) palloc(5);
strNcpy(result, (char *) &s, 4);
return(result);
char *result = (char *) palloc(5);
strNcpy(result, (char *) &s, 4);
return (result);
}
bool char4eq(uint32 a, uint32 b)
bool
char4eq(uint32 a, uint32 b)
{
return(strncmp((char *) &a, (char *) &b, 4) == 0);
return (strncmp((char *) &a, (char *) &b, 4) == 0);
}
bool char4ne(uint32 a, uint32 b)
bool
char4ne(uint32 a, uint32 b)
{
return(strncmp((char *) &a, (char *) &b, 4) != 0);
return (strncmp((char *) &a, (char *) &b, 4) != 0);
}
bool char4lt(uint32 a, uint32 b)
bool
char4lt(uint32 a, uint32 b)
{
return(strncmp((char *) &a, (char *) &b, 4) < 0);
return (strncmp((char *) &a, (char *) &b, 4) < 0);
}
bool char4le(uint32 a, uint32 b)
bool
char4le(uint32 a, uint32 b)
{
return(strncmp((char *) &a, (char *) &b, 4) <= 0);
return (strncmp((char *) &a, (char *) &b, 4) <= 0);
}
bool char4gt(uint32 a, uint32 b)
bool
char4gt(uint32 a, uint32 b)
{
return(strncmp((char *) &a, (char *) &b, 4) > 0);
return (strncmp((char *) &a, (char *) &b, 4) > 0);
}
bool char4ge(uint32 a, uint32 b)
bool
char4ge(uint32 a, uint32 b)
{
return(strncmp((char *) &a, (char *) &b, 4) >= 0);
return (strncmp((char *) &a, (char *) &b, 4) >= 0);
}
int32 char4cmp(uint32 a, uint32 b)
int32
char4cmp(uint32 a, uint32 b)
{
return(strncmp((char *) &a, (char *) &b, 4));
return (strncmp((char *) &a, (char *) &b, 4));
}
/* ============================== char8 ============================== */
char *char8in(char *s)
char *
char8in(char *s)
{
char *result;
if (s == NULL)
return((char *) NULL);
result = (char *) palloc(8);
strncpy(result, s, 8);
return(result);
char *result;
if (s == NULL)
return ((char *) NULL);
result = (char *) palloc(8);
strncpy(result, s, 8);
return (result);
}
char *char8out(char *s)
char *
char8out(char *s)
{
char *result = (char *) palloc(9);
if (s == NULL) {
result[0] = '-';
result[1] = '\0';
} else
strNcpy(result, s, 8);
return(result);
char *result = (char *) palloc(9);
if (s == NULL)
{
result[0] = '-';
result[1] = '\0';
}
else
strNcpy(result, s, 8);
return (result);
}
bool char8eq(char *arg1, char *arg2)
bool
char8eq(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 8) == 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 8) == 0);
}
bool char8ne(char *arg1, char *arg2)
bool
char8ne(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 8) != 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 8) != 0);
}
bool char8lt(char *arg1, char *arg2)
bool
char8lt(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 8) < 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 8) < 0);
}
bool char8le(char *arg1, char *arg2)
bool
char8le(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 8) <= 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 8) <= 0);
}
bool char8gt(char *arg1, char *arg2)
bool
char8gt(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 8) > 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 8) > 0);
}
bool char8ge(char *arg1, char *arg2)
bool
char8ge(char *arg1, char *arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return(strncmp(arg1, arg2, 8) >= 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return (strncmp(arg1, arg2, 8) >= 0);
}
int32 char8cmp(char *arg1, char *arg2)
int32
char8cmp(char *arg1, char *arg2)
{
return(strncmp(arg1, arg2, 8));
return (strncmp(arg1, arg2, 8));
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,17 +1,17 @@
/*-------------------------------------------------------------------------
*
* datetime.c--
* implements DATE and TIME data types specified in SQL-92 standard
* implements DATE and TIME data types specified in SQL-92 standard
*
* Copyright (c) 1994-5, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.13 1997/09/05 18:11:10 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.14 1997/09/07 04:50:08 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h> /* for sprintf() */
#include <stdio.h> /* for sprintf() */
#include <string.h>
#include <limits.h>
@@ -25,12 +25,12 @@
#include "utils/datetime.h"
#include "access/xact.h"
static int date2tm(DateADT dateVal, int *tzp, struct tm *tm, double *fsec, char **tzn);
static int date2tm(DateADT dateVal, int *tzp, struct tm * tm, double *fsec, char **tzn);
static int day_tab[2][12] = {
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31} };
static int day_tab[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
#define isleap(y) (((y % 4) == 0 && (y % 100) != 0) || (y % 400) == 0)
@@ -49,7 +49,7 @@ static int day_tab[2][12] = {
|| ((m == UTIME_MAXMONTH) && (d <= UTIME_MAXDAY))))))
/*****************************************************************************
* Date ADT
* Date ADT
*****************************************************************************/
@@ -59,234 +59,250 @@ static int day_tab[2][12] = {
DateADT
date_in(char *str)
{
DateADT date;
double fsec;
struct tm tt, *tm = &tt;
int tzp;
int dtype;
int nf;
char *field[MAXDATEFIELDS];
int ftype[MAXDATEFIELDS];
char lowstr[MAXDATELEN+1];
DateADT date;
double fsec;
struct tm tt,
*tm = &tt;
int tzp;
int dtype;
int nf;
char *field[MAXDATEFIELDS];
int ftype[MAXDATEFIELDS];
char lowstr[MAXDATELEN + 1];
if (!PointerIsValid(str))
elog(WARN,"Bad (null) date external representation",NULL);
if (!PointerIsValid(str))
elog(WARN, "Bad (null) date external representation", NULL);
#ifdef DATEDEBUG
printf( "date_in- input string is %s\n", str);
printf("date_in- input string is %s\n", str);
#endif
if ((ParseDateTime( str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|| (DecodeDateTime( field, ftype, nf, &dtype, tm, &fsec, &tzp) != 0))
elog(WARN,"Bad date external representation %s",str);
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|| (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp) != 0))
elog(WARN, "Bad date external representation %s", str);
switch (dtype) {
case DTK_DATE:
break;
switch (dtype)
{
case DTK_DATE:
break;
case DTK_CURRENT:
GetCurrentTime(tm);
break;
case DTK_CURRENT:
GetCurrentTime(tm);
break;
case DTK_EPOCH:
tm->tm_year = 1970;
tm->tm_mon = 1;
tm->tm_mday = 1;
break;
case DTK_EPOCH:
tm->tm_year = 1970;
tm->tm_mon = 1;
tm->tm_mday = 1;
break;
default:
elog(WARN,"Unrecognized date external representation %s",str);
}
default:
elog(WARN, "Unrecognized date external representation %s", str);
}
if (tm->tm_year < 0 || tm->tm_year > 32767)
elog(WARN, "date_in: year must be limited to values 0 through 32767 in '%s'", str);
if (tm->tm_mon < 1 || tm->tm_mon > 12)
elog(WARN, "date_in: month must be limited to values 1 through 12 in '%s'", str);
if (tm->tm_mday < 1 || tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon-1])
elog(WARN, "date_in: day must be limited to values 1 through %d in '%s'",
day_tab[isleap(tm->tm_year)][tm->tm_mon-1], str);
if (tm->tm_year < 0 || tm->tm_year > 32767)
elog(WARN, "date_in: year must be limited to values 0 through 32767 in '%s'", str);
if (tm->tm_mon < 1 || tm->tm_mon > 12)
elog(WARN, "date_in: month must be limited to values 1 through 12 in '%s'", str);
if (tm->tm_mday < 1 || tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
elog(WARN, "date_in: day must be limited to values 1 through %d in '%s'",
day_tab[isleap(tm->tm_year)][tm->tm_mon - 1], str);
date = (date2j( tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000,1,1));
date = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1));
return(date);
} /* date_in() */
return (date);
} /* date_in() */
/* date_out()
* Given internal format date, convert to text string.
*/
char *
char *
date_out(DateADT date)
{
char *result;
struct tm tt, *tm = &tt;
char buf[MAXDATELEN+1];
char *result;
struct tm tt,
*tm = &tt;
char buf[MAXDATELEN + 1];
#if FALSE
int year, month, day;
int year,
month,
day;
#endif
j2date( (date + date2j(2000,1,1)),
&(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
j2date((date + date2j(2000, 1, 1)),
&(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
EncodeDateOnly( tm, DateStyle, buf);
EncodeDateOnly(tm, DateStyle, buf);
#if FALSE
if (EuroDates == 1) /* Output European-format dates */
sprintf(buf, "%02d-%02d-%04d", day, month, year);
else
sprintf(buf, "%02d-%02d-%04d", month, day, year);
if (EuroDates == 1) /* Output European-format dates */
sprintf(buf, "%02d-%02d-%04d", day, month, year);
else
sprintf(buf, "%02d-%02d-%04d", month, day, year);
#endif
result = PALLOC(strlen(buf)+1);
result = PALLOC(strlen(buf) + 1);
strcpy( result, buf);
strcpy(result, buf);
return(result);
} /* date_out() */
return (result);
} /* date_out() */
bool
date_eq(DateADT dateVal1, DateADT dateVal2)
{
return(dateVal1 == dateVal2);
return (dateVal1 == dateVal2);
}
bool
date_ne(DateADT dateVal1, DateADT dateVal2)
{
return(dateVal1 != dateVal2);
return (dateVal1 != dateVal2);
}
bool
date_lt(DateADT dateVal1, DateADT dateVal2)
{
return(dateVal1 < dateVal2);
} /* date_lt() */
return (dateVal1 < dateVal2);
} /* date_lt() */
bool
date_le(DateADT dateVal1, DateADT dateVal2)
{
return(dateVal1 <= dateVal2);
} /* date_le() */
return (dateVal1 <= dateVal2);
} /* date_le() */
bool
date_gt(DateADT dateVal1, DateADT dateVal2)
{
return(dateVal1 > dateVal2);
} /* date_gt() */
return (dateVal1 > dateVal2);
} /* date_gt() */
bool
date_ge(DateADT dateVal1, DateADT dateVal2)
{
return(dateVal1 >= dateVal2);
} /* date_ge() */
return (dateVal1 >= dateVal2);
} /* date_ge() */
int
date_cmp(DateADT dateVal1, DateADT dateVal2)
{
if (dateVal1 < dateVal2) {
return -1;
} else if (dateVal1 > dateVal2) {
return 1;
}
return 0;
} /* date_cmp() */
if (dateVal1 < dateVal2)
{
return -1;
}
else if (dateVal1 > dateVal2)
{
return 1;
}
return 0;
} /* date_cmp() */
DateADT
date_larger(DateADT dateVal1, DateADT dateVal2)
{
return(date_gt(dateVal1, dateVal2) ? dateVal1 : dateVal2);
} /* date_larger() */
return (date_gt(dateVal1, dateVal2) ? dateVal1 : dateVal2);
} /* date_larger() */
DateADT
date_smaller(DateADT dateVal1, DateADT dateVal2)
{
return(date_lt(dateVal1, dateVal2) ? dateVal1 : dateVal2);
} /* date_smaller() */
return (date_lt(dateVal1, dateVal2) ? dateVal1 : dateVal2);
} /* date_smaller() */
/* Compute difference between two dates in days. */
int4
date_mi(DateADT dateVal1, DateADT dateVal2)
{
return(dateVal1-dateVal2);
} /* date_mi() */
return (dateVal1 - dateVal2);
} /* date_mi() */
/* Add a number of days to a date, giving a new date.
Must handle both positive and negative numbers of days. */
Must handle both positive and negative numbers of days. */
DateADT
date_pli(DateADT dateVal, int4 days)
{
return(dateVal+days);
} /* date_pli() */
return (dateVal + days);
} /* date_pli() */
/* Subtract a number of days from a date, giving a new date. */
DateADT
date_mii(DateADT dateVal, int4 days)
{
return(date_pli(dateVal, -days));
} /* date_mii() */
return (date_pli(dateVal, -days));
} /* date_mii() */
/* date_datetime()
* Convert date to datetime data type.
*/
DateTime *
DateTime *
date_datetime(DateADT dateVal)
{
DateTime *result;
struct tm tt, *tm = &tt;
int tz;
double fsec = 0;
char *tzn;
DateTime *result;
struct tm tt,
*tm = &tt;
int tz;
double fsec = 0;
char *tzn;
result = PALLOCTYPE(DateTime);
result = PALLOCTYPE(DateTime);
if (date2tm( dateVal, &tz, tm, &fsec, &tzn) != 0)
elog(WARN,"Unable to convert date to datetime",NULL);
if (date2tm(dateVal, &tz, tm, &fsec, &tzn) != 0)
elog(WARN, "Unable to convert date to datetime", NULL);
#ifdef DATEDEBUG
printf( "date_datetime- date is %d.%02d.%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday);
printf( "date_datetime- time is %02d:%02d:%02d %.7f\n", tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
printf("date_datetime- date is %d.%02d.%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday);
printf("date_datetime- time is %02d:%02d:%02d %.7f\n", tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
#endif
if (tm2datetime( tm, fsec, &tz, result) != 0)
elog(WARN,"Datetime out of range",NULL);
if (tm2datetime(tm, fsec, &tz, result) != 0)
elog(WARN, "Datetime out of range", NULL);
return(result);
} /* date_datetime() */
return (result);
} /* date_datetime() */
/* datetime_date()
* Convert datetime to date data type.
*/
DateADT
datetime_date(DateTime *datetime)
datetime_date(DateTime * datetime)
{
DateADT result;
struct tm tt, *tm = &tt;
int tz;
double fsec;
char *tzn;
DateADT result;
struct tm tt,
*tm = &tt;
int tz;
double fsec;
char *tzn;
if (!PointerIsValid(datetime))
elog(WARN,"Unable to convert null datetime to date",NULL);
if (!PointerIsValid(datetime))
elog(WARN, "Unable to convert null datetime to date", NULL);
if (DATETIME_NOT_FINITE(*datetime))
elog(WARN,"Unable to convert datetime to date",NULL);
if (DATETIME_NOT_FINITE(*datetime))
elog(WARN, "Unable to convert datetime to date", NULL);
if (DATETIME_IS_EPOCH(*datetime)) {
datetime2tm( SetDateTime(*datetime), NULL, tm, &fsec, NULL);
if (DATETIME_IS_EPOCH(*datetime))
{
datetime2tm(SetDateTime(*datetime), NULL, tm, &fsec, NULL);
} else if (DATETIME_IS_CURRENT(*datetime)) {
datetime2tm( SetDateTime(*datetime), &tz, tm, &fsec, &tzn);
}
else if (DATETIME_IS_CURRENT(*datetime))
{
datetime2tm(SetDateTime(*datetime), &tz, tm, &fsec, &tzn);
} else {
if (datetime2tm( *datetime, &tz, tm, &fsec, &tzn) != 0)
elog(WARN,"Unable to convert datetime to date",NULL);
}
}
else
{
if (datetime2tm(*datetime, &tz, tm, &fsec, &tzn) != 0)
elog(WARN, "Unable to convert datetime to date", NULL);
}
result = (date2j( tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j( 2000, 1, 1));
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1));
return(result);
} /* datetime_date() */
return (result);
} /* datetime_date() */
/* abstime_date()
@@ -295,289 +311,320 @@ datetime_date(DateTime *datetime)
DateADT
abstime_date(AbsoluteTime abstime)
{
DateADT result;
struct tm tt, *tm = &tt;
int tz;
DateADT result;
struct tm tt,
*tm = &tt;
int tz;
switch (abstime) {
case INVALID_ABSTIME:
case NOSTART_ABSTIME:
case NOEND_ABSTIME:
elog(WARN,"Unable to convert reserved abstime value to date",NULL);
/* pretend to drop through to make compiler think that result will be set */
switch (abstime)
{
case INVALID_ABSTIME:
case NOSTART_ABSTIME:
case NOEND_ABSTIME:
elog(WARN, "Unable to convert reserved abstime value to date", NULL);
case EPOCH_ABSTIME:
result = date2j(1970,1,1) - date2j(2000,1,1);
break;
/*
* pretend to drop through to make compiler think that result will
* be set
*/
case CURRENT_ABSTIME:
GetCurrentTime(tm);
result = date2j( tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000,1,1);
break;
case EPOCH_ABSTIME:
result = date2j(1970, 1, 1) - date2j(2000, 1, 1);
break;
default:
abstime2tm(abstime, &tz, tm, NULL);
result = date2j(tm->tm_year,tm->tm_mon,tm->tm_mday) - date2j(2000,1,1);
break;
}
case CURRENT_ABSTIME:
GetCurrentTime(tm);
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
break;
return(result);
} /* abstime_date() */
default:
abstime2tm(abstime, &tz, tm, NULL);
result = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
break;
}
return (result);
} /* abstime_date() */
/* date2tm()
* Convert date to time structure.
* Note that date is an implicit local time, but the system calls assume
* that everything is GMT. So, convert to GMT, rotate to local time,
* and then convert again to try to get the time zones correct.
* that everything is GMT. So, convert to GMT, rotate to local time,
* and then convert again to try to get the time zones correct.
*/
static int
date2tm(DateADT dateVal, int *tzp, struct tm *tm, double *fsec, char **tzn)
date2tm(DateADT dateVal, int *tzp, struct tm * tm, double *fsec, char **tzn)
{
struct tm *tx;
time_t utime;
*fsec = 0;
struct tm *tx;
time_t utime;
j2date( (dateVal + date2j( 2000, 1, 1)), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
tm->tm_isdst = -1;
*fsec = 0;
if (IS_VALID_UTIME( tm->tm_year, tm->tm_mon, tm->tm_mday)) {
/* convert to system time */
utime = ((dateVal + (date2j(2000,1,1)-date2j(1970,1,1)))*86400);
utime += (12*60*60); /* rotate to noon to get the right day in time zone */
#ifdef USE_POSIX_TIME
tx = localtime(&utime);
#ifdef DATEDEBUG
#ifdef HAVE_INT_TIMEZONE
printf( "date2tm- (localtime) %d.%02d.%02d %02d:%02d:%02.0f %s %s dst=%d\n",
tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, (double) tm->tm_sec,
tzname[0], tzname[1], tx->tm_isdst);
#endif
#endif
tm->tm_year = tx->tm_year + 1900;
tm->tm_mon = tx->tm_mon + 1;
tm->tm_mday = tx->tm_mday;
#if FALSE
tm->tm_hour = tx->tm_hour;
tm->tm_min = tx->tm_min;
tm->tm_sec = tx->tm_sec;
#endif
tm->tm_isdst = tx->tm_isdst;
#ifdef HAVE_INT_TIMEZONE
*tzp = (tm->tm_isdst? (timezone - 3600): timezone);
if (tzn != NULL) *tzn = tzname[(tm->tm_isdst > 0)];
#else /* !HAVE_INT_TIMEZONE */
tm->tm_gmtoff = tx->tm_gmtoff;
tm->tm_zone = tx->tm_zone;
*tzp = (tm->tm_isdst? (tm->tm_gmtoff - 3600): tm->tm_gmtoff); /* tm_gmtoff is Sun/DEC-ism */
if (tzn != NULL) *tzn = tm->tm_zone;
#endif
#else /* !USE_POSIX_TIME */
*tzp = CTimeZone; /* V7 conventions; don't know timezone? */
if (tzn != NULL) *tzn = CTZName;
#endif
/* otherwise, outside of timezone range so convert to GMT... */
} else {
#if FALSE
j2date( (dateVal + date2j( 2000, 1, 1)), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
j2date((dateVal + date2j(2000, 1, 1)), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
tm->tm_isdst = -1;
if (IS_VALID_UTIME(tm->tm_year, tm->tm_mon, tm->tm_mday))
{
/* convert to system time */
utime = ((dateVal + (date2j(2000, 1, 1) - date2j(1970, 1, 1))) * 86400);
utime += (12 * 60 * 60);/* rotate to noon to get the right day in
* time zone */
#ifdef USE_POSIX_TIME
tx = localtime(&utime);
#ifdef DATEDEBUG
#ifdef HAVE_INT_TIMEZONE
printf("date2tm- (localtime) %d.%02d.%02d %02d:%02d:%02.0f %s %s dst=%d\n",
tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, (double) tm->tm_sec,
tzname[0], tzname[1], tx->tm_isdst);
#endif
#endif
tm->tm_year = tx->tm_year + 1900;
tm->tm_mon = tx->tm_mon + 1;
tm->tm_mday = tx->tm_mday;
#if FALSE
tm->tm_hour = tx->tm_hour;
tm->tm_min = tx->tm_min;
tm->tm_sec = tx->tm_sec;
#endif
tm->tm_isdst = tx->tm_isdst;
#ifdef HAVE_INT_TIMEZONE
*tzp = (tm->tm_isdst ? (timezone - 3600) : timezone);
if (tzn != NULL)
*tzn = tzname[(tm->tm_isdst > 0)];
#else /* !HAVE_INT_TIMEZONE */
tm->tm_gmtoff = tx->tm_gmtoff;
tm->tm_zone = tx->tm_zone;
*tzp = (tm->tm_isdst ? (tm->tm_gmtoff - 3600) : tm->tm_gmtoff); /* tm_gmtoff is
* Sun/DEC-ism */
if (tzn != NULL)
*tzn = tm->tm_zone;
#endif
#else /* !USE_POSIX_TIME */
*tzp = CTimeZone; /* V7 conventions; don't know timezone? */
if (tzn != NULL)
*tzn = CTZName;
#endif
/* otherwise, outside of timezone range so convert to GMT... */
}
else
{
#if FALSE
j2date((dateVal + date2j(2000, 1, 1)), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
#endif
#ifdef DATEDEBUG
printf( "date2tm- convert %d-%d-%d %d:%d%d to datetime\n",
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
printf("date2tm- convert %d-%d-%d %d:%d%d to datetime\n",
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
#endif
*tzp = 0;
tm->tm_isdst = 0;
if (tzn != NULL) *tzn = NULL;
}
*tzp = 0;
tm->tm_isdst = 0;
if (tzn != NULL)
*tzn = NULL;
}
return 0;
} /* date2tm() */
return 0;
} /* date2tm() */
/*****************************************************************************
* Time ADT
* Time ADT
*****************************************************************************/
TimeADT *
TimeADT *
time_in(char *str)
{
TimeADT *time;
TimeADT *time;
double fsec;
struct tm tt, *tm = &tt;
double fsec;
struct tm tt,
*tm = &tt;
int nf;
char lowstr[MAXDATELEN+1];
char *field[MAXDATEFIELDS];
int dtype;
int ftype[MAXDATEFIELDS];
int nf;
char lowstr[MAXDATELEN + 1];
char *field[MAXDATEFIELDS];
int dtype;
int ftype[MAXDATEFIELDS];
if (!PointerIsValid(str))
elog(WARN,"Bad (null) time external representation",NULL);
if (!PointerIsValid(str))
elog(WARN, "Bad (null) time external representation", NULL);
if ((ParseDateTime( str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|| (DecodeTimeOnly( field, ftype, nf, &dtype, tm, &fsec) != 0))
elog(WARN,"Bad time external representation '%s'",str);
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|| (DecodeTimeOnly(field, ftype, nf, &dtype, tm, &fsec) != 0))
elog(WARN, "Bad time external representation '%s'", str);
if ((tm->tm_hour < 0) || (tm->tm_hour > 23))
elog(WARN,"Hour must be limited to values 0 through 23 in '%s'",str);
if ((tm->tm_min < 0) || (tm->tm_min > 59))
elog(WARN,"Minute must be limited to values 0 through 59 in '%s'",str);
if ((tm->tm_sec < 0) || ((tm->tm_sec + fsec) >= 60))
elog(WARN,"Second must be limited to values 0 through < 60 in '%s'",str);
if ((tm->tm_hour < 0) || (tm->tm_hour > 23))
elog(WARN, "Hour must be limited to values 0 through 23 in '%s'", str);
if ((tm->tm_min < 0) || (tm->tm_min > 59))
elog(WARN, "Minute must be limited to values 0 through 59 in '%s'", str);
if ((tm->tm_sec < 0) || ((tm->tm_sec + fsec) >= 60))
elog(WARN, "Second must be limited to values 0 through < 60 in '%s'", str);
time = PALLOCTYPE(TimeADT);
time = PALLOCTYPE(TimeADT);
*time = ((((tm->tm_hour*60)+tm->tm_min)*60)+tm->tm_sec+fsec);
*time = ((((tm->tm_hour * 60) + tm->tm_min) * 60) + tm->tm_sec + fsec);
return(time);
} /* time_in() */
return (time);
} /* time_in() */
char *
time_out(TimeADT *time)
char *
time_out(TimeADT * time)
{
char *result;
struct tm tt, *tm = &tt;
char *result;
struct tm tt,
*tm = &tt;
#if FALSE
int hour, min, sec;
int hour,
min,
sec;
#endif
double fsec;
char buf[MAXDATELEN+1];
double fsec;
char buf[MAXDATELEN + 1];
if (!PointerIsValid(time))
return NULL;
if (!PointerIsValid(time))
return NULL;
tm->tm_hour = (*time / (60*60));
tm->tm_min = (((int) (*time / 60)) % 60);
tm->tm_sec = (((int) *time) % 60);
tm->tm_hour = (*time / (60 * 60));
tm->tm_min = (((int) (*time / 60)) % 60);
tm->tm_sec = (((int) *time) % 60);
fsec = 0;
fsec = 0;
EncodeTimeOnly( tm, fsec, DateStyle, buf);
EncodeTimeOnly(tm, fsec, DateStyle, buf);
#if FALSE
if (sec == 0.0) {
sprintf(buf, "%02d:%02d", hour, min);
if (sec == 0.0)
{
sprintf(buf, "%02d:%02d", hour, min);
} else {
if (fsec == 0) {
sprintf(buf, "%02d:%02d:%02d", hour, min, sec);
} else {
sprintf(buf, "%02d:%02d:%05.2f", hour, min, (sec+fsec));
}
}
else
{
if (fsec == 0)
{
sprintf(buf, "%02d:%02d:%02d", hour, min, sec);
}
else
{
sprintf(buf, "%02d:%02d:%05.2f", hour, min, (sec + fsec));
}
}
#endif
result = PALLOC(strlen(buf)+1);
result = PALLOC(strlen(buf) + 1);
strcpy( result, buf);
strcpy(result, buf);
return(result);
} /* time_out() */
return (result);
} /* time_out() */
bool
time_eq(TimeADT *time1, TimeADT *time2)
time_eq(TimeADT * time1, TimeADT * time2)
{
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return(FALSE);
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return (FALSE);
return(*time1 == *time2);
} /* time_eq() */
return (*time1 == *time2);
} /* time_eq() */
bool
time_ne(TimeADT *time1, TimeADT *time2)
time_ne(TimeADT * time1, TimeADT * time2)
{
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return(FALSE);
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return (FALSE);
return(*time1 != *time2);
} /* time_eq() */
return (*time1 != *time2);
} /* time_eq() */
bool
time_lt(TimeADT *time1, TimeADT *time2)
time_lt(TimeADT * time1, TimeADT * time2)
{
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return(FALSE);
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return (FALSE);
return(*time1 < *time2);
} /* time_eq() */
return (*time1 < *time2);
} /* time_eq() */
bool
time_le(TimeADT *time1, TimeADT *time2)
time_le(TimeADT * time1, TimeADT * time2)
{
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return(FALSE);
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return (FALSE);
return(*time1 <= *time2);
} /* time_eq() */
return (*time1 <= *time2);
} /* time_eq() */
bool
time_gt(TimeADT *time1, TimeADT *time2)
time_gt(TimeADT * time1, TimeADT * time2)
{
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return(FALSE);
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return (FALSE);
return(*time1 > *time2);
} /* time_eq() */
return (*time1 > *time2);
} /* time_eq() */
bool
time_ge(TimeADT *time1, TimeADT *time2)
time_ge(TimeADT * time1, TimeADT * time2)
{
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return(FALSE);
if (!PointerIsValid(time1) || !PointerIsValid(time2))
return (FALSE);
return(*time1 >= *time2);
} /* time_eq() */
return (*time1 >= *time2);
} /* time_eq() */
int
time_cmp(TimeADT *time1, TimeADT *time2)
time_cmp(TimeADT * time1, TimeADT * time2)
{
return((*time1 < *time2)? -1: (((*time1 > *time2)? 1: 0)));
} /* time_cmp() */
return ((*time1 < *time2) ? -1 : (((*time1 > *time2) ? 1 : 0)));
} /* time_cmp() */
/* datetime_datetime()
* Convert date and time to datetime data type.
*/
DateTime *
datetime_datetime(DateADT date, TimeADT *time)
DateTime *
datetime_datetime(DateADT date, TimeADT * time)
{
DateTime *result;
DateTime *result;
if (!PointerIsValid(time)) {
result = PALLOCTYPE(DateTime);
DATETIME_INVALID(*result);
if (!PointerIsValid(time))
{
result = PALLOCTYPE(DateTime);
DATETIME_INVALID(*result);
} else {
result = date_datetime(date);
*result += *time;
}
}
else
{
result = date_datetime(date);
*result += *time;
}
return(result);
} /* datetime_datetime() */
return (result);
} /* datetime_datetime() */
int32 /* RelativeTime */
int32 /* RelativeTime */
int42reltime(int32 timevalue)
{
return(timevalue);
return (timevalue);
}

View File

@@ -6,7 +6,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datum.c,v 1.5 1997/08/19 21:34:33 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/datum.c,v 1.6 1997/09/07 04:50:09 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -49,39 +49,51 @@
Size
datumGetSize(Datum value, Oid type, bool byVal, Size len)
{
struct varlena *s;
Size size = 0;
if (byVal) {
if (len <= sizeof(Datum)) {
size = len;
} else {
elog(WARN,
"datumGetSize: Error: type=%ld, byVaL with len=%d",
(long) type, len);
struct varlena *s;
Size size = 0;
if (byVal)
{
if (len <= sizeof(Datum))
{
size = len;
}
else
{
elog(WARN,
"datumGetSize: Error: type=%ld, byVaL with len=%d",
(long) type, len);
}
}
} else { /* not byValue */
if (len == -1) {
/*
* variable length type
* Look at the varlena struct for its real length...
*/
s = (struct varlena *) DatumGetPointer(value);
if (!PointerIsValid(s)) {
elog(WARN,
"datumGetSize: Invalid Datum Pointer");
}
size = (Size) VARSIZE(s);
} else {
/*
* fixed length type
*/
size = len;
else
{ /* not byValue */
if (len == -1)
{
/*
* variable length type Look at the varlena struct for its
* real length...
*/
s = (struct varlena *) DatumGetPointer(value);
if (!PointerIsValid(s))
{
elog(WARN,
"datumGetSize: Invalid Datum Pointer");
}
size = (Size) VARSIZE(s);
}
else
{
/*
* fixed length type
*/
size = len;
}
}
}
return(size);
return (size);
}
/*-------------------------------------------------------------------------
@@ -97,29 +109,35 @@ datumGetSize(Datum value, Oid type, bool byVal, Size len)
Datum
datumCopy(Datum value, Oid type, bool byVal, Size len)
{
Size realSize;
Datum res;
char *s;
if (byVal) {
res = value;
} else {
if (value == 0) return((Datum)NULL);
realSize = datumGetSize(value, type, byVal, len);
/*
* the value is a pointer. Allocate enough space
* and copy the pointed data.
*/
s = (char *) palloc(realSize);
if (s == NULL) {
elog(WARN,"datumCopy: out of memory\n");
Size realSize;
Datum res;
char *s;
if (byVal)
{
res = value;
}
memmove(s, DatumGetPointer(value), realSize);
res = (Datum)s;
}
return(res);
else
{
if (value == 0)
return ((Datum) NULL);
realSize = datumGetSize(value, type, byVal, len);
/*
* the value is a pointer. Allocate enough space and copy the
* pointed data.
*/
s = (char *) palloc(realSize);
if (s == NULL)
{
elog(WARN, "datumCopy: out of memory\n");
}
memmove(s, DatumGetPointer(value), realSize);
res = (Datum) s;
}
return (res);
}
/*-------------------------------------------------------------------------
@@ -135,20 +153,23 @@ datumCopy(Datum value, Oid type, bool byVal, Size len)
void
datumFree(Datum value, Oid type, bool byVal, Size len)
{
Size realSize;
Pointer s;
realSize = datumGetSize(value, type, byVal, len);
if (!byVal) {
/*
* free the space palloced by "datumCopy()"
*/
s = DatumGetPointer(value);
pfree(s);
}
Size realSize;
Pointer s;
realSize = datumGetSize(value, type, byVal, len);
if (!byVal)
{
/*
* free the space palloced by "datumCopy()"
*/
s = DatumGetPointer(value);
pfree(s);
}
}
#endif
/*-------------------------------------------------------------------------
@@ -167,36 +188,40 @@ datumFree(Datum value, Oid type, bool byVal, Size len)
bool
datumIsEqual(Datum value1, Datum value2, Oid type, bool byVal, Size len)
{
Size size1, size2;
char *s1, *s2;
if (byVal) {
/*
* just compare the two datums.
* NOTE: just comparing "len" bytes will not do the
* work, because we do not know how these bytes
* are aligned inside the "Datum".
*/
if (value1 == value2)
return(true);
else
return(false);
} else {
/*
* byVal = false
* Compare the bytes pointed by the pointers stored in the
* datums.
*/
size1 = datumGetSize(value1, type, byVal, len);
size2 = datumGetSize(value2, type, byVal, len);
if (size1 != size2)
return(false);
s1 = (char *) DatumGetPointer(value1);
s2 = (char *) DatumGetPointer(value2);
if (!memcmp(s1, s2, size1))
return(true);
else
return(false);
}
}
Size size1,
size2;
char *s1,
*s2;
if (byVal)
{
/*
* just compare the two datums. NOTE: just comparing "len" bytes
* will not do the work, because we do not know how these bytes
* are aligned inside the "Datum".
*/
if (value1 == value2)
return (true);
else
return (false);
}
else
{
/*
* byVal = false Compare the bytes pointed by the pointers stored
* in the datums.
*/
size1 = datumGetSize(value1, type, byVal, len);
size2 = datumGetSize(value2, type, byVal, len);
if (size1 != size2)
return (false);
s1 = (char *) DatumGetPointer(value1);
s2 = (char *) DatumGetPointer(value2);
if (!memcmp(s1, s2, size1))
return (true);
else
return (false);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* filename.c--
*
*
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/filename.c,v 1.8 1997/08/12 20:15:58 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/filename.c,v 1.9 1997/09/07 04:50:14 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -20,95 +20,118 @@
#include "postgres.h"
#include <miscadmin.h>
#include "utils/builtins.h" /* where function declarations go */
#include "utils/builtins.h" /* where function declarations go */
char *
char *
filename_in(char *file)
{
char *str;
int ind = 0;
/*
* XXX - HACK CITY --- REDO
* should let the shell do expansions (shexpand)
*/
char *str;
int ind = 0;
str = (char *) palloc(MAXPATHLEN * sizeof(*str));
str[0] = '\0';
if (file[0] == '~') {
if (file[1] == '\0' || file[1] == '/') {
/* Home directory */
char *userName;
struct passwd *pw;
userName = GetPgUserName();
if ((pw = getpwnam(userName)) == NULL) {
elog(WARN, "User %s is not a Unix user on the db server.",
userName);
}
strcpy(str, pw->pw_dir);
ind = 1;
} else {
/* Someone else's directory */
char name[16], *p;
struct passwd *pw;
int len;
if ((p = (char *) strchr(file, '/')) == NULL) {
strcpy(name, file+1);
len = strlen(name);
} else {
len = (p - file) - 1;
strNcpy(name, file+1, len);
}
/*printf("name: %s\n");*/
if ((pw = getpwnam(name)) == NULL) {
elog(WARN, "No such user: %s\n", name);
/*
* XXX - HACK CITY --- REDO should let the shell do expansions
* (shexpand)
*/
str = (char *) palloc(MAXPATHLEN * sizeof(*str));
str[0] = '\0';
if (file[0] == '~')
{
if (file[1] == '\0' || file[1] == '/')
{
/* Home directory */
char *userName;
struct passwd *pw;
userName = GetPgUserName();
if ((pw = getpwnam(userName)) == NULL)
{
elog(WARN, "User %s is not a Unix user on the db server.",
userName);
}
strcpy(str, pw->pw_dir);
ind = 1;
}
else
{
/* Someone else's directory */
char name[16],
*p;
struct passwd *pw;
int len;
if ((p = (char *) strchr(file, '/')) == NULL)
{
strcpy(name, file + 1);
len = strlen(name);
}
else
{
len = (p - file) - 1;
strNcpy(name, file + 1, len);
}
/* printf("name: %s\n"); */
if ((pw = getpwnam(name)) == NULL)
{
elog(WARN, "No such user: %s\n", name);
ind = 0;
}
else
{
strcpy(str, pw->pw_dir);
ind = len + 1;
}
}
}
else if (file[0] == '$')
{ /* $POSTGRESHOME, etc. expand it. */
char environment[80],
*envirp,
*p;
int len;
if ((p = (char *) strchr(file, '/')) == NULL)
{
strcpy(environment, file + 1);
len = strlen(environment);
}
else
{
len = (p - file) - 1;
strNcpy(environment, file + 1, len);
}
envirp = getenv(environment);
if (envirp)
{
strcpy(str, envirp);
ind = len + 1;
}
else
{
elog(WARN, "Couldn't find %s in your environment", environment);
}
}
else
{
ind = 0;
} else {
strcpy(str, pw->pw_dir);
ind = len + 1;
}
}
} else if (file[0] == '$') { /* $POSTGRESHOME, etc. expand it. */
char environment[80], *envirp, *p;
int len;
if ((p = (char *) strchr(file, '/')) == NULL) {
strcpy(environment, file+1);
len = strlen(environment);
} else {
len = (p - file) - 1;
strNcpy(environment, file+1, len);
}
envirp = getenv(environment);
if (envirp) {
strcpy(str, envirp);
ind = len + 1;
}
else {
elog(WARN,"Couldn't find %s in your environment", environment);
}
} else {
ind = 0;
}
strcat(str, file+ind);
return(str);
strcat(str, file + ind);
return (str);
}
char *
char *
filename_out(char *s)
{
char *ret;
if (!s)
return((char *) NULL);
ret = (char *) palloc(strlen(s) + 1);
if (!ret)
elog(WARN, "filename_out: palloc failed");
return(strcpy(ret, s));
char *ret;
if (!s)
return ((char *) NULL);
ret = (char *) palloc(strlen(s) + 1);
if (!ret)
elog(WARN, "filename_out: palloc failed");
return (strcpy(ret, s));
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,16 @@
/*-------------------------------------------------------------------------
*
* geo-selfuncs.c--
* Selectivity routines registered in the operator catalog in the
* "oprrest" and "oprjoin" attributes.
* Selectivity routines registered in the operator catalog in the
* "oprrest" and "oprjoin" attributes.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_selfuncs.c,v 1.3 1997/08/19 21:34:40 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_selfuncs.c,v 1.4 1997/09/07 04:50:20 momjian Exp $
*
* XXX These are totally bogus.
* XXX These are totally bogus.
*
*-------------------------------------------------------------------------
*/
@@ -22,112 +22,116 @@
#include "utils/builtins.h"
float64
areasel(Oid opid,
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
areasel(Oid opid,
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
{
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 4.0;
return(result);
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 4.0;
return (result);
}
float64
areajoinsel(Oid opid,
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
{
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 4.0;
return(result);
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 4.0;
return (result);
}
/*
* Selectivity functions for rtrees. These are bogus -- unless we know
* the actual key distribution in the index, we can't make a good prediction
* of the selectivity of these operators.
* Selectivity functions for rtrees. These are bogus -- unless we know
* the actual key distribution in the index, we can't make a good prediction
* of the selectivity of these operators.
*
* In general, rtrees need to search multiple subtrees in order to guarantee
* that all occurrences of the same key have been found. Because of this,
* the heuristic selectivity functions we return are higher than they would
* otherwise be.
* In general, rtrees need to search multiple subtrees in order to guarantee
* that all occurrences of the same key have been found. Because of this,
* the heuristic selectivity functions we return are higher than they would
* otherwise be.
*/
/*
* left_sel -- How likely is a box to be strictly left of (right of, above,
* below) a given box?
* left_sel -- How likely is a box to be strictly left of (right of, above,
* below) a given box?
*/
#ifdef NOT_USED
float64
leftsel(Oid opid,
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
{
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 6.0;
return(result);
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 6.0;
return (result);
}
#endif
#ifdef NOT_USED
float64
leftjoinsel(Oid opid,
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
{
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 6.0;
return(result);
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 6.0;
return (result);
}
#endif
/*
* contsel -- How likely is a box to contain (be contained by) a given box?
* contsel -- How likely is a box to contain (be contained by) a given box?
*/
#ifdef NOT_USED
float64
contsel(Oid opid,
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
{
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 10.0;
return(result);
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 10.0;
return (result);
}
#endif
#ifdef NOT_USED
float64
contjoinsel(Oid opid,
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
Oid relid,
AttrNumber attno,
char *value,
int32 flag)
{
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 10.0;
return(result);
float64 result;
result = (float64) palloc(sizeof(float64data));
*result = 1.0 / 10.0;
return (result);
}
#endif

View File

@@ -1,29 +1,29 @@
/*-------------------------------------------------------------------------
*
* int.c--
* Functions for the built-in integer types.
* Functions for the built-in integer types.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.4 1997/03/14 23:20:26 scrappy Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.5 1997/09/07 04:50:21 momjian Exp $
*
*-------------------------------------------------------------------------
*/
/*
* OLD COMMENTS
* I/O routines:
* int2in, int2out, int28in, int28out, int4in, int4out
* Conversion routines:
* itoi
* Boolean operators:
* inteq, intne, intlt, intle, intgt, intge
* Arithmetic operators:
* intpl, intmi, int4mul, intdiv
* I/O routines:
* int2in, int2out, int28in, int28out, int4in, int4out
* Conversion routines:
* itoi
* Boolean operators:
* inteq, intne, intlt, intle, intgt, intge
* Arithmetic operators:
* intpl, intmi, int4mul, intdiv
*
* Arithmetic operators:
* intmod, int4fac
* Arithmetic operators:
* intmod, int4fac
*
* XXX makes massive and possibly unwarranted type promotion assumptions.
* fix me when we figure out what we want to do about ANSIfication...
@@ -32,311 +32,482 @@
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h" /* where the declarations go */
#include "utils/builtins.h" /* where the declarations go */
/*****************************************************************************
* USER I/O ROUTINES *
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
/*
* int2in - converts "num" to short
* int2in - converts "num" to short
*/
int32 int2in(char *num)
int32
int2in(char *num)
{
return((int32) pg_atoi(num, sizeof(int16), '\0'));
return ((int32) pg_atoi(num, sizeof(int16), '\0'));
}
/*
* int2out - converts short to "num"
* int2out - converts short to "num"
*/
char *int2out(int16 sh)
char *
int2out(int16 sh)
{
char *result;
result = (char *)palloc(7); /* assumes sign, 5 digits, '\0' */
itoa((int) sh, result);
return(result);
char *result;
result = (char *) palloc(7);/* assumes sign, 5 digits, '\0' */
itoa((int) sh, result);
return (result);
}
/*
* int28in - converts "num num ..." to internal form
* int28in - converts "num num ..." to internal form
*
* Note:
* Fills any nonexistent digits with NULLs.
* Note:
* Fills any nonexistent digits with NULLs.
*/
int16 *int28in(char *shs)
int16 *
int28in(char *shs)
{
register int16 (*result)[];
int nums;
if (shs == NULL)
return(NULL);
result = (int16 (*)[]) palloc(sizeof(int16 [8]));
if ((nums = sscanf(shs, "%hd%hd%hd%hd%hd%hd%hd%hd",
*result,
*result + 1,
*result + 2,
*result + 3,
*result + 4,
*result + 5,
*result + 6,
*result + 7)) != 8) {
do
(*result)[nums++] = 0;
while (nums < 8);
}
return((int16 *) result);
}
register int16(*result)[];
int nums;
/*
* int28out - converts internal form to "num num ..."
*/
char *int28out(int16 (*shs)[])
{
register int num;
register int16 *sp;
register char *rp;
char *result;
if (shs == NULL) {
result = (char *)palloc(2);
result[0] = '-';
result[1] = '\0';
return(result);
}
rp = result = (char *)palloc(8 * 7); /* assumes sign, 5 digits, ' ' */
sp = *shs;
for (num = 8; num != 0; num--) {
itoa(*sp++, rp);
while (*++rp != '\0')
;
*rp++ = ' ';
}
*--rp = '\0';
return(result);
}
/*
* int28in - converts "num num ..." to internal form
*
* Note:
* Fills any nonexistent digits with NULLs.
*/
int32 *int44in(char *input_string)
{
int32 *foo = (int32 *)palloc(4*sizeof(int32));
register int i = 0;
i = sscanf(input_string,
"%d, %d, %d, %d",
&foo[0],
&foo[1],
&foo[2],
&foo[3]);
while (i < 4)
foo[i++] = 0;
return(foo);
}
/*
* int28out - converts internal form to "num num ..."
*/
char *int44out(int32 an_array[])
{
int temp = 4;
char *output_string = NULL;
int i;
if ( temp > 0 ) {
char *walk;
output_string = (char *)palloc(16*temp); /* assume 15 digits + sign */
walk = output_string;
for ( i = 0 ; i < temp ; i++ ) {
itoa(an_array[i],walk);
while (*++walk != '\0')
;
*walk++ = ' ';
if (shs == NULL)
return (NULL);
result = (int16(*)[]) palloc(sizeof(int16[8]));
if ((nums = sscanf(shs, "%hd%hd%hd%hd%hd%hd%hd%hd",
*result,
*result + 1,
*result + 2,
*result + 3,
*result + 4,
*result + 5,
*result + 6,
*result + 7)) != 8)
{
do
(*result)[nums++] = 0;
while (nums < 8);
}
*--walk = '\0';
}
return(output_string);
return ((int16 *) result);
}
/*
* int28out - converts internal form to "num num ..."
*/
char *
int28out(int16(*shs)[])
{
register int num;
register int16 *sp;
register char *rp;
char *result;
if (shs == NULL)
{
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return (result);
}
rp = result = (char *) palloc(8 * 7); /* assumes sign, 5 digits,
* ' ' */
sp = *shs;
for (num = 8; num != 0; num--)
{
itoa(*sp++, rp);
while (*++rp != '\0')
;
*rp++ = ' ';
}
*--rp = '\0';
return (result);
}
/*
* int28in - converts "num num ..." to internal form
*
* Note:
* Fills any nonexistent digits with NULLs.
*/
int32 *
int44in(char *input_string)
{
int32 *foo = (int32 *) palloc(4 * sizeof(int32));
register int i = 0;
i = sscanf(input_string,
"%d, %d, %d, %d",
&foo[0],
&foo[1],
&foo[2],
&foo[3]);
while (i < 4)
foo[i++] = 0;
return (foo);
}
/*
* int28out - converts internal form to "num num ..."
*/
char *
int44out(int32 an_array[])
{
int temp = 4;
char *output_string = NULL;
int i;
if (temp > 0)
{
char *walk;
output_string = (char *) palloc(16 * temp); /* assume 15 digits +
* sign */
walk = output_string;
for (i = 0; i < temp; i++)
{
itoa(an_array[i], walk);
while (*++walk != '\0')
;
*walk++ = ' ';
}
*--walk = '\0';
}
return (output_string);
}
/*****************************************************************************
* PUBLIC ROUTINES *
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
/*
* int4in - converts "num" to int4
* int4in - converts "num" to int4
*/
int32 int4in(char *num)
int32
int4in(char *num)
{
return(pg_atoi(num, sizeof(int32), '\0'));
return (pg_atoi(num, sizeof(int32), '\0'));
}
/*
* int4out - converts int4 to "num"
* int4out - converts int4 to "num"
*/
char *int4out(int32 l)
char *
int4out(int32 l)
{
char *result;
result = (char *)palloc(12); /* assumes sign, 10 digits, '\0' */
ltoa(l, result);
return(result);
char *result;
result = (char *) palloc(12); /* assumes sign, 10 digits, '\0' */
ltoa(l, result);
return (result);
}
/*
* ===================
* CONVERSION ROUTINES
* ===================
* ===================
* CONVERSION ROUTINES
* ===================
*/
int32 i2toi4(int16 arg1)
int32
i2toi4(int16 arg1)
{
return((int32) arg1);
return ((int32) arg1);
}
int16 i4toi2(int32 arg1)
int16
i4toi2(int32 arg1)
{
if (arg1< -0x8000)
elog(NOTICE, "i4toi2: \"%d\" causes int2 underflow", arg1);
if (arg1 > 0x7FFF)
elog(NOTICE, "i4toi2: \"%d\" causes int2 overflow", arg1);
return((int16) arg1);
if (arg1 < -0x8000)
elog(NOTICE, "i4toi2: \"%d\" causes int2 underflow", arg1);
if (arg1 > 0x7FFF)
elog(NOTICE, "i4toi2: \"%d\" causes int2 overflow", arg1);
return ((int16) arg1);
}
/*
* =========================
* BOOLEAN OPERATOR ROUTINES
* =========================
* =========================
* BOOLEAN OPERATOR ROUTINES
* =========================
*/
/*
* inteq - returns 1 iff arg1 == arg2
* intne - returns 1 iff arg1 != arg2
* intlt - returns 1 iff arg1 < arg2
* intle - returns 1 iff arg1 <= arg2
* intgt - returns 1 iff arg1 > arg2
* intge - returns 1 iff arg1 >= arg2
* inteq - returns 1 iff arg1 == arg2
* intne - returns 1 iff arg1 != arg2
* intlt - returns 1 iff arg1 < arg2
* intle - returns 1 iff arg1 <= arg2
* intgt - returns 1 iff arg1 > arg2
* intge - returns 1 iff arg1 >= arg2
*/
bool int4eq(int32 arg1, int32 arg2) { return(arg1 == arg2); }
bool int4ne(int32 arg1, int32 arg2) { return(arg1 != arg2); }
bool int4lt(int32 arg1, int32 arg2) { return(arg1 < arg2); }
bool int4le(int32 arg1, int32 arg2) { return(arg1 <= arg2); }
bool int4gt(int32 arg1, int32 arg2) { return(arg1 > arg2); }
bool int4ge(int32 arg1, int32 arg2) { return(arg1 >= arg2); }
bool int4eq(int32 arg1, int32 arg2)
{
return (arg1 == arg2);
}
bool int4ne(int32 arg1, int32 arg2)
{
return (arg1 != arg2);
}
bool int4lt(int32 arg1, int32 arg2)
{
return (arg1 < arg2);
}
bool int4le(int32 arg1, int32 arg2)
{
return (arg1 <= arg2);
}
bool int4gt(int32 arg1, int32 arg2)
{
return (arg1 > arg2);
}
bool int4ge(int32 arg1, int32 arg2)
{
return (arg1 >= arg2);
}
bool int2eq(int16 arg1, int16 arg2) { return(arg1 == arg2); }
bool int2ne(int16 arg1, int16 arg2) { return(arg1 != arg2); }
bool int2lt(int16 arg1, int16 arg2) { return(arg1 < arg2); }
bool int2le(int16 arg1, int16 arg2) { return(arg1 <= arg2); }
bool int2gt(int16 arg1, int16 arg2) { return(arg1 > arg2); }
bool int2ge(int16 arg1, int16 arg2) { return(arg1 >= arg2); }
bool int2eq(int16 arg1, int16 arg2)
{
return (arg1 == arg2);
}
bool int2ne(int16 arg1, int16 arg2)
{
return (arg1 != arg2);
}
bool int2lt(int16 arg1, int16 arg2)
{
return (arg1 < arg2);
}
bool int2le(int16 arg1, int16 arg2)
{
return (arg1 <= arg2);
}
bool int2gt(int16 arg1, int16 arg2)
{
return (arg1 > arg2);
}
bool int2ge(int16 arg1, int16 arg2)
{
return (arg1 >= arg2);
}
bool int24eq(int32 arg1, int32 arg2) { return(arg1 == arg2); }
bool int24ne(int32 arg1, int32 arg2) { return(arg1 != arg2); }
bool int24lt(int32 arg1, int32 arg2) { return(arg1 < arg2); }
bool int24le(int32 arg1, int32 arg2) { return(arg1 <= arg2); }
bool int24gt(int32 arg1, int32 arg2) { return(arg1 > arg2); }
bool int24ge(int32 arg1, int32 arg2) { return(arg1 >= arg2); }
bool int24eq(int32 arg1, int32 arg2)
{
return (arg1 == arg2);
}
bool int24ne(int32 arg1, int32 arg2)
{
return (arg1 != arg2);
}
bool int24lt(int32 arg1, int32 arg2)
{
return (arg1 < arg2);
}
bool int24le(int32 arg1, int32 arg2)
{
return (arg1 <= arg2);
}
bool int24gt(int32 arg1, int32 arg2)
{
return (arg1 > arg2);
}
bool int24ge(int32 arg1, int32 arg2)
{
return (arg1 >= arg2);
}
bool int42eq(int32 arg1, int32 arg2) { return(arg1 == arg2); }
bool int42ne(int32 arg1, int32 arg2) { return(arg1 != arg2); }
bool int42lt(int32 arg1, int32 arg2) { return(arg1 < arg2); }
bool int42le(int32 arg1, int32 arg2) { return(arg1 <= arg2); }
bool int42gt(int32 arg1, int32 arg2) { return(arg1 > arg2); }
bool int42ge(int32 arg1, int32 arg2) { return(arg1 >= arg2); }
bool int42eq(int32 arg1, int32 arg2)
{
return (arg1 == arg2);
}
bool int42ne(int32 arg1, int32 arg2)
{
return (arg1 != arg2);
}
bool int42lt(int32 arg1, int32 arg2)
{
return (arg1 < arg2);
}
bool int42le(int32 arg1, int32 arg2)
{
return (arg1 <= arg2);
}
bool int42gt(int32 arg1, int32 arg2)
{
return (arg1 > arg2);
}
bool int42ge(int32 arg1, int32 arg2)
{
return (arg1 >= arg2);
}
bool keyfirsteq(int16 *arg1, int16 arg2) { return(*arg1 == arg2); }
bool keyfirsteq(int16 * arg1, int16 arg2)
{
return (*arg1 == arg2);
}
/*
* int[24]pl - returns arg1 + arg2
* int[24]mi - returns arg1 - arg2
* int[24]mul - returns arg1 * arg2
* int[24]div - returns arg1 / arg2
* int[24]pl - returns arg1 + arg2
* int[24]mi - returns arg1 - arg2
* int[24]mul - returns arg1 * arg2
* int[24]div - returns arg1 / arg2
*/
int32 int4um(int32 arg) { return(-arg); }
int32 int4pl(int32 arg1, int32 arg2) { return(arg1 + arg2); }
int32 int4mi(int32 arg1, int32 arg2) { return(arg1 - arg2); }
int32 int4mul(int32 arg1, int32 arg2) { return(arg1 * arg2); }
int32 int4div(int32 arg1, int32 arg2) { return(arg1 / arg2); }
int32 int4inc(int32 arg) { return(arg + (int32)1); }
int16 int2um(int16 arg) { return(-arg); }
int16 int2pl(int16 arg1, int16 arg2) { return(arg1 + arg2); }
int16 int2mi(int16 arg1, int16 arg2) { return(arg1 - arg2); }
int16 int2mul(int16 arg1, int16 arg2) { return(arg1 * arg2); }
int16 int2div(int16 arg1, int16 arg2) { return(arg1 / arg2); }
int16 int2inc(int16 arg) { return(arg + (int16)1); }
int32 int24pl(int32 arg1, int32 arg2) { return(arg1 + arg2); }
int32 int24mi(int32 arg1, int32 arg2) { return(arg1 - arg2); }
int32 int24mul(int32 arg1, int32 arg2) { return(arg1 * arg2); }
int32 int24div(int32 arg1, int32 arg2) { return(arg1 / arg2); }
int32 int4um(int32 arg)
{
return (-arg);
}
int32 int4pl(int32 arg1, int32 arg2)
{
return (arg1 + arg2);
}
int32 int4mi(int32 arg1, int32 arg2)
{
return (arg1 - arg2);
}
int32 int4mul(int32 arg1, int32 arg2)
{
return (arg1 * arg2);
}
int32 int4div(int32 arg1, int32 arg2)
{
return (arg1 / arg2);
}
int32 int4inc(int32 arg)
{
return (arg + (int32) 1);
}
int16 int2um(int16 arg)
{
return (-arg);
}
int16 int2pl(int16 arg1, int16 arg2)
{
return (arg1 + arg2);
}
int16 int2mi(int16 arg1, int16 arg2)
{
return (arg1 - arg2);
}
int16 int2mul(int16 arg1, int16 arg2)
{
return (arg1 * arg2);
}
int16 int2div(int16 arg1, int16 arg2)
{
return (arg1 / arg2);
}
int16 int2inc(int16 arg)
{
return (arg + (int16) 1);
}
int32 int24pl(int32 arg1, int32 arg2)
{
return (arg1 + arg2);
}
int32 int24mi(int32 arg1, int32 arg2)
{
return (arg1 - arg2);
}
int32 int24mul(int32 arg1, int32 arg2)
{
return (arg1 * arg2);
}
int32 int24div(int32 arg1, int32 arg2)
{
return (arg1 / arg2);
}
int32 int42pl(int32 arg1, int32 arg2)
{
return (arg1 + arg2);
}
int32 int42mi(int32 arg1, int32 arg2)
{
return (arg1 - arg2);
}
int32 int42mul(int32 arg1, int32 arg2)
{
return (arg1 * arg2);
}
int32 int42div(int32 arg1, int32 arg2)
{
return (arg1 / arg2);
}
int32 int42pl(int32 arg1, int32 arg2) { return(arg1 + arg2); }
int32 int42mi(int32 arg1, int32 arg2) { return(arg1 - arg2); }
int32 int42mul(int32 arg1, int32 arg2) { return(arg1 * arg2); }
int32 int42div(int32 arg1, int32 arg2) { return(arg1 / arg2); }
/*
* int[24]mod - returns arg1 mod arg2
* int[24]mod - returns arg1 mod arg2
*/
int32 int4mod(int32 arg1, int32 arg2) { return(arg1 % arg2); }
int32 int2mod(int16 arg1, int16 arg2) { return(arg1 % arg2); }
int32 int24mod(int32 arg1, int32 arg2) { return(arg1 % arg2); }
int32 int42mod(int32 arg1, int32 arg2) { return(arg1 % arg2); }
int32 int4mod(int32 arg1, int32 arg2)
{
return (arg1 % arg2);
}
int32 int2mod(int16 arg1, int16 arg2)
{
return (arg1 % arg2);
}
int32 int24mod(int32 arg1, int32 arg2)
{
return (arg1 % arg2);
}
int32 int42mod(int32 arg1, int32 arg2)
{
return (arg1 % arg2);
}
/*
* int[24]fac - returns arg1!
* int[24]fac - returns arg1!
*/
int32 int4fac(int32 arg1)
int32
int4fac(int32 arg1)
{
int32 result;
if (arg1 < 1)
result = 0;
else
for (result = 1; arg1 > 0; --arg1)
result *= arg1;
return(result);
int32 result;
if (arg1 < 1)
result = 0;
else
for (result = 1; arg1 > 0; --arg1)
result *= arg1;
return (result);
}
int32 int2fac(int16 arg1)
int32
int2fac(int16 arg1)
{
int16 result;
if (arg1 < 1)
result = 0;
else
for (result = 1; arg1 > 0; --arg1)
result *= arg1;
return(result);
int16 result;
if (arg1 < 1)
result = 0;
else
for (result = 1; arg1 > 0; --arg1)
result *= arg1;
return (result);
}
int16 int2larger(int16 arg1, int16 arg2)
int16
int2larger(int16 arg1, int16 arg2)
{
return ((arg1 > arg2) ? arg1 : arg2);
return ((arg1 > arg2) ? arg1 : arg2);
}
int16 int2smaller(int16 arg1, int16 arg2)
int16
int2smaller(int16 arg1, int16 arg2)
{
return ((arg1 < arg2) ? arg1 : arg2);
return ((arg1 < arg2) ? arg1 : arg2);
}
int32 int4larger(int32 arg1, int32 arg2)
int32
int4larger(int32 arg1, int32 arg2)
{
return ((arg1 > arg2) ? arg1 : arg2);
return ((arg1 > arg2) ? arg1 : arg2);
}
int32 int4smaller(int32 arg1, int32 arg2)
int32
int4smaller(int32 arg1, int32 arg2)
{
return ((arg1 < arg2) ? arg1 : arg2);
return ((arg1 < arg2) ? arg1 : arg2);
}

View File

@@ -1,225 +1,236 @@
/*-------------------------------------------------------------------------
*
* like.c--
* like expression handling code.
* like expression handling code.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* /usr/local/devel/pglite/cvs/src/backend/utils/adt/like.c,v 1.1 1995/07/30 23:55:36 emkxp01 Exp
* /usr/local/devel/pglite/cvs/src/backend/utils/adt/like.c,v 1.1 1995/07/30 23:55:36 emkxp01 Exp
*
*
* NOTES
* A big hack of the regexp.c code!! Contributed by
* Keith Parks <emkxp01@mtcc.demon.co.uk> (7/95).
* NOTES
* A big hack of the regexp.c code!! Contributed by
* Keith Parks <emkxp01@mtcc.demon.co.uk> (7/95).
*
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include "postgres.h" /* postgres system include file */
#include "postgres.h" /* postgres system include file */
#include "utils/palloc.h"
#include "utils/builtins.h" /* where the function declarations go */
#include "utils/builtins.h" /* where the function declarations go */
static int like(char *text, char *p);
static int like(char *text, char *p);
/*
* interface routines called by the function manager
* interface routines called by the function manager
*/
/*
fixedlen_like:
a generic fixed length like routine
s - the string to match against (not necessarily null-terminated)
p - the pattern
charlen - the length of the string
s - the string to match against (not necessarily null-terminated)
p - the pattern
charlen - the length of the string
*/
static bool
fixedlen_like(char *s, struct varlena* p, int charlen)
static bool
fixedlen_like(char *s, struct varlena * p, int charlen)
{
char *sterm, *pterm;
int result;
char *sterm,
*pterm;
int result;
if (!s || !p)
return FALSE;
/* be sure sterm is null-terminated */
sterm = (char *) palloc(charlen + 1);
strNcpy(sterm, s, charlen);
/* p is a text = varlena, not a string so we have to make
* a string from the vl_data field of the struct. */
/* palloc the length of the text + the null character */
pterm = (char *) palloc(VARSIZE(p) - VARHDRSZ + 1);
memmove(pterm, VARDATA(p), VARSIZE(p) - VARHDRSZ);
*(pterm + VARSIZE(p) - VARHDRSZ) = (char)NULL;
/* do the regexp matching */
result = like(sterm, pterm);
pfree(sterm);
pfree(pterm);
return ((bool) result);
if (!s || !p)
return FALSE;
/* be sure sterm is null-terminated */
sterm = (char *) palloc(charlen + 1);
strNcpy(sterm, s, charlen);
/*
* p is a text = varlena, not a string so we have to make a string
* from the vl_data field of the struct.
*/
/* palloc the length of the text + the null character */
pterm = (char *) palloc(VARSIZE(p) - VARHDRSZ + 1);
memmove(pterm, VARDATA(p), VARSIZE(p) - VARHDRSZ);
*(pterm + VARSIZE(p) - VARHDRSZ) = (char) NULL;
/* do the regexp matching */
result = like(sterm, pterm);
pfree(sterm);
pfree(pterm);
return ((bool) result);
}
bool
char2like(uint16 arg1, struct varlena *p)
bool
char2like(uint16 arg1, struct varlena * p)
{
char *s = (char *) &arg1;
return (fixedlen_like(s, p, 2));
}
char *s = (char *) &arg1;
bool
char2nlike(uint16 arg1, struct varlena *p)
{
return (!char2like(arg1, p));
return (fixedlen_like(s, p, 2));
}
bool
char4like(uint32 arg1, struct varlena *p)
bool
char2nlike(uint16 arg1, struct varlena * p)
{
char *s = (char *) &arg1;
return (fixedlen_like(s, p, 4));
return (!char2like(arg1, p));
}
bool
char4nlike(uint32 arg1, struct varlena *p)
bool
char4like(uint32 arg1, struct varlena * p)
{
return (!char4like(arg1, p));
char *s = (char *) &arg1;
return (fixedlen_like(s, p, 4));
}
bool
char8like(char *s, struct varlena *p)
bool
char4nlike(uint32 arg1, struct varlena * p)
{
return (fixedlen_like(s, p, 8));
return (!char4like(arg1, p));
}
bool
char8nlike(char *s, struct varlena *p)
bool
char8like(char *s, struct varlena * p)
{
return (!char8like(s, p));
return (fixedlen_like(s, p, 8));
}
bool
char16like(char *s, struct varlena *p)
bool
char8nlike(char *s, struct varlena * p)
{
return (fixedlen_like(s, p, 16));
}
bool
char16nlike(char *s, struct varlena *p)
{
return (!char16like(s, p));
return (!char8like(s, p));
}
bool
namelike(NameData *n, struct varlena *p)
bool
char16like(char *s, struct varlena * p)
{
if (!n) return FALSE;
return (fixedlen_like(n->data, p, NAMEDATALEN));
return (fixedlen_like(s, p, 16));
}
bool
namenlike(NameData *s, struct varlena *p)
bool
char16nlike(char *s, struct varlena * p)
{
return (!namelike(s, p));
return (!char16like(s, p));
}
bool
textlike(struct varlena *s, struct varlena *p)
bool
namelike(NameData * n, struct varlena * p)
{
if (!s) return FALSE;
return (fixedlen_like(VARDATA(s), p, VARSIZE(s) - VARHDRSZ));
if (!n)
return FALSE;
return (fixedlen_like(n->data, p, NAMEDATALEN));
}
bool textnlike(struct varlena *s, struct varlena *p)
bool
namenlike(NameData * s, struct varlena * p)
{
return (!textlike(s, p));
return (!namelike(s, p));
}
bool
textlike(struct varlena * s, struct varlena * p)
{
if (!s)
return FALSE;
return (fixedlen_like(VARDATA(s), p, VARSIZE(s) - VARHDRSZ));
}
bool
textnlike(struct varlena * s, struct varlena * p)
{
return (!textlike(s, p));
}
/* $Revision: 1.6 $
** "like.c" A first attempt at a LIKE operator for Postgres95.
/* $Revision: 1.7 $
** "like.c" A first attempt at a LIKE operator for Postgres95.
**
** Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
** Rich $alz is now <rsalz@bbn.com>.
** Special thanks to Lars Mathiesen <thorinn@diku.dk> for the LABORT code.
**
** This code was shamelessly stolen from the "pql" code by myself and
** slightly modified :)
**
** All references to the word "star" were replaced by "percent"
** All references to the word "wild" were replaced by "like"
**
** All the nice shell RE matching stuff was replaced by just "_" and "%"
**
** As I don't have a copy of the SQL standard handy I wasn't sure whether
** to leave in the '\' escape character handling. (I suspect the standard
** handles "%%" as a single literal percent)
** Originally written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
** Rich $alz is now <rsalz@bbn.com>.
** Special thanks to Lars Mathiesen <thorinn@diku.dk> for the LABORT code.
**
** Keith Parks. <keith@mtcc.demon.co.uk>
** This code was shamelessly stolen from the "pql" code by myself and
** slightly modified :)
**
** [SQL92 lets you specify the escape character by saying
** LIKE <pattern> ESCAPE <escape character>. We are a small operation
** so we force you to use '\'. - ay 7/95]
** All references to the word "star" were replaced by "percent"
** All references to the word "wild" were replaced by "like"
**
** All the nice shell RE matching stuff was replaced by just "_" and "%"
**
** As I don't have a copy of the SQL standard handy I wasn't sure whether
** to leave in the '\' escape character handling. (I suspect the standard
** handles "%%" as a single literal percent)
**
** Keith Parks. <keith@mtcc.demon.co.uk>
**
** [SQL92 lets you specify the escape character by saying
** LIKE <pattern> ESCAPE <escape character>. We are a small operation
** so we force you to use '\'. - ay 7/95]
**
*/
#define LIKE_TRUE 1
#define LIKE_FALSE 0
#define LIKE_ABORT -1
#define LIKE_TRUE 1
#define LIKE_FALSE 0
#define LIKE_ABORT -1
/*
** Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
** Match text and p, return LIKE_TRUE, LIKE_FALSE, or LIKE_ABORT.
*/
static int
DoMatch(register char *text, register char *p)
{
register int matched;
register int matched;
for ( ; *p; text++, p++) {
if (*text == '\0' && *p != '%')
return LIKE_ABORT;
switch (*p) {
case '\\':
/* Literal match with following character. */
p++;
/* FALLTHROUGH */
default:
if (*text != *p)
return LIKE_FALSE;
continue;
case '_':
/* Match anything. */
continue;
case '%':
while (*++p == '%')
/* Consecutive percents act just like one. */
continue;
if (*p == '\0')
/* Trailing percent matches everything. */
return LIKE_TRUE;
while (*text)
if ((matched = DoMatch(text++, p)) != LIKE_FALSE)
return matched;
return LIKE_ABORT;
for (; *p; text++, p++)
{
if (*text == '\0' && *p != '%')
return LIKE_ABORT;
switch (*p)
{
case '\\':
/* Literal match with following character. */
p++;
/* FALLTHROUGH */
default:
if (*text != *p)
return LIKE_FALSE;
continue;
case '_':
/* Match anything. */
continue;
case '%':
while (*++p == '%')
/* Consecutive percents act just like one. */
continue;
if (*p == '\0')
/* Trailing percent matches everything. */
return LIKE_TRUE;
while (*text)
if ((matched = DoMatch(text++, p)) != LIKE_FALSE)
return matched;
return LIKE_ABORT;
}
}
}
return *text == '\0';
return *text == '\0';
}
/*
** User-level routine. Returns TRUE or FALSE.
** User-level routine. Returns TRUE or FALSE.
*/
static int
like(char *text, char *p)
{
if (p[0] == '%' && p[1] == '\0')
return TRUE;
return (DoMatch(text, p) == LIKE_TRUE);
if (p[0] == '%' && p[1] == '\0')
return TRUE;
return (DoMatch(text, p) == LIKE_TRUE);
}

View File

@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* misc.c--
*
*
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.7 1997/07/28 00:55:58 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.8 1997/09/07 04:50:23 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -18,70 +18,73 @@
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "port-protos.h" /* For random(), sometimes */
#include "port-protos.h" /* For random(), sometimes */
/*-------------------------------------------------------------------------
* Check if data is Null
* Check if data is Null
*/
bool
nullvalue(Datum value, bool *isNull)
nullvalue(Datum value, bool * isNull)
{
if (*isNull) {
*isNull = false;
return(true);
}
return(false);
if (*isNull)
{
*isNull = false;
return (true);
}
return (false);
}
/*----------------------------------------------------------------------*
* check if data is not Null *
* check if data is not Null *
*--------------------------------------------------------------------- */
bool
nonnullvalue(Datum value, bool *isNull)
nonnullvalue(Datum value, bool * isNull)
{
if (*isNull) {
*isNull = false;
return(false);
}
return(true);
if (*isNull)
{
*isNull = false;
return (false);
}
return (true);
}
/*
* oidrand (oid o, int4 X)-
* takes in an oid and a int4 X, and will return 'true'
* about 1/X of the time.
* Useful for doing random sampling or subsetting.
* if X == 0, this will always return true;
* takes in an oid and a int4 X, and will return 'true'
* about 1/X of the time.
* Useful for doing random sampling or subsetting.
* if X == 0, this will always return true;
*
* Example use:
* select * from TEMP where oidrand(TEMP.oid, 10)
* select * from TEMP where oidrand(TEMP.oid, 10)
* will return about 1/10 of the tuples in TEMP
*
*/
bool
bool
oidrand(Oid o, int32 X)
{
bool result;
bool result;
if (X == 0) return true;
if (X == 0)
return true;
result = (random() % X == 0);
return result;
result = (random() % X == 0);
return result;
}
/*
oidsrand(int32 X) -
seeds the random number generator
always return true
*/
seeds the random number generator
always return true
*/
bool
oidsrand(int32 X)
{
srand(X);
return true;
srand(X);
return true;
}
@@ -89,5 +92,5 @@ oidsrand(int32 X)
int32
userfntest(int i)
{
return (i);
return (i);
}

View File

@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* nabstime.c--
* parse almost any absolute date getdate(3) can (& some it can't)
* parse almost any absolute date getdate(3) can (& some it can't)
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.31 1997/08/19 21:34:42 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.32 1997/09/07 04:50:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -19,10 +19,10 @@
#include "postgres.h"
#include <miscadmin.h>
#ifdef HAVE_FLOAT_H
# include <float.h>
#include <float.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#include <limits.h>
#endif
#ifndef USE_POSIX_TIME
#include <sys/timeb.h>
@@ -30,10 +30,10 @@
#include "utils/builtins.h"
#include "access/xact.h"
static AbsoluteTime tm2abstime(struct tm *tm, int tz);
static AbsoluteTime tm2abstime(struct tm * tm, int tz);
#define MIN_DAYNUM -24856 /* December 13, 1901 */
#define MAX_DAYNUM 24854 /* January 18, 2038 */
#define MIN_DAYNUM -24856 /* December 13, 1901 */
#define MAX_DAYNUM 24854 /* January 18, 2038 */
/* GetCurrentAbsoluteTime()
@@ -45,300 +45,344 @@ static AbsoluteTime tm2abstime(struct tm *tm, int tz);
AbsoluteTime
GetCurrentAbsoluteTime(void)
{
time_t now;
time_t now;
#ifdef USE_POSIX_TIME
struct tm *tm;
struct tm *tm;
now = time(NULL);
#else /* ! USE_POSIX_TIME */
struct timeb tb; /* the old V7-ism */
now = time(NULL);
#else /* ! USE_POSIX_TIME */
struct timeb tb; /* the old V7-ism */
ftime(&tb);
now = tb.time;
ftime(&tb);
now = tb.time;
#endif
if (! HasCTZSet) {
if (!HasCTZSet)
{
#ifdef USE_POSIX_TIME
#if defined(HAVE_TZSET) && defined(HAVE_INT_TIMEZONE)
tm = localtime(&now);
tm = localtime(&now);
CDayLight = tm->tm_isdst;
CTimeZone = (tm->tm_isdst? (timezone - 3600): timezone);
strcpy( CTZName, tzname[tm->tm_isdst]);
#else /* !HAVE_TZSET */
tm = localtime(&now);
CDayLight = tm->tm_isdst;
CTimeZone = (tm->tm_isdst ? (timezone - 3600) : timezone);
strcpy(CTZName, tzname[tm->tm_isdst]);
#else /* !HAVE_TZSET */
tm = localtime(&now);
CTimeZone = - tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
CDayLight = (tm->tm_isdst > 0);
/* XXX is there a better way to get local timezone string w/o tzname? - tgl 97/03/18 */
strftime( CTZName, MAXTZLEN, "%Z", tm);
CTimeZone = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
CDayLight = (tm->tm_isdst > 0);
/*
* XXX is there a better way to get local timezone string w/o
* tzname? - tgl 97/03/18
*/
strftime(CTZName, MAXTZLEN, "%Z", tm);
/*
* XXX FreeBSD man pages indicate that this should work - tgl
* 97/04/23
*/
strcpy(CTZName, tm->tm_zone);
#endif
#else /* ! USE_POSIX_TIME */
CTimeZone = tb.timezone * 60;
CDayLight = (tb.dstflag != 0);
/*
* XXX does this work to get the local timezone string in V7? -
* tgl 97/03/18
*/
strftime(CTZName, MAXTZLEN, "%Z", localtime(&now));
#endif
};
#ifdef DATEDEBUG
printf("GetCurrentAbsoluteTime- timezone is %s -> %d seconds from UTC\n",
CTZName, CTimeZone);
#endif
return ((AbsoluteTime) now);
} /* GetCurrentAbsoluteTime() */
void
GetCurrentTime(struct tm * tm)
{
int tz;
abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
return;
} /* GetCurrentTime() */
void
abstime2tm(AbsoluteTime time, int *tzp, struct tm * tm, char *tzn)
{
#ifdef USE_POSIX_TIME
struct tm *tx;
#else /* ! USE_POSIX_TIME */
struct timeb tb; /* the old V7-ism */
ftime(&tb);
#endif
#ifdef USE_POSIX_TIME
if (tzp != NULL)
{
tx = localtime((time_t *) & time);
}
else
{
tx = gmtime((time_t *) & time);
};
#else
#endif
#ifdef DATEDEBUG
#ifdef HAVE_INT_TIMEZONE
printf("datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s %s dst=%d\n",
tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, tx->tm_sec,
tzname[0], tzname[1], tx->tm_isdst);
#else
printf("datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s dst=%d\n",
tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, tx->tm_sec,
tx->tm_zone, tx->tm_isdst);
#endif
#else
#endif
tm->tm_year = tx->tm_year + 1900;
tm->tm_mon = tx->tm_mon + 1;
tm->tm_mday = tx->tm_mday;
tm->tm_hour = tx->tm_hour;
tm->tm_min = tx->tm_min;
tm->tm_sec = tx->tm_sec;
tm->tm_isdst = tx->tm_isdst;
#ifdef USE_POSIX_TIME
#ifdef HAVE_INT_TIMEZONE
if (tzp != NULL)
*tzp = (tm->tm_isdst ? (timezone - 3600) : timezone);
if (tzn != NULL)
strcpy(tzn, tzname[tm->tm_isdst]);
#else /* !HAVE_INT_TIMEZONE */
tm->tm_gmtoff = tx->tm_gmtoff;
tm->tm_zone = tx->tm_zone;
if (tzp != NULL)
*tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
/* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
strcpy(CTZName, tm->tm_zone);
if (tzn != NULL)
strcpy(tzn, tm->tm_zone);
#endif
#else /* ! USE_POSIX_TIME */
CTimeZone = tb.timezone * 60;
CDayLight = (tb.dstflag != 0);
/* XXX does this work to get the local timezone string in V7? - tgl 97/03/18 */
strftime( CTZName, MAXTZLEN, "%Z", localtime(&now));
#endif
};
#else /* ! USE_POSIX_TIME */
if (tzp != NULL)
*tzp = tb.timezone * 60;
#ifdef DATEDEBUG
printf( "GetCurrentAbsoluteTime- timezone is %s -> %d seconds from UTC\n",
CTZName, CTimeZone);
/*
* XXX does this work to get the local timezone string in V7? - tgl
* 97/03/18
*/
if (tzn != NULL)
strftime(tzn, MAXTZLEN, "%Z", localtime(&now));
#endif
return((AbsoluteTime) now);
} /* GetCurrentAbsoluteTime() */
void
GetCurrentTime(struct tm *tm)
{
int tz;
abstime2tm( GetCurrentTransactionStartTime(), &tz, tm, NULL);
return;
} /* GetCurrentTime() */
void
abstime2tm(AbsoluteTime time, int *tzp, struct tm *tm, char *tzn)
{
#ifdef USE_POSIX_TIME
struct tm *tx;
#else /* ! USE_POSIX_TIME */
struct timeb tb; /* the old V7-ism */
ftime(&tb);
#endif
#ifdef USE_POSIX_TIME
if (tzp != NULL) {
tx = localtime((time_t *) &time);
} else {
tx = gmtime((time_t *) &time);
};
#else
#endif
#ifdef DATEDEBUG
#ifdef HAVE_INT_TIMEZONE
printf( "datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s %s dst=%d\n",
tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, tx->tm_sec,
tzname[0], tzname[1], tx->tm_isdst);
#else
printf( "datetime2tm- (localtime) %d.%02d.%02d %02d:%02d:%02d %s dst=%d\n",
tx->tm_year, tx->tm_mon, tx->tm_mday, tx->tm_hour, tx->tm_min, tx->tm_sec,
tx->tm_zone, tx->tm_isdst);
#endif
#else
#endif
tm->tm_year = tx->tm_year+1900;
tm->tm_mon = tx->tm_mon+1;
tm->tm_mday = tx->tm_mday;
tm->tm_hour = tx->tm_hour;
tm->tm_min = tx->tm_min;
tm->tm_sec = tx->tm_sec;
tm->tm_isdst = tx->tm_isdst;
#ifdef USE_POSIX_TIME
#ifdef HAVE_INT_TIMEZONE
if (tzp != NULL) *tzp = (tm->tm_isdst? (timezone - 3600): timezone);
if (tzn != NULL) strcpy( tzn, tzname[tm->tm_isdst]);
#else /* !HAVE_INT_TIMEZONE */
tm->tm_gmtoff = tx->tm_gmtoff;
tm->tm_zone = tx->tm_zone;
if (tzp != NULL) *tzp = - tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
/* XXX FreeBSD man pages indicate that this should work - tgl 97/04/23 */
if (tzn != NULL) strcpy( tzn, tm->tm_zone);
#endif
#else /* ! USE_POSIX_TIME */
if (tzp != NULL) *tzp = tb.timezone * 60;
/* XXX does this work to get the local timezone string in V7? - tgl 97/03/18 */
if (tzn != NULL) strftime( tzn, MAXTZLEN, "%Z", localtime(&now));
#endif
return;
} /* abstime2tm() */
return;
} /* abstime2tm() */
/* tm2abstime()
* Convert a tm structure to abstime.
* Note that tm has full year (not 1900-based) and 1-based month.
*/
static AbsoluteTime
tm2abstime( struct tm *tm, int tz)
static AbsoluteTime
tm2abstime(struct tm * tm, int tz)
{
int day, sec;
int day,
sec;
/* validate, before going out of range on some members */
if (tm->tm_year < 1901 || tm->tm_year > 2038
|| tm->tm_mon < 1 || tm->tm_mon > 12
|| tm->tm_mday < 1 || tm->tm_mday > 31
|| tm->tm_hour < 0 || tm->tm_hour >= 24
|| tm->tm_min < 0 || tm->tm_min > 59
|| tm->tm_sec < 0 || tm->tm_sec > 59)
return(INVALID_ABSTIME);
/* validate, before going out of range on some members */
if (tm->tm_year < 1901 || tm->tm_year > 2038
|| tm->tm_mon < 1 || tm->tm_mon > 12
|| tm->tm_mday < 1 || tm->tm_mday > 31
|| tm->tm_hour < 0 || tm->tm_hour >= 24
|| tm->tm_min < 0 || tm->tm_min > 59
|| tm->tm_sec < 0 || tm->tm_sec > 59)
return (INVALID_ABSTIME);
day = (date2j( tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j( 1970, 1, 1));
day = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(1970, 1, 1));
/* check for time out of range */
if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
return(INVALID_ABSTIME);
/* check for time out of range */
if ((day < MIN_DAYNUM) || (day > MAX_DAYNUM))
return (INVALID_ABSTIME);
/* convert to seconds */
sec = tm->tm_sec + tz + (tm->tm_min +(day*24 + tm->tm_hour)*60)*60;
/* convert to seconds */
sec = tm->tm_sec + tz + (tm->tm_min + (day * 24 + tm->tm_hour) * 60) * 60;
/* check for overflow */
if ((day == MAX_DAYNUM && sec < 0) ||
(day == MIN_DAYNUM && sec > 0))
return(INVALID_ABSTIME);
/* check for overflow */
if ((day == MAX_DAYNUM && sec < 0) ||
(day == MIN_DAYNUM && sec > 0))
return (INVALID_ABSTIME);
/* check for reserved values (e.g. "current" on edge of usual range */
if (!AbsoluteTimeIsReal(sec))
return(INVALID_ABSTIME);
/* check for reserved values (e.g. "current" on edge of usual range */
if (!AbsoluteTimeIsReal(sec))
return (INVALID_ABSTIME);
return(sec);
} /* tm2abstime() */
return (sec);
} /* tm2abstime() */
/* nabstimein()
* Decode date/time string and return abstime.
*/
AbsoluteTime
nabstimein(char* str)
nabstimein(char *str)
{
AbsoluteTime result;
AbsoluteTime result;
double fsec;
int tz = 0;
struct tm date, *tm = &date;
double fsec;
int tz = 0;
struct tm date,
*tm = &date;
char *field[MAXDATEFIELDS];
char lowstr[MAXDATELEN+1];
int dtype;
int nf, ftype[MAXDATEFIELDS];
char *field[MAXDATEFIELDS];
char lowstr[MAXDATELEN + 1];
int dtype;
int nf,
ftype[MAXDATEFIELDS];
if (!PointerIsValid(str))
elog(WARN,"Bad (null) abstime external representation",NULL);
if (!PointerIsValid(str))
elog(WARN, "Bad (null) abstime external representation", NULL);
if (strlen(str) > MAXDATELEN)
elog( WARN, "Bad (length) abstime external representation '%s'",str);
if (strlen(str) > MAXDATELEN)
elog(WARN, "Bad (length) abstime external representation '%s'", str);
if ((ParseDateTime( str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|| (DecodeDateTime( field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
elog( WARN, "Bad abstime external representation '%s'",str);
if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf) != 0)
|| (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz) != 0))
elog(WARN, "Bad abstime external representation '%s'", str);
#ifdef DATEDEBUG
printf( "nabstimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE);
printf("nabstimein- %d fields are type %d (DTK_DATE=%d)\n", nf, dtype, DTK_DATE);
#endif
switch (dtype) {
case DTK_DATE:
result = tm2abstime(tm, tz);
break;
switch (dtype)
{
case DTK_DATE:
result = tm2abstime(tm, tz);
break;
case DTK_EPOCH:
result = EPOCH_ABSTIME;
break;
case DTK_EPOCH:
result = EPOCH_ABSTIME;
break;
case DTK_CURRENT:
result = CURRENT_ABSTIME;
break;
case DTK_CURRENT:
result = CURRENT_ABSTIME;
break;
case DTK_LATE:
result = NOEND_ABSTIME;
break;
case DTK_LATE:
result = NOEND_ABSTIME;
break;
case DTK_EARLY:
result = NOSTART_ABSTIME;
break;
case DTK_EARLY:
result = NOSTART_ABSTIME;
break;
case DTK_INVALID:
result = INVALID_ABSTIME;
break;
case DTK_INVALID:
result = INVALID_ABSTIME;
break;
default:
elog(WARN,"Bad abstime (internal coding error) '%s'",str);
result = INVALID_ABSTIME;
break;
};
default:
elog(WARN, "Bad abstime (internal coding error) '%s'", str);
result = INVALID_ABSTIME;
break;
};
return result;
} /* nabstimein() */
return result;
} /* nabstimein() */
/* nabstimeout()
* Given an AbsoluteTime return the English text version of the date
*/
char *
char *
nabstimeout(AbsoluteTime time)
{
char* result;
int tz;
double fsec = 0;
struct tm tt, *tm = &tt;
char buf[MAXDATELEN+1];
char zone[MAXDATELEN+1], *tzn = zone;
char *result;
int tz;
double fsec = 0;
struct tm tt,
*tm = &tt;
char buf[MAXDATELEN + 1];
char zone[MAXDATELEN + 1],
*tzn = zone;
switch (time) {
case EPOCH_ABSTIME: strcpy(buf, EPOCH); break;
case INVALID_ABSTIME: strcpy(buf, INVALID); break;
case CURRENT_ABSTIME: strcpy(buf, DCURRENT); break;
case NOEND_ABSTIME: strcpy(buf, LATE); break;
case NOSTART_ABSTIME: strcpy(buf, EARLY); break;
default:
abstime2tm( time, &tz, tm, tzn);
switch (time)
{
case EPOCH_ABSTIME:
strcpy(buf, EPOCH);
break;
case INVALID_ABSTIME:
strcpy(buf, INVALID);
break;
case CURRENT_ABSTIME:
strcpy(buf, DCURRENT);
break;
case NOEND_ABSTIME:
strcpy(buf, LATE);
break;
case NOSTART_ABSTIME:
strcpy(buf, EARLY);
break;
default:
abstime2tm(time, &tz, tm, tzn);
#if DATEDEBUG
#endif
EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
break;
}
EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
break;
}
result = PALLOC(strlen(buf) + 1);
strcpy(result, buf);
result = PALLOC(strlen(buf) + 1);
strcpy(result, buf);
return(result);
} /* nabstimeout() */
return (result);
} /* nabstimeout() */
/*
* AbsoluteTimeIsBefore -- true iff time1 is before time2.
* AbsoluteTimeIsBefore -- true iff time1 is after time2.
* AbsoluteTimeIsBefore -- true iff time1 is before time2.
* AbsoluteTimeIsBefore -- true iff time1 is after time2.
*/
bool
AbsoluteTimeIsBefore(AbsoluteTime time1, AbsoluteTime time2)
{
Assert(AbsoluteTimeIsValid(time1));
Assert(AbsoluteTimeIsValid(time2));
Assert(AbsoluteTimeIsValid(time1));
Assert(AbsoluteTimeIsValid(time2));
if (time1 == CURRENT_ABSTIME)
time1 = GetCurrentTransactionStartTime();
if (time1 == CURRENT_ABSTIME)
time1 = GetCurrentTransactionStartTime();
if (time2 == CURRENT_ABSTIME)
time2 = GetCurrentTransactionStartTime();
if (time2 == CURRENT_ABSTIME)
time2 = GetCurrentTransactionStartTime();
return (time1 < time2);
return (time1 < time2);
}
bool
AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2)
{
Assert(AbsoluteTimeIsValid(time1));
Assert(AbsoluteTimeIsValid(time2));
Assert(AbsoluteTimeIsValid(time1));
Assert(AbsoluteTimeIsValid(time2));
if (time1 == CURRENT_ABSTIME)
time1 = GetCurrentTransactionStartTime();
if (time1 == CURRENT_ABSTIME)
time1 = GetCurrentTransactionStartTime();
if (time2 == CURRENT_ABSTIME)
time2 = GetCurrentTransactionStartTime();
if (time2 == CURRENT_ABSTIME)
time2 = GetCurrentTransactionStartTime();
return (time1 > time2);
return (time1 > time2);
}
@@ -347,95 +391,95 @@ AbsoluteTimeIsAfter(AbsoluteTime time1, AbsoluteTime time2)
bool
abstime_finite(AbsoluteTime abstime)
{
return((abstime != INVALID_ABSTIME)
&& (abstime != NOSTART_ABSTIME) && (abstime != NOEND_ABSTIME));
} /* abstime_datetime() */
return ((abstime != INVALID_ABSTIME)
&& (abstime != NOSTART_ABSTIME) && (abstime != NOEND_ABSTIME));
} /* abstime_datetime() */
/*
* abstimeeq - returns 1, iff arguments are equal
* abstimene - returns 1, iff arguments are not equal
* abstimelt - returns 1, iff t1 less than t2
* abstimegt - returns 1, iff t1 greater than t2
* abstimele - returns 1, iff t1 less than or equal to t2
* abstimege - returns 1, iff t1 greater than or equal to t2
* abstimeeq - returns 1, iff arguments are equal
* abstimene - returns 1, iff arguments are not equal
* abstimelt - returns 1, iff t1 less than t2
* abstimegt - returns 1, iff t1 greater than t2
* abstimele - returns 1, iff t1 less than or equal to t2
* abstimege - returns 1, iff t1 greater than or equal to t2
*/
bool
abstimeeq(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return(FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return (FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 == t2);
return (t1 == t2);
}
bool
abstimene(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return(FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return (FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 != t2);
return (t1 != t2);
}
bool
abstimelt(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return(FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return (FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 < t2);
return (t1 < t2);
}
bool
abstimegt(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return(FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return (FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 > t2);
return (t1 > t2);
}
bool
abstimele(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return(FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return (FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 <= t2);
return (t1 <= t2);
}
bool
abstimege(AbsoluteTime t1, AbsoluteTime t2)
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return(FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
{
if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
return (FALSE);
if (t1 == CURRENT_ABSTIME)
t1 = GetCurrentTransactionStartTime();
if (t2 == CURRENT_ABSTIME)
t2 = GetCurrentTransactionStartTime();
return(t1 >= t2);
return (t1 >= t2);
}
@@ -443,77 +487,93 @@ abstimege(AbsoluteTime t1, AbsoluteTime t2)
* Convert datetime to abstime.
*/
AbsoluteTime
datetime_abstime(DateTime *datetime)
datetime_abstime(DateTime * datetime)
{
AbsoluteTime result;
AbsoluteTime result;
double fsec;
struct tm tt, *tm = &tt;
double fsec;
struct tm tt,
*tm = &tt;
if (!PointerIsValid(datetime)) {
result = INVALID_ABSTIME;
if (!PointerIsValid(datetime))
{
result = INVALID_ABSTIME;
} else if (DATETIME_IS_INVALID(*datetime)) {
result = INVALID_ABSTIME;
}
else if (DATETIME_IS_INVALID(*datetime))
{
result = INVALID_ABSTIME;
} else if (DATETIME_IS_NOBEGIN(*datetime)) {
result = NOSTART_ABSTIME;
}
else if (DATETIME_IS_NOBEGIN(*datetime))
{
result = NOSTART_ABSTIME;
} else if (DATETIME_IS_NOEND(*datetime)) {
result = NOEND_ABSTIME;
}
else if (DATETIME_IS_NOEND(*datetime))
{
result = NOEND_ABSTIME;
} else {
if (DATETIME_IS_RELATIVE(*datetime)) {
datetime2tm( SetDateTime(*datetime), NULL, tm, &fsec, NULL);
result = tm2abstime( tm, 0);
}
else
{
if (DATETIME_IS_RELATIVE(*datetime))
{
datetime2tm(SetDateTime(*datetime), NULL, tm, &fsec, NULL);
result = tm2abstime(tm, 0);
} else if (datetime2tm( *datetime, NULL, tm, &fsec, NULL) == 0) {
result = tm2abstime( tm, 0);
}
else if (datetime2tm(*datetime, NULL, tm, &fsec, NULL) == 0)
{
result = tm2abstime(tm, 0);
} else {
result = INVALID_ABSTIME;
}
else
{
result = INVALID_ABSTIME;
};
};
};
return(result);
} /* datetime_abstime() */
return (result);
} /* datetime_abstime() */
/* abstime_datetime()
* Convert datetime to abstime.
*/
DateTime *
DateTime *
abstime_datetime(AbsoluteTime abstime)
{
DateTime *result;
DateTime *result;
if (!PointerIsValid(result = PALLOCTYPE(DateTime)))
elog(WARN,"Unable to allocate space to convert abstime to datetime",NULL);
if (!PointerIsValid(result = PALLOCTYPE(DateTime)))
elog(WARN, "Unable to allocate space to convert abstime to datetime", NULL);
switch (abstime) {
case INVALID_ABSTIME:
DATETIME_INVALID(*result);
break;
switch (abstime)
{
case INVALID_ABSTIME:
DATETIME_INVALID(*result);
break;
case NOSTART_ABSTIME:
DATETIME_NOBEGIN(*result);
break;
case NOSTART_ABSTIME:
DATETIME_NOBEGIN(*result);
break;
case NOEND_ABSTIME:
DATETIME_NOEND(*result);
break;
case NOEND_ABSTIME:
DATETIME_NOEND(*result);
break;
case EPOCH_ABSTIME:
DATETIME_EPOCH(*result);
break;
case EPOCH_ABSTIME:
DATETIME_EPOCH(*result);
break;
case CURRENT_ABSTIME:
DATETIME_CURRENT(*result);
break;
case CURRENT_ABSTIME:
DATETIME_CURRENT(*result);
break;
default:
*result = abstime + ((date2j( 1970, 1, 1) - date2j( 2000, 1, 1))*86400);
break;
};
default:
*result = abstime + ((date2j(1970, 1, 1) - date2j(2000, 1, 1)) * 86400);
break;
};
return(result);
} /* abstime_datetime() */
return (result);
} /* abstime_datetime() */

View File

@@ -1,203 +1,221 @@
/*-------------------------------------------------------------------------
*
* name.c--
* Functions for the built-in type "name".
* Functions for the built-in type "name".
* name replaces char16 and is carefully implemented so that it
* is a string of length NAMEDATALEN. DO NOT use hard-coded constants anywhere
* always use NAMEDATALEN as the symbolic constant! - jolly 8/21/95
*
*
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.6 1997/08/19 21:34:45 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.7 1997/09/07 04:50:27 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include "postgres.h"
#include "utils/builtins.h" /* where the declarations go */
#include "utils/palloc.h" /* where the declarations go */
#include "utils/builtins.h" /* where the declarations go */
#include "utils/palloc.h" /* where the declarations go */
/*****************************************************************************
* USER I/O ROUTINES (none) *
/*****************************************************************************
* USER I/O ROUTINES (none) *
*****************************************************************************/
/*
* namein - converts "..." to internal representation
* namein - converts "..." to internal representation
*
* Note:
* [Old] Currently if strlen(s) < NAMEDATALEN, the extra chars are nulls
* Now, always NULL terminated
* Note:
* [Old] Currently if strlen(s) < NAMEDATALEN, the extra chars are nulls
* Now, always NULL terminated
*/
NameData *namein(char *s)
NameData *
namein(char *s)
{
NameData *result;
NameData *result;
if (s == NULL)
return(NULL);
result = (NameData*) palloc(NAMEDATALEN);
/* always keep it null-padded */
strNcpy(result->data, s, NAMEDATALEN-1);
return(result);
if (s == NULL)
return (NULL);
result = (NameData *) palloc(NAMEDATALEN);
/* always keep it null-padded */
strNcpy(result->data, s, NAMEDATALEN - 1);
return (result);
}
/*
* nameout - converts internal reprsentation to "..."
* nameout - converts internal reprsentation to "..."
*/
char *nameout(NameData *s)
char *
nameout(NameData * s)
{
if (s == NULL)
return "-";
else
return pstrdup(s->data);
if (s == NULL)
return "-";
else
return pstrdup(s->data);
}
/*****************************************************************************
* PUBLIC ROUTINES *
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
/*
* nameeq - returns 1 iff arguments are equal
* namene - returns 1 iff arguments are not equal
* nameeq - returns 1 iff arguments are equal
* namene - returns 1 iff arguments are not equal
*
* BUGS:
* Assumes that "xy\0\0a" should be equal to "xy\0b".
* If not, can do the comparison backwards for efficiency.
* BUGS:
* Assumes that "xy\0\0a" should be equal to "xy\0b".
* If not, can do the comparison backwards for efficiency.
*
* namelt - returns 1 iff a < b
* namele - returns 1 iff a <= b
* namegt - returns 1 iff a < b
* namege - returns 1 iff a <= b
* namelt - returns 1 iff a < b
* namele - returns 1 iff a <= b
* namegt - returns 1 iff a < b
* namege - returns 1 iff a <= b
*
*/
bool nameeq(NameData *arg1, NameData *arg2)
bool
nameeq(NameData * arg1, NameData * arg2)
{
if (!arg1 || !arg2)
return 0;
else
return ((bool) strncmp(arg1->data, arg2->data, NAMEDATALEN) == 0);
if (!arg1 || !arg2)
return 0;
else
return ((bool) strncmp(arg1->data, arg2->data, NAMEDATALEN) == 0);
}
bool namene(NameData *arg1, NameData *arg2)
bool
namene(NameData * arg1, NameData * arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) != 0));
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return ((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) != 0));
}
bool namelt(NameData *arg1, NameData *arg2)
bool
namelt(NameData * arg1, NameData * arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) < 0));
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return ((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) < 0));
}
bool namele(NameData *arg1, NameData *arg2)
bool
namele(NameData * arg1, NameData * arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) <= 0));
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return ((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) <= 0));
}
bool namegt(NameData *arg1, NameData *arg2)
bool
namegt(NameData * arg1, NameData * arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) > 0));
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return ((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) > 0));
}
bool namege(NameData *arg1, NameData *arg2)
bool
namege(NameData * arg1, NameData * arg2)
{
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
return((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) >= 0));
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
return ((bool) (strncmp(arg1->data, arg2->data, NAMEDATALEN) >= 0));
}
/* (see char.c for comparison/operation routines) */
int namecpy(Name n1, Name n2)
int
namecpy(Name n1, Name n2)
{
if (!n1 || !n2)
return(-1);
strncpy(n1->data, n2->data, NAMEDATALEN);
return(0);
if (!n1 || !n2)
return (-1);
strncpy(n1->data, n2->data, NAMEDATALEN);
return (0);
}
#ifdef NOT_USED
int namecat(Name n1, Name n2)
int
namecat(Name n1, Name n2)
{
return(namestrcat(n1, n2->data)); /* n2 can't be any longer than n1 */
return (namestrcat(n1, n2->data)); /* n2 can't be any longer than n1 */
}
#endif
int namecmp(Name n1, Name n2)
int
namecmp(Name n1, Name n2)
{
return(strncmp(n1->data, n2->data, NAMEDATALEN));
return (strncmp(n1->data, n2->data, NAMEDATALEN));
}
int
int
namestrcpy(Name name, char *str)
{
if (!name || !str)
return(-1);
strNcpy(name->data, str, NAMEDATALEN-1);
return(0);
if (!name || !str)
return (-1);
strNcpy(name->data, str, NAMEDATALEN - 1);
return (0);
}
#ifdef NOT_USED
int namestrcat(Name name, char *str)
int
namestrcat(Name name, char *str)
{
int i;
char *p, *q;
if (!name || !str)
return(-1);
for (i = 0, p = name->data; i < NAMEDATALEN && *p; ++i, ++p)
;
for (q = str; i < NAMEDATALEN; ++i, ++p, ++q) {
*p = *q;
if (!*q)
break;
}
return(0);
int i;
char *p,
*q;
if (!name || !str)
return (-1);
for (i = 0, p = name->data; i < NAMEDATALEN && *p; ++i, ++p)
;
for (q = str; i < NAMEDATALEN; ++i, ++p, ++q)
{
*p = *q;
if (!*q)
break;
}
return (0);
}
#endif
int
int
namestrcmp(Name name, char *str)
{
if (!name && !str)
return(0);
if (!name)
return(-1); /* NULL < anything */
if (!str)
return(1); /* NULL < anything */
return(strncmp(name->data, str, NAMEDATALEN));
if (!name && !str)
return (0);
if (!name)
return (-1); /* NULL < anything */
if (!str)
return (1); /* NULL < anything */
return (strncmp(name->data, str, NAMEDATALEN));
}
/*****************************************************************************
* PRIVATE ROUTINES *
/*****************************************************************************
* PRIVATE ROUTINES *
*****************************************************************************/
#ifdef NOT_USED
uint32
uint32
NameComputeLength(Name name)
{
char *charP;
int length;
for (length = 0, charP = name->data;
length < NAMEDATALEN && *charP != '\0';
length++, charP++) {
;
}
return (uint32)length;
char *charP;
int length;
for (length = 0, charP = name->data;
length < NAMEDATALEN && *charP != '\0';
length++, charP++)
{
;
}
return (uint32) length;
}
#endif

View File

@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* not_in.c--
* Executes the "not_in" operator for any data type
* Executes the "not_in" operator for any data type
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/not_in.c,v 1.3 1997/08/19 21:34:48 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/not_in.c,v 1.4 1997/09/07 04:50:29 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -27,81 +27,85 @@
#include "postgres.h"
#include "access/heapam.h"
#include "access/relscan.h"
#include "utils/builtins.h" /* where function decls go */
#include "utils/builtins.h" /* where function decls go */
static int my_varattno(Relation rd, char *a);
static int my_varattno(Relation rd, char *a);
/* ----------------------------------------------------------------
*
*
* ----------------------------------------------------------------
*/
bool
int4notin(int16 not_in_arg, char *relation_and_attr)
{
Relation relation_to_scan;
int left_side_argument, integer_value;
HeapTuple current_tuple;
HeapScanDesc scan_descriptor;
bool dummy, retval;
int attrid;
char *relation, *attribute;
char my_copy[32];
Datum value;
NameData relNameData;
ScanKeyData skeyData;
strcpy(my_copy, relation_and_attr);
relation = (char *) strtok(my_copy, ".");
attribute = (char *) strtok(NULL, ".");
/* fetch tuple OID */
left_side_argument = not_in_arg;
/* Open the relation and get a relation descriptor */
namestrcpy(&relNameData,relation);
relation_to_scan = heap_openr(relNameData.data);
attrid = my_varattno(relation_to_scan, attribute);
Relation relation_to_scan;
int left_side_argument,
integer_value;
HeapTuple current_tuple;
HeapScanDesc scan_descriptor;
bool dummy,
retval;
int attrid;
char *relation,
*attribute;
char my_copy[32];
Datum value;
NameData relNameData;
ScanKeyData skeyData;
/* the last argument should be a ScanKey, not an integer! - jolly*/
/* it looks like the arguments are out of order, too */
/* but skeyData is never initialized! does this work?? - ay 2/95 */
scan_descriptor = heap_beginscan(relation_to_scan, false, NULL, 0,
&skeyData);
strcpy(my_copy, relation_and_attr);
retval = true;
/* do a scan of the relation, and do the check */
for (current_tuple = heap_getnext(scan_descriptor, 0, NULL);
current_tuple != NULL && retval;
current_tuple = heap_getnext(scan_descriptor, 0, NULL))
relation = (char *) strtok(my_copy, ".");
attribute = (char *) strtok(NULL, ".");
/* fetch tuple OID */
left_side_argument = not_in_arg;
/* Open the relation and get a relation descriptor */
namestrcpy(&relNameData, relation);
relation_to_scan = heap_openr(relNameData.data);
attrid = my_varattno(relation_to_scan, attribute);
/* the last argument should be a ScanKey, not an integer! - jolly */
/* it looks like the arguments are out of order, too */
/* but skeyData is never initialized! does this work?? - ay 2/95 */
scan_descriptor = heap_beginscan(relation_to_scan, false, NULL, 0,
&skeyData);
retval = true;
/* do a scan of the relation, and do the check */
for (current_tuple = heap_getnext(scan_descriptor, 0, NULL);
current_tuple != NULL && retval;
current_tuple = heap_getnext(scan_descriptor, 0, NULL))
{
value = PointerGetDatum(heap_getattr(current_tuple,
InvalidBuffer,
(AttrNumber) attrid,
RelationGetTupleDescriptor(relation_to_scan),
&dummy));
integer_value = DatumGetInt16(value);
if (left_side_argument == integer_value)
value = PointerGetDatum(heap_getattr(current_tuple,
InvalidBuffer,
(AttrNumber) attrid,
RelationGetTupleDescriptor(relation_to_scan),
&dummy));
integer_value = DatumGetInt16(value);
if (left_side_argument == integer_value)
{
retval = false;
retval = false;
}
}
/* close the relation */
heap_close(relation_to_scan);
return(retval);
/* close the relation */
heap_close(relation_to_scan);
return (retval);
}
bool oidnotin(Oid the_oid, char *compare)
bool
oidnotin(Oid the_oid, char *compare)
{
if (the_oid == InvalidOid)
return false;
return(int4notin(the_oid, compare));
if (the_oid == InvalidOid)
return false;
return (int4notin(the_oid, compare));
}
/*
@@ -109,15 +113,17 @@ bool oidnotin(Oid the_oid, char *compare)
* If varattno (in parser/catalog_utils.h) ever is added to
* cinterface.a, this routine should go away
*/
static int my_varattno(Relation rd, char *a)
static int
my_varattno(Relation rd, char *a)
{
int i;
for (i = 0; i < rd->rd_rel->relnatts; i++) {
if (!namestrcmp(&rd->rd_att->attrs[i]->attname, a)) {
return(i+1);
}
}
return(-1);
}
int i;
for (i = 0; i < rd->rd_rel->relnatts; i++)
{
if (!namestrcmp(&rd->rd_att->attrs[i]->attname, a))
{
return (i + 1);
}
}
return (-1);
}

View File

@@ -1,410 +1,449 @@
/*-------------------------------------------------------------------------
*
* numutils.c--
* utility functions for I/O of built-in numeric types.
* utility functions for I/O of built-in numeric types.
*
* integer: itoa, ltoa
* floating point: ftoa, atof1
* integer: itoa, ltoa
* floating point: ftoa, atof1
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.13 1997/08/19 21:34:51 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.14 1997/09/07 04:50:33 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h> /* for sprintf() */
#include <stdio.h> /* for sprintf() */
#include <errno.h>
#include <math.h>
#include "postgres.h"
#include "utils/builtins.h" /* where the declarations go */
#ifndef HAVE_MEMMOVE
# include <regex/utils.h>
#include <regex/utils.h>
#else
# include <string.h>
#include <string.h>
#endif
#include <port-protos.h> /* ecvt(), fcvt() */
#include <port-protos.h> /* ecvt(), fcvt() */
int32
pg_atoi(char *s, int size, int c)
{
long l;
char *badp = (char *) NULL;
Assert(s);
errno = 0;
l = strtol(s, &badp, 10);
if (errno) /* strtol must set ERANGE */
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
if (badp && *badp && (*badp != c))
elog(WARN, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);
switch (size) {
case sizeof(int32):
long l;
char *badp = (char *) NULL;
Assert(s);
errno = 0;
l = strtol(s, &badp, 10);
if (errno) /* strtol must set ERANGE */
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
if (badp && *badp && (*badp != c))
elog(WARN, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);
switch (size)
{
case sizeof(int32):
#ifdef HAS_LONG_LONG
/* won't get ERANGE on these with 64-bit longs... */
if (l < -0x80000000L) {
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
/* won't get ERANGE on these with 64-bit longs... */
if (l < -0x80000000L)
{
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
if (l > 0x7fffffffL)
{
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
#endif /* HAS_LONG_LONG */
break;
case sizeof(int16):
if (l < -0x8000)
{
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
if (l > 0x7fff)
{
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
break;
case sizeof(int8):
if (l < -0x80)
{
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
if (l > 0x7f)
{
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
break;
default:
elog(WARN, "pg_atoi: invalid result size: %d", size);
}
if (l > 0x7fffffffL) {
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
#endif /* HAS_LONG_LONG */
break;
case sizeof(int16):
if (l < -0x8000) {
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
if (l > 0x7fff) {
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
break;
case sizeof(int8):
if (l < -0x80) {
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
if (l > 0x7f) {
errno = ERANGE;
elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
}
break;
default:
elog(WARN, "pg_atoi: invalid result size: %d", size);
}
return((int32) l);
return ((int32) l);
}
/*
* itoa - converts a short int to its string represention
* itoa - converts a short int to its string represention
*
* Note:
* previously based on ~ingres/source/gutil/atoi.c
* now uses vendor's sprintf conversion
* Note:
* previously based on ~ingres/source/gutil/atoi.c
* now uses vendor's sprintf conversion
*/
void
itoa(int i, char *a)
{
sprintf(a, "%hd", (short)i);
sprintf(a, "%hd", (short) i);
}
/*
* ltoa - converts a long int to its string represention
* ltoa - converts a long int to its string represention
*
* Note:
* previously based on ~ingres/source/gutil/atoi.c
* now uses vendor's sprintf conversion
* Note:
* previously based on ~ingres/source/gutil/atoi.c
* now uses vendor's sprintf conversion
*/
void
ltoa(int32 l, char *a)
{
sprintf(a, "%d", l);
sprintf(a, "%d", l);
}
/*
** ftoa - FLOATING POINT TO ASCII CONVERSION
** ftoa - FLOATING POINT TO ASCII CONVERSION
**
** CODE derived from ingres, ~ingres/source/gutil/ftoa.c
** CODE derived from ingres, ~ingres/source/gutil/ftoa.c
**
** 'Value' is converted to an ascii character string and stored
** into 'ascii'. Ascii should have room for at least 'width' + 1
** characters. 'Width' is the width of the output field (max).
** 'Prec' is the number of characters to put after the decimal
** point. The format of the output string is controlled by
** 'format'.
** 'Value' is converted to an ascii character string and stored
** into 'ascii'. Ascii should have room for at least 'width' + 1
** characters. 'Width' is the width of the output field (max).
** 'Prec' is the number of characters to put after the decimal
** point. The format of the output string is controlled by
** 'format'.
**
** 'Format' can be:
** e or E: "E" format output
** f or F: "F" format output
** g or G: "F" format output if it will fit, otherwise
** use "E" format.
** n or N: same as G, but decimal points will not always
** be aligned.
** 'Format' can be:
** e or E: "E" format output
** f or F: "F" format output
** g or G: "F" format output if it will fit, otherwise
** use "E" format.
** n or N: same as G, but decimal points will not always
** be aligned.
**
** If 'format' is upper case, the "E" comes out in upper case;
** otherwise it comes out in lower case.
** If 'format' is upper case, the "E" comes out in upper case;
** otherwise it comes out in lower case.
**
** When the field width is not big enough, it fills the field with
** stars ("*****") and returns zero. Normal return is the width
** of the output field (sometimes shorter than 'width').
** When the field width is not big enough, it fills the field with
** stars ("*****") and returns zero. Normal return is the width
** of the output field (sometimes shorter than 'width').
*/
#ifdef NOT_USED
int
ftoa(double value, char *ascii, int width, int prec1, char format)
{
#ifndef HAVE_FCVT
char out[256];
char fmt[256];
int ret;
char out[256];
char fmt[256];
int ret;
sprintf(fmt, "%%%d.%d%c", width, prec1, format);
sprintf(out, fmt, value);
if ((ret = strlen(out)) > width) {
if ((ret = strlen(out)) > width)
{
memset(ascii, '*', width - 2);
ascii[width] = 0;
return(0);
return (0);
}
strcpy(ascii, out);
return(ret);
return (ret);
#else
auto int expon;
auto int sign;
register int avail = 0;
register char *a = NULL;
register char *p = NULL;
char mode;
int lowercase;
int prec;
/* extern char *ecvt(), *fcvt();*/
prec = prec1;
mode = format;
lowercase = 'a' - 'A';
if (mode >= 'a')
mode -= 'a' - 'A';
else
lowercase = 0;
if (mode != 'E') {
/* try 'F' style output */
p = fcvt(value, prec, &expon, &sign);
avail = width;
a = ascii;
/* output sign */
if (sign) {
avail--;
*a++ = '-';
auto int expon;
auto int sign;
register int avail = 0;
register char *a = NULL;
register char *p = NULL;
char mode;
int lowercase;
int prec;
/* extern char *ecvt(), *fcvt();*/
prec = prec1;
mode = format;
lowercase = 'a' - 'A';
if (mode >= 'a')
mode -= 'a' - 'A';
else
lowercase = 0;
if (mode != 'E')
{
/* try 'F' style output */
p = fcvt(value, prec, &expon, &sign);
avail = width;
a = ascii;
/* output sign */
if (sign)
{
avail--;
*a++ = '-';
}
/* output '0' before the decimal point */
if (expon <= 0)
{
*a++ = '0';
avail--;
}
/* compute space length left after dec pt and fraction */
avail -= prec + 1;
if (mode == 'G')
avail -= 4;
if (avail >= expon)
{
/* it fits. output */
while (expon > 0)
{
/* output left of dp */
expon--;
if (*p)
{
*a++ = *p++;
}
else
*a++ = '0';
}
/* output fraction (right of dec pt) */
avail = expon;
goto frac_out;
}
/* won't fit; let's hope for G format */
}
/* output '0' before the decimal point */
if (expon <= 0) {
*a++ = '0';
avail--;
if (mode != 'F')
{
/* try to do E style output */
p = ecvt(value, prec + 1, &expon, &sign);
avail = width - 5;
a = ascii;
/* output the sign */
if (sign)
{
*a++ = '-';
avail--;
}
}
/* compute space length left after dec pt and fraction */
avail -= prec + 1;
/* check for field too small */
if (mode == 'F' || avail < prec)
{
/* sorry joker, you lose */
a = ascii;
for (avail = width; avail > 0; avail--)
*a++ = '*';
*a = 0;
return (0);
}
/* it fits; output the number */
mode = 'E';
/* output the LHS single digit */
*a++ = *p++;
expon--;
/* output the rhs */
avail = 1;
frac_out:
*a++ = '.';
while (prec > 0)
{
prec--;
if (avail < 0)
{
avail++;
*a++ = '0';
}
else
{
if (*p)
*a++ = *p++;
else
*a++ = '0';
}
}
/* output the exponent */
if (mode == 'E')
{
*a++ = 'E' + lowercase;
if (expon < 0)
{
*a++ = '-';
expon = -expon;
}
else
*a++ = '+';
*a++ = (expon / 10) % 10 + '0';
*a++ = expon % 10 + '0';
}
/* output spaces on the end in G format */
if (mode == 'G')
avail -= 4;
if (avail >= expon) {
/* it fits. output */
while (expon > 0) {
/* output left of dp */
expon--;
if (*p) {
*a++ = *p++;
} else
*a++ = '0';
}
/* output fraction (right of dec pt) */
avail = expon;
goto frac_out;
{
*a++ = ' ';
*a++ = ' ';
*a++ = ' ';
*a++ = ' ';
}
/* won't fit; let's hope for G format */
}
if (mode != 'F') {
/* try to do E style output */
p = ecvt(value, prec + 1, &expon, &sign);
avail = width - 5;
a = ascii;
/* output the sign */
if (sign) {
*a++ = '-';
avail--;
}
}
/* check for field too small */
if (mode == 'F' || avail < prec) {
/* sorry joker, you lose */
a = ascii;
for (avail = width; avail > 0; avail--)
*a++ = '*';
/* finally, we can return */
*a = 0;
return (0);
}
/* it fits; output the number */
mode = 'E';
/* output the LHS single digit */
*a++ = *p++;
expon--;
/* output the rhs */
avail = 1;
frac_out:
*a++ = '.';
while (prec > 0) {
prec--;
if (avail < 0) {
avail++;
*a++ = '0';
} else {
if (*p)
*a++ = *p++;
else
*a++ = '0';
}
}
/* output the exponent */
if (mode == 'E') {
*a++ = 'E' + lowercase;
if (expon < 0) {
*a++ = '-';
expon = -expon;
} else
*a++ = '+';
*a++ = (expon / 10) % 10 + '0';
*a++ = expon % 10 + '0';
}
/* output spaces on the end in G format */
if (mode == 'G') {
*a++ = ' ';
*a++ = ' ';
*a++ = ' ';
*a++ = ' ';
}
/* finally, we can return */
*a = 0;
avail = a - ascii;
return (avail);
#endif /* !BSD44_derived */
avail = a - ascii;
return (avail);
#endif /* !BSD44_derived */
}
#endif
/*
** atof1 - ASCII TO FLOATING CONVERSION
** atof1 - ASCII TO FLOATING CONVERSION
**
** CODE derived from ~ingres/source/gutil/atof.c
** CODE derived from ~ingres/source/gutil/atof.c
**
** Converts the string 'str' to floating point and stores the
** result into the cell pointed to by 'val'.
** Converts the string 'str' to floating point and stores the
** result into the cell pointed to by 'val'.
**
** The syntax which it accepts is pretty much what you would
** expect. Basically, it is:
** {<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
** where <exp> is "e" or "E" followed by an integer, <sp> is a
** space character, <digit> is zero through nine, [] is zero or
** one, and {} is zero or more.
** The syntax which it accepts is pretty much what you would
** expect. Basically, it is:
** {<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
** where <exp> is "e" or "E" followed by an integer, <sp> is a
** space character, <digit> is zero through nine, [] is zero or
** one, and {} is zero or more.
**
** Parameters:
** str -- string to convert.
** val -- pointer to place to put the result (which
** must be type double).
** Parameters:
** str -- string to convert.
** val -- pointer to place to put the result (which
** must be type double).
**
** Returns:
** zero -- ok.
** -1 -- syntax error.
** +1 -- overflow (not implemented).
** Returns:
** zero -- ok.
** -1 -- syntax error.
** +1 -- overflow (not implemented).
**
** Side Effects:
** clobbers *val.
** Side Effects:
** clobbers *val.
*/
#ifdef NOT_USED
int
atof1(char *str, double *val)
{
register char *p;
double v;
double fact;
int minus;
register char c;
int expon;
register int gotmant;
v = 0.0;
p = str;
minus = 0;
/* skip leading blanks */
while ((c = *p) != '\0') {
if (c != ' ')
break;
p++;
}
/* handle possible sign */
switch (c) {
case '-':
minus++;
case '+':
p++;
}
/* skip blanks after sign */
while ((c = *p) != '\0') {
if (c != ' ')
break;
p++;
}
/* start collecting the number to the decimal point */
gotmant = 0;
for (;;) {
c = *p;
if (c < '0' || c > '9')
break;
v = v * 10.0 + (c - '0');
gotmant++;
p++;
}
/* check for fractional part */
if (c == '.') {
fact = 1.0;
for (;;) {
c = *++p;
if (c < '0' || c > '9')
break;
fact *= 0.1;
v += (c - '0') * fact;
gotmant++;
register char *p;
double v;
double fact;
int minus;
register char c;
int expon;
register int gotmant;
v = 0.0;
p = str;
minus = 0;
/* skip leading blanks */
while ((c = *p) != '\0')
{
if (c != ' ')
break;
p++;
}
}
/* skip blanks before possible exponent */
while ((c = *p) != '\0') {
if (c != ' ')
break;
p++;
}
/* test for exponent */
if (c == 'e' || c == 'E') {
p++;
expon = pg_atoi(p, sizeof(expon), '\0');
if (!gotmant)
v = 1.0;
fact = expon;
v *= pow(10.0, fact);
} else {
/* if no exponent, then nothing */
if (c != 0)
return (-1);
}
/* store the result and exit */
if (minus)
v = -v;
*val = v;
return (0);
/* handle possible sign */
switch (c)
{
case '-':
minus++;
case '+':
p++;
}
/* skip blanks after sign */
while ((c = *p) != '\0')
{
if (c != ' ')
break;
p++;
}
/* start collecting the number to the decimal point */
gotmant = 0;
for (;;)
{
c = *p;
if (c < '0' || c > '9')
break;
v = v * 10.0 + (c - '0');
gotmant++;
p++;
}
/* check for fractional part */
if (c == '.')
{
fact = 1.0;
for (;;)
{
c = *++p;
if (c < '0' || c > '9')
break;
fact *= 0.1;
v += (c - '0') * fact;
gotmant++;
}
}
/* skip blanks before possible exponent */
while ((c = *p) != '\0')
{
if (c != ' ')
break;
p++;
}
/* test for exponent */
if (c == 'e' || c == 'E')
{
p++;
expon = pg_atoi(p, sizeof(expon), '\0');
if (!gotmant)
v = 1.0;
fact = expon;
v *= pow(10.0, fact);
}
else
{
/* if no exponent, then nothing */
if (c != 0)
return (-1);
}
/* store the result and exit */
if (minus)
v = -v;
*val = v;
return (0);
}
#endif

View File

@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* oid.c--
* Functions for the built-in type Oid.
* Functions for the built-in type Oid.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/oid.c,v 1.8 1997/08/24 23:07:35 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/oid.c,v 1.9 1997/09/07 04:50:34 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,114 +16,125 @@
#include <string.h>
#include "postgres.h"
#include "utils/builtins.h" /* where function declarations go */
#include "utils/builtins.h" /* where function declarations go */
/*****************************************************************************
* USER I/O ROUTINES *
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
/*
* oid8in - converts "num num ..." to internal form
* oid8in - converts "num num ..." to internal form
*
* Note:
* Fills any nonexistent digits with NULL oids.
* Note:
* Fills any nonexistent digits with NULL oids.
*/
Oid *oid8in(char *oidString)
Oid *
oid8in(char *oidString)
{
register Oid (*result)[];
int nums;
if (oidString == NULL)
return(NULL);
result = (Oid (*)[]) palloc(sizeof(Oid [8]));
if ((nums = sscanf(oidString, "%d%d%d%d%d%d%d%d",
&(*result)[0],
&(*result)[1],
&(*result)[2],
&(*result)[3],
&(*result)[4],
&(*result)[5],
&(*result)[6],
&(*result)[7])) != 8) {
do
(*result)[nums++] = 0;
while (nums < 8);
}
return((Oid *) result);
register Oid(*result)[];
int nums;
if (oidString == NULL)
return (NULL);
result = (Oid(*)[]) palloc(sizeof(Oid[8]));
if ((nums = sscanf(oidString, "%d%d%d%d%d%d%d%d",
&(*result)[0],
&(*result)[1],
&(*result)[2],
&(*result)[3],
&(*result)[4],
&(*result)[5],
&(*result)[6],
&(*result)[7])) != 8)
{
do
(*result)[nums++] = 0;
while (nums < 8);
}
return ((Oid *) result);
}
/*
* oid8out - converts internal form to "num num ..."
* oid8out - converts internal form to "num num ..."
*/
char *oid8out(Oid (*oidArray)[])
char *
oid8out(Oid(*oidArray)[])
{
register int num;
register Oid *sp;
register char *rp;
char *result;
if (oidArray == NULL) {
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return(result);
}
/* assumes sign, 10 digits, ' ' */
rp = result = (char *) palloc(8 * 12);
sp = *oidArray;
for (num = 8; num != 0; num--) {
ltoa(*sp++, rp);
while (*++rp != '\0')
;
*rp++ = ' ';
}
*--rp = '\0';
return(result);
register int num;
register Oid *sp;
register char *rp;
char *result;
if (oidArray == NULL)
{
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return (result);
}
/* assumes sign, 10 digits, ' ' */
rp = result = (char *) palloc(8 * 12);
sp = *oidArray;
for (num = 8; num != 0; num--)
{
ltoa(*sp++, rp);
while (*++rp != '\0')
;
*rp++ = ' ';
}
*--rp = '\0';
return (result);
}
Oid oidin(char *s)
Oid
oidin(char *s)
{
return(int4in(s));
return (int4in(s));
}
char *oidout(Oid o)
char *
oidout(Oid o)
{
return(int4out(o));
return (int4out(o));
}
/*****************************************************************************
* PUBLIC ROUTINES *
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
/*
* If you change this function, change heap_keytest()
* because we have hardcoded this in there as an optimization
*/
bool oideq(Oid arg1, Oid arg2)
bool
oideq(Oid arg1, Oid arg2)
{
return(arg1 == arg2);
return (arg1 == arg2);
}
bool oidne(Oid arg1, Oid arg2)
bool
oidne(Oid arg1, Oid arg2)
{
return(arg1 != arg2);
return (arg1 != arg2);
}
bool oid8eq(Oid arg1[], Oid arg2[])
bool
oid8eq(Oid arg1[], Oid arg2[])
{
return (bool)(memcmp(arg1, arg2, 8 * sizeof(Oid)) == 0);
return (bool) (memcmp(arg1, arg2, 8 * sizeof(Oid)) == 0);
}
bool oideqint4(Oid arg1, int32 arg2)
bool
oideqint4(Oid arg1, int32 arg2)
{
/* oid is unsigned, but int4 is signed */
return (arg2 >= 0 && arg1 == arg2);
return (arg2 >= 0 && arg1 == arg2);
}
bool int4eqoid(int32 arg1, Oid arg2)
bool
int4eqoid(int32 arg1, Oid arg2)
{
/* oid is unsigned, but int4 is signed */
return (arg1 >= 0 && arg1 == arg2);
return (arg1 >= 0 && arg1 == arg2);
}

View File

@@ -1,120 +1,120 @@
/*-------------------------------------------------------------------------
*
* oidint2.c--
* Functions for the built-in type "oidint2".
* Functions for the built-in type "oidint2".
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/oidint2.c,v 1.1.1.1 1996/07/09 06:22:05 scrappy Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/oidint2.c,v 1.2 1997/09/07 04:50:35 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h>
#include "postgres.h"
#include "utils/palloc.h"
#include "utils/builtins.h" /* for pg_atoi() */
#include "utils/builtins.h" /* for pg_atoi() */
#include "utils/oidcompos.h" /* where function declarations go */
OidInt2
oidint2in(char *o)
{
OidInt2 oi;
char *p;
oi = (OidInt2) palloc(sizeof(OidInt2Data));
for (p = o; *p != '\0' && *p != '/'; p++)
continue;
oi->oi_oid = (Oid) pg_atoi(o, sizeof(Oid), '/');
if (*p == '\0') {
oi->oi_int2 = 0;
} else {
oi->oi_int2 = (int16) pg_atoi(++p, sizeof(int2), '\0');
}
return (oi);
OidInt2 oi;
char *p;
oi = (OidInt2) palloc(sizeof(OidInt2Data));
for (p = o; *p != '\0' && *p != '/'; p++)
continue;
oi->oi_oid = (Oid) pg_atoi(o, sizeof(Oid), '/');
if (*p == '\0')
{
oi->oi_int2 = 0;
}
else
{
oi->oi_int2 = (int16) pg_atoi(++p, sizeof(int2), '\0');
}
return (oi);
}
char *
char *
oidint2out(OidInt2 o)
{
char *r;
/*
* -2147483647/-32767
* 0 1
* 1234567890123456789
*/
r = (char *) palloc(19);
sprintf(r, "%d/%d", o->oi_oid, o->oi_int2);
return (r);
char *r;
/*
* -2147483647/-32767 0 1 1234567890123456789
*/
r = (char *) palloc(19);
sprintf(r, "%d/%d", o->oi_oid, o->oi_int2);
return (r);
}
bool
oidint2lt(OidInt2 o1, OidInt2 o2)
{
return
((bool) (o1->oi_oid < o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int2 < o2->oi_int2)));
return
((bool) (o1->oi_oid < o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int2 < o2->oi_int2)));
}
bool
oidint2le(OidInt2 o1, OidInt2 o2)
{
return ((bool) (o1->oi_oid < o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int2 <= o2->oi_int2)));
return ((bool) (o1->oi_oid < o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int2 <= o2->oi_int2)));
}
bool
oidint2eq(OidInt2 o1, OidInt2 o2)
{
return ((bool) (o1->oi_oid == o2->oi_oid && o1->oi_int2 == o2->oi_int2));
return ((bool) (o1->oi_oid == o2->oi_oid && o1->oi_int2 == o2->oi_int2));
}
bool
oidint2ge(OidInt2 o1, OidInt2 o2)
{
return ((bool) (o1->oi_oid > o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int2 >= o2->oi_int2)));
return ((bool) (o1->oi_oid > o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int2 >= o2->oi_int2)));
}
bool
oidint2gt(OidInt2 o1, OidInt2 o2)
{
return ((bool) (o1->oi_oid > o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int2 > o2->oi_int2)));
return ((bool) (o1->oi_oid > o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int2 > o2->oi_int2)));
}
bool
oidint2ne(OidInt2 o1, OidInt2 o2)
{
return ((bool) (o1->oi_oid != o2->oi_oid || o1->oi_int2 != o2->oi_int2));
return ((bool) (o1->oi_oid != o2->oi_oid || o1->oi_int2 != o2->oi_int2));
}
int
oidint2cmp(OidInt2 o1, OidInt2 o2)
{
if (oidint2lt(o1, o2))
return (-1);
else if (oidint2eq(o1, o2))
return (0);
else
return (1);
if (oidint2lt(o1, o2))
return (-1);
else if (oidint2eq(o1, o2))
return (0);
else
return (1);
}
OidInt2
mkoidint2(Oid v_oid, uint16 v_int2)
{
OidInt2 o;
o = (OidInt2) palloc(sizeof(OidInt2Data));
o->oi_oid = v_oid;
o->oi_int2 = v_int2;
return (o);
}
OidInt2 o;
o = (OidInt2) palloc(sizeof(OidInt2Data));
o->oi_oid = v_oid;
o->oi_int2 = v_int2;
return (o);
}

View File

@@ -1,111 +1,120 @@
/*-------------------------------------------------------------------------
*
* oidint4.c--
* Functions for the built-in type "oidint4".
* Functions for the built-in type "oidint4".
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/oidint4.c,v 1.1.1.1 1996/07/09 06:22:05 scrappy Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/oidint4.c,v 1.2 1997/09/07 04:50:36 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h> /* for sprintf() */
#include <stdio.h> /* for sprintf() */
#include "postgres.h"
#include "utils/palloc.h"
#include "utils/builtins.h"
#include "utils/oidcompos.h" /* where function declarations go */
OidInt4 oidint4in(char *o)
OidInt4
oidint4in(char *o)
{
OidInt4 oi;
char *p;
oi = (OidInt4) palloc(sizeof(OidInt4Data));
for (p = o; *p != '\0' && *p != '/'; p++)
continue;
oi->oi_oid = (Oid) pg_atoi(o, sizeof(Oid), '/');
if (*p == '\0') {
oi->oi_int4 = 0;
} else {
oi->oi_int4 = pg_atoi(++p, sizeof(int4), '\0');
}
return (oi);
OidInt4 oi;
char *p;
oi = (OidInt4) palloc(sizeof(OidInt4Data));
for (p = o; *p != '\0' && *p != '/'; p++)
continue;
oi->oi_oid = (Oid) pg_atoi(o, sizeof(Oid), '/');
if (*p == '\0')
{
oi->oi_int4 = 0;
}
else
{
oi->oi_int4 = pg_atoi(++p, sizeof(int4), '\0');
}
return (oi);
}
char *oidint4out(OidInt4 o)
char *
oidint4out(OidInt4 o)
{
char *r;
/*
* -2147483647/-2147483647
* 0 1 2
* 123456789012345678901234
*/
r = (char *) palloc(24);
sprintf(r, "%d/%d", o->oi_oid, o->oi_int4);
return (r);
char *r;
/*
* -2147483647/-2147483647 0 1 2
* 123456789012345678901234
*/
r = (char *) palloc(24);
sprintf(r, "%d/%d", o->oi_oid, o->oi_int4);
return (r);
}
bool oidint4lt(OidInt4 o1, OidInt4 o2)
bool
oidint4lt(OidInt4 o1, OidInt4 o2)
{
return
return
((bool) (o1->oi_oid < o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int4 < o2->oi_int4)));
(o1->oi_oid == o2->oi_oid && o1->oi_int4 < o2->oi_int4)));
}
bool oidint4le(OidInt4 o1, OidInt4 o2)
bool
oidint4le(OidInt4 o1, OidInt4 o2)
{
return ((bool) (o1->oi_oid < o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int4 <= o2->oi_int4)));
return ((bool) (o1->oi_oid < o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int4 <= o2->oi_int4)));
}
bool oidint4eq(OidInt4 o1, OidInt4 o2)
bool
oidint4eq(OidInt4 o1, OidInt4 o2)
{
return ((bool) (o1->oi_oid == o2->oi_oid && o1->oi_int4 == o2->oi_int4));
return ((bool) (o1->oi_oid == o2->oi_oid && o1->oi_int4 == o2->oi_int4));
}
bool oidint4ge(OidInt4 o1, OidInt4 o2)
bool
oidint4ge(OidInt4 o1, OidInt4 o2)
{
return ((bool) (o1->oi_oid > o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int4 >= o2->oi_int4)));
return ((bool) (o1->oi_oid > o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int4 >= o2->oi_int4)));
}
bool oidint4gt(OidInt4 o1, OidInt4 o2)
bool
oidint4gt(OidInt4 o1, OidInt4 o2)
{
return ((bool) (o1->oi_oid > o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int4 > o2->oi_int4)));
return ((bool) (o1->oi_oid > o2->oi_oid ||
(o1->oi_oid == o2->oi_oid && o1->oi_int4 > o2->oi_int4)));
}
bool oidint4ne(OidInt4 o1, OidInt4 o2)
bool
oidint4ne(OidInt4 o1, OidInt4 o2)
{
return ((bool) (o1->oi_oid != o2->oi_oid || o1->oi_int4 != o2->oi_int4));
return ((bool) (o1->oi_oid != o2->oi_oid || o1->oi_int4 != o2->oi_int4));
}
int oidint4cmp(OidInt4 o1, OidInt4 o2)
int
oidint4cmp(OidInt4 o1, OidInt4 o2)
{
if (oidint4lt(o1, o2))
return (-1);
else if (oidint4eq(o1, o2))
return (0);
else
return (1);
if (oidint4lt(o1, o2))
return (-1);
else if (oidint4eq(o1, o2))
return (0);
else
return (1);
}
OidInt4 mkoidint4(Oid v_oid, uint32 v_int4)
OidInt4
mkoidint4(Oid v_oid, uint32 v_int4)
{
OidInt4 o;
o = (OidInt4) palloc(sizeof(OidInt4Data));
o->oi_oid = v_oid;
o->oi_int4 = v_int4;
return (o);
OidInt4 o;
o = (OidInt4) palloc(sizeof(OidInt4Data));
o->oi_oid = v_oid;
o->oi_int4 = v_int4;
return (o);
}

View File

@@ -1,14 +1,14 @@
/*-------------------------------------------------------------------------
*
* oidname.c--
* adt for multiple key indices involving oid and name. Used for cache
* index scans (could also be used in the general case with name).
* adt for multiple key indices involving oid and name. Used for cache
* index scans (could also be used in the general case with name).
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/oidname.c,v 1.4 1997/08/12 20:16:03 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/oidname.c,v 1.5 1997/09/07 04:50:36 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -17,106 +17,109 @@
#include "postgres.h"
#include "utils/oidcompos.h" /* where function declarations go */
#include "utils/builtins.h" /* for pg_atoi() */
#include "utils/builtins.h" /* for pg_atoi() */
#include "utils/palloc.h"
OidName
oidnamein(char *inStr)
{
OidName oc;
char *inptr;
oc = (OidName) palloc(sizeof(OidNameData));
memset(oc, 0, sizeof(OidNameData));
for (inptr = inStr; *inptr && *inptr != ','; inptr++)
;
if (*inptr) {
oc->id = (Oid) pg_atoi(inStr, sizeof(Oid), ',');
/* copy one less to ensure null-padding */
++inptr;
strNcpy(oc->name.data,inptr,NAMEDATALEN-1);
}else
elog(WARN, "Bad input data for type oidname");
return oc;
OidName oc;
char *inptr;
oc = (OidName) palloc(sizeof(OidNameData));
memset(oc, 0, sizeof(OidNameData));
for (inptr = inStr; *inptr && *inptr != ','; inptr++)
;
if (*inptr)
{
oc->id = (Oid) pg_atoi(inStr, sizeof(Oid), ',');
/* copy one less to ensure null-padding */
++inptr;
strNcpy(oc->name.data, inptr, NAMEDATALEN - 1);
}
else
elog(WARN, "Bad input data for type oidname");
return oc;
}
char *
char *
oidnameout(OidName oidname)
{
char buf[30+NAMEDATALEN]; /* oidname length + oid length + some safety */
char *res;
sprintf(buf, "%d,%s", oidname->id, oidname->name.data);
res = pstrdup(buf);
return(res);
char buf[30 + NAMEDATALEN]; /* oidname length + oid
* length + some safety */
char *res;
sprintf(buf, "%d,%s", oidname->id, oidname->name.data);
res = pstrdup(buf);
return (res);
}
bool
oidnamelt(OidName o1, OidName o2)
{
return (bool)
(o1->id < o2->id ||
(o1->id == o2->id && namecmp(&o1->name, &o2->name) < 0));
return (bool)
(o1->id < o2->id ||
(o1->id == o2->id && namecmp(&o1->name, &o2->name) < 0));
}
bool
oidnamele(OidName o1, OidName o2)
{
return (bool)
(o1->id < o2->id ||
(o1->id == o2->id && namecmp(&o1->name,&o2->name) <= 0));
return (bool)
(o1->id < o2->id ||
(o1->id == o2->id && namecmp(&o1->name, &o2->name) <= 0));
}
bool
oidnameeq(OidName o1, OidName o2)
{
return (bool)
(o1->id == o2->id &&
(namecmp(&o1->name, &o2->name) == 0));
return (bool)
(o1->id == o2->id &&
(namecmp(&o1->name, &o2->name) == 0));
}
bool
oidnamene(OidName o1, OidName o2)
{
return (bool)
(o1->id != o2->id ||
(namecmp(&o1->name,&o2->name) != 0));
return (bool)
(o1->id != o2->id ||
(namecmp(&o1->name, &o2->name) != 0));
}
bool
oidnamege(OidName o1, OidName o2)
{
return (bool) (o1->id > o2->id || (o1->id == o2->id &&
namecmp(&o1->name, &o2->name) >= 0));
return (bool) (o1->id > o2->id || (o1->id == o2->id &&
namecmp(&o1->name, &o2->name) >= 0));
}
bool
oidnamegt(OidName o1, OidName o2)
{
return (bool) (o1->id > o2->id || (o1->id == o2->id &&
namecmp(&o1->name, &o2->name) > 0));
return (bool) (o1->id > o2->id || (o1->id == o2->id &&
namecmp(&o1->name, &o2->name) > 0));
}
int
oidnamecmp(OidName o1, OidName o2)
{
if (o1->id == o2->id)
return (namecmp(&o1->name,&o2->name));
return (o1->id < o2->id) ? -1 : 1;
if (o1->id == o2->id)
return (namecmp(&o1->name, &o2->name));
return (o1->id < o2->id) ? -1 : 1;
}
OidName
mkoidname(Oid id, char *name)
{
OidName oidname;
oidname = (OidName) palloc(sizeof(Oid)+NAMEDATALEN);
oidname->id = id;
namestrcpy(&oidname->name,name);
return oidname;
OidName oidname;
oidname = (OidName) palloc(sizeof(Oid) + NAMEDATALEN);
oidname->id = id;
namestrcpy(&oidname->name, name);
return oidname;
}

View File

@@ -1,7 +1,7 @@
/*
* Edmund Mergl <E.Mergl@bawue.de>
* Edmund Mergl <E.Mergl@bawue.de>
*
* $Id: oracle_compat.c,v 1.7 1997/07/29 16:12:01 thomas Exp $
* $Id: oracle_compat.c,v 1.8 1997/09/07 04:50:38 momjian Exp $
*
*/
@@ -10,16 +10,16 @@
#include "postgres.h"
text *lower(text *string);
text *upper(text *string);
text *initcap(text *string);
text *lpad(text *string1, int4 len, text *string2);
text *rpad(text *string1, int4 len, text *string2);
text *btrim(text *string, text *set);
text *ltrim(text *string, text *set);
text *rtrim(text *string, text *set);
text *substr(text *string, int4 m, int4 n);
text *translate(text *string, char from, char to);
text *lower(text * string);
text *upper(text * string);
text *initcap(text * string);
text *lpad(text * string1, int4 len, text * string2);
text *rpad(text * string1, int4 len, text * string2);
text *btrim(text * string, text * set);
text *ltrim(text * string, text * set);
text *rtrim(text * string, text * set);
text *substr(text * string, int4 m, int4 n);
text *translate(text * string, char from, char to);
/********************************************************************
@@ -28,35 +28,37 @@ text *translate(text *string, char from, char to);
*
* Syntax:
*
* text *lower(text *string)
* text *lower(text *string)
*
* Purpose:
*
* Returns string, with all letters forced to lowercase.
* Returns string, with all letters forced to lowercase.
*
********************************************************************/
text *
lower(text *string)
text *
lower(text * string)
{
text *ret;
char *ptr, *ptr_ret;
int m;
text *ret;
char *ptr,
*ptr_ret;
int m;
if ((string == (text *)NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
return string;
if ((string == (text *) NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
return string;
ret = (text *)palloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ret = (text *) palloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
while (m--) {
*ptr_ret++ = tolower(*ptr++);
}
while (m--)
{
*ptr_ret++ = tolower(*ptr++);
}
return ret;
return ret;
}
@@ -66,35 +68,37 @@ lower(text *string)
*
* Syntax:
*
* text *upper(text *string)
* text *upper(text *string)
*
* Purpose:
*
* Returns string, with all letters forced to uppercase.
* Returns string, with all letters forced to uppercase.
*
********************************************************************/
text *
upper(text *string)
text *
upper(text * string)
{
text *ret;
char *ptr, *ptr_ret;
int m;
text *ret;
char *ptr,
*ptr_ret;
int m;
if ((string == (text *)NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
return string;
if ((string == (text *) NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
return string;
ret = (text *)palloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ret = (text *) palloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
while (m--) {
*ptr_ret++ = toupper(*ptr++);
}
while (m--)
{
*ptr_ret++ = toupper(*ptr++);
}
return ret;
return ret;
}
@@ -104,44 +108,49 @@ upper(text *string)
*
* Syntax:
*
* text *initcap(text *string)
* text *initcap(text *string)
*
* Purpose:
*
* Returns string, with first letter of each word in uppercase,
* all other letters in lowercase. A word is delimited by white
* space.
* Returns string, with first letter of each word in uppercase,
* all other letters in lowercase. A word is delimited by white
* space.
*
********************************************************************/
text *
initcap(text *string)
text *
initcap(text * string)
{
text *ret;
char *ptr, *ptr_ret;
int m;
text *ret;
char *ptr,
*ptr_ret;
int m;
if ((string == (text *)NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
return string;
if ((string == (text *) NULL) || ((m = VARSIZE(string) - VARHDRSZ) <= 0))
return string;
ret = (text *)palloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ret = (text *) palloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
*ptr_ret++ = toupper(*ptr++);
--m;
*ptr_ret++ = toupper(*ptr++);
--m;
while (m--) {
if (*(ptr_ret - 1) == ' ' || *(ptr_ret - 1) == ' ') {
*ptr_ret++ = toupper(*ptr++);
} else {
*ptr_ret++ = tolower(*ptr++);
}
}
while (m--)
{
if (*(ptr_ret - 1) == ' ' || *(ptr_ret - 1) == ' ')
{
*ptr_ret++ = toupper(*ptr++);
}
else
{
*ptr_ret++ = tolower(*ptr++);
}
}
return ret;
return ret;
}
@@ -151,48 +160,53 @@ initcap(text *string)
*
* Syntax:
*
* text *lpad(text *string1, int4 len, text *string2)
* text *lpad(text *string1, int4 len, text *string2)
*
* Purpose:
*
* Returns string1, left-padded to length len with the sequence of
* characters in string2.
* Returns string1, left-padded to length len with the sequence of
* characters in string2.
*
********************************************************************/
text *
lpad(text *string1, int4 len, text *string2)
text *
lpad(text * string1, int4 len, text * string2)
{
text *ret;
char *ptr1, *ptr2, *ptr_ret;
int m, n;
text *ret;
char *ptr1,
*ptr2,
*ptr_ret;
int m,
n;
if ((string1 == (text *)NULL) ||
(len <= (VARSIZE(string1) - VARHDRSZ)) ||
((m = len - VARSIZE(string1) + VARHDRSZ) <= 0) ||
(string2 == (text *)NULL) ||
((VARSIZE(string2) - VARHDRSZ) <= 0))
return string1;
if ((string1 == (text *) NULL) ||
(len <= (VARSIZE(string1) - VARHDRSZ)) ||
((m = len - VARSIZE(string1) + VARHDRSZ) <= 0) ||
(string2 == (text *) NULL) ||
((VARSIZE(string2) - VARHDRSZ) <= 0))
return string1;
ret = (text *)palloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ret = (text *) palloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ptr2 = VARDATA(string2);
ptr_ret = VARDATA(ret);
ptr2 = VARDATA(string2);
ptr_ret = VARDATA(ret);
while (m--) {
*ptr_ret++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
}
while (m--)
{
*ptr_ret++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
}
n = VARSIZE(string1) - VARHDRSZ;
ptr1 = VARDATA(string1);
n = VARSIZE(string1) - VARHDRSZ;
ptr1 = VARDATA(string1);
while (n--) {
*ptr_ret++ = *ptr1++;
}
while (n--)
{
*ptr_ret++ = *ptr1++;
}
return ret;
return ret;
}
@@ -202,48 +216,53 @@ lpad(text *string1, int4 len, text *string2)
*
* Syntax:
*
* text *rpad(text *string1, int4 len, text *string2)
* text *rpad(text *string1, int4 len, text *string2)
*
* Purpose:
*
* Returns string1, right-padded to length len with the sequence of
* characters in string2.
* Returns string1, right-padded to length len with the sequence of
* characters in string2.
*
********************************************************************/
text *
rpad(text *string1, int4 len, text *string2)
text *
rpad(text * string1, int4 len, text * string2)
{
text *ret;
char *ptr1, *ptr2, *ptr_ret;
int m, n;
text *ret;
char *ptr1,
*ptr2,
*ptr_ret;
int m,
n;
if ((string1 == (text *)NULL) ||
(len <= (VARSIZE(string1) - VARHDRSZ)) ||
((m = len - VARSIZE(string1) + VARHDRSZ) <= 0) ||
(string2 == (text *)NULL) ||
((VARSIZE(string2) - VARHDRSZ) <= 0))
return string1;
if ((string1 == (text *) NULL) ||
(len <= (VARSIZE(string1) - VARHDRSZ)) ||
((m = len - VARSIZE(string1) + VARHDRSZ) <= 0) ||
(string2 == (text *) NULL) ||
((VARSIZE(string2) - VARHDRSZ) <= 0))
return string1;
ret = (text *)palloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ret = (text *) palloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
n = VARSIZE(string1) - VARHDRSZ;
ptr1 = VARDATA(string1);
ptr_ret = VARDATA(ret);
n = VARSIZE(string1) - VARHDRSZ;
ptr1 = VARDATA(string1);
ptr_ret = VARDATA(ret);
while (n--) {
*ptr_ret++ = *ptr1++;
}
while (n--)
{
*ptr_ret++ = *ptr1++;
}
ptr2 = VARDATA(string2);
ptr2 = VARDATA(string2);
while (m--) {
*ptr_ret++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
}
while (m--)
{
*ptr_ret++ = *ptr2;
ptr2 = ptr2 == VARDATA(string2) + VARSIZE(string2) - VARHDRSZ - 1 ? VARDATA(string2) : ++ptr2;
}
return ret;
return ret;
}
@@ -253,73 +272,84 @@ rpad(text *string1, int4 len, text *string2)
*
* Syntax:
*
* text *btrim(text *string, text *set)
* text *btrim(text *string, text *set)
*
* Purpose:
*
* Returns string with characters removed from the front and back
* up to the first character not in set.
* Returns string with characters removed from the front and back
* up to the first character not in set.
*
********************************************************************/
text *
btrim(text *string, text *set)
text *
btrim(text * string, text * set)
{
text *ret;
char *ptr, *end, *ptr2, *end2;
int m;
text *ret;
char *ptr,
*end,
*ptr2,
*end2;
int m;
if ((string == (text *)NULL) ||
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
(set == (text *)NULL) ||
((VARSIZE(set) - VARHDRSZ) <= 0))
return string;
if ((string == (text *) NULL) ||
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
(set == (text *) NULL) ||
((VARSIZE(set) - VARHDRSZ) <= 0))
return string;
ptr = VARDATA(string);
ptr2 = VARDATA(set);
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
while (m--) {
while (ptr2 <= end2) {
if (*ptr == *ptr2) {
break;
}
++ptr2;
}
if (*ptr != *ptr2) {
break;
}
ptr++;
ptr2 = VARDATA(set);
}
ptr = VARDATA(string);
ptr2 = VARDATA(set);
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
++m;
while (m--)
{
while (ptr2 <= end2)
{
if (*ptr == *ptr2)
{
break;
}
++ptr2;
}
if (*ptr != *ptr2)
{
break;
}
ptr++;
ptr2 = VARDATA(set);
}
end = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
ptr2 = VARDATA(set);
++m;
while (m--) {
while (ptr2 <= end2) {
if (*end == *ptr2) {
break;
}
++ptr2;
}
if (*end != *ptr2) {
break;
}
--end;
ptr2 = VARDATA(set);
}
end = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
ptr2 = VARDATA(set);
++m;
while (m--)
{
while (ptr2 <= end2)
{
if (*end == *ptr2)
{
break;
}
++ptr2;
}
if (*end != *ptr2)
{
break;
}
--end;
ptr2 = VARDATA(set);
}
ret = (text *)palloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
memcpy(VARDATA(ret),ptr,m);
++m;
return ret;
} /* btrim() */
ret = (text *) palloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
memcpy(VARDATA(ret), ptr, m);
return ret;
} /* btrim() */
/********************************************************************
@@ -328,54 +358,60 @@ btrim(text *string, text *set)
*
* Syntax:
*
* text *ltrim(text *string, text *set)
* text *ltrim(text *string, text *set)
*
* Purpose:
*
* Returns string with initial characters removed up to the first
* character not in set.
* Returns string with initial characters removed up to the first
* character not in set.
*
********************************************************************/
text *
ltrim(text *string, text *set)
text *
ltrim(text * string, text * set)
{
text *ret;
char *ptr, *ptr2, *end2;
int m;
text *ret;
char *ptr,
*ptr2,
*end2;
int m;
if ((string == (text *)NULL) ||
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
(set == (text *)NULL) ||
((VARSIZE(set) - VARHDRSZ) <= 0))
return string;
if ((string == (text *) NULL) ||
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
(set == (text *) NULL) ||
((VARSIZE(set) - VARHDRSZ) <= 0))
return string;
ptr = VARDATA(string);
ptr2 = VARDATA(set);
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
while (m--) {
while (ptr2 <= end2) {
if (*ptr == *ptr2) {
break;
}
++ptr2;
}
if (*ptr != *ptr2) {
break;
}
ptr++;
ptr2 = VARDATA(set);
}
ptr = VARDATA(string);
ptr2 = VARDATA(set);
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
++m;
while (m--)
{
while (ptr2 <= end2)
{
if (*ptr == *ptr2)
{
break;
}
++ptr2;
}
if (*ptr != *ptr2)
{
break;
}
ptr++;
ptr2 = VARDATA(set);
}
ret = (text *)palloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
++m;
memcpy(VARDATA(ret),ptr,m);
ret = (text *) palloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
return ret;
memcpy(VARDATA(ret), ptr, m);
return ret;
}
@@ -385,61 +421,69 @@ ltrim(text *string, text *set)
*
* Syntax:
*
* text *rtrim(text *string, text *set)
* text *rtrim(text *string, text *set)
*
* Purpose:
*
* Returns string with final characters removed after the last
* character not in set.
* Returns string with final characters removed after the last
* character not in set.
*
********************************************************************/
text *
rtrim(text *string, text *set)
text *
rtrim(text * string, text * set)
{
text *ret;
char *ptr, *ptr2, *end2, *ptr_ret;
int m;
text *ret;
char *ptr,
*ptr2,
*end2,
*ptr_ret;
int m;
if ((string == (text *)NULL) ||
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
(set == (text *)NULL) ||
((VARSIZE(set) - VARHDRSZ) <= 0))
return string;
if ((string == (text *) NULL) ||
((m = VARSIZE(string) - VARHDRSZ) <= 0) ||
(set == (text *) NULL) ||
((VARSIZE(set) - VARHDRSZ) <= 0))
return string;
ptr = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
ptr2 = VARDATA(set);
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
while (m--) {
while (ptr2 <= end2) {
if (*ptr == *ptr2) {
break;
}
++ptr2;
}
if (*ptr != *ptr2) {
break;
}
--ptr;
ptr2 = VARDATA(set);
}
ptr = VARDATA(string) + VARSIZE(string) - VARHDRSZ - 1;
ptr2 = VARDATA(set);
end2 = VARDATA(set) + VARSIZE(set) - VARHDRSZ - 1;
++m;
while (m--)
{
while (ptr2 <= end2)
{
if (*ptr == *ptr2)
{
break;
}
++ptr2;
}
if (*ptr != *ptr2)
{
break;
}
--ptr;
ptr2 = VARDATA(set);
}
ret = (text *)palloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
++m;
ret = (text *) palloc(VARHDRSZ + m);
VARSIZE(ret) = VARHDRSZ + m;
#if FALSE
memcpy(VARDATA(ret),ptr-VARSIZE(ret)+m,m);
memcpy(VARDATA(ret), ptr - VARSIZE(ret) + m, m);
#endif
ptr_ret = VARDATA(ret) + m - 1;
ptr_ret = VARDATA(ret) + m - 1;
while (m--) {
*ptr_ret-- = *ptr--;
}
while (m--)
{
*ptr_ret-- = *ptr--;
}
return ret;
return ret;
}
@@ -449,40 +493,42 @@ rtrim(text *string, text *set)
*
* Syntax:
*
* text *substr(text *string, int4 m, int4 n)
* text *substr(text *string, int4 m, int4 n)
*
* Purpose:
*
* Returns a portion of string, beginning at character m, n
* characters long. The first position of string is 1.
* Returns a portion of string, beginning at character m, n
* characters long. The first position of string is 1.
*
********************************************************************/
text *
substr(text *string, int4 m, int4 n)
text *
substr(text * string, int4 m, int4 n)
{
text *ret;
char *ptr, *ptr_ret;
int len;
text *ret;
char *ptr,
*ptr_ret;
int len;
if ((string == (text *)NULL) ||
(m <= 0) || (n <= 0) ||
((len = VARSIZE(string) - VARHDRSZ - m) <= 0))
return string;
if ((string == (text *) NULL) ||
(m <= 0) || (n <= 0) ||
((len = VARSIZE(string) - VARHDRSZ - m) <= 0))
return string;
len = len + 1 < n ? len + 1 : n;
len = len + 1 < n ? len + 1 : n;
ret = (text *)palloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ret = (text *) palloc(VARHDRSZ + len);
VARSIZE(ret) = VARHDRSZ + len;
ptr = VARDATA(string) + m - 1;
ptr_ret = VARDATA(ret);
ptr = VARDATA(string) + m - 1;
ptr_ret = VARDATA(ret);
while (len--) {
*ptr_ret++ = *ptr++;
}
while (len--)
{
*ptr_ret++ = *ptr++;
}
return ret;
return ret;
}
@@ -492,39 +538,41 @@ substr(text *string, int4 m, int4 n)
*
* Syntax:
*
* text *translate(text *string, char from, char to)
* text *translate(text *string, char from, char to)
*
* Purpose:
*
* Returns string after replacing all occurences of from with
* the corresponding character in to. TRANSLATE will not remove
* characters.
* Returns string after replacing all occurences of from with
* the corresponding character in to. TRANSLATE will not remove
* characters.
*
********************************************************************/
text *
translate(text *string, char from, char to)
text *
translate(text * string, char from, char to)
{
text *ret;
char *ptr, *ptr_ret;
int m;
text *ret;
char *ptr,
*ptr_ret;
int m;
if ((string == (text *)NULL) ||
((m = VARSIZE(string) - VARHDRSZ) <= 0))
return string;
if ((string == (text *) NULL) ||
((m = VARSIZE(string) - VARHDRSZ) <= 0))
return string;
ret = (text *)palloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ret = (text *) palloc(VARSIZE(string));
VARSIZE(ret) = VARSIZE(string);
ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
ptr = VARDATA(string);
ptr_ret = VARDATA(ret);
while (m--) {
*ptr_ret++ = *ptr == from ? to : *ptr;
ptr++;
}
while (m--)
{
*ptr_ret++ = *ptr == from ? to : *ptr;
ptr++;
}
return ret;
return ret;
}

View File

@@ -1,42 +1,42 @@
/*-------------------------------------------------------------------------
*
* regexp.c--
* regular expression handling code.
* regular expression handling code.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/regexp.c,v 1.7 1997/08/12 22:54:36 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/regexp.c,v 1.8 1997/09/07 04:50:39 momjian Exp $
*
* Alistair Crooks added the code for the regex caching
* agc - cached the regular expressions used - there's a good chance
* that we'll get a hit, so this saves a compile step for every
* attempted match. I haven't actually measured the speed improvement,
* but it `looks' a lot quicker visually when watching regression
* test output.
* Alistair Crooks added the code for the regex caching
* agc - cached the regular expressions used - there's a good chance
* that we'll get a hit, so this saves a compile step for every
* attempted match. I haven't actually measured the speed improvement,
* but it `looks' a lot quicker visually when watching regression
* test output.
*
* agc - incorporated Keith Bostic's Berkeley regex code into
* the tree for all ports. To distinguish this regex code from any that
* is existent on a platform, I've prepended the string "pg95_" to
* the functions regcomp, regerror, regexec and regfree.
* Fixed a bug that was originally a typo by me, where `i' was used
* instead of `oldest' when compiling regular expressions - benign
* results mostly, although occasionally it bit you...
* agc - incorporated Keith Bostic's Berkeley regex code into
* the tree for all ports. To distinguish this regex code from any that
* is existent on a platform, I've prepended the string "pg95_" to
* the functions regcomp, regerror, regexec and regfree.
* Fixed a bug that was originally a typo by me, where `i' was used
* instead of `oldest' when compiling regular expressions - benign
* results mostly, although occasionally it bit you...
*
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include "postgres.h" /* postgres system include file */
#include "postgres.h" /* postgres system include file */
#include <regex/regex.h>
#include "utils/builtins.h" /* where the function declarations go */
#include "utils/builtins.h" /* where the function declarations go */
#if defined(DISABLE_XOPEN_NLS)
#undef _XOPEN_SOURCE
#endif /* DISABLE_XOPEN_NLS */
#endif /* DISABLE_XOPEN_NLS */
/* this is the number of cached regular expressions held. */
#ifndef MAX_CACHED_RES
@@ -44,297 +44,329 @@
#endif
/* this structure describes a cached regular expression */
struct cached_re_str {
struct varlena *cre_text; /* pattern as a text* */
char *cre_s; /* pattern as null-terminated string */
int cre_type; /* compiled-type: extended,icase etc */
regex_t cre_re; /* the compiled regular expression */
struct cached_re_str
{
struct varlena *cre_text; /* pattern as a text* */
char *cre_s; /* pattern as null-terminated string */
int cre_type; /* compiled-type: extended,icase etc */
regex_t cre_re; /* the compiled regular expression */
unsigned long cre_lru; /* lru tag */
};
static int rec = 0; /* # of cached re's */
static struct cached_re_str rev[MAX_CACHED_RES]; /* cached re's */
static unsigned long lru; /* system lru tag */
static int rec = 0; /* # of cached re's */
static struct cached_re_str rev[MAX_CACHED_RES]; /* cached re's */
static unsigned long lru; /* system lru tag */
/* attempt to compile `re' as an re, then match it against text */
/* cflags - flag to regcomp indicates case sensitivity */
static int
RE_compile_and_execute(struct varlena *text_re, char *text, int cflags)
RE_compile_and_execute(struct varlena * text_re, char *text, int cflags)
{
int oldest;
int n;
int i;
char *re;
int regcomp_result;
int oldest;
int n;
int i;
char *re;
int regcomp_result;
re = textout(text_re);
/* find a previously compiled regular expression */
for (i = 0 ; i < rec ; i++) {
if (rev[i].cre_s) {
if (strcmp(rev[i].cre_s, re) == 0) {
if (rev[i].cre_type == cflags) {
rev[i].cre_lru = ++lru;
pfree(re);
return(pg95_regexec(&rev[i].cre_re,
text, 0,
(regmatch_t *) NULL, 0) == 0);
}
re = textout(text_re);
/* find a previously compiled regular expression */
for (i = 0; i < rec; i++)
{
if (rev[i].cre_s)
{
if (strcmp(rev[i].cre_s, re) == 0)
{
if (rev[i].cre_type == cflags)
{
rev[i].cre_lru = ++lru;
pfree(re);
return (pg95_regexec(&rev[i].cre_re,
text, 0,
(regmatch_t *) NULL, 0) == 0);
}
}
}
}
}
}
/* we didn't find it - make room in the cache for it */
if (rec == MAX_CACHED_RES) {
if (rec == MAX_CACHED_RES)
{
/* cache is full - find the oldest entry */
for (oldest = 0, i = 1 ; i < rec ; i++) {
if (rev[i].cre_lru < rev[oldest].cre_lru) {
for (oldest = 0, i = 1; i < rec; i++)
{
if (rev[i].cre_lru < rev[oldest].cre_lru)
{
oldest = i;
}
}
} else {
}
else
{
oldest = rec++;
}
/* if there was an old re, then de-allocate the space it used */
if (rev[oldest].cre_s != (char *) NULL) {
for (lru = i = 0 ; i < rec ; i++) {
if (rev[oldest].cre_s != (char *) NULL)
{
for (lru = i = 0; i < rec; i++)
{
rev[i].cre_lru =
(rev[i].cre_lru - rev[oldest].cre_lru) / 2;
if (rev[i].cre_lru > lru) {
if (rev[i].cre_lru > lru)
{
lru = rev[i].cre_lru;
}
}
}
pg95_regfree(&rev[oldest].cre_re);
/* use malloc/free for the cre_s field because the storage
has to persist across transactions */
free(rev[oldest].cre_s);
/*
* use malloc/free for the cre_s field because the storage has to
* persist across transactions
*/
free(rev[oldest].cre_s);
}
/* compile the re */
regcomp_result = pg95_regcomp(&rev[oldest].cre_re, re, cflags);
if ( regcomp_result == 0) {
if (regcomp_result == 0)
{
n = strlen(re);
/* use malloc/free for the cre_s field because the storage
has to persist across transactions */
/*
* use malloc/free for the cre_s field because the storage has to
* persist across transactions
*/
rev[oldest].cre_s = (char *) malloc(n + 1);
memmove(rev[oldest].cre_s, re, n);
rev[oldest].cre_s[n] = 0;
rev[oldest].cre_text = text_re;
rev[oldest].cre_text = text_re;
rev[oldest].cre_lru = ++lru;
rev[oldest].cre_type = cflags;
rev[oldest].cre_type = cflags;
pfree(re);
/* agc - fixed an old typo here */
return(pg95_regexec(&rev[oldest].cre_re, text, 0,
(regmatch_t *) NULL, 0) == 0);
} else {
char errMsg[1000];
/* re didn't compile */
rev[oldest].cre_s = (char *) NULL;
pg95_regerror(regcomp_result, &rev[oldest].cre_re, errMsg,
sizeof(errMsg));
elog(WARN,"regcomp failed with error %s",errMsg);
/* agc - fixed an old typo here */
return (pg95_regexec(&rev[oldest].cre_re, text, 0,
(regmatch_t *) NULL, 0) == 0);
}
else
{
char errMsg[1000];
/* re didn't compile */
rev[oldest].cre_s = (char *) NULL;
pg95_regerror(regcomp_result, &rev[oldest].cre_re, errMsg,
sizeof(errMsg));
elog(WARN, "regcomp failed with error %s", errMsg);
}
/* not reached */
return(0);
return (0);
}
/*
* interface routines called by the function manager
* interface routines called by the function manager
*/
/*
fixedlen_regexeq:
a generic fixed length regexp routine
s - the string to match against (not necessarily null-terminated)
p - the pattern
charlen - the length of the string
s - the string to match against (not necessarily null-terminated)
p - the pattern
charlen - the length of the string
*/
static bool
fixedlen_regexeq(char *s, struct varlena* p, int charlen, int cflags)
static bool
fixedlen_regexeq(char *s, struct varlena * p, int charlen, int cflags)
{
char *sterm;
int result;
if (!s || !p)
return FALSE;
/* be sure sterm is null-terminated */
sterm = (char *) palloc(charlen + 1);
strNcpy(sterm, s, charlen);
result = RE_compile_and_execute(p, sterm, cflags);
char *sterm;
int result;
pfree(sterm);
return ((bool) result);
if (!s || !p)
return FALSE;
/* be sure sterm is null-terminated */
sterm = (char *) palloc(charlen + 1);
strNcpy(sterm, s, charlen);
result = RE_compile_and_execute(p, sterm, cflags);
pfree(sterm);
return ((bool) result);
}
/*
* routines that use the regexp stuff
* routines that use the regexp stuff
*/
bool
char2regexeq(uint16 arg1, struct varlena *p)
bool
char2regexeq(uint16 arg1, struct varlena * p)
{
char *s = (char *) &arg1;
return (fixedlen_regexeq(s, p, 2, REG_EXTENDED));
char *s = (char *) &arg1;
return (fixedlen_regexeq(s, p, 2, REG_EXTENDED));
}
bool
char2regexne(uint16 arg1, struct varlena *p)
bool
char2regexne(uint16 arg1, struct varlena * p)
{
return (!char2regexeq(arg1, p));
return (!char2regexeq(arg1, p));
}
bool
char4regexeq(uint32 arg1, struct varlena *p)
bool
char4regexeq(uint32 arg1, struct varlena * p)
{
char *s = (char *) &arg1;
return (fixedlen_regexeq(s, p, 4, REG_EXTENDED));
char *s = (char *) &arg1;
return (fixedlen_regexeq(s, p, 4, REG_EXTENDED));
}
bool
char4regexne(uint32 arg1, struct varlena *p)
bool
char4regexne(uint32 arg1, struct varlena * p)
{
return (!char4regexeq(arg1, p));
return (!char4regexeq(arg1, p));
}
bool
char8regexeq(char *s, struct varlena *p)
bool
char8regexeq(char *s, struct varlena * p)
{
return (fixedlen_regexeq(s, p, 8, REG_EXTENDED));
return (fixedlen_regexeq(s, p, 8, REG_EXTENDED));
}
bool
char8regexne(char *s, struct varlena *p)
bool
char8regexne(char *s, struct varlena * p)
{
return (!char8regexeq(s, p));
return (!char8regexeq(s, p));
}
bool
char16regexeq(char *s, struct varlena *p)
bool
char16regexeq(char *s, struct varlena * p)
{
return (fixedlen_regexeq(s, p, 16, REG_EXTENDED));
return (fixedlen_regexeq(s, p, 16, REG_EXTENDED));
}
bool
char16regexne(char *s, struct varlena *p)
bool
char16regexne(char *s, struct varlena * p)
{
return (!char16regexeq(s, p));
return (!char16regexeq(s, p));
}
bool
nameregexeq(NameData *n, struct varlena *p)
bool
nameregexeq(NameData * n, struct varlena * p)
{
if (!n) return FALSE;
return (fixedlen_regexeq(n->data, p, NAMEDATALEN, REG_EXTENDED));
}
bool
nameregexne(NameData *s, struct varlena *p)
{
return (!nameregexeq(s, p));
if (!n)
return FALSE;
return (fixedlen_regexeq(n->data, p, NAMEDATALEN, REG_EXTENDED));
}
bool
textregexeq(struct varlena *s, struct varlena *p)
bool
nameregexne(NameData * s, struct varlena * p)
{
if (!s) return (FALSE);
return (fixedlen_regexeq(VARDATA(s), p, VARSIZE(s) - VARHDRSZ, REG_EXTENDED));
return (!nameregexeq(s, p));
}
bool
textregexne(struct varlena *s, struct varlena *p)
bool
textregexeq(struct varlena * s, struct varlena * p)
{
return (!textregexeq(s, p));
if (!s)
return (FALSE);
return (fixedlen_regexeq(VARDATA(s), p, VARSIZE(s) - VARHDRSZ, REG_EXTENDED));
}
bool
textregexne(struct varlena * s, struct varlena * p)
{
return (!textregexeq(s, p));
}
/*
* routines that use the regexp stuff, but ignore the case.
* for this, we use the REG_ICASE flag to pg95_regcomp
* for this, we use the REG_ICASE flag to pg95_regcomp
*/
bool
char2icregexeq(uint16 arg1, struct varlena *p)
bool
char2icregexeq(uint16 arg1, struct varlena * p)
{
char *s = (char *) &arg1;
return (fixedlen_regexeq(s, p, 2, REG_ICASE | REG_EXTENDED));
char *s = (char *) &arg1;
return (fixedlen_regexeq(s, p, 2, REG_ICASE | REG_EXTENDED));
}
bool
char2icregexne(uint16 arg1, struct varlena *p)
bool
char2icregexne(uint16 arg1, struct varlena * p)
{
return (!char2icregexeq(arg1, p));
return (!char2icregexeq(arg1, p));
}
bool
char4icregexeq(uint32 arg1, struct varlena *p)
bool
char4icregexeq(uint32 arg1, struct varlena * p)
{
char *s = (char *) &arg1;
return (fixedlen_regexeq(s, p, 4, REG_ICASE | REG_EXTENDED ));
char *s = (char *) &arg1;
return (fixedlen_regexeq(s, p, 4, REG_ICASE | REG_EXTENDED));
}
bool
char4icregexne(uint32 arg1, struct varlena *p)
bool
char4icregexne(uint32 arg1, struct varlena * p)
{
return (!char4icregexeq(arg1, p));
}
bool
char8icregexeq(char *s, struct varlena *p)
{
return (fixedlen_regexeq(s, p, 8, REG_ICASE | REG_EXTENDED));
return (!char4icregexeq(arg1, p));
}
bool
char8icregexne(char *s, struct varlena *p)
bool
char8icregexeq(char *s, struct varlena * p)
{
return (!char8icregexeq(s, p));
return (fixedlen_regexeq(s, p, 8, REG_ICASE | REG_EXTENDED));
}
bool
char16icregexeq(char *s, struct varlena *p)
bool
char8icregexne(char *s, struct varlena * p)
{
return (fixedlen_regexeq(s, p, 16, REG_ICASE | REG_EXTENDED));
return (!char8icregexeq(s, p));
}
bool
char16icregexne(char *s, struct varlena *p)
bool
char16icregexeq(char *s, struct varlena * p)
{
return (!char16icregexeq(s, p));
return (fixedlen_regexeq(s, p, 16, REG_ICASE | REG_EXTENDED));
}
bool
texticregexeq(struct varlena *s, struct varlena *p)
bool
char16icregexne(char *s, struct varlena * p)
{
if (!s) return FALSE;
return (fixedlen_regexeq(VARDATA(s), p, VARSIZE(s) - VARHDRSZ,
REG_ICASE | REG_EXTENDED));
return (!char16icregexeq(s, p));
}
bool
texticregexne(struct varlena *s, struct varlena *p)
bool
texticregexeq(struct varlena * s, struct varlena * p)
{
return (!texticregexeq(s, p));
if (!s)
return FALSE;
return (fixedlen_regexeq(VARDATA(s), p, VARSIZE(s) - VARHDRSZ,
REG_ICASE | REG_EXTENDED));
}
bool
nameicregexeq(NameData *n, struct varlena *p)
bool
texticregexne(struct varlena * s, struct varlena * p)
{
if (!n) return FALSE;
return (fixedlen_regexeq(n->data, p, NAMEDATALEN,
REG_ICASE | REG_EXTENDED));
}
bool
nameicregexne(NameData *s, struct varlena *p)
{
return (!nameicregexeq(s, p));
return (!texticregexeq(s, p));
}
bool
nameicregexeq(NameData * n, struct varlena * p)
{
if (!n)
return FALSE;
return (fixedlen_regexeq(n->data, p, NAMEDATALEN,
REG_ICASE | REG_EXTENDED));
}
bool
nameicregexne(NameData * s, struct varlena * p)
{
return (!nameicregexeq(s, p));
}

View File

@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* regproc.c--
* Functions for the built-in type "RegProcedure".
* Functions for the built-in type "RegProcedure".
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.5 1997/08/12 20:16:05 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.6 1997/09/07 04:50:41 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -19,129 +19,140 @@
#include "utils/palloc.h"
#include "catalog/catname.h"
#include "utils/builtins.h" /* where function declarations go */
#include "utils/builtins.h" /* where function declarations go */
/*****************************************************************************
* USER I/O ROUTINES *
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
/*
* regprocin - converts "proname" to proid
* regprocin - converts "proname" to proid
*
* proid of NULL signifies unknown
* proid of NULL signifies unknown
*/
int32 regprocin(char *proname)
int32
regprocin(char *proname)
{
Relation proc;
HeapScanDesc procscan;
HeapTuple proctup;
ScanKeyData key;
RegProcedure result = (Oid)0;
bool isnull;
if (proname == NULL)
return(0);
proc = heap_openr(ProcedureRelationName);
if (!RelationIsValid(proc)) {
elog(WARN, "regprocin: could not open %s",
ProcedureRelationName);
return(0);
}
ScanKeyEntryInitialize(&key,
(bits16)0,
(AttrNumber)1,
(RegProcedure)F_CHAR16EQ,
(Datum)proname);
procscan = heap_beginscan(proc, 0, NowTimeQual, 1, &key);
if (!HeapScanIsValid(procscan)) {
heap_close(proc);
elog(WARN, "regprocin: could not being scan of %s",
ProcedureRelationName);
return(0);
}
proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
switch (HeapTupleIsValid(proctup)) {
case 1:
result = (RegProcedure) heap_getattr(proctup,
InvalidBuffer,
ObjectIdAttributeNumber,
RelationGetTupleDescriptor(proc),
&isnull);
if (isnull) {
elog(FATAL, "regprocin: null procedure %s", proname);
Relation proc;
HeapScanDesc procscan;
HeapTuple proctup;
ScanKeyData key;
RegProcedure result = (Oid) 0;
bool isnull;
if (proname == NULL)
return (0);
proc = heap_openr(ProcedureRelationName);
if (!RelationIsValid(proc))
{
elog(WARN, "regprocin: could not open %s",
ProcedureRelationName);
return (0);
}
break;
case 0:
result = (RegProcedure) 0;
ScanKeyEntryInitialize(&key,
(bits16) 0,
(AttrNumber) 1,
(RegProcedure) F_CHAR16EQ,
(Datum) proname);
procscan = heap_beginscan(proc, 0, NowTimeQual, 1, &key);
if (!HeapScanIsValid(procscan))
{
heap_close(proc);
elog(WARN, "regprocin: could not being scan of %s",
ProcedureRelationName);
return (0);
}
proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
switch (HeapTupleIsValid(proctup))
{
case 1:
result = (RegProcedure) heap_getattr(proctup,
InvalidBuffer,
ObjectIdAttributeNumber,
RelationGetTupleDescriptor(proc),
&isnull);
if (isnull)
{
elog(FATAL, "regprocin: null procedure %s", proname);
}
break;
case 0:
result = (RegProcedure) 0;
#ifdef EBUG
elog(DEBUG, "regprocin: no such procedure %s", proname);
#endif /* defined(EBUG) */
}
heap_endscan(procscan);
heap_close(proc);
return((int32) result);
elog(DEBUG, "regprocin: no such procedure %s", proname);
#endif /* defined(EBUG) */
}
heap_endscan(procscan);
heap_close(proc);
return ((int32) result);
}
/*
* regprocout - converts proid to "proname"
* regprocout - converts proid to "proname"
*/
char *regprocout(RegProcedure proid)
char *
regprocout(RegProcedure proid)
{
Relation proc;
HeapScanDesc procscan;
HeapTuple proctup;
char *result;
ScanKeyData key;
result = (char *)palloc(NAMEDATALEN);
proc = heap_openr(ProcedureRelationName);
if (!RelationIsValid(proc)) {
elog(WARN, "regprocout: could not open %s",
ProcedureRelationName);
return(0);
}
ScanKeyEntryInitialize(&key,
(bits16)0,
(AttrNumber)ObjectIdAttributeNumber,
(RegProcedure)F_INT4EQ,
(Datum)proid);
procscan = heap_beginscan(proc, 0, NowTimeQual, 1, &key);
if (!HeapScanIsValid(procscan)) {
heap_close(proc);
elog(WARN, "regprocin: could not being scan of %s",
ProcedureRelationName);
return(0);
}
proctup = heap_getnext(procscan, 0, (Buffer *)NULL);
switch (HeapTupleIsValid(proctup)) {
char *s;
bool isnull;
case 1:
s = (char *) heap_getattr(proctup, InvalidBuffer, 1,
RelationGetTupleDescriptor(proc), &isnull);
if (!isnull) {
strNcpy(result, s, 16);
break;
Relation proc;
HeapScanDesc procscan;
HeapTuple proctup;
char *result;
ScanKeyData key;
result = (char *) palloc(NAMEDATALEN);
proc = heap_openr(ProcedureRelationName);
if (!RelationIsValid(proc))
{
elog(WARN, "regprocout: could not open %s",
ProcedureRelationName);
return (0);
}
elog(FATAL, "regprocout: null procedure %d", proid);
/*FALLTHROUGH*/
case 0:
result[0] = '-';
result[1] = '\0';
ScanKeyEntryInitialize(&key,
(bits16) 0,
(AttrNumber) ObjectIdAttributeNumber,
(RegProcedure) F_INT4EQ,
(Datum) proid);
procscan = heap_beginscan(proc, 0, NowTimeQual, 1, &key);
if (!HeapScanIsValid(procscan))
{
heap_close(proc);
elog(WARN, "regprocin: could not being scan of %s",
ProcedureRelationName);
return (0);
}
proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
switch (HeapTupleIsValid(proctup))
{
char *s;
bool isnull;
case 1:
s = (char *) heap_getattr(proctup, InvalidBuffer, 1,
RelationGetTupleDescriptor(proc), &isnull);
if (!isnull)
{
strNcpy(result, s, 16);
break;
}
elog(FATAL, "regprocout: null procedure %d", proid);
/* FALLTHROUGH */
case 0:
result[0] = '-';
result[1] = '\0';
#ifdef EBUG
elog(DEBUG, "regprocout: no such procedure %d", proid);
#endif /* defined(EBUG) */
}
heap_endscan(procscan);
heap_close(proc);
return(result);
elog(DEBUG, "regprocout: no such procedure %d", proid);
#endif /* defined(EBUG) */
}
heap_endscan(procscan);
heap_close(proc);
return (result);
}
/*****************************************************************************
* PUBLIC ROUTINES *
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
/* regproctooid()
@@ -149,13 +160,13 @@ char *regprocout(RegProcedure proid)
* Define RegprocToOid() as a macro in builtins.h.
* Referenced in pg_proc.h. - tgl 97/04/26
*/
Oid regproctooid(RegProcedure rp)
Oid
regproctooid(RegProcedure rp)
{
return (Oid)rp;
return (Oid) rp;
}
/* (see int.c for comparison/operation routines) */
/* ========== PRIVATE ROUTINES ========== */

File diff suppressed because it is too large Load Diff

View File

@@ -1,163 +1,173 @@
/*-------------------------------------------------------------------------
*
* sets.c--
* Functions for sets, which are defined by queries.
* Example: a set is defined as being the result of the query
* retrieve (X.all)
* Functions for sets, which are defined by queries.
* Example: a set is defined as being the result of the query
* retrieve (X.all)
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/sets.c,v 1.5 1997/08/12 22:54:38 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/sets.c,v 1.6 1997/09/07 04:50:43 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h> /* for sprintf() */
#include <stdio.h> /* for sprintf() */
#include <string.h>
#include "postgres.h"
#include "access/heapam.h"
#include "access/relscan.h"
#include "access/xact.h"
#include "catalog/pg_proc.h" /* for Form_pg_proc */
#include "utils/syscache.h" /* for PROOID */
#include "catalog/catname.h" /* for ProcedureRelationName */
#include "catalog/indexing.h" /* for Num_pg_proc_indices */
#include "catalog/pg_proc.h" /* for Form_pg_proc */
#include "utils/syscache.h" /* for PROOID */
#include "catalog/catname.h" /* for ProcedureRelationName */
#include "catalog/indexing.h" /* for Num_pg_proc_indices */
#include "storage/lmgr.h"
#include "utils/sets.h" /* for GENERICSETNAME */
#include "utils/sets.h" /* for GENERICSETNAME */
#include "tcop/dest.h"
#include "fmgr.h"
extern CommandDest whereToSendOutput; /* defined in tcop/postgres.c */
extern CommandDest whereToSendOutput; /* defined in tcop/postgres.c */
/*
* SetDefine - converts query string defining set to an oid
* SetDefine - converts query string defining set to an oid
*
* The query string is used to store the set as a function in
* pg_proc. The name of the function is then changed to use the
* OID of its tuple in pg_proc.
* The query string is used to store the set as a function in
* pg_proc. The name of the function is then changed to use the
* OID of its tuple in pg_proc.
*/
Oid
SetDefine(char *querystr, char *typename)
{
Oid setoid;
char *procname = GENERICSETNAME;
char *fileName = "-";
char realprocname[16];
HeapTuple tup, newtup = NULL;
Form_pg_proc proc;
Relation procrel;
int i;
Datum replValue[Natts_pg_proc];
char replNull[Natts_pg_proc];
char repl[Natts_pg_proc];
HeapScanDesc pg_proc_scan;
Buffer buffer;
ItemPointerData ipdata;
Oid setoid;
char *procname = GENERICSETNAME;
char *fileName = "-";
char realprocname[16];
HeapTuple tup,
newtup = NULL;
Form_pg_proc proc;
Relation procrel;
int i;
Datum replValue[Natts_pg_proc];
char replNull[Natts_pg_proc];
char repl[Natts_pg_proc];
HeapScanDesc pg_proc_scan;
Buffer buffer;
ItemPointerData ipdata;
static ScanKeyData oidKey[1] = {
{ 0, ObjectIdAttributeNumber, ObjectIdEqualRegProcedure }};
static ScanKeyData oidKey[1] = {
{0, ObjectIdAttributeNumber, ObjectIdEqualRegProcedure}};
setoid = ProcedureCreate(procname, /* changed below, after oid known */
true, /* returnsSet */
typename, /* returnTypeName */
"sql", /* languageName */
querystr, /* sourceCode */
fileName, /* fileName */
false, /* canCache */
true, /* trusted */
100, /* byte_pct */
0, /* perbyte_cpu */
0, /* percall_cpu */
100, /* outin_ratio */
NIL, /* argList */
whereToSendOutput);
/* Since we're still inside this command of the transaction, we can't
* see the results of the procedure definition unless we pretend
* we've started the next command. (Postgres's solution to the
* Halloween problem is to not allow you to see the results of your
* command until you start the next command.)
*/
CommandCounterIncrement();
tup = SearchSysCacheTuple(PROOID,
ObjectIdGetDatum(setoid),
0,0,0);
if (!HeapTupleIsValid(tup))
elog(WARN, "setin: unable to define set %s", querystr);
/* We can tell whether the set was already defined by checking
* the name. If it's GENERICSETNAME, the set is new. If it's
* "set<some oid>" it's already defined.
*/
proc = (Form_pg_proc)GETSTRUCT(tup);
if (!strcmp((char*)procname, (char*)&(proc->proname))) {
/* make the real proc name */
sprintf(realprocname, "set%u", setoid);
/* set up the attributes to be modified or kept the same */
repl[0] = 'r';
for (i = 1; i < Natts_pg_proc; i++) repl[i] = ' ';
replValue[0] = (Datum)realprocname;
for (i = 1; i < Natts_pg_proc; i++) replValue[i] = (Datum)0;
for (i = 0; i < Natts_pg_proc; i++) replNull[i] = ' ';
/* change the pg_proc tuple */
procrel = heap_openr(ProcedureRelationName);
RelationSetLockForWrite(procrel);
fmgr_info(ObjectIdEqualRegProcedure,
&oidKey[0].sk_func,
&oidKey[0].sk_nargs);
oidKey[0].sk_argument = ObjectIdGetDatum(setoid);
pg_proc_scan = heap_beginscan(procrel,
0,
SelfTimeQual,
1,
oidKey);
tup = heap_getnext(pg_proc_scan, 0, &buffer);
if (HeapTupleIsValid(tup)) {
newtup = heap_modifytuple(tup,
buffer,
procrel,
replValue,
replNull,
repl);
/* XXX may not be necessary */
ItemPointerCopy(&tup->t_ctid, &ipdata);
setheapoverride(true);
heap_replace(procrel, &ipdata, newtup);
setheapoverride(false);
setoid = newtup->t_oid;
} else
elog(WARN, "setin: could not find new set oid tuple");
heap_endscan(pg_proc_scan);
if (RelationGetRelationTupleForm(procrel)->relhasindex)
{
Relation idescs[Num_pg_proc_indices];
CatalogOpenIndices(Num_pg_proc_indices, Name_pg_proc_indices, idescs);
CatalogIndexInsert(idescs, Num_pg_proc_indices, procrel, newtup);
CatalogCloseIndices(Num_pg_proc_indices, idescs);
}
RelationUnsetLockForWrite(procrel);
heap_close(procrel);
}
return setoid;
setoid = ProcedureCreate(procname, /* changed below, after oid known */
true, /* returnsSet */
typename, /* returnTypeName */
"sql", /* languageName */
querystr, /* sourceCode */
fileName, /* fileName */
false, /* canCache */
true, /* trusted */
100, /* byte_pct */
0, /* perbyte_cpu */
0, /* percall_cpu */
100, /* outin_ratio */
NIL, /* argList */
whereToSendOutput);
/*
* Since we're still inside this command of the transaction, we can't
* see the results of the procedure definition unless we pretend we've
* started the next command. (Postgres's solution to the Halloween
* problem is to not allow you to see the results of your command
* until you start the next command.)
*/
CommandCounterIncrement();
tup = SearchSysCacheTuple(PROOID,
ObjectIdGetDatum(setoid),
0, 0, 0);
if (!HeapTupleIsValid(tup))
elog(WARN, "setin: unable to define set %s", querystr);
/*
* We can tell whether the set was already defined by checking the
* name. If it's GENERICSETNAME, the set is new. If it's "set<some
* oid>" it's already defined.
*/
proc = (Form_pg_proc) GETSTRUCT(tup);
if (!strcmp((char *) procname, (char *) &(proc->proname)))
{
/* make the real proc name */
sprintf(realprocname, "set%u", setoid);
/* set up the attributes to be modified or kept the same */
repl[0] = 'r';
for (i = 1; i < Natts_pg_proc; i++)
repl[i] = ' ';
replValue[0] = (Datum) realprocname;
for (i = 1; i < Natts_pg_proc; i++)
replValue[i] = (Datum) 0;
for (i = 0; i < Natts_pg_proc; i++)
replNull[i] = ' ';
/* change the pg_proc tuple */
procrel = heap_openr(ProcedureRelationName);
RelationSetLockForWrite(procrel);
fmgr_info(ObjectIdEqualRegProcedure,
&oidKey[0].sk_func,
&oidKey[0].sk_nargs);
oidKey[0].sk_argument = ObjectIdGetDatum(setoid);
pg_proc_scan = heap_beginscan(procrel,
0,
SelfTimeQual,
1,
oidKey);
tup = heap_getnext(pg_proc_scan, 0, &buffer);
if (HeapTupleIsValid(tup))
{
newtup = heap_modifytuple(tup,
buffer,
procrel,
replValue,
replNull,
repl);
/* XXX may not be necessary */
ItemPointerCopy(&tup->t_ctid, &ipdata);
setheapoverride(true);
heap_replace(procrel, &ipdata, newtup);
setheapoverride(false);
setoid = newtup->t_oid;
}
else
elog(WARN, "setin: could not find new set oid tuple");
heap_endscan(pg_proc_scan);
if (RelationGetRelationTupleForm(procrel)->relhasindex)
{
Relation idescs[Num_pg_proc_indices];
CatalogOpenIndices(Num_pg_proc_indices, Name_pg_proc_indices, idescs);
CatalogIndexInsert(idescs, Num_pg_proc_indices, procrel, newtup);
CatalogCloseIndices(Num_pg_proc_indices, idescs);
}
RelationUnsetLockForWrite(procrel);
heap_close(procrel);
}
return setoid;
}
/* This function is a placeholder. The parser uses the OID of this
/* This function is a placeholder. The parser uses the OID of this
* function to fill in the :funcid field of a set. This routine is
* never executed. At runtime, the OID of the actual set is substituted
* never executed. At runtime, the OID of the actual set is substituted
* into the :funcid.
*/
int
seteval(Oid funcoid)
{
return 17;
return 17;
}

View File

@@ -1,89 +1,90 @@
/*-------------------------------------------------------------------------
*
* tid.c--
* Functions for the built-in type tuple id
* Functions for the built-in type tuple id
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.2 1996/11/06 06:50:05 scrappy Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.3 1997/09/07 04:50:46 momjian Exp $
*
* NOTES
* input routine largely stolen from boxin().
* input routine largely stolen from boxin().
*
*-------------------------------------------------------------------------
*/
#include <stdio.h> /* for sprintf() */
#include <stdio.h> /* for sprintf() */
#include <string.h>
#include "postgres.h"
#include "storage/bufpage.h"
#include "utils/palloc.h"
#include "utils/builtins.h" /* where function declarations go */
#include "utils/builtins.h" /* where function declarations go */
#define LDELIM '('
#define RDELIM ')'
#define DELIM ','
#define NTIDARGS 2
#define LDELIM '('
#define RDELIM ')'
#define DELIM ','
#define NTIDARGS 2
/* ----------------------------------------------------------------
* tidin
* tidin
* ----------------------------------------------------------------
*/
ItemPointer
tidin(char *str)
{
char *p, *coord[NTIDARGS];
int i;
ItemPointer result;
BlockNumber blockNumber;
OffsetNumber offsetNumber;
if (str == NULL)
return NULL;
for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
if (*p == DELIM || (*p == LDELIM && !i))
coord[i++] = p + 1;
if (i < NTIDARGS - 1)
return NULL;
blockNumber = (BlockNumber) atoi(coord[0]);
offsetNumber = (OffsetNumber) atoi(coord[1]);
result = (ItemPointer) palloc(sizeof(ItemPointerData));
ItemPointerSet(result, blockNumber, offsetNumber);
return result;
char *p,
*coord[NTIDARGS];
int i;
ItemPointer result;
BlockNumber blockNumber;
OffsetNumber offsetNumber;
if (str == NULL)
return NULL;
for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
if (*p == DELIM || (*p == LDELIM && !i))
coord[i++] = p + 1;
if (i < NTIDARGS - 1)
return NULL;
blockNumber = (BlockNumber) atoi(coord[0]);
offsetNumber = (OffsetNumber) atoi(coord[1]);
result = (ItemPointer) palloc(sizeof(ItemPointerData));
ItemPointerSet(result, blockNumber, offsetNumber);
return result;
}
/* ----------------------------------------------------------------
* tidout
* tidout
* ----------------------------------------------------------------
*/
char *
char *
tidout(ItemPointer itemPtr)
{
BlockNumber blockNumber;
OffsetNumber offsetNumber;
BlockId blockId;
char buf[32];
char *str;
blockId = &(itemPtr->ip_blkid);
blockNumber = BlockIdGetBlockNumber(blockId);
offsetNumber = itemPtr->ip_posid;
sprintf(buf, "(%d,%d)", blockNumber, offsetNumber);
str = (char *) palloc(strlen(buf)+1);
strcpy(str, buf);
return str;
BlockNumber blockNumber;
OffsetNumber offsetNumber;
BlockId blockId;
char buf[32];
char *str;
blockId = &(itemPtr->ip_blkid);
blockNumber = BlockIdGetBlockNumber(blockId);
offsetNumber = itemPtr->ip_posid;
sprintf(buf, "(%d,%d)", blockNumber, offsetNumber);
str = (char *) palloc(strlen(buf) + 1);
strcpy(str, buf);
return str;
}

View File

@@ -16,7 +16,7 @@
static const char *
cpstr(const char *s, char *buf)
{
char in = 0;
char in = 0;
while (isspace(*s))
s++;
@@ -26,7 +26,7 @@ cpstr(const char *s, char *buf)
if (strchr("-,:/", *s))
{
buf[in] = 0;
return(s + 1);
return (s + 1);
}
if (in < 16)
@@ -36,72 +36,76 @@ cpstr(const char *s, char *buf)
buf[in] = 0;
return s;
}
#endif
/* assumes dd/mm/yyyy unless first item is month in word form */
time_t
timestamp_in(const char *timestamp_str)
{
int4 result;
int4 result;
#if FALSE
struct tm input_time;
char buf[18];
const char *p;
struct tm input_time;
char buf[18];
const char *p;
static const char *mstr[] = {
"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december"
};
memset(&input_time, 0, sizeof(input_time));
memset(&input_time, 0, sizeof(input_time));
p = cpstr(timestamp_str, buf);
if (isdigit(buf[0])) /* must be dd/mm/yyyy */
if (isdigit(buf[0])) /* must be dd/mm/yyyy */
{
input_time.tm_mday = atoi(buf);
p = cpstr(p, buf);
if (!buf[0])
elog(WARN, "timestamp_in: timestamp \"%s\" not a proper date",
timestamp_str);
timestamp_str);
if (isdigit(buf[0]))
{
input_time.tm_mon = atoi(buf) - 1;
if (input_time.tm_mon < 0 || input_time.tm_mon > 11)
elog(WARN, "timestamp_in: timestamp \"%s\" invalid month",
timestamp_str);
timestamp_str);
}
else
{
int i;
int i;
for (i = 0; i < 12; i++)
if (strncmp(mstr[i], buf, strlen(buf)) == 0)
break;
if (1 > 11)
elog(WARN, "timestamp_in: timestamp \"%s\" invalid month",
timestamp_str);
timestamp_str);
input_time.tm_mon = i;
}
}
else /* must be month/dd/yyyy */
else
/* must be month/dd/yyyy */
{
int i;
int i;
for (i = 0; i < 12; i++)
if (strncmp(mstr[i], buf, strlen(buf)) == 0)
break;
if (1 > 11)
elog(WARN, "timestamp_in: timestamp \"%s\" invalid month",
timestamp_str);
timestamp_str);
input_time.tm_mon = i;
p = cpstr(p, buf);
input_time.tm_mday = atoi(buf);
if (!input_time.tm_mday || input_time.tm_mday > 31)
elog(WARN, "timestamp_in: timestamp \"%s\" not a proper date",
timestamp_str);
}
timestamp_str);
}
p = cpstr(p, buf);
if (!buf[0] || !isdigit(buf[0]))
elog(WARN, "timestamp_in: timestamp \"%s\" not a proper date",
timestamp_str);
timestamp_str);
if ((input_time.tm_year = atoi(buf)) < 1900)
input_time.tm_year += 1900;
@@ -113,102 +117,104 @@ timestamp_in(const char *timestamp_str)
p = cpstr(p, buf);
input_time.tm_sec = atoi(buf);
/* use mktime(), but make this GMT, not local time */
result = mktime(&input_time);
/* use mktime(), but make this GMT, not local time */
result = mktime(&input_time);
#endif
result = nabstimein( (char *) timestamp_str);
result = nabstimein((char *) timestamp_str);
return result;
return result;
}
char *
char *
timestamp_out(time_t timestamp)
{
char *result;
int tz;
double fsec = 0;
struct tm tt, *tm = &tt;
char buf[MAXDATELEN+1];
char zone[MAXDATELEN+1], *tzn = zone;
char *result;
int tz;
double fsec = 0;
struct tm tt,
*tm = &tt;
char buf[MAXDATELEN + 1];
char zone[MAXDATELEN + 1],
*tzn = zone;
#if FALSE
time = localtime(&timestamp);
time = localtime(&timestamp);
sprintf(result, "%04d-%02d-%02d %02d:%02d:%02d",
time->tm_year+1900, time->tm_mon+1, time->tm_mday,
time->tm_hour, time->tm_min, time->tm_sec);
sprintf(result, "%04d-%02d-%02d %02d:%02d:%02d",
time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
time->tm_hour, time->tm_min, time->tm_sec);
#endif
abstime2tm( timestamp, &tz, tm, tzn);
EncodeDateTime( tm, fsec, &tz, &tzn, USE_ISO_DATES, buf);
abstime2tm(timestamp, &tz, tm, tzn);
EncodeDateTime(tm, fsec, &tz, &tzn, USE_ISO_DATES, buf);
result = palloc(strlen(buf)+1);
strcpy( result, buf);
return result;
result = palloc(strlen(buf) + 1);
strcpy(result, buf);
return result;
}
time_t
now(void)
{
time_t sec;
time_t sec;
time(&sec);
return(sec);
time(&sec);
return (sec);
}
bool
timestampeq(time_t t1, time_t t2)
{
return difftime(t1, t2) == 0;
return difftime(t1, t2) == 0;
}
bool
timestampne(time_t t1, time_t t2)
{
return difftime(t1, t2) != 0;
return difftime(t1, t2) != 0;
}
bool
timestamplt(time_t t1, time_t t2)
{
return difftime(t1, t2) > 0;
return difftime(t1, t2) > 0;
}
bool
timestampgt(time_t t1, time_t t2)
{
return difftime(t1, t2) < 0;
return difftime(t1, t2) < 0;
}
bool
timestample(time_t t1, time_t t2)
{
return difftime(t1, t2) >= 0;
return difftime(t1, t2) >= 0;
}
bool
timestampge(time_t t1, time_t t2)
{
return difftime(t1, t2) <= 0;
return difftime(t1, t2) <= 0;
}
DateTime *
DateTime *
timestamp_datetime(time_t timestamp)
{
DateTime *result;
DateTime *result;
double fsec = 0;
struct tm *tm;
double fsec = 0;
struct tm *tm;
if (!PointerIsValid(result = PALLOCTYPE(DateTime)))
elog(WARN,"Memory allocation failed, can't convert timestamp to datetime",NULL);
if (!PointerIsValid(result = PALLOCTYPE(DateTime)))
elog(WARN, "Memory allocation failed, can't convert timestamp to datetime", NULL);
tm = localtime((time_t *) &timestamp);
tm->tm_year += 1900;
tm->tm_mon += 1;
tm = localtime((time_t *) & timestamp);
tm->tm_year += 1900;
tm->tm_mon += 1;
if (tm2datetime(tm, fsec, NULL, result) != 0)
elog(WARN,"Unable to convert timestamp to datetime",timestamp_out(timestamp));
if (tm2datetime(tm, fsec, NULL, result) != 0)
elog(WARN, "Unable to convert timestamp to datetime", timestamp_out(timestamp));
return(result);
} /* timestamp_datetime() */
return (result);
} /* timestamp_datetime() */

View File

@@ -1,17 +1,17 @@
/*-------------------------------------------------------------------------
*
* varchar.c--
* Functions for the built-in type char() and varchar().
* Functions for the built-in type char() and varchar().
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.8 1997/08/12 20:16:07 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.9 1997/09/07 04:52:53 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h> /* for sprintf() */
#include <stdio.h> /* for sprintf() */
#include <string.h>
#include "postgres.h"
#include "utils/builtins.h"
@@ -35,488 +35,524 @@
* the length for the comparison functions. (The difference between "text"
* is that we truncate and possibly blank-pad the string at insertion time.)
*
* - ay 6/95
* - ay 6/95
*/
/*****************************************************************************
* bpchar - char() *
/*****************************************************************************
* bpchar - char() *
*****************************************************************************/
/*
* bpcharin -
* converts a string of char() type to the internal representation.
* len is the length specified in () plus 4 bytes. (XXX dummy is here
* because we pass typelem as the second argument for array_in.)
* converts a string of char() type to the internal representation.
* len is the length specified in () plus 4 bytes. (XXX dummy is here
* because we pass typelem as the second argument for array_in.)
*/
char *
char *
bpcharin(char *s, int dummy, int typlen)
{
char *result, *r;
int len = typlen - 4;
int i;
if (s == NULL)
return((char *) NULL);
char *result,
*r;
int len = typlen - 4;
int i;
if (typlen == -1) {
/*
* this is here because some functions can't supply the typlen
*/
len = strlen(s);
typlen = len + 4;
}
if (len > 4096)
elog(WARN, "bpcharin: length of char() must be less than 4096");
result = (char *) palloc(typlen);
*(int32*)result = typlen;
r = result + 4;
for(i=0; i < len; i++, r++, s++) {
*r = *s;
if (*r == '\0')
break;
}
/* blank pad the string if necessary */
for(; i < len; i++) {
*r++ = ' ';
}
return(result);
if (s == NULL)
return ((char *) NULL);
if (typlen == -1)
{
/*
* this is here because some functions can't supply the typlen
*/
len = strlen(s);
typlen = len + 4;
}
if (len > 4096)
elog(WARN, "bpcharin: length of char() must be less than 4096");
result = (char *) palloc(typlen);
*(int32 *) result = typlen;
r = result + 4;
for (i = 0; i < len; i++, r++, s++)
{
*r = *s;
if (*r == '\0')
break;
}
/* blank pad the string if necessary */
for (; i < len; i++)
{
*r++ = ' ';
}
return (result);
}
char *
char *
bpcharout(char *s)
{
char *result;
int len;
char *result;
int len;
if (s == NULL) {
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
} else {
len = *(int32*)s - 4;
result = (char *) palloc(len+1);
strNcpy(result, s+4, len); /* these are blank-padded */
}
return(result);
if (s == NULL)
{
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
}
else
{
len = *(int32 *) s - 4;
result = (char *) palloc(len + 1);
strNcpy(result, s + 4, len); /* these are blank-padded */
}
return (result);
}
/*****************************************************************************
* varchar - varchar() *
/*****************************************************************************
* varchar - varchar() *
*****************************************************************************/
/*
* vcharin -
* converts a string of varchar() type to the internal representation.
* len is the length specified in () plus 4 bytes. (XXX dummy is here
* because we pass typelem as the second argument for array_in.)
* converts a string of varchar() type to the internal representation.
* len is the length specified in () plus 4 bytes. (XXX dummy is here
* because we pass typelem as the second argument for array_in.)
*/
char *
char *
varcharin(char *s, int dummy, int typlen)
{
char *result;
int len = typlen - 4;
if (s == NULL)
return((char *) NULL);
char *result;
int len = typlen - 4;
if (typlen == -1) {
/*
* this is here because some functions can't supply the typlen
*/
len = strlen(s);
typlen = len + 4;
}
if (len > 4096)
elog(WARN, "varcharin: length of char() must be less than 4096");
result = (char *) palloc(typlen);
*(int32*)result = typlen;
strncpy(result+4, s, len);
if (s == NULL)
return ((char *) NULL);
return(result);
if (typlen == -1)
{
/*
* this is here because some functions can't supply the typlen
*/
len = strlen(s);
typlen = len + 4;
}
if (len > 4096)
elog(WARN, "varcharin: length of char() must be less than 4096");
result = (char *) palloc(typlen);
*(int32 *) result = typlen;
strncpy(result + 4, s, len);
return (result);
}
char *
char *
varcharout(char *s)
{
char *result;
int len;
char *result;
int len;
if (s == NULL) {
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
} else {
len = *(int32*)s - 4;
result = (char *) palloc(len+1);
strNcpy(result, s+4, len);
}
return(result);
if (s == NULL)
{
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
}
else
{
len = *(int32 *) s - 4;
result = (char *) palloc(len + 1);
strNcpy(result, s + 4, len);
}
return (result);
}
/*****************************************************************************
* Comparison Functions used for bpchar
* Comparison Functions used for bpchar
*****************************************************************************/
static int
bcTruelen(char *arg)
{
char *s = arg + 4;
int i;
int len;
char *s = arg + 4;
int i;
int len;
len = *(int32*)arg - 4;
for(i=len-1; i >= 0; i--) {
if (s[i] != ' ')
break;
}
return (i+1);
len = *(int32 *) arg - 4;
for (i = len - 1; i >= 0; i--)
{
if (s[i] != ' ')
break;
}
return (i + 1);
}
bool
bpchareq(char *arg1, char *arg2)
{
int len1, len2;
int len1,
len2;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
if (len1!=len2)
return 0;
return(strncmp(arg1+4, arg2+4, len1) == 0);
if (len1 != len2)
return 0;
return (strncmp(arg1 + 4, arg2 + 4, len1) == 0);
}
bool
bpcharne(char *arg1, char *arg2)
{
int len1, len2;
int len1,
len2;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
if (len1!=len2)
return 1;
if (len1 != len2)
return 1;
return(strncmp(arg1+4, arg2+4, len1) != 0);
return (strncmp(arg1 + 4, arg2 + 4, len1) != 0);
}
bool
bpcharlt(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
int len1,
len2;
int cmp;
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if (cmp == 0)
return (len1<len2);
else
return (cmp < 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if (cmp == 0)
return (len1 < len2);
else
return (cmp < 0);
}
bool
bpcharle(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
int len1,
len2;
int cmp;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if (0 == cmp)
return (bool)(len1 <= len2 ? 1 : 0);
else
return (bool)(cmp <= 0);
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if (0 == cmp)
return (bool) (len1 <= len2 ? 1 : 0);
else
return (bool) (cmp <= 0);
}
bool
bpchargt(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
int len1,
len2;
int cmp;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if (cmp == 0)
return (len1 > len2);
else
return (cmp > 0);
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if (cmp == 0)
return (len1 > len2);
else
return (cmp > 0);
}
bool
bpcharge(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
int len1,
len2;
int cmp;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if (0 == cmp)
return (bool)(len1 >= len2 ? 1 : 0);
else
return (bool)(cmp >= 0);
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if (0 == cmp)
return (bool) (len1 >= len2 ? 1 : 0);
else
return (bool) (cmp >= 0);
}
int32
bpcharcmp(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
int len1,
len2;
int cmp;
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
len1 = bcTruelen(arg1);
len2 = bcTruelen(arg2);
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if ((0 == cmp) && (len1 != len2))
return (int32)(len1 < len2 ? -1 : 1);
else
return cmp;
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if ((0 == cmp) && (len1 != len2))
return (int32) (len1 < len2 ? -1 : 1);
else
return cmp;
}
/*****************************************************************************
* Comparison Functions used for varchar
* Comparison Functions used for varchar
*****************************************************************************/
static int
vcTruelen(char *arg)
{
char *s = arg + 4;
int i;
int len;
char *s = arg + 4;
int i;
int len;
len = *(int32*)arg - 4;
for(i=0; i < len; i++) {
if (*s++ == '\0')
break;
}
return i;
len = *(int32 *) arg - 4;
for (i = 0; i < len; i++)
{
if (*s++ == '\0')
break;
}
return i;
}
bool
varchareq(char *arg1, char *arg2)
{
int len1, len2;
int len1,
len2;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
if (len1!=len2)
return 0;
return(strncmp(arg1+4, arg2+4, len1) == 0);
if (len1 != len2)
return 0;
return (strncmp(arg1 + 4, arg2 + 4, len1) == 0);
}
bool
varcharne(char *arg1, char *arg2)
{
int len1, len2;
int len1,
len2;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
if (len1!=len2)
return 1;
if (len1 != len2)
return 1;
return(strncmp(arg1+4, arg2+4, len1) != 0);
return (strncmp(arg1 + 4, arg2 + 4, len1) != 0);
}
bool
varcharlt(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
int len1,
len2;
int cmp;
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if (cmp == 0)
return (len1<len2);
else
return (cmp < 0);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if (cmp == 0)
return (len1 < len2);
else
return (cmp < 0);
}
bool
varcharle(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
int len1,
len2;
int cmp;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if (0 == cmp)
return (bool)( len1 <= len2 ? 1 : 0);
else
return (bool)(cmp <= 0);
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if (0 == cmp)
return (bool) (len1 <= len2 ? 1 : 0);
else
return (bool) (cmp <= 0);
}
bool
varchargt(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
int len1,
len2;
int cmp;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if (cmp == 0)
return (len1 > len2);
else
return (cmp > 0);
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if (cmp == 0)
return (len1 > len2);
else
return (cmp > 0);
}
bool
varcharge(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
int len1,
len2;
int cmp;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
cmp = strncmp(arg1+4, arg2+4, Min(len1,len2));
if (0 == cmp)
return (bool)(len1 >= len2 ? 1 : 0);
else
return (bool)(cmp >= 0);
cmp = strncmp(arg1 + 4, arg2 + 4, Min(len1, len2));
if (0 == cmp)
return (bool) (len1 >= len2 ? 1 : 0);
else
return (bool) (cmp >= 0);
}
int32
varcharcmp(char *arg1, char *arg2)
{
int len1, len2;
int cmp;
int len1,
len2;
int cmp;
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
cmp = (strncmp(arg1+4, arg2+4, Min(len1,len2)));
if ((0 == cmp) && (len1 != len2))
return (int32)(len1 < len2 ? -1 : 1);
else
return (int32)(cmp);
len1 = vcTruelen(arg1);
len2 = vcTruelen(arg2);
cmp = (strncmp(arg1 + 4, arg2 + 4, Min(len1, len2)));
if ((0 == cmp) && (len1 != len2))
return (int32) (len1 < len2 ? -1 : 1);
else
return (int32) (cmp);
}
/*****************************************************************************
* Hash functions (modified from hashtext in access/hash/hashfunc.c)
*****************************************************************************/
uint32 hashbpchar(struct varlena *key)
uint32
hashbpchar(struct varlena * key)
{
int keylen;
char *keydata;
uint32 n;
int loop;
int keylen;
char *keydata;
uint32 n;
int loop;
keydata = VARDATA(key);
keylen = bcTruelen((char*)key);
keydata = VARDATA(key);
keylen = bcTruelen((char *) key);
#define HASHC n = *keydata++ + 65599 * n
#define HASHC n = *keydata++ + 65599 * n
n = 0;
if (keylen > 0) {
loop = (keylen + 8 - 1) >> 3;
switch (keylen & (8 - 1)) {
case 0:
do { /* All fall throughs */
HASHC;
case 7:
HASHC;
case 6:
HASHC;
case 5:
HASHC;
case 4:
HASHC;
case 3:
HASHC;
case 2:
HASHC;
case 1:
HASHC;
} while (--loop);
n = 0;
if (keylen > 0)
{
loop = (keylen + 8 - 1) >> 3;
switch (keylen & (8 - 1))
{
case 0:
do
{ /* All fall throughs */
HASHC;
case 7:
HASHC;
case 6:
HASHC;
case 5:
HASHC;
case 4:
HASHC;
case 3:
HASHC;
case 2:
HASHC;
case 1:
HASHC;
} while (--loop);
}
}
}
return (n);
}
return (n);
}
uint32 hashvarchar(struct varlena *key)
uint32
hashvarchar(struct varlena * key)
{
int keylen;
char *keydata;
uint32 n;
int loop;
int keylen;
char *keydata;
uint32 n;
int loop;
keydata = VARDATA(key);
keylen = vcTruelen((char*)key);
keydata = VARDATA(key);
keylen = vcTruelen((char *) key);
#define HASHC n = *keydata++ + 65599 * n
#define HASHC n = *keydata++ + 65599 * n
n = 0;
if (keylen > 0) {
loop = (keylen + 8 - 1) >> 3;
switch (keylen & (8 - 1)) {
case 0:
do { /* All fall throughs */
HASHC;
case 7:
HASHC;
case 6:
HASHC;
case 5:
HASHC;
case 4:
HASHC;
case 3:
HASHC;
case 2:
HASHC;
case 1:
HASHC;
} while (--loop);
n = 0;
if (keylen > 0)
{
loop = (keylen + 8 - 1) >> 3;
switch (keylen & (8 - 1))
{
case 0:
do
{ /* All fall throughs */
HASHC;
case 7:
HASHC;
case 6:
HASHC;
case 5:
HASHC;
case 4:
HASHC;
case 3:
HASHC;
case 2:
HASHC;
case 1:
HASHC;
} while (--loop);
}
}
}
return (n);
}
return (n);
}

View File

@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* varlena.c--
* Functions for the variable-length built-in types.
* Functions for the variable-length built-in types.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.18 1997/08/19 21:34:54 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.19 1997/09/07 04:52:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,64 +16,65 @@
#include "postgres.h"
#include "utils/palloc.h"
#include "utils/builtins.h" /* where function declarations go */
#include "utils/builtins.h" /* where function declarations go */
/*****************************************************************************
* USER I/O ROUTINES *
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
#define VAL(CH) ((CH) - '0')
#define DIG(VAL) ((VAL) + '0')
#define VAL(CH) ((CH) - '0')
#define DIG(VAL) ((VAL) + '0')
/*
* byteain - converts from printable representation of byte array
* byteain - converts from printable representation of byte array
*
* Non-printable characters must be passed as '\nnn' (octal) and are
* converted to internal form. '\' must be passed as '\\'.
* elog(WARN, ...) if bad form.
* Non-printable characters must be passed as '\nnn' (octal) and are
* converted to internal form. '\' must be passed as '\\'.
* elog(WARN, ...) if bad form.
*
* BUGS:
* The input is scaned twice.
* The error checking of input is minimal.
* BUGS:
* The input is scaned twice.
* The error checking of input is minimal.
*/
struct varlena *
byteain(char *inputText)
{
register char *tp;
register char *rp;
register int byte;
struct varlena *result;
if (inputText == NULL)
elog(WARN, "Bad input string for type bytea");
for (byte = 0, tp = inputText; *tp != '\0'; byte++)
if (*tp++ == '\\')
{
if (*tp == '\\')
tp++;
else if (!isdigit(*tp++) ||
!isdigit(*tp++) ||
!isdigit(*tp++))
elog(WARN, "Bad input string for type bytea");
}
tp = inputText;
byte += sizeof(int32); /* varlena? */
result = (struct varlena *) palloc(byte);
result->vl_len = byte; /* varlena? */
rp = result->vl_dat;
while (*tp != '\0')
if (*tp != '\\' || *++tp == '\\')
*rp++ = *tp++;
else {
byte = VAL(*tp++);
byte <<= 3;
byte += VAL(*tp++);
byte <<= 3;
*rp++ = byte + VAL(*tp++);
}
return(result);
register char *tp;
register char *rp;
register int byte;
struct varlena *result;
if (inputText == NULL)
elog(WARN, "Bad input string for type bytea");
for (byte = 0, tp = inputText; *tp != '\0'; byte++)
if (*tp++ == '\\')
{
if (*tp == '\\')
tp++;
else if (!isdigit(*tp++) ||
!isdigit(*tp++) ||
!isdigit(*tp++))
elog(WARN, "Bad input string for type bytea");
}
tp = inputText;
byte += sizeof(int32); /* varlena? */
result = (struct varlena *) palloc(byte);
result->vl_len = byte; /* varlena? */
rp = result->vl_dat;
while (*tp != '\0')
if (*tp != '\\' || *++tp == '\\')
*rp++ = *tp++;
else
{
byte = VAL(*tp++);
byte <<= 3;
byte += VAL(*tp++);
byte <<= 3;
*rp++ = byte + VAL(*tp++);
}
return (result);
}
/*
@@ -85,114 +86,120 @@ byteain(char *inputText)
struct varlena *
shove_bytes(unsigned char *stuff, int len)
{
struct varlena *result;
result = (struct varlena *) palloc(len + sizeof(int32));
result->vl_len = len;
memmove(result->vl_dat,
stuff + sizeof(int32),
len - sizeof(int32));
return(result);
struct varlena *result;
result = (struct varlena *) palloc(len + sizeof(int32));
result->vl_len = len;
memmove(result->vl_dat,
stuff + sizeof(int32),
len - sizeof(int32));
return (result);
}
#endif
/*
* byteaout - converts to printable representation of byte array
* byteaout - converts to printable representation of byte array
*
* Non-printable characters are inserted as '\nnn' (octal) and '\' as
* '\\'.
* Non-printable characters are inserted as '\nnn' (octal) and '\' as
* '\\'.
*
* NULL vlena should be an error--returning string with NULL for now.
* NULL vlena should be an error--returning string with NULL for now.
*/
char *
byteaout(struct varlena *vlena)
char *
byteaout(struct varlena * vlena)
{
register char *vp;
register char *rp;
register int val; /* holds unprintable chars */
int i;
int len;
static char *result;
if (vlena == NULL) {
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return(result);
}
vp = vlena->vl_dat;
len = 1; /* empty string has 1 char */
for (i = vlena->vl_len - sizeof(int32); i != 0; i--, vp++) /* varlena? */
if (*vp == '\\')
len += 2;
else if (isascii(*vp) && isprint(*vp))
len++;
else
len += 4;
rp = result = (char *) palloc(len);
vp = vlena->vl_dat;
for (i = vlena->vl_len - sizeof(int32); i != 0; i--) /* varlena? */
if (*vp == '\\') {
vp++;
*rp++ = '\\';
*rp++ = '\\';
} else if (isascii(*vp) && isprint(*vp))
*rp++ = *vp++;
else {
val = *vp++;
*rp = '\\';
rp += 3;
*rp-- = DIG(val & 07);
val >>= 3;
*rp-- = DIG(val & 07);
val >>= 3;
*rp = DIG(val & 03);
rp += 3;
register char *vp;
register char *rp;
register int val; /* holds unprintable chars */
int i;
int len;
static char *result;
if (vlena == NULL)
{
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return (result);
}
*rp = '\0';
return(result);
vp = vlena->vl_dat;
len = 1; /* empty string has 1 char */
for (i = vlena->vl_len - sizeof(int32); i != 0; i--, vp++) /* varlena? */
if (*vp == '\\')
len += 2;
else if (isascii(*vp) && isprint(*vp))
len++;
else
len += 4;
rp = result = (char *) palloc(len);
vp = vlena->vl_dat;
for (i = vlena->vl_len - sizeof(int32); i != 0; i--) /* varlena? */
if (*vp == '\\')
{
vp++;
*rp++ = '\\';
*rp++ = '\\';
}
else if (isascii(*vp) && isprint(*vp))
*rp++ = *vp++;
else
{
val = *vp++;
*rp = '\\';
rp += 3;
*rp-- = DIG(val & 07);
val >>= 3;
*rp-- = DIG(val & 07);
val >>= 3;
*rp = DIG(val & 03);
rp += 3;
}
*rp = '\0';
return (result);
}
/*
* textin - converts "..." to internal representation
* textin - converts "..." to internal representation
*/
struct varlena *
textin(char *inputText)
{
struct varlena *result;
int len;
if (inputText == NULL)
return(NULL);
len = strlen(inputText) + VARHDRSZ;
result = (struct varlena *) palloc(len);
VARSIZE(result) = len;
memmove(VARDATA(result), inputText, len - VARHDRSZ);
return(result);
struct varlena *result;
int len;
if (inputText == NULL)
return (NULL);
len = strlen(inputText) + VARHDRSZ;
result = (struct varlena *) palloc(len);
VARSIZE(result) = len;
memmove(VARDATA(result), inputText, len - VARHDRSZ);
return (result);
}
/*
* textout - converts internal representation to "..."
* textout - converts internal representation to "..."
*/
char *
textout(struct varlena *vlena)
char *
textout(struct varlena * vlena)
{
int len;
char *result;
if (vlena == NULL) {
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return(result);
}
len = VARSIZE(vlena) - VARHDRSZ;
result = (char *) palloc(len + 1);
memmove(result, VARDATA(vlena), len);
result[len] = '\0';
return(result);
int len;
char *result;
if (vlena == NULL)
{
result = (char *) palloc(2);
result[0] = '-';
result[1] = '\0';
return (result);
}
len = VARSIZE(vlena) - VARHDRSZ;
result = (char *) palloc(len + 1);
memmove(result, VARDATA(vlena), len);
result[len] = '\0';
return (result);
}
@@ -200,25 +207,28 @@ textout(struct varlena *vlena)
/*
* textlen -
* returns the actual length of a text* (which may be less than
* the VARSIZE of the text*)
* returns the actual length of a text* (which may be less than
* the VARSIZE of the text*)
*/
#ifdef NOT_USED
int textlen (text* t)
int
textlen(text * t)
{
int i = 0;
int max = VARSIZE(t) - VARHDRSZ;
char *ptr = VARDATA(t);
while (i < max && *ptr++)
i++;
return i;
int i = 0;
int max = VARSIZE(t) - VARHDRSZ;
char *ptr = VARDATA(t);
while (i < max && *ptr++)
i++;
return i;
}
#endif
/*
* textcat -
* takes two text* and returns a text* that is the concatentation of
* the two.
* takes two text* and returns a text* that is the concatentation of
* the two.
*
* Rewritten by Sapa, sapa@hq.icb.chel.su. 8-Jul-96.
* Updated by Thomas, Thomas.Lockhart@jpl.nasa.gov 1997-07-10.
@@ -228,222 +238,241 @@ int textlen (text* t)
* Is this OK?
*/
text*
textcat(text* t1, text* t2)
text *
textcat(text * t1, text * t2)
{
int len1, len2, len;
char *ptr;
text* result;
int len1,
len2,
len;
char *ptr;
text *result;
if (!PointerIsValid(t1) && !PointerIsValid(t2))
return(NULL);
if (!PointerIsValid(t1) && !PointerIsValid(t2))
return (NULL);
len1 = (PointerIsValid(t1)? (VARSIZE(t1) - VARHDRSZ): 0);
if (len1 < 0) len1 = 0;
len2 = (PointerIsValid(t2)? (VARSIZE(t2) - VARHDRSZ): 0);
if (len2 < 0) len2 = 0;
len1 = (PointerIsValid(t1) ? (VARSIZE(t1) - VARHDRSZ) : 0);
if (len1 < 0)
len1 = 0;
len2 = (PointerIsValid(t2) ? (VARSIZE(t2) - VARHDRSZ) : 0);
if (len2 < 0)
len2 = 0;
result = PALLOC(len = len1 + len2 + VARHDRSZ);
result = PALLOC(len = len1 + len2 + VARHDRSZ);
/* Fill data field of result string... */
ptr = VARDATA(result);
if (PointerIsValid(t1)) memcpy(ptr, VARDATA(t1), len1);
if (PointerIsValid(t2)) memcpy(ptr + len1, VARDATA(t2), len2);
/* Fill data field of result string... */
ptr = VARDATA(result);
if (PointerIsValid(t1))
memcpy(ptr, VARDATA(t1), len1);
if (PointerIsValid(t2))
memcpy(ptr + len1, VARDATA(t2), len2);
/* Set size of result string... */
VARSIZE(result) = len;
/* Set size of result string... */
VARSIZE(result) = len;
return(result);
} /* textcat() */
return (result);
} /* textcat() */
/*
* textpos -
* Return the position of the specified substring.
* Implements the SQL92 POSITION() function.
* Ref: A Guide To The SQL Standard, Date & Darwen, 1997
* Return the position of the specified substring.
* Implements the SQL92 POSITION() function.
* Ref: A Guide To The SQL Standard, Date & Darwen, 1997
* - thomas 1997-07-27
*/
int32
textpos(text* t1, text* t2)
textpos(text * t1, text * t2)
{
int pos;
int px, p;
int len1, len2;
char *p1, *p2;
int pos;
int px,
p;
int len1,
len2;
char *p1,
*p2;
if (!PointerIsValid(t1) || !PointerIsValid(t2))
return(0);
if (!PointerIsValid(t1) || !PointerIsValid(t2))
return (0);
if (VARSIZE(t2) <= 0)
return(1);
if (VARSIZE(t2) <= 0)
return (1);
len1 = (VARSIZE(t1) - VARHDRSZ);
len2 = (VARSIZE(t2) - VARHDRSZ);
p1 = VARDATA(t1);
p2 = VARDATA(t2);
pos = 0;
px = (len1 - len2);
for (p = 0; p <= px; p++) {
if ((*p2 == *p1) && (strncmp(p1, p2, len2) == 0)) {
pos = p + 1;
break;
len1 = (VARSIZE(t1) - VARHDRSZ);
len2 = (VARSIZE(t2) - VARHDRSZ);
p1 = VARDATA(t1);
p2 = VARDATA(t2);
pos = 0;
px = (len1 - len2);
for (p = 0; p <= px; p++)
{
if ((*p2 == *p1) && (strncmp(p1, p2, len2) == 0))
{
pos = p + 1;
break;
};
p1++;
};
p1++;
};
return(pos);
} /* textpos() */
return (pos);
} /* textpos() */
/*
* texteq - returns 1 iff arguments are equal
* textne - returns 1 iff arguments are not equal
* texteq - returns 1 iff arguments are equal
* textne - returns 1 iff arguments are not equal
*/
bool
texteq(struct varlena *arg1, struct varlena *arg2)
texteq(struct varlena * arg1, struct varlena * arg2)
{
register int len;
register char *a1p, *a2p;
if (arg1 == NULL || arg2 == NULL)
return((bool) NULL);
if ((len = arg1->vl_len) != arg2->vl_len)
return((bool) 0);
a1p = arg1->vl_dat;
a2p = arg2->vl_dat;
/*
* Varlenas are stored as the total size (data + size variable)
* followed by the data.
* Use VARHDRSZ instead of explicit sizeof() - thomas 1997-07-10
*/
len -= VARHDRSZ;
while (len-- != 0)
if (*a1p++ != *a2p++)
return((bool) 0);
return((bool) 1);
} /* texteq() */
register int len;
register char *a1p,
*a2p;
if (arg1 == NULL || arg2 == NULL)
return ((bool) NULL);
if ((len = arg1->vl_len) != arg2->vl_len)
return ((bool) 0);
a1p = arg1->vl_dat;
a2p = arg2->vl_dat;
/*
* Varlenas are stored as the total size (data + size variable)
* followed by the data. Use VARHDRSZ instead of explicit sizeof() -
* thomas 1997-07-10
*/
len -= VARHDRSZ;
while (len-- != 0)
if (*a1p++ != *a2p++)
return ((bool) 0);
return ((bool) 1);
} /* texteq() */
bool
textne(struct varlena *arg1, struct varlena *arg2)
textne(struct varlena * arg1, struct varlena * arg2)
{
return((bool) !texteq(arg1, arg2));
return ((bool) ! texteq(arg1, arg2));
}
/* text_lt()
* Comparison function for text strings.
* Includes locale support, but must copy strings to temporary memory
* to allow null-termination for inputs to strcoll().
* to allow null-termination for inputs to strcoll().
* XXX HACK code for textlen() indicates that there can be embedded nulls
* but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
* but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
*/
bool
text_lt(struct varlena *arg1, struct varlena *arg2)
text_lt(struct varlena * arg1, struct varlena * arg2)
{
bool result;
bool result;
#ifdef USE_LOCALE
int cval;
int cval;
#endif
int len;
unsigned char *a1p, *a2p;
if (arg1 == NULL || arg2 == NULL)
return((bool) FALSE);
len = (((VARSIZE(arg1) <= VARSIZE(arg2))? VARSIZE(arg1): VARSIZE(arg2))-VARHDRSZ);
int len;
unsigned char *a1p,
*a2p;
if (arg1 == NULL || arg2 == NULL)
return ((bool) FALSE);
len = (((VARSIZE(arg1) <= VARSIZE(arg2)) ? VARSIZE(arg1) : VARSIZE(arg2)) - VARHDRSZ);
#ifdef USE_LOCALE
a1p = (unsigned char *) palloc (len+1);
a2p = (unsigned char *) palloc (len+1);
a1p = (unsigned char *) palloc(len + 1);
a2p = (unsigned char *) palloc(len + 1);
memcpy(a1p, VARDATA(arg1), len);
*(a1p+len) = '\0';
memcpy(a2p, VARDATA(arg2), len);
*(a2p+len) = '\0';
memcpy(a1p, VARDATA(arg1), len);
*(a1p + len) = '\0';
memcpy(a2p, VARDATA(arg2), len);
*(a2p + len) = '\0';
cval = strcoll(a1p,a2p);
result = ((cval < 0) || ((cval == 0) && (VARSIZE(arg1) < VARSIZE(arg2))));
cval = strcoll(a1p, a2p);
result = ((cval < 0) || ((cval == 0) && (VARSIZE(arg1) < VARSIZE(arg2))));
pfree (a1p);
pfree (a2p);
pfree(a1p);
pfree(a2p);
#else
a1p = (unsigned char *)VARDATA(arg1);
a2p = (unsigned char *)VARDATA(arg2);
while (len != 0 && *a1p == *a2p) {
a1p++;
a2p++;
len--;
};
a1p = (unsigned char *) VARDATA(arg1);
a2p = (unsigned char *) VARDATA(arg2);
result = (len? (*a1p < *a2p): (VARSIZE(arg1) < VARSIZE(arg2)));
while (len != 0 && *a1p == *a2p)
{
a1p++;
a2p++;
len--;
};
result = (len ? (*a1p < *a2p) : (VARSIZE(arg1) < VARSIZE(arg2)));
#endif
return(result);
} /* text_lt() */
return (result);
} /* text_lt() */
/* text_le()
* Comparison function for text strings.
* Includes locale support, but must copy strings to temporary memory
* to allow null-termination for inputs to strcoll().
* to allow null-termination for inputs to strcoll().
* XXX HACK code for textlen() indicates that there can be embedded nulls
* but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
* but it appears that most routines (incl. this one) assume not! - tgl 97/04/07
*/
bool
text_le(struct varlena *arg1, struct varlena *arg2)
text_le(struct varlena * arg1, struct varlena * arg2)
{
bool result;
bool result;
#ifdef USE_LOCALE
int cval;
int cval;
#endif
int len;
unsigned char *a1p, *a2p;
if (arg1 == NULL || arg2 == NULL)
return((bool) 0);
len = (((VARSIZE(arg1) <= VARSIZE(arg2))? VARSIZE(arg1): VARSIZE(arg2))-VARHDRSZ);
int len;
unsigned char *a1p,
*a2p;
if (arg1 == NULL || arg2 == NULL)
return ((bool) 0);
len = (((VARSIZE(arg1) <= VARSIZE(arg2)) ? VARSIZE(arg1) : VARSIZE(arg2)) - VARHDRSZ);
#ifdef USE_LOCALE
a1p = (unsigned char *) palloc (len+1);
a2p = (unsigned char *) palloc (len+1);
a1p = (unsigned char *) palloc(len + 1);
a2p = (unsigned char *) palloc(len + 1);
memcpy(a1p, VARDATA(arg1), len);
*(a1p+len) = '\0';
memcpy(a2p, VARDATA(arg2), len);
*(a2p+len) = '\0';
memcpy(a1p, VARDATA(arg1), len);
*(a1p + len) = '\0';
memcpy(a2p, VARDATA(arg2), len);
*(a2p + len) = '\0';
cval = strcoll(a1p,a2p);
result = ((cval < 0) || ((cval == 0) && (VARSIZE(arg1) <= VARSIZE(arg2))));
cval = strcoll(a1p, a2p);
result = ((cval < 0) || ((cval == 0) && (VARSIZE(arg1) <= VARSIZE(arg2))));
pfree (a1p);
pfree (a2p);
pfree(a1p);
pfree(a2p);
#else
a1p = (unsigned char *)VARDATA(arg1);
a2p = (unsigned char *)VARDATA(arg2);
while (len != 0 && *a1p == *a2p) {
a1p++;
a2p++;
len--;
};
a1p = (unsigned char *) VARDATA(arg1);
a2p = (unsigned char *) VARDATA(arg2);
result = (len? (*a1p <= *a2p): (VARSIZE(arg1) <= VARSIZE(arg2)));
while (len != 0 && *a1p == *a2p)
{
a1p++;
a2p++;
len--;
};
result = (len ? (*a1p <= *a2p) : (VARSIZE(arg1) <= VARSIZE(arg2)));
#endif
return(result);
} /* text_le() */
return (result);
} /* text_le() */
bool
text_gt(struct varlena *arg1, struct varlena *arg2)
text_gt(struct varlena * arg1, struct varlena * arg2)
{
return ((bool) !text_le(arg1, arg2));
return ((bool) ! text_le(arg1, arg2));
}
bool
text_ge(struct varlena *arg1, struct varlena *arg2)
text_ge(struct varlena * arg1, struct varlena * arg2)
{
return ((bool) !text_lt(arg1, arg2));
return ((bool) ! text_lt(arg1, arg2));
}
/*-------------------------------------------------------------
@@ -453,13 +482,13 @@ text_ge(struct varlena *arg1, struct varlena *arg2)
*-------------------------------------------------------------
*/
int32
byteaGetSize(struct varlena *v)
byteaGetSize(struct varlena * v)
{
register int len;
len = v->vl_len - sizeof(v->vl_len);
return(len);
register int len;
len = v->vl_len - sizeof(v->vl_len);
return (len);
}
/*-------------------------------------------------------------
@@ -471,21 +500,22 @@ byteaGetSize(struct varlena *v)
*-------------------------------------------------------------
*/
int32
byteaGetByte(struct varlena *v, int32 n)
byteaGetByte(struct varlena * v, int32 n)
{
int len;
int byte;
len = byteaGetSize(v);
if (n>=len) {
elog(WARN, "byteaGetByte: index (=%d) out of range [0..%d]",
n,len-1);
}
byte = (unsigned char) (v->vl_dat[n]);
return((int32) byte);
int len;
int byte;
len = byteaGetSize(v);
if (n >= len)
{
elog(WARN, "byteaGetByte: index (=%d) out of range [0..%d]",
n, len - 1);
}
byte = (unsigned char) (v->vl_dat[n]);
return ((int32) byte);
}
/*-------------------------------------------------------------
@@ -498,22 +528,27 @@ byteaGetByte(struct varlena *v, int32 n)
*-------------------------------------------------------------
*/
int32
byteaGetBit(struct varlena *v, int32 n)
byteaGetBit(struct varlena * v, int32 n)
{
int byteNo, bitNo;
int byte;
byteNo = n/8;
bitNo = n%8;
byte = byteaGetByte(v, byteNo);
if (byte & (1<<bitNo)) {
return((int32)1);
} else {
return((int32)0);
}
int byteNo,
bitNo;
int byte;
byteNo = n / 8;
bitNo = n % 8;
byte = byteaGetByte(v, byteNo);
if (byte & (1 << bitNo))
{
return ((int32) 1);
}
else
{
return ((int32) 0);
}
}
/*-------------------------------------------------------------
* byteaSetByte
*
@@ -523,35 +558,37 @@ byteaGetBit(struct varlena *v, int32 n)
*-------------------------------------------------------------
*/
struct varlena *
byteaSetByte(struct varlena *v, int32 n, int32 newByte)
byteaSetByte(struct varlena * v, int32 n, int32 newByte)
{
int len;
struct varlena *res;
len = byteaGetSize(v);
if (n>=len) {
elog(WARN,
"byteaSetByte: index (=%d) out of range [0..%d]",
n, len-1);
}
/*
* Make a copy of the original varlena.
*/
res = (struct varlena *) palloc(VARSIZE(v));
if (res==NULL) {
elog(WARN, "byteaSetByte: Out of memory (%d bytes requested)",
VARSIZE(v));
}
memmove((char *)res, (char *)v, VARSIZE(v));
/*
* Now set the byte.
*/
res->vl_dat[n] = newByte;
return(res);
int len;
struct varlena *res;
len = byteaGetSize(v);
if (n >= len)
{
elog(WARN,
"byteaSetByte: index (=%d) out of range [0..%d]",
n, len - 1);
}
/*
* Make a copy of the original varlena.
*/
res = (struct varlena *) palloc(VARSIZE(v));
if (res == NULL)
{
elog(WARN, "byteaSetByte: Out of memory (%d bytes requested)",
VARSIZE(v));
}
memmove((char *) res, (char *) v, VARSIZE(v));
/*
* Now set the byte.
*/
res->vl_dat[n] = newByte;
return (res);
}
/*-------------------------------------------------------------
@@ -563,39 +600,45 @@ byteaSetByte(struct varlena *v, int32 n, int32 newByte)
*-------------------------------------------------------------
*/
struct varlena *
byteaSetBit(struct varlena *v, int32 n, int32 newBit)
byteaSetBit(struct varlena * v, int32 n, int32 newBit)
{
struct varlena *res;
int oldByte, newByte;
int byteNo, bitNo;
/*
* sanity check!
*/
if (newBit != 0 && newBit != 1) {
elog(WARN, "byteaSetByte: new bit must be 0 or 1");
}
/*
* get the byte where the bit we want is stored.
*/
byteNo = n / 8;
bitNo = n % 8;
oldByte = byteaGetByte(v, byteNo);
/*
* calculate the new value for that byte
*/
if (newBit == 0) {
newByte = oldByte & (~(1<<bitNo));
} else {
newByte = oldByte | (1<<bitNo);
}
/*
* NOTE: 'byteaSetByte' creates a copy of 'v' & sets the byte.
*/
res = byteaSetByte(v, byteNo, newByte);
return(res);
struct varlena *res;
int oldByte,
newByte;
int byteNo,
bitNo;
/*
* sanity check!
*/
if (newBit != 0 && newBit != 1)
{
elog(WARN, "byteaSetByte: new bit must be 0 or 1");
}
/*
* get the byte where the bit we want is stored.
*/
byteNo = n / 8;
bitNo = n % 8;
oldByte = byteaGetByte(v, byteNo);
/*
* calculate the new value for that byte
*/
if (newBit == 0)
{
newByte = oldByte & (~(1 << bitNo));
}
else
{
newByte = oldByte | (1 << bitNo);
}
/*
* NOTE: 'byteaSetByte' creates a copy of 'v' & sets the byte.
*/
res = byteaSetByte(v, byteNo, newByte);
return (res);
}