| 1 |
/* @(#)dstring.c 17.1 (ESO-IPG) 01/25/02 17:26:33 */ |
| 2 |
/*--------------------------------------------------------------------- |
| 3 |
* $Date: 2003-06-04 14:49:18 $ $Revision: 1.1.1.1 $ |
| 4 |
*--------------------------------------------------------------------- |
| 5 |
* |
| 6 |
* |
| 7 |
* Copyright (c) 1990, Visual Edge Software Ltd. |
| 8 |
* |
| 9 |
* ALL RIGHTS RESERVED. Permission to use, copy, modify, and |
| 10 |
* distribute this software and its documentation for any purpose |
| 11 |
* and without fee is hereby granted, provided that the above |
| 12 |
* copyright notice appear in all copies and that both that |
| 13 |
* copyright notice and this permission notice appear in supporting |
| 14 |
* documentation, and that the name of Visual Edge Software not be |
| 15 |
* used in advertising or publicity pertaining to distribution of |
| 16 |
* the software without specific, written prior permission. The year |
| 17 |
* included in the notice is the year of the creation of the work. |
| 18 |
*-------------------------------------------------------------------*/ |
| 19 |
/*------------------------------------------------------------------------ |
| 20 |
* Dynamic strings. |
| 21 |
*------------------------------------------------------------------------*/ |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
#include "valloc.h" |
| 26 |
#include "dstring.h" |
| 27 |
|
| 28 |
#define BLOCKSIZE 64 |
| 29 |
#define round(len, sz) ( ((len)/(sz)+1)*(sz) ) |
| 30 |
|
| 31 |
dstring UxDefaultDstring = {NULL, 0, 0}; |
| 32 |
|
| 33 |
|
| 34 |
/***************************************************************************** |
| 35 |
NAME: void UxFree_dstring(ds) |
| 36 |
|
| 37 |
INPUT: dstring *ds - dstring to free |
| 38 |
|
| 39 |
DESCRIPTION: free the contents of the dstring |
| 40 |
CREATION: Jan 14/1989 |
| 41 |
REVISIONS: -- |
| 42 |
-----------------------------------------------------------------------------*/ |
| 43 |
|
| 44 |
void UxFree_dstring(ds) |
| 45 |
dstring *ds; |
| 46 |
{ |
| 47 |
UxFree(ds->str); |
| 48 |
|
| 49 |
ds->str = NULL; |
| 50 |
ds->size = 0; |
| 51 |
ds->len = 0; |
| 52 |
} |
| 53 |
|
| 54 |
/*------------------------------------------------------------------------ |
| 55 |
* NAME: DstringAppend |
| 56 |
* INPUT: dstring *s; -- target dstring to be appended to |
| 57 |
* char *dat; -- non-NULL string to append |
| 58 |
* int datlen -- strlen of dat. |
| 59 |
* DESCRIPTION: |
| 60 |
* Appends datlen characters of 'dat' to the dstring 's', |
| 61 |
* resizing the storage of 's' as needed. |
| 62 |
* |
| 63 |
* LAST REV: May 92 fix3545 extracted from UxAppend_dstring. |
| 64 |
*------------------------------------------------------------------------*/ |
| 65 |
|
| 66 |
|
| 67 |
static void DstringAppend(s, dat, datlen) |
| 68 |
dstring *s; |
| 69 |
char *dat; |
| 70 |
int datlen; |
| 71 |
{ |
| 72 |
if (s->str == NULL) { |
| 73 |
|
| 74 |
s->size = round(datlen + 1, BLOCKSIZE); |
| 75 |
s->str = UxRealloc(s->str, s->size); |
| 76 |
memcpy(s->str, dat, datlen); |
| 77 |
s->len = datlen; |
| 78 |
|
| 79 |
} else { |
| 80 |
|
| 81 |
if (s->len + datlen + 1 > s->size) |
| 82 |
{ |
| 83 |
s->size = round(s->len + datlen + 1, BLOCKSIZE); |
| 84 |
s->str = UxRealloc(s->str, s->size); |
| 85 |
} |
| 86 |
|
| 87 |
memcpy(&(s->str[s->len]), dat, datlen); |
| 88 |
s->len += datlen; |
| 89 |
} |
| 90 |
s->str[s->len] = 0; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/***************************************************************************** |
| 95 |
NAME: void UxAppend_to_dstring(s, dat) |
| 96 |
|
| 97 |
INPUT: dstring *s - string to append to |
| 98 |
*dat - string to append |
| 99 |
|
| 100 |
DESCRIPTION: |
| 101 |
Append 'dat' to 's' resizing it if needed. |
| 102 |
|
| 103 |
CREATION: Dec 16, 1988 |
| 104 |
REVISIONS: May 92 fix3545 memcpy rather than strcat. |
| 105 |
-----------------------------------------------------------------------------*/ |
| 106 |
|
| 107 |
void UxAppend_to_dstring(s, dat) |
| 108 |
dstring *s; |
| 109 |
char *dat; |
| 110 |
{ |
| 111 |
if (dat) { |
| 112 |
DstringAppend(s, dat, strlen(dat)); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/***************************************************************************** |
| 117 |
NAME: void UxAppend_dstring(s, dat) |
| 118 |
|
| 119 |
INPUT: dstring *s - string to append to |
| 120 |
*dat - string to append |
| 121 |
|
| 122 |
DESCRIPTION: |
| 123 |
Append 'dat' to 's' resizing it if needed |
| 124 |
|
| 125 |
CREATION: Dec 16, 1988 |
| 126 |
REVISIONS: May 92 fix3545 memcpy rather than strcat. |
| 127 |
-----------------------------------------------------------------------------*/ |
| 128 |
|
| 129 |
void UxAppend_dstring(s, dat) |
| 130 |
dstring *s, dat; |
| 131 |
{ |
| 132 |
if (dat.str) { |
| 133 |
DstringAppend(s, dat.str, dat.len); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/***************************************************************************** |
| 139 |
NAME: dstring UxDcreate(str) |
| 140 |
|
| 141 |
INPUT: char *str - initial string |
| 142 |
|
| 143 |
RETURN: created dstring |
| 144 |
|
| 145 |
DESCRIPTION: create a dstring given the initial string |
| 146 |
CREATION: Jan 14/1989 |
| 147 |
REVISIONS: May 92 fix3545 DstringAppend added. |
| 148 |
-----------------------------------------------------------------------------*/ |
| 149 |
|
| 150 |
dstring UxDcreate(str) |
| 151 |
char *str; |
| 152 |
{ |
| 153 |
dstring ds; |
| 154 |
|
| 155 |
ds.str = NULL; |
| 156 |
ds.size = 0; |
| 157 |
ds.len = 0; |
| 158 |
|
| 159 |
if (str) { |
| 160 |
DstringAppend(&ds, str, strlen(str)); |
| 161 |
} |
| 162 |
|
| 163 |
return(ds); |
| 164 |
} |