summaryrefslogtreecommitdiff
path: root/util.h
blob: 1b4922640f1c970db9fa903bf106421ab2b936a8 (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
/*-------------------------------------------------------------------------
 *
 * util.h
 *	  General purpose routines used by JSON data type support.
 *
 * Copyright (c) 2010, PostgreSQL Global Development Group
 * Written by Joey Adams <joeyadams3.14159@gmail.com>.
 *
 *-------------------------------------------------------------------------
 */

#ifndef JSON_UTIL_H
#define JSON_UTIL_H

#include "postgres.h"

#include "funcapi.h"
#include "utils/lsyscache.h"

/* TODO:  Make this less "magic" (e.g. make it a function instead of a macro). */
#define FN_EXTRA(var, ...) FN_EXTRA_SZ(var, sizeof(*(var)), __VA_ARGS__)
#define FN_EXTRA_SZ(var, alloc, ...) do \
	{ \
		var = fcinfo->flinfo->fn_extra; \
		if (var == NULL) \
		{ \
			var = fcinfo->flinfo->fn_extra = \
				MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, alloc); \
			__VA_ARGS__; \
		} \
	} while(0)

typedef struct
{
	Oid			type;
	IOFuncSelector which_func;
	MemoryContext mcxt;

	bool		not_set;

	int16		typlen;
	bool		typbyval;
	char		typalign;
	char		typdelim;
	Oid			typioparam;
	Oid			typiofunc;
	FmgrInfo	proc;

	char		typcategory;
	bool		typispreferred;
} TypeInfo;

void		initTypeInfo(TypeInfo *d, MemoryContext mcxt);
void		getTypeInfo(TypeInfo *d, Oid type, IOFuncSelector which_func);

Oid			enumLabelToOid(const char *typname, const char *label);

size_t utf8_substring(const char *src, size_t srcbytes,
			   size_t start, size_t length,
			   const char **out_start, size_t *out_bytes);
void		utf8_decode_char_nocheck(const char **sp, unsigned int *uc);
bool		utf8_validate(const char *str, size_t length);
int			utf8_encode_char(char *out, unsigned int uc);

/*
 * Adaptations of pg_do_encoding_conversion for simplifying UTF-8 conversions.
 *
 * These are used frequently in the JSON code because JSON nodes are encoded
 * in UTF-8.  The reason they are encoded in UTF-8 is because we need to
 * be able to handle Unicode escapes, and there's
 * no simple and efficient way to do that with the server encoding.
 *
 * Just like pg_do_encoding_conversion, if no conversion is done, the original
 * pointer given is returned.
 */
char	   *server_to_utf8(const char *str, int len);
char	   *utf8_to_server(const char *str, int len);

/*
 * Adaptations of text_to_cstring and cstring_to_text for simplifying UTF-8 conversions.
 *
 * Just like text_to_cstring, text_to_utf8_cstring will always return a palloc'd,
 * null-terminated C-string.
 */
char	   *text_to_utf8_cstring(const text *t);
text	   *utf8_cstring_to_text(const char *s);
text	   *utf8_cstring_to_text_with_len(const char *s, int len);


#endif