bool.c 2.45 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * bool.c
4
 *	  Functions for the built-in type "bool".
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/bool.c,v 1.17 1999/07/14 01:19:56 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14

15
#include <string.h>
16 17
#include "postgres.h"

18
#include "utils/builtins.h"		/* where the declarations go */
19
#include "utils/palloc.h"
20
#include "utils/mcxt.h"
21

22 23
/*****************************************************************************
 *	 USER I/O ROUTINES														 *
24 25 26
 *****************************************************************************/

/*
27
 *		boolin			- converts "t" or "f" to 1 or 0
28
 *
29 30 31 32
 * 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.
33
 */
34
bool
35 36
boolin(char *b)
{
37 38 39 40
	switch (*b)
	{
			case 't':
			case 'T':
41
			if (strncasecmp(b, "true", strlen(b)) == 0)
42
				return TRUE;
43 44 45 46
			break;

		case 'f':
		case 'F':
47
			if (strncasecmp(b, "false", strlen(b)) == 0)
48
				return FALSE;
49 50 51 52
			break;

		case 'y':
		case 'Y':
53
			if (strncasecmp(b, "yes", strlen(b)) == 0)
54
				return TRUE;
55 56
			break;

57
		case '1':
58
			if (strncasecmp(b, "1", strlen(b)) == 0)
59
				return TRUE;
60 61 62 63
			break;

		case 'n':
		case 'N':
64
			if (strncasecmp(b, "no", strlen(b)) == 0)
65
				return FALSE;
66 67
			break;

68
		case '0':
69
			if (strncasecmp(b, "0", strlen(b)) == 0)
70
				return FALSE;
71 72 73 74 75 76
			break;

		default:
			break;
	}

77
	elog(ERROR, "Bad boolean external representation '%s'", b);
78
	/* not reached */
79
	return FALSE;
80
}	/* boolin() */
81 82

/*
83
 *		boolout			- converts 1 or 0 to "t" or "f"
84
 */
85 86
char *
boolout(bool b)
87
{
88
	char	   *result = (char *) palloc(2);
89 90 91

	*result = (b) ? 't' : 'f';
	result[1] = '\0';
92
	return result;
93
}	/* boolout() */
94

95

96 97
/*****************************************************************************
 *	 PUBLIC ROUTINES														 *
98 99
 *****************************************************************************/

100
bool
101
booleq(bool arg1, bool arg2)
102
{
103
	return arg1 == arg2;
104 105
}

106
bool
107
boolne(bool arg1, bool arg2)
108
{
109
	return arg1 != arg2;
110 111
}

112
bool
113
boollt(bool arg1, bool arg2)
114
{
115
	return arg1 < arg2;
116
}
117

118
bool
119
boolgt(bool arg1, bool arg2)
120
{
121
	return arg1 > arg2;
122
}
123 124 125 126

bool
istrue(bool arg1)
{
127
	return arg1 == TRUE;
128
}	/* istrue() */
129 130 131 132

bool
isfalse(bool arg1)
{
133
	return arg1 != TRUE;
134
}	/* isfalse() */