PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dict_ispell.c File Reference
#include "postgres.h"
#include "catalog/pg_collation_d.h"
#include "commands/defrem.h"
#include "tsearch/dicts/spell.h"
#include "tsearch/ts_public.h"
#include "utils/fmgrprotos.h"
#include "utils/formatting.h"
Include dependency graph for dict_ispell.c:

Go to the source code of this file.

Data Structures

struct  DictISpell
 

Functions

Datum dispell_init (PG_FUNCTION_ARGS)
 
Datum dispell_lexize (PG_FUNCTION_ARGS)
 

Function Documentation

◆ dispell_init()

Datum dispell_init ( PG_FUNCTION_ARGS  )

Definition at line 31 of file dict_ispell.c.

32{
33 List *dictoptions = (List *) PG_GETARG_POINTER(0);
34 DictISpell *d;
35 bool affloaded = false,
36 dictloaded = false,
37 stoploaded = false;
38 ListCell *l;
39
40 d = (DictISpell *) palloc0(sizeof(DictISpell));
41
42 NIStartBuild(&(d->obj));
43
44 foreach(l, dictoptions)
45 {
46 DefElem *defel = (DefElem *) lfirst(l);
47
48 if (strcmp(defel->defname, "dictfile") == 0)
49 {
50 if (dictloaded)
52 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
53 errmsg("multiple DictFile parameters")));
56 "dict"));
57 dictloaded = true;
58 }
59 else if (strcmp(defel->defname, "afffile") == 0)
60 {
61 if (affloaded)
63 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
64 errmsg("multiple AffFile parameters")));
65 NIImportAffixes(&(d->obj),
67 "affix"));
68 affloaded = true;
69 }
70 else if (strcmp(defel->defname, "stopwords") == 0)
71 {
72 if (stoploaded)
74 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
75 errmsg("multiple StopWords parameters")));
77 stoploaded = true;
78 }
79 else
80 {
82 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
83 errmsg("unrecognized Ispell parameter: \"%s\"",
84 defel->defname)));
85 }
86 }
87
88 if (affloaded && dictloaded)
89 {
90 NISortDictionary(&(d->obj));
91 NISortAffixes(&(d->obj));
92 }
93 else if (!affloaded)
94 {
96 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
97 errmsg("missing AffFile parameter")));
98 }
99 else
100 {
102 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
103 errmsg("missing DictFile parameter")));
104 }
105
106 NIFinishBuild(&(d->obj));
107
109}
char * defGetString(DefElem *def)
Definition: define.c:35
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1591
void * palloc0(Size size)
Definition: mcxt.c:1347
#define lfirst(lc)
Definition: pg_list.h:172
void NIStartBuild(IspellDict *Conf)
Definition: spell.c:89
void NIFinishBuild(IspellDict *Conf)
Definition: spell.c:104
void NIImportAffixes(IspellDict *Conf, const char *filename)
Definition: spell.c:1426
void NISortDictionary(IspellDict *Conf)
Definition: spell.c:1723
void NISortAffixes(IspellDict *Conf)
Definition: spell.c:1977
void NIImportDictionary(IspellDict *Conf, const char *filename)
Definition: spell.c:518
char * defname
Definition: parsenodes.h:817
StopList stoplist
Definition: dict_ispell.c:26
IspellDict obj
Definition: dict_ispell.c:27
Definition: pg_list.h:54
void readstoplist(const char *fname, StopList *s, char *(*wordop)(const char *, size_t, Oid))
Definition: ts_utils.c:69
char * get_tsearch_config_filename(const char *basename, const char *extension)
Definition: ts_utils.c:34

References defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, get_tsearch_config_filename(), lfirst, NIFinishBuild(), NIImportAffixes(), NIImportDictionary(), NISortAffixes(), NISortDictionary(), NIStartBuild(), DictISpell::obj, palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, readstoplist(), DictISpell::stoplist, and str_tolower().

◆ dispell_lexize()

Datum dispell_lexize ( PG_FUNCTION_ARGS  )

Definition at line 112 of file dict_ispell.c.

113{
115 char *in = (char *) PG_GETARG_POINTER(1);
117 char *txt;
118 TSLexeme *res;
119 TSLexeme *ptr,
120 *cptr;
121
122 if (len <= 0)
123 PG_RETURN_POINTER(NULL);
124
125 txt = str_tolower(in, len, DEFAULT_COLLATION_OID);
126 res = NINormalizeWord(&(d->obj), txt);
127
128 if (res == NULL)
129 PG_RETURN_POINTER(NULL);
130
131 cptr = res;
132 for (ptr = cptr; ptr->lexeme; ptr++)
133 {
134 if (searchstoplist(&(d->stoplist), ptr->lexeme))
135 {
136 pfree(ptr->lexeme);
137 ptr->lexeme = NULL;
138 }
139 else
140 {
141 if (cptr != ptr)
142 memcpy(cptr, ptr, sizeof(TSLexeme));
143 cptr++;
144 }
145 }
146 cptr->lexeme = NULL;
147
149}
int32_t int32
Definition: c.h:481
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
void pfree(void *pointer)
Definition: mcxt.c:1521
const void size_t len
TSLexeme * NINormalizeWord(IspellDict *Conf, const char *word)
Definition: spell.c:2541
char * lexeme
Definition: ts_public.h:138
bool searchstoplist(StopList *s, char *key)
Definition: ts_utils.c:141

References len, TSLexeme::lexeme, NINormalizeWord(), DictISpell::obj, pfree(), PG_GETARG_INT32, PG_GETARG_POINTER, PG_RETURN_POINTER, res, searchstoplist(), DictISpell::stoplist, and str_tolower().