You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postgres/contrib/tsearch2/common.c

103 lines
1.5 KiB

23 years ago
#include "postgres.h"
#include "common.h"
#include "wparser.h"
#include "ts_cfg.h"
#include "dict.h"
23 years ago
text *
char2text(char *in)
{
23 years ago
return charl2text(in, strlen(in));
}
23 years ago
text *
charl2text(char *in, int len)
{
text *out = (text *) palloc(len + VARHDRSZ);
23 years ago
memcpy(VARDATA(out), in, len);
23 years ago
VARATT_SIZEP(out) = len + VARHDRSZ;
23 years ago
return out;
}
23 years ago
char
*
text2char(text *in)
{
char *out = palloc(VARSIZE(in));
memcpy(out, VARDATA(in), VARSIZE(in) - VARHDRSZ);
out[VARSIZE(in) - VARHDRSZ] = '\0';
23 years ago
return out;
}
23 years ago
char
*
pnstrdup(char *in, int len)
{
char *out = palloc(len + 1);
23 years ago
memcpy(out, in, len);
23 years ago
out[len] = '\0';
23 years ago
return out;
}
23 years ago
text
*
ptextdup(text *in)
{
text *out = (text *) palloc(VARSIZE(in));
memcpy(out, in, VARSIZE(in));
23 years ago
return out;
}
23 years ago
text
*
mtextdup(text *in)
{
text *out = (text *) malloc(VARSIZE(in));
if (!out)
23 years ago
ts_error(ERROR, "No memory");
23 years ago
memcpy(out, in, VARSIZE(in));
23 years ago
return out;
}
23 years ago
void
ts_error(int state, const char *format,...)
{
va_list args;
int tlen = 128,
len = 0;
char *buf;
23 years ago
reset_cfg();
reset_dict();
reset_prs();
va_start(args, format);
buf = palloc(tlen);
23 years ago
len = vsnprintf(buf, tlen - 1, format, args);
if (len >= tlen)
{
tlen = len + 1;
buf = repalloc(buf, tlen);
vsnprintf(buf, tlen - 1, format, args);
23 years ago
}
va_end(args);
23 years ago
/* ?? internal error ?? */
elog(state, "%s", buf);
23 years ago
pfree(buf);
}
23 years ago
int
text_cmp(text *a, text *b)
{
if (VARSIZE(a) == VARSIZE(b))
return strncmp(VARDATA(a), VARDATA(b), VARSIZE(a) - VARHDRSZ);
return (int) VARSIZE(a) - (int) VARSIZE(b);
23 years ago
}