diff options
Diffstat (limited to 'contrib/string')
-rw-r--r-- | contrib/string/Makefile | 15 | ||||
-rw-r--r-- | contrib/string/README.string_io | 23 | ||||
-rw-r--r-- | contrib/string/string_io.c | 370 | ||||
-rw-r--r-- | contrib/string/string_io.h | 24 | ||||
-rw-r--r-- | contrib/string/string_io.sql.in | 84 |
5 files changed, 0 insertions, 516 deletions
diff --git a/contrib/string/Makefile b/contrib/string/Makefile deleted file mode 100644 index 72e868ab5b9..00000000000 --- a/contrib/string/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -# $PostgreSQL: pgsql/contrib/string/Makefile,v 1.18 2004/08/20 20:13:08 momjian Exp $ - -MODULES = string_io -DATA_built = string_io.sql -DOCS = README.string_io - -ifdef USE_PGXS -PGXS = $(shell pg_config --pgxs) -include $(PGXS) -else -subdir = contrib/string -top_builddir = ../.. -include $(top_builddir)/src/Makefile.global -include $(top_srcdir)/contrib/contrib-global.mk -endif diff --git a/contrib/string/README.string_io b/contrib/string/README.string_io deleted file mode 100644 index 4b4d10166f8..00000000000 --- a/contrib/string/README.string_io +++ /dev/null @@ -1,23 +0,0 @@ -String io module for postgresql. -Copyright (C) 1999, Massimo Dal Zotto <dz@cs.unitn.it> - -This software is distributed under the GNU General Public License -either version 2, or (at your option) any later version. - - -These output functions can be used as substitution of the standard text -output functions to get the value of text fields printed in the format -used for C strings. This allows the output of queries or the exported -files to be processed more easily using standard unix filter programs -like perl or awk. - -If you use the standard functions instead you could find a single tuple -splitted into many lines and the tabs embedded in the values could be -confused with those used as field delimters. - -My function translates all non-printing characters into corresponding -esacape sequences as defined by the C syntax. All you need to reconstruct -the exact value in your application is a corresponding unescape function -like the string_input defined in the source code. - -Massimo Dal Zotto <dz@cs.unitn.it> diff --git a/contrib/string/string_io.c b/contrib/string/string_io.c deleted file mode 100644 index a6f49e9afe7..00000000000 --- a/contrib/string/string_io.c +++ /dev/null @@ -1,370 +0,0 @@ -/* - * string_io.c -- - * - * This file defines C-like input/output conversion routines for strings. - * - * Copyright (C) 1999, Massimo Dal Zotto <dz@cs.unitn.it> - * - * This software is distributed under the GNU General Public License - * either version 2, or (at your option) any later version. - */ - -#include "postgres.h" - -#include <ctype.h> - -#include "utils/builtins.h" - -#include "string_io.h" - -/* define this if you want to see iso-8859 characters */ -#define ISO8859 - -#define VALUE(char) ((char) - '0') -#define DIGIT(val) ((val) + '0') -#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7')) -#ifndef ISO8859 -#define NOTPRINTABLE(c) (!isprint((unsigned char) (c))) -#else -#define NOTPRINTABLE(c) (!isprint((unsigned char) (c)) && \ - ((unsigned char) (c) < (unsigned char) 0xa0)) -#endif - -/* - * string_output() -- - * - * This function takes a pointer to a string data and an optional - * data size and returns a printable representation of the string - * translating all escape sequences to C-like \nnn or \c escapes. - * The function is used by output methods of various string types. - * - * Arguments: - * data - input data (can be NULL) - * size - optional size of data. A negative value indicates - * that data is a null terminated string. - * - * Returns: - * a pointer to a new string containing the printable - * representation of data. - */ - -unsigned char * -string_output(unsigned char *data, int size) -{ - register unsigned char c, - *p, - *r, - *result; - register int l, - len; - - if (data == NULL) - { - result = (char *) palloc(2); - result[0] = '-'; - result[1] = '\0'; - return (result); - } - - if (size < 0) - size = strlen(data); - - /* adjust string length for escapes */ - len = size; - for (p = data, l = size; l > 0; p++, l--) - { - switch (*p) - { - case '\\': - case '"': - case '\b': - case '\f': - case '\n': - case '\r': - case '\t': - case '\v': - len++; - break; - case '{': - /* Escape beginning of string, to distinguish from arrays */ - if (p == data) - len++; - break; - default: - if (NOTPRINTABLE(*p)) - len += 3; - } - } - len++; - - result = (char *) palloc(len); - - for (p = data, r = result, l = size; (l > 0) && (c = *p); p++, l--) - { - switch (c) - { - case '\\': - case '"': - *r++ = '\\'; - *r++ = c; - break; - case '\b': - *r++ = '\\'; - *r++ = 'b'; - break; - case '\f': - *r++ = '\\'; - *r++ = 'f'; - break; - case '\n': - *r++ = '\\'; - *r++ = 'n'; - break; - case '\r': - *r++ = '\\'; - *r++ = 'r'; - break; - case '\t': - *r++ = '\\'; - *r++ = 't'; - break; - case '\v': - *r++ = '\\'; - *r++ = 'v'; - break; - case '{': - /* Escape beginning of string, to distinguish from arrays */ - if (p == data) - *r++ = '\\'; - *r++ = c; - break; - default: - if (NOTPRINTABLE(c)) - { - *r = '\\'; - r += 3; - *r-- = DIGIT(c & 07); - c >>= 3; - *r-- = DIGIT(c & 07); - c >>= 3; - *r = DIGIT(c & 03); - r += 3; - } - else - *r++ = c; - } - } - *r = '\0'; - - return ((char *) result); -} - -/* - * string_input() -- - * - * This function accepts a C string in input and copies it into a new - * object allocated with palloc() translating all escape sequences. - * An optional header can be allocated before the string, for example - * to hold the length of a varlena object. - * This function is not necessary for input from sql commands because - * the parser already does escape translation, all data input routines - * receive strings in internal form. - * - * Arguments: - * str - input string possibly with escapes - * size - the required size of new data. A value of 0 - * indicates a variable size string, while a - * negative value indicates a variable size string - * of size not greater than this absolute value. - * hdrsize - size of an optional header to be allocated before - * the data. It must then be filled by the caller. - * rtn_size - an optional pointer to an int variable where the - * size of the new string is stored back. - * - * Returns: - * a pointer to the new string or the header. - */ - -unsigned char * -string_input(unsigned char *str, int size, int hdrsize, int *rtn_size) -{ - register unsigned char *p, - *r; - unsigned char *result; - int len; - - if ((str == NULL) || (hdrsize < 0)) - return (char *) NULL; - - /* Compute result size */ - len = strlen(str); - for (p = str; *p;) - { - if (*p++ == '\\') - { - if (ISOCTAL(*p)) - { - if (ISOCTAL(*(p + 1))) - { - p++; - len--; - } - if (ISOCTAL(*(p + 1))) - { - p++; - len--; - } - } - if (*p) - p++; - len--; - } - } - - /* result has variable length */ - if (size == 0) - size = len + 1; - else - /* result has variable length with maximum size */ - if (size < 0) - size = Min(len, -size) + 1; - - result = (char *) palloc(hdrsize + size); - memset(result, 0, hdrsize + size); - if (rtn_size) - *rtn_size = size; - - r = result + hdrsize; - for (p = str; *p;) - { - register unsigned char c; - - if ((c = *p++) == '\\') - { - switch (c = *p++) - { - case '\0': - p--; - break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - c = VALUE(c); - if (isdigit(*p)) - c = (c << 3) + VALUE(*p++); - if (isdigit(*p)) - c = (c << 3) + VALUE(*p++); - *r++ = c; - break; - case 'b': - *r++ = '\b'; - break; - case 'f': - *r++ = '\f'; - break; - case 'n': - *r++ = '\n'; - break; - case 'r': - *r++ = '\r'; - break; - case 't': - *r++ = '\t'; - break; - case 'v': - *r++ = '\v'; - break; - default: - *r++ = c; - } - } - else - *r++ = c; - } - - return ((char *) result); -} - -unsigned char * -c_charout(int32 c) -{ - char str[2]; - - str[0] = (char) c; - str[1] = '\0'; - - return (string_output(str, 1)); -} - -/* - * This can be used for SET, bytea, text and unknown data types - */ - -unsigned char * -c_textout(struct varlena * vlena) -{ - int len = 0; - char *s = NULL; - - if (vlena) - { - len = VARSIZE(vlena) - VARHDRSZ; - s = VARDATA(vlena); - } - return (string_output(s, len)); -} - -/* - * This can be used for varchar and bpchar strings - */ - -unsigned char * -c_varcharout(unsigned char *s) -{ - int len = 0; - - if (s) - { - len = *(int32 *) s - 4; - s += 4; - } - return (string_output(s, len)); -} - -#if 0 -struct varlena * -c_textin(unsigned char *str) -{ - struct varlena *result; - int len; - - if (str == NULL) - return ((struct varlena *) NULL); - - result = (struct varlena *) string_input(str, 0, VARHDRSZ, &len); - VARSIZE(result) = len; - - return (result); -} - -int32 * -c_charin(unsigned char *str) -{ - return (string_input(str, 1, 0, NULL)); -} -#endif - -/* end of file */ - -/* - * Local Variables: - * tab-width: 4 - * c-indent-level: 4 - * c-basic-offset: 4 - * End: - */ diff --git a/contrib/string/string_io.h b/contrib/string/string_io.h deleted file mode 100644 index b6a368698af..00000000000 --- a/contrib/string/string_io.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef STRING_IO_H -#define STRING_IO_H - -unsigned char *string_output(unsigned char *data, int size); -unsigned char *string_input(unsigned char *str, int size, int hdrsize, - int *rtn_size); -unsigned char *c_charout(int32 c); -unsigned char *c_textout(struct varlena * vlena); -unsigned char *c_varcharout(unsigned char *s); - -#if 0 -struct varlena *c_textin(unsigned char *str); -int32 * -c_charin(unsigned char *str) -#endif -#endif - -/* - * Local Variables: - * tab-width: 4 - * c-indent-level: 4 - * c-basic-offset: 4 - * End: - */ diff --git a/contrib/string/string_io.sql.in b/contrib/string/string_io.sql.in deleted file mode 100644 index 392cda2ee78..00000000000 --- a/contrib/string/string_io.sql.in +++ /dev/null @@ -1,84 +0,0 @@ --- string_io.sql -- --- --- SQL code to define the new string I/O functions --- --- Copyright (c) 1998, Massimo Dal Zotto <dz@cs.unitn.it> --- --- This file is distributed under the GNU General Public License --- either version 2, or (at your option) any later version. - --- Define the new output functions. --- -CREATE FUNCTION c_charout(bpchar) -RETURNS cstring -AS 'MODULE_PATHNAME' -LANGUAGE 'C'; - -CREATE FUNCTION c_textout(text) -RETURNS cstring -AS 'MODULE_PATHNAME' -LANGUAGE 'C'; - -CREATE FUNCTION c_varcharout(varchar) -RETURNS cstring -AS 'MODULE_PATHNAME' -LANGUAGE 'C'; - --- This is not needed because escapes are handled by the parser --- --- CREATE FUNCTION c_textin(cstring) --- RETURNS text --- AS 'MODULE_PATHNAME' --- LANGUAGE 'c'; - --- Define a function which sets the new output routines for char types. --- --- SELECT c_mode(); --- -CREATE FUNCTION c_mode() -RETURNS text -AS ' UPDATE pg_type SET typoutput=''c_textout'' WHERE typname=''SET''; - UPDATE pg_type SET typoutput=''c_varcharout'' WHERE typname=''bpchar''; - UPDATE pg_type SET typoutput=''c_textout'' WHERE typname=''bytea''; - UPDATE pg_type SET typoutput=''c_charout'' WHERE typname=''char''; - UPDATE pg_type SET typoutput=''c_textout'' WHERE typname=''text''; - UPDATE pg_type SET typoutput=''c_textout'' WHERE typname=''unknown''; - UPDATE pg_type SET typoutput=''c_varcharout'' WHERE typname=''varchar''; - select ''c_mode''::text;' -LANGUAGE 'SQL'; - --- Define a function which restores the standard routines for char types. --- --- SELECT pg_mode(); --- -CREATE FUNCTION pg_mode() -RETURNS text -AS ' UPDATE pg_type SET typoutput=''textout'' WHERE typname=''SET''; - UPDATE pg_type SET typoutput=''varcharout'' WHERE typname=''bpchar''; - UPDATE pg_type SET typoutput=''textout'' WHERE typname=''bytea''; - UPDATE pg_type SET typoutput=''charout'' WHERE typname=''char''; - UPDATE pg_type SET typoutput=''textout'' WHERE typname=''text''; - UPDATE pg_type SET typoutput=''textout'' WHERE typname=''unknown''; - UPDATE pg_type SET typoutput=''varcharout'' WHERE typname=''varchar''; - select ''pg_mode''::text;' -LANGUAGE 'SQL'; - --- Use these to do the changes manually. --- --- UPDATE pg_type SET typoutput='textout' WHERE typname='SET'; --- UPDATE pg_type SET typoutput='varcharout' WHERE typname='bpchar'; --- UPDATE pg_type SET typoutput='textout' WHERE typname='bytea'; --- UPDATE pg_type SET typoutput='charout' WHERE typname='char'; --- UPDATE pg_type SET typoutput='textout' WHERE typname='text'; --- UPDATE pg_type SET typoutput='textout' WHERE typname='unknown'; --- UPDATE pg_type SET typoutput='varcharout' WHERE typname='varchar'; --- --- UPDATE pg_type SET typoutput='c_textout' WHERE typname='SET'; --- UPDATE pg_type SET typoutput='c_varcharout' WHERE typname='bpchar'; --- UPDATE pg_type SET typoutput='c_textout' WHERE typname='bytea'; --- UPDATE pg_type SET typoutput='c_charout' WHERE typname='char'; --- UPDATE pg_type SET typoutput='c_textout' WHERE typname='text'; --- UPDATE pg_type SET typoutput='c_textout' WHERE typname='unknown'; --- UPDATE pg_type SET typoutput='c_varcharout' WHERE typname='varchar'; - --- end of file |