aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/mb/common.c
blob: 1b3fe5fcf94364311ea450140baf78e9773c34b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * This file contains some public functions
 * usable for both the backend and the frontend.
 * Tatsuo Ishii
 * $Id: common.c,v 1.12 2001/02/11 01:59:22 ishii Exp $
 */
#include "postgres.h"

#ifdef WIN32
#include "win32.h"
#else
#include <unistd.h>
#endif

#include "miscadmin.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"

#ifndef FRONTEND
/*
 * convert encoding char to encoding symbol value.
 * case is ignored.
 * if there's no valid encoding, returns -1
 */
int
pg_char_to_encoding(const char *s)
{
	pg_encoding_conv_tbl *p = pg_conv_tbl;

	if (!s)
		return (-1);

	for (; p->encoding >= 0; p++)
	{
		if (!strcasecmp(s, p->name))
			break;
	}
	return (p->encoding);
}

/* Same, as an fmgr-callable function */
Datum
PG_char_to_encoding(PG_FUNCTION_ARGS)
{
	Name		s = PG_GETARG_NAME(0);

	PG_RETURN_INT32(pg_char_to_encoding(NameStr(*s)));
}

/*
 * check to see if encoding name is valid
 */
int
pg_valid_client_encoding(const char *name)
{
	return (pg_char_to_encoding(name));
}

/*
 * find encoding table entry by encoding
 */
pg_encoding_conv_tbl *
pg_get_encent_by_encoding(int encoding)
{
	pg_encoding_conv_tbl *p = pg_conv_tbl;

	for (; p->encoding >= 0; p++)
	{
		if (p->encoding == encoding)
			return (p);
	}
	return (0);
}

/*
 * convert encoding symbol to encoding char.
 * if there's no valid encoding symbol, returns ""
 */
const char *
pg_encoding_to_char(int encoding)
{
	pg_encoding_conv_tbl *p = pg_get_encent_by_encoding(encoding);

	if (!p)
		return ("");
	return (p->name);
}

/* Same, as an fmgr-callable function */
Datum
PG_encoding_to_char(PG_FUNCTION_ARGS)
{
	int32		encoding = PG_GETARG_INT32(0);

	PG_RETURN_NAME(pg_encoding_to_char(encoding));
}

#endif