OSDN Git Service

Pgindent run for 8.0.
[pg-rex/syncrep.git] / contrib / tsearch2 / common.c
1 #include "postgres.h"
2
3 #include "postgres.h"
4 #include "fmgr.h"
5 #include "catalog/pg_proc.h"
6 #include "catalog/pg_namespace.h"
7 #include "utils/syscache.h"
8
9 #include "ts_cfg.h"
10 #include "dict.h"
11 #include "wparser.h"
12 #include "snmap.h"
13 #include "common.h"
14 #include "tsvector.h"
15
16
17
18 #include "common.h"
19 #include "wparser.h"
20 #include "ts_cfg.h"
21 #include "dict.h"
22
23
24 Oid                     TSNSP_FunctionOid = InvalidOid;
25
26
27 text *
28 char2text(char *in)
29 {
30         return charl2text(in, strlen(in));
31 }
32
33 text *
34 charl2text(char *in, int len)
35 {
36         text       *out = (text *) palloc(len + VARHDRSZ);
37
38         memcpy(VARDATA(out), in, len);
39         VARATT_SIZEP(out) = len + VARHDRSZ;
40         return out;
41 }
42
43 char
44                    *
45 text2char(text *in)
46 {
47         char       *out = palloc(VARSIZE(in));
48
49         memcpy(out, VARDATA(in), VARSIZE(in) - VARHDRSZ);
50         out[VARSIZE(in) - VARHDRSZ] = '\0';
51         return out;
52 }
53
54 char
55                    *
56 pnstrdup(char *in, int len)
57 {
58         char       *out = palloc(len + 1);
59
60         memcpy(out, in, len);
61         out[len] = '\0';
62         return out;
63 }
64
65 text
66                    *
67 ptextdup(text *in)
68 {
69         text       *out = (text *) palloc(VARSIZE(in));
70
71         memcpy(out, in, VARSIZE(in));
72         return out;
73 }
74
75 text
76                    *
77 mtextdup(text *in)
78 {
79         text       *out = (text *) malloc(VARSIZE(in));
80
81         if (!out)
82                 ts_error(ERROR, "No memory");
83         memcpy(out, in, VARSIZE(in));
84         return out;
85 }
86
87 void
88 ts_error(int state, const char *format,...)
89 {
90         va_list         args;
91         int                     tlen = 128,
92                                 len = 0;
93         char       *buf;
94
95         reset_cfg();
96         reset_dict();
97         reset_prs();
98
99         va_start(args, format);
100         buf = palloc(tlen);
101         len = vsnprintf(buf, tlen - 1, format, args);
102         if (len >= tlen)
103         {
104                 tlen = len + 1;
105                 buf = repalloc(buf, tlen);
106                 vsnprintf(buf, tlen - 1, format, args);
107         }
108         va_end(args);
109
110         /* ?? internal error ?? */
111         elog(state, "%s", buf);
112         pfree(buf);
113 }
114
115 int
116 text_cmp(text *a, text *b)
117 {
118         if (VARSIZE(a) == VARSIZE(b))
119                 return strncmp(VARDATA(a), VARDATA(b), VARSIZE(a) - VARHDRSZ);
120         return (int) VARSIZE(a) - (int) VARSIZE(b);
121
122 }
123
124 char *
125 get_namespace(Oid funcoid)
126 {
127         HeapTuple       tuple;
128         Form_pg_proc proc;
129         Form_pg_namespace nsp;
130         Oid                     nspoid;
131         char       *txt;
132
133         tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
134         if (!HeapTupleIsValid(tuple))
135                 elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
136         proc = (Form_pg_proc) GETSTRUCT(tuple);
137         nspoid = proc->pronamespace;
138         ReleaseSysCache(tuple);
139
140         tuple = SearchSysCache(NAMESPACEOID, ObjectIdGetDatum(nspoid), 0, 0, 0);
141         if (!HeapTupleIsValid(tuple))
142                 elog(ERROR, "cache lookup failed for namespace oid %u", nspoid);
143         nsp = (Form_pg_namespace) GETSTRUCT(tuple);
144         txt = pstrdup(NameStr((nsp->nspname)));
145         ReleaseSysCache(tuple);
146
147         return txt;
148 }
149
150 Oid
151 get_oidnamespace(Oid funcoid)
152 {
153         HeapTuple       tuple;
154         Form_pg_proc proc;
155         Oid                     nspoid;
156
157         tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
158         if (!HeapTupleIsValid(tuple))
159                 elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
160         proc = (Form_pg_proc) GETSTRUCT(tuple);
161         nspoid = proc->pronamespace;
162         ReleaseSysCache(tuple);
163
164         return nspoid;
165 }