postgresql/src/backend/utils/adt/bool.c

132 lines
2.4 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* bool.c
* 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.20 1999/07/17 20:17:53 momjian Exp $
*
*-------------------------------------------------------------------------
*/
1996-11-03 07:54:38 +01:00
#include "postgres.h"
1999-07-16 05:14:30 +02:00
#include "utils/builtins.h"
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
/*
* boolin - converts "t" or "f" to 1 or 0
*
* Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO.
* Reject other values. - thomas 1997-10-05
*
* In the switch statement, check the most-used possibilities first.
*/
1997-03-15 00:21:12 +01:00
bool
boolin(char *b)
{
switch (*b)
{
case 't':
case 'T':
if (strncasecmp(b, "true", strlen(b)) == 0)
1998-09-01 05:29:17 +02:00
return TRUE;
break;
case 'f':
case 'F':
if (strncasecmp(b, "false", strlen(b)) == 0)
1998-09-01 05:29:17 +02:00
return FALSE;
break;
case 'y':
case 'Y':
if (strncasecmp(b, "yes", strlen(b)) == 0)
1998-09-01 05:29:17 +02:00
return TRUE;
break;
case '1':
if (strncasecmp(b, "1", strlen(b)) == 0)
1998-09-01 05:29:17 +02:00
return TRUE;
break;
case 'n':
case 'N':
if (strncasecmp(b, "no", strlen(b)) == 0)
1998-09-01 05:29:17 +02:00
return FALSE;
break;
case '0':
if (strncasecmp(b, "0", strlen(b)) == 0)
1998-09-01 05:29:17 +02:00
return FALSE;
break;
default:
break;
}
elog(ERROR, "Bad boolean external representation '%s'", b);
/* not reached */
1998-09-01 05:29:17 +02:00
return FALSE;
} /* boolin() */
/*
* boolout - converts 1 or 0 to "t" or "f"
*/
char *
boolout(bool b)
{
char *result = (char *) palloc(2);
*result = (b) ? 't' : 'f';
result[1] = '\0';
1998-09-01 05:29:17 +02:00
return result;
} /* boolout() */
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
1997-03-15 00:21:12 +01:00
bool
booleq(bool arg1, bool arg2)
{
1998-09-01 05:29:17 +02:00
return arg1 == arg2;
}
1997-03-15 00:21:12 +01:00
bool
boolne(bool arg1, bool arg2)
{
1998-09-01 05:29:17 +02:00
return arg1 != arg2;
}
bool
boollt(bool arg1, bool arg2)
{
1998-09-01 05:29:17 +02:00
return arg1 < arg2;
}
bool
boolgt(bool arg1, bool arg2)
{
1998-09-01 05:29:17 +02:00
return arg1 > arg2;
}
bool
istrue(bool arg1)
{
1998-09-01 05:29:17 +02:00
return arg1 == TRUE;
} /* istrue() */
bool
isfalse(bool arg1)
{
1998-09-01 05:29:17 +02:00
return arg1 != TRUE;
} /* isfalse() */