File: strings.c

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (207 lines) | stat: -rw-r--r-- 5,352 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207





/* 
 * from TclUtil.c --
 *
 *      This file contains utility procedures that are used by many Sci
 *      commands.
 *
 * Copyright (c) 1987-1993 The Regents of the University of California.
 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * SCCS: @(#) SciUtil.c 1.161 97/08/12 17:07:18
 */

#include "dstrings.h"

/*
 *----------------------------------------------------------------------
 *
 * Sci_DStringInit --
 *
 *      Initializes a dynamic string, discarding any previous contents
 *      of the string (Sci_DStringFree should have been called already
 *      if the dynamic string was previously in use).
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      The dynamic string is initialized to be empty.
 *
 *----------------------------------------------------------------------
 */

void
Sci_DStringInit (dsPtr)
     Sci_DString *dsPtr;	/* Pointer to structure for dynamic string. */
{
  dsPtr->string = dsPtr->staticSpace;
  dsPtr->length = 0;
  dsPtr->spaceAvl = SCI_DSTRING_STATIC_SIZE;
  dsPtr->staticSpace[0] = 0;
}


/*
 *----------------------------------------------------------------------
 *
 * Sci_DStringAppend --
 *
 *      Append more characters to the current value of a dynamic string.
 *
 * Results:
 *      The return value is a pointer to the dynamic string's new value.
 *
 * Side effects:
 *      Length bytes from string (or all of string if length is less
 *      than zero) are added to the current value of the string. Memory
 *      gets reallocated if needed to accomodate the string's new size.
 *
 *----------------------------------------------------------------------
 */

char *
Sci_DStringAppend (dsPtr, string, length)
     Sci_DString *dsPtr;	/* Structure describing dynamic string. */
     const char *string;	/* String to append.  If length is -1 then
				 * this must be null-terminated. */
     int length;		/* Number of characters from string to
				 * append.  If < 0, then append all of string,
				 * up to null at end. */
{
  int newSize;
  char *newString, *dst;
  const char *end;

  if (length < 0)
    {
      length = strlen (string);
    }
  newSize = length + dsPtr->length;

  /*
   * Allocate a larger buffer for the string if the current one isn't
   * large enough. Allocate extra space in the new buffer so that there
   * will be room to grow before we have to allocate again.
   */

  if (newSize >= dsPtr->spaceAvl)
    {
      dsPtr->spaceAvl = newSize * 2;
      newString = (char *) malloc ((unsigned) dsPtr->spaceAvl);
      memcpy ((void *) newString, (void *) dsPtr->string,
	      (size_t) dsPtr->length);
      if (dsPtr->string != dsPtr->staticSpace)
	{
	  free (dsPtr->string);
	}
      dsPtr->string = newString;
    }

  /*
   * Copy the new string into the buffer at the end of the old
   * one.
   */

  for (dst = dsPtr->string + dsPtr->length, end = string + length;
       string < end; string++, dst++)
    {
      *dst = *string;
    }
  *dst = '\0';
  dsPtr->length += length;
  return dsPtr->string;
}

/*
 *----------------------------------------------------------------------
 *
 * Sci_DStringSetLength --
 *
 *      Change the length of a dynamic string.  This can cause the
 *      string to either grow or shrink, depending on the value of
 *      length.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      The length of dsPtr is changed to length and a null byte is
 *      stored at that position in the string.  If length is larger
 *      than the space allocated for dsPtr, then a panic occurs.
 *
 *----------------------------------------------------------------------
 */

void
Sci_DStringSetLength (dsPtr, length)
     Sci_DString *dsPtr;	/* Structure describing dynamic string. */
     int length;		/* New length for dynamic string. */
{
  if (length < 0)
    {
      length = 0;
    }
  if (length >= dsPtr->spaceAvl)
    {
      char *newString;

      dsPtr->spaceAvl = length + 1;
      newString = (char *) malloc ((unsigned) dsPtr->spaceAvl);

      /*
       * SPECIAL NOTE: must use memcpy, not strcpy, to copy the string
       * to a larger buffer, since there may be embedded NULLs in the
       * string in some cases.
       */

      memcpy ((void *) newString, (void *) dsPtr->string,
	      (size_t) dsPtr->length);
      if (dsPtr->string != dsPtr->staticSpace)
	{
	  free (dsPtr->string);
	}
      dsPtr->string = newString;
    }
  dsPtr->length = length;
  dsPtr->string[length] = 0;
}

/*
 *----------------------------------------------------------------------
 *
 * Sci_DStringFree --
 *
 *      Frees up any memory allocated for the dynamic string and
 *      reinitializes the string to an empty state.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      The previous contents of the dynamic string are lost, and
 *      the new value is an empty string.
 *
 *----------------------------------------------------------------------
 */

void
Sci_DStringFree (dsPtr)
     Sci_DString *dsPtr;	/* Structure describing dynamic string. */
{
  if (dsPtr->string != dsPtr->staticSpace)
    {
      free (dsPtr->string);
    }
  dsPtr->string = dsPtr->staticSpace;
  dsPtr->length = 0;
  dsPtr->spaceAvl = SCI_DSTRING_STATIC_SIZE;
  dsPtr->staticSpace[0] = 0;
}