Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

ts_utils.c File Reference

#include "postgres.h"
#include <ctype.h>
#include "miscadmin.h"
#include "tsearch/ts_locale.h"
#include "tsearch/ts_utils.h"
Include dependency graph for ts_utils.c:

Go to the source code of this file.

Functions

char * get_tsearch_config_filename (const char *basename, const char *extension)
void readstoplist (const char *fname, StopList *s, char *(*wordop)(const char *))
bool searchstoplist (StopList *s, char *key)

Function Documentation

char* get_tsearch_config_filename ( const char *  basename,
const char *  extension 
)

Definition at line 33 of file ts_utils.c.

References ereport, errcode(), errmsg(), ERROR, get_share_path(), MAXPGPATH, my_exec_path, palloc(), and snprintf().

Referenced by dispell_init(), dsynonym_init(), initSuffixTree(), read_dictionary(), readstoplist(), and thesaurusRead().

{
    char        sharepath[MAXPGPATH];
    char       *result;

    /*
     * We limit the basename to contain a-z, 0-9, and underscores.  This may
     * be overly restrictive, but we don't want to allow access to anything
     * outside the tsearch_data directory, so for instance '/' *must* be
     * rejected, and on some platforms '\' and ':' are risky as well. Allowing
     * uppercase might result in incompatible behavior between case-sensitive
     * and case-insensitive filesystems, and non-ASCII characters create other
     * interesting risks, so on the whole a tight policy seems best.
     */
    if (strspn(basename, "abcdefghijklmnopqrstuvwxyz0123456789_") != strlen(basename))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("invalid text search configuration file name \"%s\"",
                        basename)));

    get_share_path(my_exec_path, sharepath);
    result = palloc(MAXPGPATH);
    snprintf(result, MAXPGPATH, "%s/tsearch_data/%s.%s",
             sharepath, basename, extension);

    return result;
}

void readstoplist ( const char *  fname,
StopList s,
char *(*)(const char *)  wordop 
)

Definition at line 68 of file ts_utils.c.

References ereport, errcode(), errmsg(), ERROR, filename, get_tsearch_config_filename(), StopList::len, NULL, palloc(), pfree(), pg_mblen(), pg_qsort_strcmp(), qsort, repalloc(), StopList::stop, t_isspace, tsearch_readline(), tsearch_readline_begin(), and tsearch_readline_end().

Referenced by dispell_init(), dsimple_init(), and dsnowball_init().

{
    char      **stop = NULL;

    s->len = 0;
    if (fname && *fname)
    {
        char       *filename = get_tsearch_config_filename(fname, "stop");
        tsearch_readline_state trst;
        char       *line;
        int         reallen = 0;

        if (!tsearch_readline_begin(&trst, filename))
            ereport(ERROR,
                    (errcode(ERRCODE_CONFIG_FILE_ERROR),
                     errmsg("could not open stop-word file \"%s\": %m",
                            filename)));

        while ((line = tsearch_readline(&trst)) != NULL)
        {
            char       *pbuf = line;

            /* Trim trailing space */
            while (*pbuf && !t_isspace(pbuf))
                pbuf += pg_mblen(pbuf);
            *pbuf = '\0';

            /* Skip empty lines */
            if (*line == '\0')
            {
                pfree(line);
                continue;
            }

            if (s->len >= reallen)
            {
                if (reallen == 0)
                {
                    reallen = 64;
                    stop = (char **) palloc(sizeof(char *) * reallen);
                }
                else
                {
                    reallen *= 2;
                    stop = (char **) repalloc((void *) stop,
                                              sizeof(char *) * reallen);
                }
            }

            if (wordop)
            {
                stop[s->len] = wordop(line);
                if (stop[s->len] != line)
                    pfree(line);
            }
            else
                stop[s->len] = line;

            (s->len)++;
        }

        tsearch_readline_end(&trst);
        pfree(filename);
    }

    s->stop = stop;

    /* Sort to allow binary searching */
    if (s->stop && s->len > 0)
        qsort(s->stop, s->len, sizeof(char *), pg_qsort_strcmp);
}

bool searchstoplist ( StopList s,
char *  key 
)

Definition at line 141 of file ts_utils.c.

References StopList::len, pg_qsort_strcmp(), and StopList::stop.

Referenced by dispell_lexize(), dsimple_lexize(), and dsnowball_lexize().

{
    return (s->stop && s->len > 0 &&
            bsearch(&key, s->stop, s->len,
                    sizeof(char *), pg_qsort_strcmp)) ? true : false;
}