PostgreSQL Source Code  git master
ts_locale.c File Reference
#include "postgres.h"
#include "catalog/pg_collation.h"
#include "common/string.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.

Macros

#define WC_BUF_LEN   3
 

Functions

static void tsearch_readline_callback (void *arg)
 
int t_isdigit (const char *ptr)
 
int t_isspace (const char *ptr)
 
int t_isalpha (const char *ptr)
 
int t_isalnum (const char *ptr)
 
int t_isprint (const char *ptr)
 
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 * lowerstr (const char *str)
 
char * lowerstr_with_len (const char *str, int len)
 

Macro Definition Documentation

◆ WC_BUF_LEN

#define WC_BUF_LEN   3

Definition at line 34 of file ts_locale.c.

Function Documentation

◆ lowerstr()

char* lowerstr ( const char *  str)

◆ lowerstr_with_len()

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

Definition at line 268 of file ts_locale.c.

269 {
270  char *out;
271  pg_locale_t mylocale = 0; /* TODO */
272 
273  if (len == 0)
274  return pstrdup("");
275 
276  /*
277  * Use wide char code only when max encoding length > 1 and ctype != C.
278  * Some operating systems fail with multi-byte encodings and a C locale.
279  * Also, for a C locale there is no need to process as multibyte. From
280  * backend/utils/adt/oracle_compat.c Teodor
281  */
283  {
284  wchar_t *wstr,
285  *wptr;
286  int wlen;
287 
288  /*
289  * alloc number of wchar_t for worst case, len contains number of
290  * bytes >= number of characters and alloc 1 wchar_t for 0, because
291  * wchar2char wants zero-terminated string
292  */
293  wptr = wstr = (wchar_t *) palloc(sizeof(wchar_t) * (len + 1));
294 
295  wlen = char2wchar(wstr, len + 1, str, len, mylocale);
296  Assert(wlen <= len);
297 
298  while (*wptr)
299  {
300  *wptr = towlower((wint_t) *wptr);
301  wptr++;
302  }
303 
304  /*
305  * Alloc result string for worst case + '\0'
306  */
307  len = pg_database_encoding_max_length() * wlen + 1;
308  out = (char *) palloc(len);
309 
310  wlen = wchar2char(out, wstr, len, mylocale);
311 
312  pfree(wstr);
313 
314  if (wlen < 0)
315  ereport(ERROR,
316  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
317  errmsg("conversion from wchar_t to server encoding failed: %m")));
318  Assert(wlen < len);
319  }
320  else
321  {
322  const char *ptr = str;
323  char *outptr;
324 
325  outptr = out = (char *) palloc(sizeof(char) * (len + 1));
326  while ((ptr - str) < len && *ptr)
327  {
328  *outptr++ = tolower(TOUCHAR(ptr));
329  ptr++;
330  }
331  *outptr = '\0';
332  }
333 
334  return out;
335 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Assert(fmt[strlen(fmt) - 1] !='\n')
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1553
char * pstrdup(const char *in)
Definition: mcxt.c:1644
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc(Size size)
Definition: mcxt.c:1226
const void size_t len
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:2934
bool database_ctype_is_c
Definition: pg_locale.c:118
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:2990
#define TOUCHAR(x)
Definition: ts_locale.h:35

References Assert(), char2wchar(), database_ctype_is_c, ereport, errcode(), errmsg(), ERROR, len, palloc(), pfree(), pg_database_encoding_max_length(), pstrdup(), generate_unaccent_rules::str, TOUCHAR, and wchar2char().

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

◆ t_isalnum()

int t_isalnum ( const char *  ptr)

Definition at line 82 of file ts_locale.c.

83 {
84  int clen = pg_mblen(ptr);
85  wchar_t character[WC_BUF_LEN];
86  pg_locale_t mylocale = 0; /* TODO */
87 
88  if (clen == 1 || database_ctype_is_c)
89  return isalnum(TOUCHAR(ptr));
90 
91  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
92 
93  return iswalnum((wint_t) character[0]);
94 }
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1024
#define WC_BUF_LEN
Definition: ts_locale.c:34

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by parse_or_operator().

◆ t_isalpha()

int t_isalpha ( const char *  ptr)

Definition at line 67 of file ts_locale.c.

68 {
69  int clen = pg_mblen(ptr);
70  wchar_t character[WC_BUF_LEN];
71  pg_locale_t mylocale = 0; /* TODO */
72 
73  if (clen == 1 || database_ctype_is_c)
74  return isalpha(TOUCHAR(ptr));
75 
76  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
77 
78  return iswalpha((wint_t) character[0]);
79 }

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by parse_affentry(), RS_compile(), and RS_isRegis().

◆ t_isdigit()

int t_isdigit ( const char *  ptr)

Definition at line 37 of file ts_locale.c.

38 {
39  int clen = pg_mblen(ptr);
40  wchar_t character[WC_BUF_LEN];
41  pg_locale_t mylocale = 0; /* TODO */
42 
43  if (clen == 1 || database_ctype_is_c)
44  return isdigit(TOUCHAR(ptr));
45 
46  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
47 
48  return iswdigit((wint_t) character[0]);
49 }

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by getNextFlagFromString(), gettoken_tsvector(), NISortDictionary(), parse_lquery(), and parse_phrase_operator().

◆ t_isprint()

int t_isprint ( const char *  ptr)

Definition at line 97 of file ts_locale.c.

98 {
99  int clen = pg_mblen(ptr);
100  wchar_t character[WC_BUF_LEN];
101  pg_locale_t mylocale = 0; /* TODO */
102 
103  if (clen == 1 || database_ctype_is_c)
104  return isprint(TOUCHAR(ptr));
105 
106  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
107 
108  return iswprint((wint_t) character[0]);
109 }

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by NIImportDictionary().

◆ t_isspace()

int t_isspace ( const char *  ptr)

Definition at line 52 of file ts_locale.c.

53 {
54  int clen = pg_mblen(ptr);
55  wchar_t character[WC_BUF_LEN];
56  pg_locale_t mylocale = 0; /* TODO */
57 
58  if (clen == 1 || database_ctype_is_c)
59  return isspace(TOUCHAR(ptr));
60 
61  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
62 
63  return iswspace((wint_t) character[0]);
64 }

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by addCompoundAffixFlagValue(), find_word(), findwrd(), get_nextfield(), getNextFlagFromString(), gettoken_query(), gettoken_query_standard(), gettoken_query_websearch(), gettoken_tsvector(), initTrie(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), NISortDictionary(), parse_affentry(), parse_or_operator(), readstoplist(), and thesaurusRead().

◆ tsearch_readline()

char* tsearch_readline ( tsearch_readline_state stp)

Definition at line 159 of file ts_locale.c.

160 {
161  char *recoded;
162 
163  /* Advance line number to use in error reports */
164  stp->lineno++;
165 
166  /* Clear curline, it's no longer relevant */
167  if (stp->curline)
168  {
169  if (stp->curline != stp->buf.data)
170  pfree(stp->curline);
171  stp->curline = NULL;
172  }
173 
174  /* Collect next line, if there is one */
175  if (!pg_get_line_buf(stp->fp, &stp->buf))
176  return NULL;
177 
178  /* Validate the input as UTF-8, then convert to DB encoding if needed */
179  recoded = pg_any_to_server(stp->buf.data, stp->buf.len, PG_UTF8);
180 
181  /* Save the correctly-encoded string for possible error reports */
182  stp->curline = recoded; /* might be equal to buf.data */
183 
184  /*
185  * We always return a freshly pstrdup'd string. This is clearly necessary
186  * if pg_any_to_server() returned buf.data, and we need a second copy even
187  * if encoding conversion did occur. The caller is entitled to pfree the
188  * returned string at any time, which would leave curline pointing to
189  * recycled storage, causing problems if an error occurs after that point.
190  * (It's preferable to return the result of pstrdup instead of the output
191  * of pg_any_to_server, because the conversion result tends to be
192  * over-allocated. Since callers might save the result string directly
193  * into a long-lived dictionary structure, we don't want it to be a larger
194  * palloc chunk than necessary. We'll reclaim the conversion result on
195  * the next call.)
196  */
197  return pstrdup(recoded);
198 }
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:677
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:95
@ PG_UTF8
Definition: pg_wchar.h:235
StringInfoData buf
Definition: ts_locale.h:29

References tsearch_readline_state::buf, tsearch_readline_state::curline, StringInfoData::data, tsearch_readline_state::fp, StringInfoData::len, tsearch_readline_state::lineno, pfree(), pg_any_to_server(), pg_get_line_buf(), PG_UTF8, and pstrdup().

Referenced by dsynonym_init(), initTrie(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), read_dictionary(), readstoplist(), and thesaurusRead().

◆ tsearch_readline_begin()

bool tsearch_readline_begin ( tsearch_readline_state stp,
const char *  filename 
)

Definition at line 136 of file ts_locale.c.

138 {
139  if ((stp->fp = AllocateFile(filename, "r")) == NULL)
140  return false;
141  stp->filename = filename;
142  stp->lineno = 0;
143  initStringInfo(&stp->buf);
144  stp->curline = NULL;
145  /* Setup error traceback support for ereport() */
147  stp->cb.arg = (void *) stp;
149  error_context_stack = &stp->cb;
150  return true;
151 }
ErrorContextCallback * error_context_stack
Definition: elog.c:95
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2553
static char * filename
Definition: pg_dumpall.c:121
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
ErrorContextCallback cb
Definition: ts_locale.h:32
const char * filename
Definition: ts_locale.h:27
static void tsearch_readline_callback(void *arg)
Definition: ts_locale.c:227

References AllocateFile(), ErrorContextCallback::arg, tsearch_readline_state::buf, ErrorContextCallback::callback, tsearch_readline_state::cb, tsearch_readline_state::curline, error_context_stack, filename, tsearch_readline_state::filename, tsearch_readline_state::fp, initStringInfo(), tsearch_readline_state::lineno, ErrorContextCallback::previous, and tsearch_readline_callback().

Referenced by dsynonym_init(), initTrie(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), read_dictionary(), readstoplist(), and thesaurusRead().

◆ tsearch_readline_callback()

static void tsearch_readline_callback ( void *  arg)
static

Definition at line 227 of file ts_locale.c.

228 {
230 
231  /*
232  * We can't include the text of the config line for errors that occur
233  * during tsearch_readline() itself. The major cause of such errors is
234  * encoding violations, and we daren't try to print error messages
235  * containing badly-encoded data.
236  */
237  if (stp->curline)
238  errcontext("line %d of configuration file \"%s\": \"%s\"",
239  stp->lineno,
240  stp->filename,
241  stp->curline);
242  else
243  errcontext("line %d of configuration file \"%s\"",
244  stp->lineno,
245  stp->filename);
246 }
#define errcontext
Definition: elog.h:196
void * arg

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

Referenced by tsearch_readline_begin().

◆ tsearch_readline_end()

void tsearch_readline_end ( tsearch_readline_state stp)

Definition at line 204 of file ts_locale.c.

205 {
206  /* Suppress use of curline in any error reported below */
207  if (stp->curline)
208  {
209  if (stp->curline != stp->buf.data)
210  pfree(stp->curline);
211  stp->curline = NULL;
212  }
213 
214  /* Release other resources */
215  pfree(stp->buf.data);
216  FreeFile(stp->fp);
217 
218  /* Pop the error context stack */
220 }
int FreeFile(FILE *file)
Definition: fd.c:2751

References tsearch_readline_state::buf, tsearch_readline_state::cb, tsearch_readline_state::curline, StringInfoData::data, error_context_stack, tsearch_readline_state::fp, FreeFile(), pfree(), and ErrorContextCallback::previous.

Referenced by dsynonym_init(), initTrie(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), read_dictionary(), readstoplist(), and thesaurusRead().