]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/bool.c
Add:
[postgresql] / src / backend / utils / adt / bool.c
1 /*-------------------------------------------------------------------------
2  *
3  * bool.c
4  *        Functions for the built-in type "bool".
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/bool.c,v 1.21 2000/01/26 05:57:13 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include "utils/builtins.h"
19
20 /*****************************************************************************
21  *       USER I/O ROUTINES                                                                                                               *
22  *****************************************************************************/
23
24 /*
25  *              boolin                  - converts "t" or "f" to 1 or 0
26  *
27  * Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO.
28  * Reject other values. - thomas 1997-10-05
29  *
30  * In the switch statement, check the most-used possibilities first.
31  */
32 bool
33 boolin(char *b)
34 {
35         switch (*b)
36         {
37                         case 't':
38                         case 'T':
39                         if (strncasecmp(b, "true", strlen(b)) == 0)
40                                 return TRUE;
41                         break;
42
43                 case 'f':
44                 case 'F':
45                         if (strncasecmp(b, "false", strlen(b)) == 0)
46                                 return FALSE;
47                         break;
48
49                 case 'y':
50                 case 'Y':
51                         if (strncasecmp(b, "yes", strlen(b)) == 0)
52                                 return TRUE;
53                         break;
54
55                 case '1':
56                         if (strncasecmp(b, "1", strlen(b)) == 0)
57                                 return TRUE;
58                         break;
59
60                 case 'n':
61                 case 'N':
62                         if (strncasecmp(b, "no", strlen(b)) == 0)
63                                 return FALSE;
64                         break;
65
66                 case '0':
67                         if (strncasecmp(b, "0", strlen(b)) == 0)
68                                 return FALSE;
69                         break;
70
71                 default:
72                         break;
73         }
74
75         elog(ERROR, "Bad boolean external representation '%s'", b);
76         /* not reached */
77         return FALSE;
78 }       /* boolin() */
79
80 /*
81  *              boolout                 - converts 1 or 0 to "t" or "f"
82  */
83 char *
84 boolout(bool b)
85 {
86         char       *result = (char *) palloc(2);
87
88         *result = (b) ? 't' : 'f';
89         result[1] = '\0';
90         return result;
91 }       /* boolout() */
92
93
94 /*****************************************************************************
95  *       PUBLIC ROUTINES                                                                                                                 *
96  *****************************************************************************/
97
98 bool
99 booleq(bool arg1, bool arg2)
100 {
101         return arg1 == arg2;
102 }
103
104 bool
105 boolne(bool arg1, bool arg2)
106 {
107         return arg1 != arg2;
108 }
109
110 bool
111 boollt(bool arg1, bool arg2)
112 {
113         return arg1 < arg2;
114 }
115
116 bool
117 boolgt(bool arg1, bool arg2)
118 {
119         return arg1 > arg2;
120 }
121
122 bool
123 istrue(bool arg1)
124 {
125         return arg1 == TRUE;
126 }       /* istrue() */
127
128 bool
129 isfalse(bool arg1)
130 {
131         return arg1 != TRUE;
132 }       /* isfalse() */