* client encoding and server internal encoding.
* (currently mule internal code (mic) is used)
* Tatsuo Ishii
- * $Id: mbutils.c,v 1.34 2002/09/03 21:45:43 petere Exp $
+ * $Id: mbutils.c,v 1.35 2002/09/04 20:31:31 momjian Exp $
*/
#include "postgres.h"
#include "access/xact.h"
* allocated in TopMemoryContext so that it survives outside
* transactions. See SetClientEncoding() for more details.
*/
-static FmgrInfo *ToServerConvPorc = NULL;
-static FmgrInfo *ToClientConvPorc = NULL;
+static FmgrInfo *ToServerConvPorc = NULL;
+static FmgrInfo *ToClientConvPorc = NULL;
/* Internal functions */
static unsigned char *
-perform_default_encoding_conversion(unsigned char *src, int len, bool is_client_to_server);
+ perform_default_encoding_conversion(unsigned char *src, int len, bool is_client_to_server);
static int
-cliplen(const unsigned char *str, int len, int limit);
+ cliplen(const unsigned char *str, int len, int limit);
/*
* Set the client encoding and save fmgrinfo for the converion
SetClientEncoding(int encoding, bool doit)
{
int current_server_encoding;
- Oid to_server_proc, to_client_proc;
- FmgrInfo *to_server = NULL;
- FmgrInfo *to_client = NULL;
+ Oid to_server_proc,
+ to_client_proc;
+ FmgrInfo *to_server = NULL;
+ FmgrInfo *to_client = NULL;
MemoryContext oldcontext;
current_server_encoding = GetDatabaseEncoding();
return (-1);
if (current_server_encoding == encoding ||
- (current_server_encoding == PG_SQL_ASCII || encoding == PG_SQL_ASCII))
+ (current_server_encoding == PG_SQL_ASCII || encoding == PG_SQL_ASCII))
{
ClientEncoding = &pg_enc2name_tbl[encoding];
return 0;
}
- /* XXX We cannot use FindDefaultConversionProc() while in
- * bootstrap or initprocessing mode since namespace functions will
- * not work.
+ /*
+ * XXX We cannot use FindDefaultConversionProc() while in bootstrap or
+ * initprocessing mode since namespace functions will not work.
*/
if (IsTransactionState())
{
return -1;
/*
- * load the fmgr info into TopMemoryContext so that it
- * survives outside transaction.
+ * load the fmgr info into TopMemoryContext so that it survives
+ * outside transaction.
*/
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
to_server = palloc(sizeof(FmgrInfo));
{
ClientEncoding = &pg_enc2name_tbl[encoding];
- if(ToServerConvPorc != NULL)
+ if (ToServerConvPorc != NULL)
{
if (ToServerConvPorc->fn_extra)
pfree(ToServerConvPorc->fn_extra);
}
ToServerConvPorc = to_server;
- if(ToClientConvPorc != NULL)
+ if (ToClientConvPorc != NULL)
{
if (ToClientConvPorc->fn_extra)
pfree(ToClientConvPorc->fn_extra);
int src_encoding, int dest_encoding)
{
unsigned char *result;
- Oid proc;
+ Oid proc;
if (!IsTransactionState())
return src;
-
+
if (src_encoding == dest_encoding)
return src;
return src;
}
- /* XXX we shoud avoid throwing errors in OidFuctionCall. Otherwise
- * we are going into inifinite loop! So we have to make sure that
- * the function exists before calling OidFunctionCall.
+ /*
+ * XXX we shoud avoid throwing errors in OidFuctionCall. Otherwise we
+ * are going into inifinite loop! So we have to make sure that the
+ * function exists before calling OidFunctionCall.
*/
if (!SearchSysCacheExists(PROCOID,
- ObjectIdGetDatum(proc),
- 0, 0, 0))
+ ObjectIdGetDatum(proc),
+ 0, 0, 0))
{
elog(LOG, "default conversion proc %u for %s to %s not found in pg_proc",
proc,
Datum
pg_convert(PG_FUNCTION_ARGS)
{
- Datum string = PG_GETARG_DATUM(0);
- Datum dest_encoding_name = PG_GETARG_DATUM(1);
- Datum src_encoding_name = DirectFunctionCall1(
- namein, CStringGetDatum(DatabaseEncoding->name));
- Datum result;
+ Datum string = PG_GETARG_DATUM(0);
+ Datum dest_encoding_name = PG_GETARG_DATUM(1);
+ Datum src_encoding_name = DirectFunctionCall1(
+ namein, CStringGetDatum(DatabaseEncoding->name));
+ Datum result;
result = DirectFunctionCall3(
- pg_convert2, string, src_encoding_name, dest_encoding_name);
+ pg_convert2, string, src_encoding_name, dest_encoding_name);
/* free memory allocated by namein */
- pfree((void *)src_encoding_name);
+ pfree((void *) src_encoding_name);
PG_RETURN_TEXT_P(result);
}
unsigned char *result;
text *retval;
unsigned char *str;
- int len;
+ int len;
if (src_encoding < 0)
elog(ERROR, "Invalid source encoding name %s", src_encoding_name);
if (result == NULL)
elog(ERROR, "Encoding conversion failed");
- /* build text data type structre. we cannot use textin() here,
- since textin assumes that input string encoding is same as
- database encoding. */
+ /*
+ * build text data type structre. we cannot use textin() here, since
+ * textin assumes that input string encoding is same as database
+ * encoding.
+ */
len = strlen(result) + VARHDRSZ;
retval = palloc(len);
VARATT_SIZEP(retval) = len;
if (result != str)
pfree(result);
- pfree(str);
+ pfree(str);
/* free memory if allocated by the toaster */
PG_FREE_IF_COPY(string, 0);
}
/*
- * Perform default encoding conversion using cached FmgrInfo. Since
- * this function does not access database at all, it is safe to call
- * outside transactions. Explicit setting client encoding required
- * before calling this function. Otherwise no conversion is
- * performed.
+ * Perform default encoding conversion using cached FmgrInfo. Since
+ * this function does not access database at all, it is safe to call
+ * outside transactions. Explicit setting client encoding required
+ * before calling this function. Otherwise no conversion is
+ * performed.
*/
static unsigned char *
perform_default_encoding_conversion(unsigned char *src, int len, bool is_client_to_server)
{
unsigned char *result;
- int src_encoding, dest_encoding;
- FmgrInfo *flinfo;
+ int src_encoding,
+ dest_encoding;
+ FmgrInfo *flinfo;
if (is_client_to_server)
{
/* optimization for single byte encoding */
if (pg_database_encoding_max_length() == 1)
- return strlen((char *)mbstr);
+ return strlen((char *) mbstr);
while (*mbstr)
{
}
static int
-cliplen(const unsigned char *str, int len, int limit)
+cliplen(const unsigned char *str, int len, int limit)
{
- int l = 0;
+ int l = 0;
const unsigned char *s;
for (s = str; *s; s++, l++)