4 * This file defines new input/output conversion routines for strings.
6 * Copyright (c) 1996, Massimo Dal Zotto <dz@cs.unitn.it>
13 #include "utils/elog.h"
14 #include "utils/palloc.h"
15 #include "utils/builtins.h"
17 /* define this if you want to see iso-8859 characters */
20 #define MIN(x, y) ((x) < (y) ? (x) : (y))
21 #define VALUE(char) ((char) - '0')
22 #define DIGIT(val) ((val) + '0')
23 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
25 #define NOTPRINTABLE(c) (!isprint(c))
27 #define NOTPRINTABLE(c) (!isprint(c) && ((c) < 0xa0))
33 * This function takes a pointer to a string data and an optional
34 * data size and returns a printable representation of the data
35 * translating all escape sequences to C-like \nnn or \c escapes.
36 * The function is used by output methods of various string types.
39 * data - input data (can be NULL)
40 * size - optional size of data. A negative value indicates
41 * that data is a null terminated string.
44 * a pointer to a new string containing the printable
45 * representation of data.
49 string_output(char *data, int size)
51 register unsigned char c, *p, *r, *result;
55 result = (char *) palloc(2);
65 /* adjust string length for escapes */
67 for (p=data,l=size; l>0; p++,l--) {
82 if (NOTPRINTABLE(*p)) {
89 result = (char *) palloc(len);
91 for (p=data,r=result,l=size; (l > 0) && (c = *p); p++,l--) {
125 if (NOTPRINTABLE(c)) {
128 *r-- = DIGIT(c & 07);
130 *r-- = DIGIT(c & 07);
141 return((char *) result);
147 * This function accepts a C string in input and copies it into a new
148 * object allocated with palloc() translating all escape sequences.
149 * An optional header can be allocatd before the string, for example
150 * to hold the length of a varlena object.
151 * This function is not necessary for input from sql commands because
152 * the parser already does escape translation, all data input routines
153 * receive strings in internal form.
156 * str - input string possibly with escapes
157 * size - the required size of new data. A value of 0
158 * indicates a variable size string, while a
159 * negative value indicates a variable size string
160 * of size not greater than this absolute value.
161 * hdrsize - size of an optional header to be allocated before
162 * the data. It must then be filled by the caller.
163 * rtn_size - an optional pointer to an int variable where the
164 * size of the new string is stored back.
167 * a pointer to the new string or the header.
171 string_input(char *str, int size, int hdrsize, int *rtn_size)
173 register unsigned char *p, *r;
174 unsigned char *result;
177 if ((str == NULL) || (hdrsize < 0)) {
178 return (char *) NULL;
181 /* Compute result size */
186 if (ISOCTAL(*(p+1))) {
190 if (ISOCTAL(*(p+1))) {
200 /* result has variable length */
205 /* result has variable length with maximum size */
207 size = MIN(len, - size)+1;
210 result = (char *) palloc(hdrsize+size);
211 memset(result, 0, hdrsize+size);
216 r = result + hdrsize;
218 register unsigned char c;
219 if ((c = *p++) == '\\') {
234 c = (c<<3) + VALUE(*p++);
237 c = (c<<3) + VALUE(*p++);
267 return((char *) result);
278 return (string_output(str, 1));
284 return (string_output((char *) &s, 2));
290 return (string_output((char *) &s, 4));
296 return (string_output(s, 8));
302 return (string_output(s, 16));
306 * This can be used for text, bytea, SET and unknown data types
310 c_textout(struct varlena *vlena)
316 len = VARSIZE(vlena) - VARHDRSZ;
319 return (string_output(s, len));
323 * This can be used for varchar and bpchar strings
327 c_varcharout(char *s)
332 len = *(int32*)s - 4;
335 return (string_output(s, len));
342 struct varlena *result;
346 return ((struct varlena *) NULL);
349 result = (struct varlena *) string_input(str, 0, VARHDRSZ, &len);
350 VARSIZE(result) = len;
356 c_char16in(char *str)
358 return (string_input(str, 16, 0, NULL));