/
device_type.c
208 lines (174 loc) · 4.89 KB
/
device_type.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "device_type.h"
PG_MODULE_MAGIC;
static inline
char * create_string (size_t size, const char *instr)
{
char *str = palloc0(size);
memcpy(str, instr, size);
return str;
}
static inline
char *device_type_to_str(device_type c)
{
switch (c)
{
case BOT: return create_string(CONST_STRING("bot"));
case CONSOLE: return create_string(CONST_STRING("console"));
case IPOD: return create_string(CONST_STRING("ipod"));
case MAC: return create_string(CONST_STRING("mac"));
case PC: return create_string(CONST_STRING("pc"));
case PHONE: return create_string(CONST_STRING("phone"));
case SERVER: return create_string(CONST_STRING("server"));
case SIMULATOR: return create_string(CONST_STRING("simulator"));
case TABLET: return create_string(CONST_STRING("tablet"));
case TV: return create_string(CONST_STRING("tv"));
case UNKNOWN: return create_string(CONST_STRING("unknown"));
default: elog(ERROR, "internal error unexpected num in device_type_to_str");
}
}
static inline uint8
get_device_type_num_p(const char *str)
{
switch (str[1]) {
case 'c': if (strcmp(str, "pc") == 0) return PC;
case 'h': if (strcmp(str, "phone") == 0) return PHONE;
}
elog(ERROR, "unknown input device_type: %s", str);
return 0; //keep compiler quiet//
}
static inline uint8
get_device_type_num_s(const char *str)
{
switch (str[1]) {
case 'e': if (strcmp(str, "server") == 0) return SERVER;
case 'i': if (strcmp(str, "simulator") == 0) return SIMULATOR;
}
elog(ERROR, "unknown input device_type: %s", str);
return 0; //keep compiler quiet//
}
static inline uint8
get_device_type_num_t(const char *str)
{
switch (str[1]) {
case 'v': if (strcmp(str, "tv") == 0) return TV;
case 'a': if (strcmp(str, "tablet") == 0) return TABLET;
}
elog(ERROR, "unknown input device_type: %s", str);
return 0; //keep compiler quiet//
}
static inline
device_type device_type_from_str(const char *str)
{
if (strlen(str) < 2)
elog(ERROR, "unknown input device_type: %s", str);
switch (str[0])
{
case 'b': if (strcmp(str, "bot") == 0) return BOT;
case 'c': if (strcmp(str, "console") == 0) return CONSOLE;
case 'i': if (strcmp(str, "ipod") == 0) return IPOD;
case 'm': if (strcmp(str, "mac") == 0) return MAC;
case 'p': return get_device_type_num_p(str);
case 's': return get_device_type_num_s(str);
case 't': return get_device_type_num_t(str);
case 'u': if (strcmp(str, "unknown") == 0) return UNKNOWN;
}
elog(ERROR, "unknown input device_type: %s", str);
return 0; //keep compiler quiet//
}
PG_FUNCTION_INFO_V1(device_type_in);
Datum
device_type_in(PG_FUNCTION_ARGS)
{
int i;
char *str = PG_GETARG_CSTRING(0);
for(i = 0; str[i]; i++){
str[i] = tolower(str[i]);
}
PG_RETURN_UINT8(device_type_from_str(str));
}
PG_FUNCTION_INFO_V1(device_type_out);
Datum
device_type_out(PG_FUNCTION_ARGS)
{
device_type c = PG_GETARG_UINT8(0);
PG_RETURN_CSTRING(device_type_to_str(c));
}
PG_FUNCTION_INFO_V1(device_type_recv);
Datum
device_type_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_UINT8(pq_getmsgbyte(buf));
}
PG_FUNCTION_INFO_V1(device_type_send);
Datum
device_type_send(PG_FUNCTION_ARGS)
{
device_type c = PG_GETARG_UINT8(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendbyte(&buf, c);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
PG_FUNCTION_INFO_V1(device_type_eq);
Datum
device_type_eq(PG_FUNCTION_ARGS)
{
device_type arg1 = PG_GETARG_UINT8(0);
device_type arg2 = PG_GETARG_UINT8(1);
PG_RETURN_BOOL(arg1 == arg2);
}
PG_FUNCTION_INFO_V1(device_type_ne);
Datum
device_type_ne(PG_FUNCTION_ARGS)
{
device_type arg1 = PG_GETARG_UINT8(0);
device_type arg2 = PG_GETARG_UINT8(1);
PG_RETURN_BOOL(arg1 != arg2);
}
PG_FUNCTION_INFO_V1(device_type_lt);
Datum
device_type_lt(PG_FUNCTION_ARGS)
{
device_type arg1 = PG_GETARG_UINT8(0);
device_type arg2 = PG_GETARG_UINT8(1);
PG_RETURN_BOOL(arg1 < arg2);
}
PG_FUNCTION_INFO_V1(device_type_le);
Datum
device_type_le(PG_FUNCTION_ARGS)
{
device_type arg1 = PG_GETARG_UINT8(0);
device_type arg2 = PG_GETARG_UINT8(1);
PG_RETURN_BOOL(arg1 <= arg2);
}
PG_FUNCTION_INFO_V1(device_type_gt);
Datum
device_type_gt(PG_FUNCTION_ARGS)
{
device_type arg1 = PG_GETARG_UINT8(0);
device_type arg2 = PG_GETARG_UINT8(1);
PG_RETURN_BOOL(arg1 > arg2);
}
PG_FUNCTION_INFO_V1(device_type_ge);
Datum
device_type_ge(PG_FUNCTION_ARGS)
{
device_type arg1 = PG_GETARG_UINT8(0);
device_type arg2 = PG_GETARG_UINT8(1);
PG_RETURN_BOOL(arg1 >= arg2);
}
PG_FUNCTION_INFO_V1(device_type_cmp);
Datum
device_type_cmp(PG_FUNCTION_ARGS)
{
device_type a = PG_GETARG_UINT8(0);
device_type b = PG_GETARG_UINT8(1);
PG_RETURN_INT32((int32) a - (int32) b);
}
PG_FUNCTION_INFO_V1(hash_device_type);
Datum
hash_device_type(PG_FUNCTION_ARGS)
{
return hash_uint32((int32) PG_GETARG_UINT8(0));
}