PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
encnames.c File Reference
#include "postgres.h"
#include "utils/builtins.h"
#include <ctype.h>
#include <unistd.h>
#include "mb/pg_wchar.h"
Include dependency graph for encnames.c:

Go to the source code of this file.

Data Structures

struct  pg_encname
 

Macros

#define DEF_ENC2NAME(name, codepage)   { #name, PG_##name }
 

Typedefs

typedef struct pg_encname pg_encname
 

Functions

bool is_encoding_supported_by_icu (int encoding)
 
const char * get_encoding_name_for_icu (int encoding)
 
int pg_valid_client_encoding (const char *name)
 
int pg_valid_server_encoding (const char *name)
 
int pg_valid_server_encoding_id (int encoding)
 
static char * clean_encoding_name (const char *key, char *newkey)
 
int pg_char_to_encoding (const char *name)
 
Datum PG_char_to_encoding (PG_FUNCTION_ARGS)
 
const char * pg_encoding_to_char (int encoding)
 
Datum PG_encoding_to_char (PG_FUNCTION_ARGS)
 

Variables

static const pg_encname pg_encname_tbl []
 
const pg_enc2name pg_enc2name_tbl []
 
const pg_enc2gettext pg_enc2gettext_tbl []
 
static const char *const pg_enc2icu_tbl []
 

Macro Definition Documentation

#define DEF_ENC2NAME (   name,
  codepage 
)    { #name, PG_##name }

Definition at line 303 of file encnames.c.

Typedef Documentation

Function Documentation

static char* clean_encoding_name ( const char *  key,
char *  newkey 
)
static

Definition at line 525 of file encnames.c.

Referenced by pg_char_to_encoding().

526 {
527  const char *p;
528  char *np;
529 
530  for (p = key, np = newkey; *p != '\0'; p++)
531  {
532  if (isalnum((unsigned char) *p))
533  {
534  if (*p >= 'A' && *p <= 'Z')
535  *np++ = *p + 'a' - 'A';
536  else
537  *np++ = *p;
538  }
539  }
540  *np = '\0';
541  return newkey;
542 }
const char* get_encoding_name_for_icu ( int  encoding)

Definition at line 461 of file encnames.c.

References encoding, ereport, errcode(), errmsg(), ERROR, lengthof, pg_enc2icu_tbl, PG_ENCODING_BE_LAST, pg_encoding_to_char(), and StaticAssertStmt.

462 {
463  const char *icu_encoding_name;
464 
466  "pg_enc2icu_tbl incomplete");
467 
468  icu_encoding_name = pg_enc2icu_tbl[encoding];
469 
470  if (!icu_encoding_name)
471  ereport(ERROR,
472  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
473  errmsg("encoding \"%s\" not supported by ICU",
475 
476  return icu_encoding_name;
477 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define lengthof(array)
Definition: c.h:562
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:757
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_ENCODING_BE_LAST
Definition: pg_wchar.h:289
static const char *const pg_enc2icu_tbl[]
Definition: encnames.c:415
static char * encoding
Definition: initdb.c:122
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool is_encoding_supported_by_icu ( int  encoding)

Definition at line 455 of file encnames.c.

References NULL, and pg_enc2icu_tbl.

Referenced by lookup_collation().

456 {
457  return (pg_enc2icu_tbl[encoding] != NULL);
458 }
static const char *const pg_enc2icu_tbl[]
Definition: encnames.c:415
static char * encoding
Definition: initdb.c:122
#define NULL
Definition: c.h:229
int pg_char_to_encoding ( const char *  name)

Definition at line 551 of file encnames.c.

References clean_encoding_name(), ereport, errcode(), errmsg(), ERROR, lengthof, NAMEDATALEN, NULL, pg_encname_tbl, and result.

Referenced by CreateConversionCommand(), length_in_encoding(), main(), PG_char_to_encoding(), pg_convert(), pg_get_utf8_id(), pg_valid_client_encoding(), pg_valid_server_encoding(), PQenv2encoding(), pqSaveParameterStatus(), ProcessCopyOptions(), processEncodingEntry(), and to_ascii_encname().

552 {
553  unsigned int nel = lengthof(pg_encname_tbl);
554  const pg_encname *base = pg_encname_tbl,
555  *last = base + nel - 1,
556  *position;
557  int result;
558  char buff[NAMEDATALEN],
559  *key;
560 
561  if (name == NULL || *name == '\0')
562  return -1;
563 
564  if (strlen(name) >= NAMEDATALEN)
565  {
566 #ifdef FRONTEND
567  fprintf(stderr, "encoding name too long\n");
568  return -1;
569 #else
570  ereport(ERROR,
571  (errcode(ERRCODE_NAME_TOO_LONG),
572  errmsg("encoding name too long")));
573 #endif
574  }
575  key = clean_encoding_name(name, buff);
576 
577  while (last >= base)
578  {
579  position = base + ((last - base) >> 1);
580  result = key[0] - position->name[0];
581 
582  if (result == 0)
583  {
584  result = strcmp(key, position->name);
585  if (result == 0)
586  return position->encoding;
587  }
588  if (result < 0)
589  last = position - 1;
590  else
591  base = position + 1;
592  }
593  return -1;
594 }
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
#define lengthof(array)
Definition: c.h:562
#define NAMEDATALEN
#define ERROR
Definition: elog.h:43
static const pg_encname pg_encname_tbl[]
Definition: encnames.c:38
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
static char * clean_encoding_name(const char *key, char *newkey)
Definition: encnames.c:525
Datum PG_char_to_encoding ( PG_FUNCTION_ARGS  )

Definition at line 598 of file encnames.c.

References NameStr, pg_char_to_encoding(), PG_GETARG_NAME, and PG_RETURN_INT32.

599 {
600  Name s = PG_GETARG_NAME(0);
601 
603 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
Definition: c.h:493
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
const char* pg_encoding_to_char ( int  encoding)
Datum PG_encoding_to_char ( PG_FUNCTION_ARGS  )

Definition at line 621 of file encnames.c.

References CStringGetDatum, DirectFunctionCall1, encoding, namein(), pg_encoding_to_char(), and PG_GETARG_INT32.

622 {
624  const char *encoding_name = pg_encoding_to_char(encoding);
625 
626  return DirectFunctionCall1(namein, CStringGetDatum(encoding_name));
627 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
signed int int32
Definition: c.h:256
#define CStringGetDatum(X)
Definition: postgres.h:584
static char * encoding
Definition: initdb.c:122
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
int pg_valid_client_encoding ( const char *  name)

Definition at line 487 of file encnames.c.

References enc, pg_char_to_encoding(), and PG_VALID_FE_ENCODING.

Referenced by check_client_encoding().

488 {
489  int enc;
490 
491  if ((enc = pg_char_to_encoding(name)) < 0)
492  return -1;
493 
494  if (!PG_VALID_FE_ENCODING(enc))
495  return -1;
496 
497  return enc;
498 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
struct pg_encoding enc
Definition: encode.c:522
#define PG_VALID_FE_ENCODING(_enc)
Definition: pg_wchar.h:305
const char * name
Definition: encode.c:521
int pg_valid_server_encoding ( const char *  name)

Definition at line 501 of file encnames.c.

References enc, pg_char_to_encoding(), and PG_VALID_BE_ENCODING.

Referenced by createdb(), get_encoding_id(), and parse_extension_control_file().

502 {
503  int enc;
504 
505  if ((enc = pg_char_to_encoding(name)) < 0)
506  return -1;
507 
508  if (!PG_VALID_BE_ENCODING(enc))
509  return -1;
510 
511  return enc;
512 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
struct pg_encoding enc
Definition: encode.c:522
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:295
const char * name
Definition: encode.c:521
int pg_valid_server_encoding_id ( int  encoding)

Definition at line 515 of file encnames.c.

References PG_VALID_BE_ENCODING.

Referenced by setup_locale_encoding().

516 {
518 }
static char * encoding
Definition: initdb.c:122
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:295

Variable Documentation

const pg_enc2gettext pg_enc2gettext_tbl[]

Definition at line 359 of file encnames.c.

const char* const pg_enc2icu_tbl[]
static

Definition at line 415 of file encnames.c.

Referenced by get_encoding_name_for_icu(), and is_encoding_supported_by_icu().

const pg_encname pg_encname_tbl[]
static

Definition at line 38 of file encnames.c.

Referenced by pg_char_to_encoding().