| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH Tcl_DString 3 7.4 Tcl "Tcl Library Procedures" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
Tcl_DStringInit, Tcl_DStringAppend, Tcl_DStringAppendElement, Tcl_DStringStartSublist, Tcl_DStringEndSublist, Tcl_DStringLength, Tcl_DStringValue, Tcl_DStringSetLength, Tcl_DStringTrunc, Tcl_DStringFree, Tcl_DStringResult, Tcl_DStringGetResult \- manipulate dynamic strings |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
\fBTcl_DStringInit\fR(\fIdsPtr\fR) |
| 18 |
.sp |
| 19 |
char * |
| 20 |
\fBTcl_DStringAppend\fR(\fIdsPtr, bytes, length\fR) |
| 21 |
.sp |
| 22 |
char * |
| 23 |
\fBTcl_DStringAppendElement\fR(\fIdsPtr, element\fR) |
| 24 |
.sp |
| 25 |
\fBTcl_DStringStartSublist\fR(\fIdsPtr\fR) |
| 26 |
.sp |
| 27 |
\fBTcl_DStringEndSublist\fR(\fIdsPtr\fR) |
| 28 |
.sp |
| 29 |
int |
| 30 |
\fBTcl_DStringLength\fR(\fIdsPtr\fR) |
| 31 |
.sp |
| 32 |
char * |
| 33 |
\fBTcl_DStringValue\fR(\fIdsPtr\fR) |
| 34 |
.sp |
| 35 |
\fBTcl_DStringSetLength\fR(\fIdsPtr, newLength\fR) |
| 36 |
.sp |
| 37 |
\fBTcl_DStringTrunc\fR(\fIdsPtr, newLength\fR) |
| 38 |
.sp |
| 39 |
\fBTcl_DStringFree\fR(\fIdsPtr\fR) |
| 40 |
.sp |
| 41 |
\fBTcl_DStringResult\fR(\fIinterp, dsPtr\fR) |
| 42 |
.sp |
| 43 |
\fBTcl_DStringGetResult\fR(\fIinterp, dsPtr\fR) |
| 44 |
.SH ARGUMENTS |
| 45 |
.AS Tcl_DString newLength in/out |
| 46 |
.AP Tcl_DString *dsPtr in/out |
| 47 |
Pointer to structure that is used to manage a dynamic string. |
| 48 |
.AP "const char" *bytes in |
| 49 |
Pointer to characters to append to dynamic string. |
| 50 |
.AP "const char" *element in |
| 51 |
Pointer to characters to append as list element to dynamic string. |
| 52 |
.AP int length in |
| 53 |
Number of bytes from \fIbytes\fR to add to dynamic string. If -1, |
| 54 |
add all characters up to null terminating character. |
| 55 |
.AP int newLength in |
| 56 |
New length for dynamic string, not including null terminating |
| 57 |
character. |
| 58 |
.AP Tcl_Interp *interp in/out |
| 59 |
Interpreter whose result is to be set from or moved to the |
| 60 |
dynamic string. |
| 61 |
.BE |
| 62 |
|
| 63 |
.SH DESCRIPTION |
| 64 |
.PP |
| 65 |
Dynamic strings provide a mechanism for building up arbitrarily long |
| 66 |
strings by gradually appending information. If the dynamic string is |
| 67 |
short then there will be no memory allocation overhead; as the string |
| 68 |
gets larger, additional space will be allocated as needed. |
| 69 |
.PP |
| 70 |
\fBTcl_DStringInit\fR initializes a dynamic string to zero length. |
| 71 |
The Tcl_DString structure must have been allocated by the caller. |
| 72 |
No assumptions are made about the current state of the structure; |
| 73 |
anything already in it is discarded. |
| 74 |
If the structure has been used previously, \fBTcl_DStringFree\fR should |
| 75 |
be called first to free up any memory allocated for the old |
| 76 |
string. |
| 77 |
.PP |
| 78 |
\fBTcl_DStringAppend\fR adds new information to a dynamic string, |
| 79 |
allocating more memory for the string if needed. |
| 80 |
If \fIlength\fR is less than zero then everything in \fIbytes\fR |
| 81 |
is appended to the dynamic string; otherwise \fIlength\fR |
| 82 |
specifies the number of bytes to append. |
| 83 |
\fBTcl_DStringAppend\fR returns a pointer to the characters of |
| 84 |
the new string. The string can also be retrieved from the |
| 85 |
\fIstring\fR field of the Tcl_DString structure. |
| 86 |
.PP |
| 87 |
\fBTcl_DStringAppendElement\fR is similar to \fBTcl_DStringAppend\fR |
| 88 |
except that it does not take a \fIlength\fR argument (it appends |
| 89 |
all of \fIelement\fR) and it converts the string to a proper list element |
| 90 |
before appending. |
| 91 |
\fBTcl_DStringAppendElement\fR adds a separator space before the |
| 92 |
new list element unless the new list element is the first in a |
| 93 |
list or sub-list (i.e. either the current string is empty, or it |
| 94 |
contains the single character |
| 95 |
.QW { , |
| 96 |
or the last two characters of the current string are |
| 97 |
.QW " {" ). |
| 98 |
\fBTcl_DStringAppendElement\fR returns a pointer to the |
| 99 |
characters of the new string. |
| 100 |
.PP |
| 101 |
\fBTcl_DStringStartSublist\fR and \fBTcl_DStringEndSublist\fR can be |
| 102 |
used to create nested lists. |
| 103 |
To append a list element that is itself a sublist, first |
| 104 |
call \fBTcl_DStringStartSublist\fR, then call \fBTcl_DStringAppendElement\fR |
| 105 |
for each of the elements in the sublist, then call |
| 106 |
\fBTcl_DStringEndSublist\fR to end the sublist. |
| 107 |
\fBTcl_DStringStartSublist\fR appends a space character if needed, |
| 108 |
followed by an open brace; \fBTcl_DStringEndSublist\fR appends |
| 109 |
a close brace. |
| 110 |
Lists can be nested to any depth. |
| 111 |
.PP |
| 112 |
\fBTcl_DStringLength\fR is a macro that returns the current length |
| 113 |
of a dynamic string (not including the terminating null character). |
| 114 |
\fBTcl_DStringValue\fR is a macro that returns a pointer to the |
| 115 |
current contents of a dynamic string. |
| 116 |
.PP |
| 117 |
.PP |
| 118 |
\fBTcl_DStringSetLength\fR changes the length of a dynamic string. |
| 119 |
If \fInewLength\fR is less than the string's current length, then |
| 120 |
the string is truncated. |
| 121 |
If \fInewLength\fR is greater than the string's current length, |
| 122 |
then the string will become longer and new space will be allocated |
| 123 |
for the string if needed. |
| 124 |
However, \fBTcl_DStringSetLength\fR will not initialize the new |
| 125 |
space except to provide a terminating null character; it is up to the |
| 126 |
caller to fill in the new space. |
| 127 |
\fBTcl_DStringSetLength\fR does not free up the string's storage space |
| 128 |
even if the string is truncated to zero length, so \fBTcl_DStringFree\fR |
| 129 |
will still need to be called. |
| 130 |
.PP |
| 131 |
\fBTcl_DStringTrunc\fR changes the length of a dynamic string. |
| 132 |
This procedure is now deprecated. \fBTcl_DStringSetLength\fR should |
| 133 |
be used instead. |
| 134 |
.PP |
| 135 |
\fBTcl_DStringFree\fR should be called when you are finished using |
| 136 |
the string. It frees up any memory that was allocated for the string |
| 137 |
and reinitializes the string's value to an empty string. |
| 138 |
.PP |
| 139 |
\fBTcl_DStringResult\fR sets the result of \fIinterp\fR to the value of |
| 140 |
the dynamic string given by \fIdsPtr\fR. It does this by moving |
| 141 |
a pointer from \fIdsPtr\fR to the interpreter's result. |
| 142 |
This saves the cost of allocating new memory and copying the string. |
| 143 |
\fBTcl_DStringResult\fR also reinitializes the dynamic string to |
| 144 |
an empty string. |
| 145 |
.PP |
| 146 |
\fBTcl_DStringGetResult\fR does the opposite of \fBTcl_DStringResult\fR. |
| 147 |
It sets the value of \fIdsPtr\fR to the result of \fIinterp\fR and |
| 148 |
it clears \fIinterp\fR's result. |
| 149 |
If possible it does this by moving a pointer rather than by copying |
| 150 |
the string. |
| 151 |
|
| 152 |
.SH KEYWORDS |
| 153 |
append, dynamic string, free, result |