Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1f747c6

Browse files
committed
Attached are the C-routines that implement a BIT and BIT VARYING type.
Adriaan Joubert
1 parent47e5168 commit1f747c6

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+
#ifndefPOSTGRES_H
2+
#definePOSTGRES_H
3+
4+
#include<stdio.h>
5+
6+
typedefcharbool;
7+
typedefsignedcharint8;
8+
typedefsigned shortint16;
9+
typedefsignedintint32;
10+
11+
/*#define NULL ((void *) 0)*/
12+
#defineMin(x,y) ((x) < (y) ? (x) : (y))
13+
#defineMax(x,y) ((x) > (y) ? (x) : (y))
14+
#definePointerIsValid(pointer) (bool)((void*)(pointer) != NULL)
15+
16+
17+
typedefunsignedintOid;
18+
typedefint16int2;
19+
typedefint32int4;
20+
typedeffloatfloat4;
21+
typedefdoublefloat8;
22+
typedefunsignedcharuint8;/* == 8 bits */
23+
typedefunsigned shortuint16;/* == 16 bits */
24+
typedefunsignedintuint32;/* == 32 bits */
25+
typedefuint8bits8;/* >= 8 bits */
26+
typedefuint16bits16;/* >= 16 bits */
27+
typedefuint32bits32;/* >= 32 bits */
28+
29+
30+
typedefint4aclitem;
31+
32+
#defineInvalidOid0
33+
#defineOidIsValid(objectId) ((bool) (objectId != InvalidOid))
34+
35+
/* unfortunately, both regproc and RegProcedure are used */
36+
typedefOidregproc;
37+
typedefOidRegProcedure;
38+
39+
typedefchar*((*func_ptr) ());
40+
41+
42+
#defineRegProcedureIsValid(p)OidIsValid(p)
43+
44+
/* ----------------------------------------------------------------
45+
*Section 2:variable length and array types
46+
* ----------------------------------------------------------------
47+
*/
48+
/* ----------------
49+
*struct varlena
50+
* ----------------
51+
*/
52+
structvarlena
53+
{
54+
int32vl_len;
55+
charvl_dat[1];
56+
};
57+
58+
#defineVARSIZE(PTR)(((struct varlena *)(PTR))->vl_len)
59+
#defineVARDATA(PTR)(((struct varlena *)(PTR))->vl_dat)
60+
#defineVARHDRSZsizeof(int32)
61+
62+
typedefstructvarlenabytea;
63+
typedefstructvarlenatext;
64+
65+
typedefint2int28[8];
66+
typedefOidoid8[8];
67+
68+
#defineERROR stderr
69+
#defineelog fprintf
70+
71+
#defineMaxAttrSize 10000
72+
73+
#definepalloc malloc
74+
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp