Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1f747c6

Browse files
committed
Attached are the C-routines that implement a BIT and BIT VARYING type.
Adriaan Joubert
1 parent 47e5168 commit 1f747c6

File tree

6 files changed

+1213
-0
lines changed

6 files changed

+1213
-0
lines changed

contrib/bit/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CFLAGS = -g
2+
3+
varbit: vartest.o varbit.o
4+
$(CC) $(CFLAGS) -o $@ $^
5+
6+
varbit.o: varbit.c varbit.h
7+
vartest.o: vartest.c varbit.h
8+
9+
clean:
10+
rm -f *.o varbit

contrib/bit/README

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
A set of C routines to implement an SQL-compliant bitstring type.
2+
3+
The file varbit.c contains the c-functions to implement both BIT and
4+
BIT VARYING. Both types are implemented in essentially the same way,
5+
except that BIT is zero padded to a specified length. I've tried to
6+
make this code as independent as possible of the byte length, but it
7+
is quite possible that there may be problems on machines that don't
8+
have 8 bits/byte (are there still any around?).
9+
10+
In the input routines I have assumed that the parser eats the quotes
11+
in B'...' or X'...'.
12+
13+
The SQL standard only defines comparison, SUBSTR and concatenation
14+
operators, and these have been implemented. In addition all logical
15+
operators have been implemented, i.e. ~,|,&,^,<< and >>. This is
16+
useful if one wants to build bit masks. If the two strings are not of
17+
the same length the longer string is truncated (truncation was the
18+
only real option, as padding with zeros could give unintuitive results
19+
for ^) and the result has the length of the shorter string. If there
20+
is a requirement for any other functions, let me know, and I will have
21+
a look.
22+
23+
My knowledge of postgres is not up to integrating a type, so I'm hoping
24+
that somebody can integrate this type for me, or give me some hints as
25+
to what needs to be done. These routines were developed outside the
26+
postgres source tree, with a hacked version of postgres.h. The header
27+
files probably need some ammending.
28+
29+
The included files are
30+
31+
varbit.h -- bit string header type
32+
varbit.c -- the routines
33+
vartest.c -- a few calls to the routines to
34+
35+
The following routines are available.
36+
37+
char * zpbitin(char *s, int dummy, int32 atttypmod);
38+
Read in a zero padded bit string of the form X'...' or B'...'
39+
40+
char * zpbitout(char *s);
41+
Print a zero padded bit string in hex X'...'
42+
43+
char * zpbitsout(char *s);
44+
Print a zero padded bit string in binary B'...'
45+
46+
char * varbitin(char *s, int dummy, int32 atttypmod);
47+
Read in a varying length bit string of the form X'...' or B'...'
48+
49+
[There is no need for separate output functions for varying bit, as
50+
zpbitout will print them out correctly]
51+
52+
char * bitcat (char *arg1, char *arg2);
53+
Bit concatenation.
54+
55+
char * bitsubstr (char *arg, int32 s, int32 l);
56+
Substring of a bit string.
57+
58+
bool biteq (char *arg1, char *arg2);
59+
bool bitne (char *arg1, char *arg2);
60+
bool bitge (char *arg1, char *arg2);
61+
bool bitgt (char *arg1, char *arg2);
62+
bool bitle (char *arg1, char *arg2);
63+
bool bitlt (char *arg1, char *arg2);
64+
int bitcmp (char *arg1, char *arg2);
65+
Comparison operators
66+
67+
char * bitand (char * arg1, char * arg2);
68+
char * bitor (char * arg1, char * arg2);
69+
char * bitxor (char * arg1, char * arg2);
70+
char * bitnot (char * arg);
71+
char * bitshiftright (char * arg, int shft);
72+
char * bitshiftleft (char * arg, int shft);
73+
Bit operations.
74+
75+
If anything else needs to be done, please let me know.
76+
77+
Adriaan (adriaan@albourne.com)

contrib/bit/postgres.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#ifndef POSTGRES_H
2+
#define POSTGRES_H
3+
4+
#include <stdio.h>
5+
6+
typedef char bool;
7+
typedef signed char int8;
8+
typedef signed short int16;
9+
typedef signed int int32;
10+
11+
/*#define NULL ((void *) 0)*/
12+
#define Min(x, y) ((x) < (y) ? (x) : (y))
13+
#define Max(x, y) ((x) > (y) ? (x) : (y))
14+
#define PointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
15+
16+
17+
typedef unsigned int Oid;
18+
typedef int16 int2;
19+
typedef int32 int4;
20+
typedef float float4;
21+
typedef double float8;
22+
typedef unsigned char uint8; /* == 8 bits */
23+
typedef unsigned short uint16; /* == 16 bits */
24+
typedef unsigned int uint32; /* == 32 bits */
25+
typedef uint8 bits8; /* >= 8 bits */
26+
typedef uint16 bits16; /* >= 16 bits */
27+
typedef uint32 bits32; /* >= 32 bits */
28+
29+
30+
typedef int4 aclitem;
31+
32+
#define InvalidOid 0
33+
#define OidIsValid(objectId) ((bool) (objectId != InvalidOid))
34+
35+
/* unfortunately, both regproc and RegProcedure are used */
36+
typedef Oid regproc;
37+
typedef Oid RegProcedure;
38+
39+
typedef char *((*func_ptr) ());
40+
41+
42+
#define RegProcedureIsValid(p) OidIsValid(p)
43+
44+
/* ----------------------------------------------------------------
45+
* Section 2: variable length and array types
46+
* ----------------------------------------------------------------
47+
*/
48+
/* ----------------
49+
* struct varlena
50+
* ----------------
51+
*/
52+
struct varlena
53+
{
54+
int32 vl_len;
55+
char vl_dat[1];
56+
};
57+
58+
#define VARSIZE(PTR) (((struct varlena *)(PTR))->vl_len)
59+
#define VARDATA(PTR) (((struct varlena *)(PTR))->vl_dat)
60+
#define VARHDRSZ sizeof(int32)
61+
62+
typedef struct varlena bytea;
63+
typedef struct varlena text;
64+
65+
typedef int2 int28[8];
66+
typedef Oid oid8[8];
67+
68+
#define ERROR stderr
69+
#define elog fprintf
70+
71+
#define MaxAttrSize 10000
72+
73+
#define palloc malloc
74+
#endif

0 commit comments

Comments
 (0)