Skip to content

Commit 457b6ef

Browse files
author
Thomas G. Lockhart
committed
Add conversion functions to and from the "name" data type.
1 parent e8cbf3a commit 457b6ef

File tree

2 files changed

+126
-4
lines changed

2 files changed

+126
-4
lines changed

src/backend/utils/adt/varchar.c

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.31 1998/05/09 22:42:07 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.32 1998/05/29 13:33:24 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -201,8 +201,7 @@ int32
201201
bpchar_char(char *s)
202202
{
203203
return ((int32) *VARDATA(s));
204-
} /* char_bpchar() */
205-
204+
} /* bpchar_char() */
206205

207206
/* char_bpchar()
208207
* Convert char to bpchar(1).
@@ -221,6 +220,70 @@ char_bpchar(int32 c)
221220
} /* char_bpchar() */
222221

223222

223+
/* bpchar_name()
224+
* Converts a bpchar() type to a NameData type.
225+
*/
226+
NameData *
227+
bpchar_name(char *s)
228+
{
229+
NameData *result;
230+
int len;
231+
232+
if (s == NULL)
233+
return (NULL);
234+
235+
len = VARSIZE(s) - VARHDRSZ;
236+
if (len > NAMEDATALEN) len = NAMEDATALEN;
237+
238+
while (len > 0) {
239+
if (*(VARDATA(s)+len-1) != ' ') break;
240+
len--;
241+
}
242+
243+
#ifdef STRINGDEBUG
244+
printf("bpchar- convert string length %d (%d) ->%d\n",
245+
VARSIZE(s)-VARHDRSZ, VARSIZE(s), len);
246+
#endif
247+
248+
result = (NameData *) palloc(NAMEDATALEN);
249+
StrNCpy(result->data, VARDATA(s), NAMEDATALEN);
250+
251+
/* now null pad to full length... */
252+
while (len < NAMEDATALEN) {
253+
*(result->data + len) = '\0';
254+
len++;
255+
}
256+
257+
return (result);
258+
} /* bpchar_name() */
259+
260+
/* name_bpchar()
261+
* Converts a NameData type to a bpchar type.
262+
*/
263+
char *
264+
name_bpchar(NameData *s)
265+
{
266+
char *result;
267+
int len;
268+
269+
if (s == NULL)
270+
return (NULL);
271+
272+
len = strlen(s->data);
273+
274+
#ifdef STRINGDEBUG
275+
printf("bpchar- convert string length %d (%d) ->%d\n",
276+
VARSIZE(s)-VARHDRSZ, VARSIZE(s), len);
277+
#endif
278+
279+
result = (char *) palloc(VARHDRSZ + len);
280+
strncpy(VARDATA(result), s->data, len);
281+
VARSIZE(result) = len + VARHDRSZ;
282+
283+
return (result);
284+
} /* name_bpchar() */
285+
286+
224287
/*****************************************************************************
225288
* varchar - varchar() *
226289
*****************************************************************************/

src/backend/utils/adt/varlena.c

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.34 1998/05/09 22:42:07 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.35 1998/05/29 13:33:58 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -759,3 +759,62 @@ byteaSetBit(text *v, int32 n, int32 newBit)
759759

760760
return (res);
761761
}
762+
763+
764+
/* text_name()
765+
* Converts a text() type to a NameData type.
766+
*/
767+
NameData *
768+
text_name(text *s)
769+
{
770+
NameData *result;
771+
int len;
772+
773+
if (s == NULL)
774+
return (NULL);
775+
776+
len = VARSIZE(s) - VARHDRSZ;
777+
if (len > NAMEDATALEN) len = NAMEDATALEN;
778+
779+
#ifdef STRINGDEBUG
780+
printf("text- convert string length %d (%d) ->%d\n",
781+
VARSIZE(s)-VARHDRSZ, VARSIZE(s), len);
782+
#endif
783+
784+
result = palloc(NAMEDATALEN);
785+
StrNCpy(result->data, VARDATA(s), NAMEDATALEN);
786+
787+
/* now null pad to full length... */
788+
while (len < NAMEDATALEN) {
789+
*(result->data + len) = '\0';
790+
len++;
791+
}
792+
793+
return (result);
794+
} /* text_name() */
795+
796+
/* name_text()
797+
* Converts a NameData type to a text type.
798+
*/
799+
text *
800+
name_text(NameData *s)
801+
{
802+
text *result;
803+
int len;
804+
805+
if (s == NULL)
806+
return (NULL);
807+
808+
len = strlen(s->data);
809+
810+
#ifdef STRINGDEBUG
811+
printf("text- convert string length %d (%d) ->%d\n",
812+
VARSIZE(s)-VARHDRSZ, VARSIZE(s), len);
813+
#endif
814+
815+
result = palloc(VARHDRSZ + len);
816+
strncpy(VARDATA(result), s->data, len);
817+
VARSIZE(result) = len + VARHDRSZ;
818+
819+
return (result);
820+
} /* name_text() */

0 commit comments

Comments
 (0)