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

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 []
 

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 449 of file encnames.c.

Referenced by pg_char_to_encoding().

450 {
451  const char *p;
452  char *np;
453 
454  for (p = key, np = newkey; *p != '\0'; p++)
455  {
456  if (isalnum((unsigned char) *p))
457  {
458  if (*p >= 'A' && *p <= 'Z')
459  *np++ = *p + 'a' - 'A';
460  else
461  *np++ = *p;
462  }
463  }
464  *np = '\0';
465  return newkey;
466 }
int pg_char_to_encoding ( const char *  name)

Definition at line 475 of file encnames.c.

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

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().

476 {
477  unsigned int nel = lengthof(pg_encname_tbl);
478  const pg_encname *base = pg_encname_tbl,
479  *last = base + nel - 1,
480  *position;
481  int result;
482  char buff[NAMEDATALEN],
483  *key;
484 
485  if (name == NULL || *name == '\0')
486  return -1;
487 
488  if (strlen(name) >= NAMEDATALEN)
489  {
490 #ifdef FRONTEND
491  fprintf(stderr, "encoding name too long\n");
492  return -1;
493 #else
494  ereport(ERROR,
495  (errcode(ERRCODE_NAME_TOO_LONG),
496  errmsg("encoding name too long")));
497 #endif
498  }
499  key = clean_encoding_name(name, buff);
500 
501  while (last >= base)
502  {
503  position = base + ((last - base) >> 1);
504  result = key[0] - position->name[0];
505 
506  if (result == 0)
507  {
508  result = strcmp(key, position->name);
509  if (result == 0)
510  return position->encoding;
511  }
512  if (result < 0)
513  last = position - 1;
514  else
515  base = position + 1;
516  }
517  return -1;
518 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define lengthof(array)
Definition: c.h:557
#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:226
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:449
Datum PG_char_to_encoding ( PG_FUNCTION_ARGS  )

Definition at line 522 of file encnames.c.

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

523 {
524  Name s = PG_GETARG_NAME(0);
525 
527 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:475
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
Definition: c.h:488
#define NameStr(name)
Definition: c.h:494
#define PG_GETARG_NAME(n)
Definition: fmgr.h:234
const char* pg_encoding_to_char ( int  encoding)
Datum PG_encoding_to_char ( PG_FUNCTION_ARGS  )

Definition at line 545 of file encnames.c.

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

546 {
548  const char *encoding_name = pg_encoding_to_char(encoding);
549 
550  return DirectFunctionCall1(namein, CStringGetDatum(encoding_name));
551 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
signed int int32
Definition: c.h:253
#define CStringGetDatum(X)
Definition: postgres.h:586
static char * encoding
Definition: initdb.c:121
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:531
int pg_valid_client_encoding ( const char *  name)

Definition at line 411 of file encnames.c.

References enc, pg_char_to_encoding(), and PG_VALID_FE_ENCODING.

Referenced by check_client_encoding().

412 {
413  int enc;
414 
415  if ((enc = pg_char_to_encoding(name)) < 0)
416  return -1;
417 
418  if (!PG_VALID_FE_ENCODING(enc))
419  return -1;
420 
421  return enc;
422 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:475
struct pg_encoding enc
Definition: encode.c:522
#define PG_VALID_FE_ENCODING(_enc)
Definition: pg_wchar.h:303
const char * name
Definition: encode.c:521
int pg_valid_server_encoding ( const char *  name)

Definition at line 425 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().

426 {
427  int enc;
428 
429  if ((enc = pg_char_to_encoding(name)) < 0)
430  return -1;
431 
432  if (!PG_VALID_BE_ENCODING(enc))
433  return -1;
434 
435  return enc;
436 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:475
struct pg_encoding enc
Definition: encode.c:522
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:293
const char * name
Definition: encode.c:521
int pg_valid_server_encoding_id ( int  encoding)

Definition at line 439 of file encnames.c.

References PG_VALID_BE_ENCODING.

Referenced by setup_locale_encoding().

440 {
442 }
static char * encoding
Definition: initdb.c:121
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:293

Variable Documentation

const pg_enc2gettext pg_enc2gettext_tbl[]

Definition at line 359 of file encnames.c.

const pg_encname pg_encname_tbl[]
static

Definition at line 38 of file encnames.c.

Referenced by pg_char_to_encoding().