1998-07-24 05:32:46 +02:00
|
|
|
/*
|
|
|
|
* This file contains some public functions
|
|
|
|
* usable for both the backend and the frontend.
|
|
|
|
* Tatsuo Ishii
|
2000-01-18 06:14:24 +01:00
|
|
|
* $Id: common.c,v 1.8 2000/01/18 05:14:24 ishii Exp $ */
|
1998-10-06 05:02:29 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
1999-07-19 04:27:16 +02:00
|
|
|
#include <string.h>
|
1998-10-06 05:02:29 +02:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include "win32.h"
|
1999-07-19 08:25:40 +02:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
1998-10-06 05:02:29 +02:00
|
|
|
#endif
|
1998-07-24 05:32:46 +02:00
|
|
|
|
2000-01-18 06:14:24 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
#include "miscadmin.h"
|
1998-07-24 05:32:46 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert encoding char to encoding symbol value.
|
|
|
|
* case is ignored.
|
|
|
|
* if there's no valid encoding, returns -1
|
|
|
|
*/
|
1998-09-01 06:40:42 +02:00
|
|
|
int
|
|
|
|
pg_char_to_encoding(const char *s)
|
1998-07-24 05:32:46 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
pg_encoding_conv_tbl *p = pg_conv_tbl;
|
1998-07-24 05:32:46 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!s)
|
1999-05-13 12:28:26 +02:00
|
|
|
return (-1);
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
for (; p->encoding >= 0; p++)
|
|
|
|
{
|
|
|
|
if (!strcasecmp(s, p->name))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (p->encoding);
|
1998-07-24 05:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check to see if encoding name is valid
|
|
|
|
*/
|
1998-09-01 06:40:42 +02:00
|
|
|
int
|
|
|
|
pg_valid_client_encoding(const char *name)
|
1998-07-24 05:32:46 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
return (pg_char_to_encoding(name));
|
1998-07-24 05:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find encoding table entry by encoding
|
|
|
|
*/
|
1998-09-01 06:40:42 +02:00
|
|
|
pg_encoding_conv_tbl *
|
|
|
|
pg_get_encent_by_encoding(int encoding)
|
1998-07-24 05:32:46 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
pg_encoding_conv_tbl *p = pg_conv_tbl;
|
|
|
|
|
|
|
|
for (; p->encoding >= 0; p++)
|
|
|
|
{
|
|
|
|
if (p->encoding == encoding)
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
return (0);
|
1998-07-24 05:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert encoding symbol to encoding char.
|
|
|
|
* if there's no valid encoding symbol, returns ""
|
|
|
|
*/
|
1998-09-01 06:40:42 +02:00
|
|
|
const char *
|
|
|
|
pg_encoding_to_char(int encoding)
|
1998-07-24 05:32:46 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
pg_encoding_conv_tbl *p = pg_get_encent_by_encoding(encoding);
|
1998-07-24 05:32:46 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
if (!p)
|
|
|
|
return ("");
|
|
|
|
return (p->name);
|
1998-07-24 05:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns the byte length of a multi-byte word for an encoding */
|
1998-09-01 06:40:42 +02:00
|
|
|
int
|
|
|
|
pg_encoding_mblen(int encoding, const unsigned char *mbstr)
|
1998-07-24 05:32:46 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
return ((*pg_wchar_table[encoding].mblen) (mbstr));
|
1998-07-24 05:32:46 +02:00
|
|
|
}
|