1996-08-19 06:14:33 +08:00
|
|
|
/*
|
|
|
|
* string_io.c --
|
|
|
|
*
|
1998-08-31 03:37:51 +08:00
|
|
|
* This file defines C-like input/output conversion routines for strings.
|
1996-08-19 06:14:33 +08:00
|
|
|
*
|
1999-09-28 04:04:14 +08:00
|
|
|
* Copyright (C) 1999, Massimo Dal Zotto <dz@cs.unitn.it>
|
1998-08-31 03:37:51 +08:00
|
|
|
*
|
1999-09-28 04:04:14 +08:00
|
|
|
* This software is distributed under the GNU General Public License
|
1998-08-31 03:37:51 +08:00
|
|
|
* either version 2, or (at your option) any later version.
|
1996-08-19 06:14:33 +08:00
|
|
|
*/
|
|
|
|
|
2001-02-10 10:31:31 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-08-19 06:14:33 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
1997-11-06 05:38:25 +08:00
|
|
|
#include "string_io.h"
|
|
|
|
|
1996-08-19 06:14:33 +08:00
|
|
|
/* define this if you want to see iso-8859 characters */
|
|
|
|
#define ISO8859
|
|
|
|
|
2000-05-29 13:45:56 +08:00
|
|
|
#undef MIN
|
1997-11-06 05:38:25 +08:00
|
|
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
2000-04-13 01:17:23 +08:00
|
|
|
#define VALUE(char) ((char) - '0')
|
1998-02-26 12:46:47 +08:00
|
|
|
#define DIGIT(val) ((val) + '0')
|
|
|
|
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
|
1996-08-19 06:14:33 +08:00
|
|
|
#ifndef ISO8859
|
2000-12-04 04:45:40 +08:00
|
|
|
#define NOTPRINTABLE(c) (!isprint((unsigned char) (c)))
|
1996-08-19 06:14:33 +08:00
|
|
|
#else
|
2000-12-04 04:45:40 +08:00
|
|
|
#define NOTPRINTABLE(c) (!isprint((unsigned char) (c)) && \
|
|
|
|
((unsigned char) (c) < (unsigned char) 0xa0))
|
1996-08-19 06:14:33 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* string_output() --
|
|
|
|
*
|
|
|
|
* This function takes a pointer to a string data and an optional
|
1998-08-31 03:37:51 +08:00
|
|
|
* data size and returns a printable representation of the string
|
1996-08-19 06:14:33 +08:00
|
|
|
* translating all escape sequences to C-like \nnn or \c escapes.
|
|
|
|
* The function is used by output methods of various string types.
|
|
|
|
*
|
|
|
|
* Arguments:
|
1997-11-06 05:38:25 +08:00
|
|
|
* data - input data (can be NULL)
|
|
|
|
* size - optional size of data. A negative value indicates
|
1998-08-31 03:37:51 +08:00
|
|
|
* that data is a null terminated string.
|
1996-08-19 06:14:33 +08:00
|
|
|
*
|
|
|
|
* Returns:
|
1997-11-06 05:38:25 +08:00
|
|
|
* a pointer to a new string containing the printable
|
|
|
|
* representation of data.
|
1996-08-19 06:14:33 +08:00
|
|
|
*/
|
|
|
|
|
2000-02-14 02:59:53 +08:00
|
|
|
unsigned char *
|
|
|
|
string_output(unsigned char *data, int size)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
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);
|
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
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;
|
2000-02-14 02:59:53 +08:00
|
|
|
case '{':
|
|
|
|
/* Escape beginning of string, to distinguish from arrays */
|
2000-04-13 01:17:23 +08:00
|
|
|
if (p == data)
|
2000-02-14 02:59:53 +08:00
|
|
|
len++;
|
|
|
|
break;
|
1998-02-26 12:46:47 +08:00
|
|
|
default:
|
|
|
|
if (NOTPRINTABLE(*p))
|
|
|
|
len += 3;
|
|
|
|
}
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
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;
|
2000-02-14 02:59:53 +08:00
|
|
|
case '{':
|
|
|
|
/* Escape beginning of string, to distinguish from arrays */
|
2000-04-13 01:17:23 +08:00
|
|
|
if (p == data)
|
2000-02-14 02:59:53 +08:00
|
|
|
*r++ = '\\';
|
|
|
|
*r++ = c;
|
|
|
|
break;
|
1998-02-26 12:46:47 +08:00
|
|
|
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;
|
|
|
|
}
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
*r = '\0';
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
return ((char *) result);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* string_input() --
|
|
|
|
*
|
1998-02-26 12:46:47 +08:00
|
|
|
* This function accepts a C string in input and copies it into a new
|
1996-08-19 06:14:33 +08:00
|
|
|
* object allocated with palloc() translating all escape sequences.
|
2003-03-11 06:28:22 +08:00
|
|
|
* An optional header can be allocated before the string, for example
|
1996-08-19 06:14:33 +08:00
|
|
|
* 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:
|
1997-11-06 05:38:25 +08:00
|
|
|
* str - input string possibly with escapes
|
|
|
|
* size - the required size of new data. A value of 0
|
1998-08-31 03:37:51 +08:00
|
|
|
* indicates a variable size string, while a
|
|
|
|
* negative value indicates a variable size string
|
|
|
|
* of size not greater than this absolute value.
|
1997-11-06 05:38:25 +08:00
|
|
|
* hdrsize - size of an optional header to be allocated before
|
1998-08-31 03:37:51 +08:00
|
|
|
* the data. It must then be filled by the caller.
|
1997-11-06 05:38:25 +08:00
|
|
|
* rtn_size - an optional pointer to an int variable where the
|
1998-08-31 03:37:51 +08:00
|
|
|
* size of the new string is stored back.
|
1996-08-19 06:14:33 +08:00
|
|
|
*
|
|
|
|
* Returns:
|
1997-11-06 05:38:25 +08:00
|
|
|
* a pointer to the new string or the header.
|
1996-08-19 06:14:33 +08:00
|
|
|
*/
|
|
|
|
|
2000-02-14 02:59:53 +08:00
|
|
|
unsigned char *
|
|
|
|
string_input(unsigned char *str, int size, int hdrsize, int *rtn_size)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
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--;
|
1997-11-06 05:38:25 +08:00
|
|
|
}
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
else
|
|
|
|
*r++ = c;
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
return ((char *) result);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
2000-02-14 02:59:53 +08:00
|
|
|
unsigned char *
|
1996-08-19 06:14:33 +08:00
|
|
|
c_charout(int32 c)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
char str[2];
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
str[0] = (char) c;
|
|
|
|
str[1] = '\0';
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
return (string_output(str, 1));
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-08-31 03:37:51 +08:00
|
|
|
* This can be used for SET, bytea, text and unknown data types
|
1996-08-19 06:14:33 +08:00
|
|
|
*/
|
|
|
|
|
2000-02-14 02:59:53 +08:00
|
|
|
unsigned char *
|
1998-02-26 12:46:47 +08:00
|
|
|
c_textout(struct varlena * vlena)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
int len = 0;
|
|
|
|
char *s = NULL;
|
|
|
|
|
|
|
|
if (vlena)
|
|
|
|
{
|
|
|
|
len = VARSIZE(vlena) - VARHDRSZ;
|
|
|
|
s = VARDATA(vlena);
|
|
|
|
}
|
|
|
|
return (string_output(s, len));
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This can be used for varchar and bpchar strings
|
|
|
|
*/
|
|
|
|
|
2000-02-14 02:59:53 +08:00
|
|
|
unsigned char *
|
|
|
|
c_varcharout(unsigned char *s)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
int len = 0;
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
len = *(int32 *) s - 4;
|
|
|
|
s += 4;
|
|
|
|
}
|
|
|
|
return (string_output(s, len));
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
1997-11-06 05:38:25 +08:00
|
|
|
#if 0
|
1996-08-19 06:14:33 +08:00
|
|
|
struct varlena *
|
2000-02-14 02:59:53 +08:00
|
|
|
c_textin(unsigned char *str)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
struct varlena *result;
|
|
|
|
int len;
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
if (str == NULL)
|
|
|
|
return ((struct varlena *) NULL);
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
result = (struct varlena *) string_input(str, 0, VARHDRSZ, &len);
|
|
|
|
VARSIZE(result) = len;
|
1996-08-19 06:14:33 +08:00
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
return (result);
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
|
|
|
|
1998-08-31 03:37:51 +08:00
|
|
|
int32 *
|
2000-02-14 02:59:53 +08:00
|
|
|
c_charin(unsigned char *str)
|
1996-08-19 06:14:33 +08:00
|
|
|
{
|
1998-08-31 03:37:51 +08:00
|
|
|
return (string_input(str, 1, 0, NULL));
|
1996-08-19 06:14:33 +08:00
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
#endif
|
1997-11-06 05:38:25 +08:00
|
|
|
|
|
|
|
/* end of file */
|
1998-08-31 03:37:51 +08:00
|
|
|
|
|
|
|
/*
|
1999-06-06 03:09:48 +08:00
|
|
|
* Local Variables:
|
2000-04-13 01:17:23 +08:00
|
|
|
* tab-width: 4
|
|
|
|
* c-indent-level: 4
|
|
|
|
* c-basic-offset: 4
|
1998-08-31 03:37:51 +08:00
|
|
|
* End:
|
|
|
|
*/
|