summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorJoey Adams2010-07-24 00:19:26 +0000
committerJoey Adams2010-07-24 00:19:26 +0000
commitbc9e250d090da889a3e1e1eb82b5dfc0a32bc52e (patch)
tree9068b19215f90ca971d294b1fa4c60b353125acb /util.c
parent8a019e04b817ffca00ec9759f3d12c60c808e67f (diff)
* Migrated general-purpose functions to new files util.c/util.h
* A few various cleanups.
Diffstat (limited to 'util.c')
-rw-r--r--util.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..39fb0b8
--- /dev/null
+++ b/util.c
@@ -0,0 +1,96 @@
+#include "util.h"
+
+#include "catalog/namespace.h"
+#include "mb/pg_wchar.h"
+#include "utils/syscache.h"
+
+void
+initTypeInfo(TypeInfo *d, MemoryContext mcxt)
+{
+ d->mcxt = mcxt;
+ d->not_set = true;
+}
+
+void
+getTypeInfo(TypeInfo *d, Oid type, IOFuncSelector which_func)
+{
+ if (d->not_set || d->type != type || d->which_func != which_func)
+ {
+ get_type_io_data(type, which_func,
+ &d->typlen, &d->typbyval, &d->typalign,
+ &d->typdelim, &d->typioparam, &d->typiofunc);
+ fmgr_info_cxt(d->typiofunc, &d->proc, d->mcxt);
+
+ get_type_category_preferred(type,
+ &d->typcategory, &d->typispreferred);
+
+ d->type = type;
+ d->which_func = which_func;
+ d->not_set = false;
+ }
+}
+
+Oid
+enumLabelToOid(const char *typname, const char *label)
+{
+ Oid enumtypoid;
+ HeapTuple tup;
+ Oid ret;
+
+ enumtypoid = TypenameGetTypid(typname);
+ Assert(OidIsValid(enumtypoid));
+
+ tup = SearchSysCache2(ENUMTYPOIDNAME,
+ ObjectIdGetDatum(enumtypoid),
+ CStringGetDatum(label));
+ Assert(HeapTupleIsValid(tup));
+
+ ret = HeapTupleGetOid(tup);
+
+ ReleaseSysCache(tup);
+
+ return ret;
+}
+
+size_t
+utf8_substring(
+ const char *src, size_t srcbytes,
+ size_t start, size_t length,
+ const char **out_start, size_t *out_bytes)
+{
+ const char *e = src + srcbytes;
+ const char *sub_start;
+ const char *sub_end;
+ size_t sub_length;
+
+ sub_start = src;
+ while (start > 0 && sub_start < e)
+ {
+ sub_start += pg_utf_mblen((const unsigned char *) sub_start);
+ start--;
+ }
+
+ sub_end = sub_start;
+ sub_length = 0;
+ while (sub_length < length && sub_end < e)
+ {
+ sub_end += pg_utf_mblen((const unsigned char *) sub_end);
+ sub_length++;
+ }
+
+ /* Make sure the input didn't have a clipped UTF-8 character */
+ if (sub_start > e)
+ {
+ Assert(false);
+ sub_start = sub_end = e;
+ }
+ else if (sub_end > e)
+ {
+ Assert(false);
+ sub_end = e;
+ }
+
+ *out_start = sub_start;
+ *out_bytes = sub_end - sub_start;
+ return sub_length;
+}