PostgreSQL Source Code git master
Loading...
Searching...
No Matches
ts_locale.c File Reference
#include "postgres.h"
#include "common/string.h"
#include "storage/fd.h"
#include "tsearch/ts_locale.h"
Include dependency graph for ts_locale.c:

Go to the source code of this file.

Macros

#define WC_BUF_LEN   2
 
#define GENERATE_T_ISCLASS_DEF(character_class)
 

Functions

static void tsearch_readline_callback (void *arg)
 
bool tsearch_readline_begin (tsearch_readline_state *stp, const char *filename)
 
chartsearch_readline (tsearch_readline_state *stp)
 
void tsearch_readline_end (tsearch_readline_state *stp)
 

Macro Definition Documentation

◆ GENERATE_T_ISCLASS_DEF

#define GENERATE_T_ISCLASS_DEF (   character_class)
Value:
/* mblen shall be that of the first character */ \
t_is##character_class##_with_len(const char *ptr, int mblen) \
{ \
int wlen pg_attribute_unused(); \
wlen = pg_mb2wchar_with_len(ptr, wstr, mblen); \
Assert(wlen <= 1); \
/* pass single character, or NUL if empty */ \
} \
\
/* ptr shall point to a NUL-terminated string */ \
t_is##character_class##_cstr(const char *ptr) \
{ \
} \
/* ptr shall point to a string with pre-validated encoding */ \
t_is##character_class##_unbounded(const char *ptr) \
{ \
} \
/* historical name for _unbounded */ \
t_is##character_class(const char *ptr) \
{ \
return t_is##character_class##_unbounded(ptr); \
}
#define pg_attribute_unused()
Definition c.h:132
unsigned int pg_wchar
Definition mbprint.c:31
int pg_mblen_cstr(const char *mbstr)
Definition mbutils.c:1045
int pg_mblen_unbounded(const char *mbstr)
Definition mbutils.c:1137
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition mbutils.c:997
pg_locale_t pg_database_locale(void)
Definition pg_locale.c:1175
static int fb(int x)
#define WC_BUF_LEN
Definition ts_locale.c:24

Definition at line 26 of file ts_locale.c.

30{ \
32 int wlen pg_attribute_unused(); \
33 wlen = pg_mb2wchar_with_len(ptr, wstr, mblen); \
34 Assert(wlen <= 1); \
35 /* pass single character, or NUL if empty */ \
37} \
38\
39/* ptr shall point to a NUL-terminated string */ \
41t_is##character_class##_cstr(const char *ptr) \
42{ \
43 return t_is##character_class##_with_len(ptr, pg_mblen_cstr(ptr)); \
44} \
45/* ptr shall point to a string with pre-validated encoding */ \
47t_is##character_class##_unbounded(const char *ptr) \
48{ \
50} \
51/* historical name for _unbounded */ \
53t_is##character_class(const char *ptr) \
54{ \
55 return t_is##character_class##_unbounded(ptr); \
56}

◆ WC_BUF_LEN

#define WC_BUF_LEN   2

Definition at line 24 of file ts_locale.c.

Function Documentation

◆ tsearch_readline()

char * tsearch_readline ( tsearch_readline_state stp)

Definition at line 108 of file ts_locale.c.

109{
110 char *recoded;
111
112 /* Advance line number to use in error reports */
113 stp->lineno++;
114
115 /* Clear curline, it's no longer relevant */
116 if (stp->curline)
117 {
118 if (stp->curline != stp->buf.data)
119 pfree(stp->curline);
120 stp->curline = NULL;
121 }
122
123 /* Collect next line, if there is one */
124 if (!pg_get_line_buf(stp->fp, &stp->buf))
125 return NULL;
126
127 /* Validate the input as UTF-8, then convert to DB encoding if needed */
128 recoded = pg_any_to_server(stp->buf.data, stp->buf.len, PG_UTF8);
129
130 /* Save the correctly-encoded string for possible error reports */
131 stp->curline = recoded; /* might be equal to buf.data */
132
133 /*
134 * We always return a freshly pstrdup'd string. This is clearly necessary
135 * if pg_any_to_server() returned buf.data, and we need a second copy even
136 * if encoding conversion did occur. The caller is entitled to pfree the
137 * returned string at any time, which would leave curline pointing to
138 * recycled storage, causing problems if an error occurs after that point.
139 * (It's preferable to return the result of pstrdup instead of the output
140 * of pg_any_to_server, because the conversion result tends to be
141 * over-allocated. Since callers might save the result string directly
142 * into a long-lived dictionary structure, we don't want it to be a larger
143 * palloc chunk than necessary. We'll reclaim the conversion result on
144 * the next call.)
145 */
146 return pstrdup(recoded);
147}
#define PG_UTF8
Definition mbprint.c:43
char * pg_any_to_server(const char *s, int len, int encoding)
Definition mbutils.c:687
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition pg_get_line.c:95

References fb(), 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 85 of file ts_locale.c.

87{
88 if ((stp->fp = AllocateFile(filename, "r")) == NULL)
89 return false;
90 stp->filename = filename;
91 stp->lineno = 0;
92 initStringInfo(&stp->buf);
93 stp->curline = NULL;
94 /* Setup error traceback support for ereport() */
95 stp->cb.callback = tsearch_readline_callback;
96 stp->cb.arg = stp;
97 stp->cb.previous = error_context_stack;
99 return true;
100}
ErrorContextCallback * error_context_stack
Definition elog.c:99
FILE * AllocateFile(const char *name, const char *mode)
Definition fd.c:2627
static char * filename
Definition pg_dumpall.c:132
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
static void tsearch_readline_callback(void *arg)
Definition ts_locale.c:176

References AllocateFile(), error_context_stack, fb(), filename, initStringInfo(), 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 176 of file ts_locale.c.

177{
179
180 /*
181 * We can't include the text of the config line for errors that occur
182 * during tsearch_readline() itself. The major cause of such errors is
183 * encoding violations, and we daren't try to print error messages
184 * containing badly-encoded data.
185 */
186 if (stp->curline)
187 errcontext("line %d of configuration file \"%s\": \"%s\"",
188 stp->lineno,
189 stp->filename,
190 stp->curline);
191 else
192 errcontext("line %d of configuration file \"%s\"",
193 stp->lineno,
194 stp->filename);
195}
Datum arg
Definition elog.c:1322
#define errcontext
Definition elog.h:198

References arg, errcontext, and fb().

Referenced by tsearch_readline_begin().

◆ tsearch_readline_end()

void tsearch_readline_end ( tsearch_readline_state stp)

Definition at line 153 of file ts_locale.c.

154{
155 /* Suppress use of curline in any error reported below */
156 if (stp->curline)
157 {
158 if (stp->curline != stp->buf.data)
159 pfree(stp->curline);
160 stp->curline = NULL;
161 }
162
163 /* Release other resources */
164 pfree(stp->buf.data);
165 FreeFile(stp->fp);
166
167 /* Pop the error context stack */
169}
int FreeFile(FILE *file)
Definition fd.c:2826
struct ErrorContextCallback * previous
Definition elog.h:297

References error_context_stack, fb(), FreeFile(), pfree(), and ErrorContextCallback::previous.

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