-
Notifications
You must be signed in to change notification settings - Fork 1
/
dec2pgbit.c
60 lines (55 loc) · 1023 Bytes
/
dec2pgbit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Synopsis:
* Convert a list of byte decimal values into postgres bit(256) cast.
* Usage:
* echo 10 | dec2pgbitmap
* dec2pgbitmap <<END
* 11
* 12
* 13
* 14
* END
* B'111'::bit(256) >> 244
* Blame:
* Note:
* Error handling code is atrocious.
*/
#include <stdio.h>
#include <stdlib.h>
int
main()
{
int byte, b_min, b_max;
int i;
unsigned char bitmap[256] = {0};
b_max = 0;
b_min = 256;
while (scanf("%d\n", &byte) > 0) {
if (byte < 0 || byte > 255) {
fprintf(stderr, "decimal not >= 0 || < 256: %d\n",
byte);
exit(1);
}
if (b_min > byte)
b_min = byte;
if (b_max < byte)
b_max = byte;
bitmap[255 - byte] = 1;
if (feof(stdin))
break;
}
putchar('B');
putchar('\'');
// no bits set
if (b_max == 0) {
puts("0'::bit(256)");
exit(0);
}
// write compact bit string
for (i = b_min; i <= b_max; i++)
putchar(bitmap[255 - i] == 0 ? '0' : '1');
printf("'::bit(256) >> %d\n", 255 - b_max);
exit(0);
}