Header And Logo

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

Functions

ts_locale.c File Reference

#include "postgres.h"
#include "catalog/pg_collation.h"
#include "storage/fd.h"
#include "tsearch/ts_locale.h"
#include "tsearch/ts_public.h"
Include dependency graph for ts_locale.c:

Go to the source code of this file.

Functions

static void tsearch_readline_callback (void *arg)
bool tsearch_readline_begin (tsearch_readline_state *stp, const char *filename)
char * tsearch_readline (tsearch_readline_state *stp)
void tsearch_readline_end (tsearch_readline_state *stp)
char * t_readline (FILE *fp)
char * lowerstr (const char *str)
char * lowerstr_with_len (const char *str, int len)

Function Documentation

char* lowerstr ( const char *  str  ) 
char* lowerstr_with_len ( const char *  str,
int  len 
)

Definition at line 247 of file ts_locale.c.

References Assert, ereport, errcode(), errmsg(), ERROR, lc_ctype_is_c(), palloc(), pfree(), pg_database_encoding_max_length(), pstrdup(), and TOUCHAR.

Referenced by dispell_lexize(), dsimple_lexize(), dsnowball_lexize(), dsynonym_lexize(), generate_trgm(), generate_wildcard_trgm(), and lowerstr().

{
    char       *out;

#ifdef USE_WIDE_UPPER_LOWER
    Oid         collation = DEFAULT_COLLATION_OID;      /* TODO */
    pg_locale_t mylocale = 0;   /* TODO */
#endif

    if (len == 0)
        return pstrdup("");

#ifdef USE_WIDE_UPPER_LOWER

    /*
     * Use wide char code only when max encoding length > 1 and ctype != C.
     * Some operating systems fail with multi-byte encodings and a C locale.
     * Also, for a C locale there is no need to process as multibyte. From
     * backend/utils/adt/oracle_compat.c Teodor
     */
    if (pg_database_encoding_max_length() > 1 && !lc_ctype_is_c(collation))
    {
        wchar_t    *wstr,
                   *wptr;
        int         wlen;

        /*
         * alloc number of wchar_t for worst case, len contains number of
         * bytes >= number of characters and alloc 1 wchar_t for 0, because
         * wchar2char wants zero-terminated string
         */
        wptr = wstr = (wchar_t *) palloc(sizeof(wchar_t) * (len + 1));

        wlen = char2wchar(wstr, len + 1, str, len, mylocale);
        Assert(wlen <= len);

        while (*wptr)
        {
            *wptr = towlower((wint_t) *wptr);
            wptr++;
        }

        /*
         * Alloc result string for worst case + '\0'
         */
        len = pg_database_encoding_max_length() * wlen + 1;
        out = (char *) palloc(len);

        wlen = wchar2char(out, wstr, len, mylocale);

        pfree(wstr);

        if (wlen < 0)
            ereport(ERROR,
                    (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
            errmsg("conversion from wchar_t to server encoding failed: %m")));
        Assert(wlen < len);
    }
    else
#endif   /* USE_WIDE_UPPER_LOWER */
    {
        const char *ptr = str;
        char       *outptr;

        outptr = out = (char *) palloc(sizeof(char) * (len + 1));
        while ((ptr - str) < len && *ptr)
        {
            *outptr++ = tolower(TOUCHAR(ptr));
            ptr++;
        }
        *outptr = '\0';
    }

    return out;
}

char* t_readline ( FILE *  fp  ) 

Definition at line 197 of file ts_locale.c.

References buf, GetDatabaseEncoding(), NULL, pg_do_encoding_conversion(), PG_UTF8, pg_verify_mbstr(), and pnstrdup().

Referenced by tsearch_readline().

{
    int         len;
    char       *recoded;
    char        buf[4096];      /* lines must not be longer than this */

    if (fgets(buf, sizeof(buf), fp) == NULL)
        return NULL;

    len = strlen(buf);

    /* Make sure the input is valid UTF-8 */
    (void) pg_verify_mbstr(PG_UTF8, buf, len, false);

    /* And convert */
    recoded = (char *) pg_do_encoding_conversion((unsigned char *) buf,
                                                 len,
                                                 PG_UTF8,
                                                 GetDatabaseEncoding());
    if (recoded == buf)
    {
        /*
         * conversion didn't pstrdup, so we must. We can use the length of the
         * original string, because no conversion was done.
         */
        recoded = pnstrdup(recoded, len);
    }

    return recoded;
}

char* tsearch_readline ( tsearch_readline_state stp  ) 
bool tsearch_readline_begin ( tsearch_readline_state stp,
const char *  filename 
)
static void tsearch_readline_callback ( void *  arg  )  [static]

Definition at line 165 of file ts_locale.c.

References tsearch_readline_state::curline, errcontext, tsearch_readline_state::filename, and tsearch_readline_state::lineno.

{
    tsearch_readline_state *stp = (tsearch_readline_state *) arg;

    /*
     * We can't include the text of the config line for errors that occur
     * during t_readline() itself.  This is only partly a consequence of our
     * arms-length use of that routine: the major cause of such errors is
     * encoding violations, and we daren't try to print error messages
     * containing badly-encoded data.
     */
    if (stp->curline)
        errcontext("line %d of configuration file \"%s\": \"%s\"",
                   stp->lineno,
                   stp->filename,
                   stp->curline);
    else
        errcontext("line %d of configuration file \"%s\"",
                   stp->lineno,
                   stp->filename);
}

void tsearch_readline_end ( tsearch_readline_state stp  )