PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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)

Definition at line 231 of file ts_locale.c.

References lowerstr_with_len().

Referenced by convertPgWchar(), dispell_init(), dsimple_init(), dsnowball_init(), dsynonym_init(), dxsyn_lexize(), lowerstr_ctx(), NIImportAffixes(), and read_dictionary().

232 {
233  return lowerstr_with_len(str, strlen(str));
234 }
char * lowerstr_with_len(const char *str, int len)
Definition: ts_locale.c:244
char* lowerstr_with_len ( const char *  str,
int  len 
)

Definition at line 244 of file ts_locale.c.

References Assert, DEFAULT_COLLATION_OID, 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_only(), generate_wildcard_trgm(), and lowerstr().

245 {
246  char *out;
247 
248 #ifdef USE_WIDE_UPPER_LOWER
249  Oid collation = DEFAULT_COLLATION_OID; /* TODO */
250  pg_locale_t mylocale = 0; /* TODO */
251 #endif
252 
253  if (len == 0)
254  return pstrdup("");
255 
256 #ifdef USE_WIDE_UPPER_LOWER
257 
258  /*
259  * Use wide char code only when max encoding length > 1 and ctype != C.
260  * Some operating systems fail with multi-byte encodings and a C locale.
261  * Also, for a C locale there is no need to process as multibyte. From
262  * backend/utils/adt/oracle_compat.c Teodor
263  */
264  if (pg_database_encoding_max_length() > 1 && !lc_ctype_is_c(collation))
265  {
266  wchar_t *wstr,
267  *wptr;
268  int wlen;
269 
270  /*
271  * alloc number of wchar_t for worst case, len contains number of
272  * bytes >= number of characters and alloc 1 wchar_t for 0, because
273  * wchar2char wants zero-terminated string
274  */
275  wptr = wstr = (wchar_t *) palloc(sizeof(wchar_t) * (len + 1));
276 
277  wlen = char2wchar(wstr, len + 1, str, len, mylocale);
278  Assert(wlen <= len);
279 
280  while (*wptr)
281  {
282  *wptr = towlower((wint_t) *wptr);
283  wptr++;
284  }
285 
286  /*
287  * Alloc result string for worst case + '\0'
288  */
289  len = pg_database_encoding_max_length() * wlen + 1;
290  out = (char *) palloc(len);
291 
292  wlen = wchar2char(out, wstr, len, mylocale);
293 
294  pfree(wstr);
295 
296  if (wlen < 0)
297  ereport(ERROR,
298  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
299  errmsg("conversion from wchar_t to server encoding failed: %m")));
300  Assert(wlen < len);
301  }
302  else
303 #endif /* USE_WIDE_UPPER_LOWER */
304  {
305  const char *ptr = str;
306  char *outptr;
307 
308  outptr = out = (char *) palloc(sizeof(char) * (len + 1));
309  while ((ptr - str) < len && *ptr)
310  {
311  *outptr++ = tolower(TOUCHAR(ptr));
312  ptr++;
313  }
314  *outptr = '\0';
315  }
316 
317  return out;
318 }
char * pstrdup(const char *in)
Definition: mcxt.c:1077
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
int pg_database_encoding_max_length(void)
Definition: wchar.c:1833
#define ereport(elevel, rest)
Definition: elog.h:122
#define Assert(condition)
Definition: c.h:675
#define TOUCHAR(x)
Definition: ts_locale.h:42
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1178
char* t_readline ( FILE *  fp)

Definition at line 197 of file ts_locale.c.

References buf, NULL, pg_any_to_server(), PG_UTF8, pg_verify_mbstr(), and pnstrdup().

Referenced by tsearch_readline().

198 {
199  int len;
200  char *recoded;
201  char buf[4096]; /* lines must not be longer than this */
202 
203  if (fgets(buf, sizeof(buf), fp) == NULL)
204  return NULL;
205 
206  len = strlen(buf);
207 
208  /* Make sure the input is valid UTF-8 */
209  (void) pg_verify_mbstr(PG_UTF8, buf, len, false);
210 
211  /* And convert */
212  recoded = pg_any_to_server(buf, len, PG_UTF8);
213  if (recoded == buf)
214  {
215  /*
216  * conversion didn't pstrdup, so we must. We can use the length of the
217  * original string, because no conversion was done.
218  */
219  recoded = pnstrdup(recoded, len);
220  }
221 
222  return recoded;
223 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
static char * buf
Definition: pg_test_fsync.c:65
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: wchar.c:1877
#define NULL
Definition: c.h:229
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:572
char* tsearch_readline ( tsearch_readline_state stp)

Definition at line 138 of file ts_locale.c.

References tsearch_readline_state::curline, tsearch_readline_state::fp, tsearch_readline_state::lineno, NULL, result, and t_readline().

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

139 {
140  char *result;
141 
142  stp->lineno++;
143  stp->curline = NULL;
144  result = t_readline(stp->fp);
145  stp->curline = result;
146  return result;
147 }
return result
Definition: formatting.c:1618
char * t_readline(FILE *fp)
Definition: ts_locale.c:197
#define NULL
Definition: c.h:229
bool tsearch_readline_begin ( tsearch_readline_state stp,
const char *  filename 
)

Definition at line 116 of file ts_locale.c.

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

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

118 {
119  if ((stp->fp = AllocateFile(filename, "r")) == NULL)
120  return false;
121  stp->filename = filename;
122  stp->lineno = 0;
123  stp->curline = NULL;
124  /* Setup error traceback support for ereport() */
126  stp->cb.arg = (void *) stp;
128  error_context_stack = &stp->cb;
129  return true;
130 }
static void tsearch_readline_callback(void *arg)
Definition: ts_locale.c:165
struct ErrorContextCallback * previous
Definition: elog.h:238
ErrorContextCallback * error_context_stack
Definition: elog.c:88
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2057
ErrorContextCallback cb
Definition: ts_locale.h:39
const char * filename
Definition: ts_locale.h:36
#define NULL
Definition: c.h:229
static char * filename
Definition: pg_dumpall.c:87
void(* callback)(void *arg)
Definition: elog.h:239
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.

Referenced by tsearch_readline_begin().

166 {
168 
169  /*
170  * We can't include the text of the config line for errors that occur
171  * during t_readline() itself. This is only partly a consequence of our
172  * arms-length use of that routine: the major cause of such errors is
173  * encoding violations, and we daren't try to print error messages
174  * containing badly-encoded data.
175  */
176  if (stp->curline)
177  errcontext("line %d of configuration file \"%s\": \"%s\"",
178  stp->lineno,
179  stp->filename,
180  stp->curline);
181  else
182  errcontext("line %d of configuration file \"%s\"",
183  stp->lineno,
184  stp->filename);
185 }
const char * filename
Definition: ts_locale.h:36
#define errcontext
Definition: elog.h:164
void * arg
void tsearch_readline_end ( tsearch_readline_state stp)

Definition at line 153 of file ts_locale.c.

References tsearch_readline_state::cb, error_context_stack, tsearch_readline_state::fp, FreeFile(), and ErrorContextCallback::previous.

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

154 {
155  FreeFile(stp->fp);
156  /* Pop the error context stack */
158 }
struct ErrorContextCallback * previous
Definition: elog.h:238
ErrorContextCallback * error_context_stack
Definition: elog.c:88
ErrorContextCallback cb
Definition: ts_locale.h:39
int FreeFile(FILE *file)
Definition: fd.c:2240