2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * File: stringbuf.c $XConsortium: stringbuf.c /main/3 1995/10/26 15:41:59 rswiston $
27 * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
29 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
30 * (c) Copyright 1993, 1994 International Business Machines Corp. *
31 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
32 * (c) Copyright 1993, 1994 Novell, Inc. *
35 #include <bms/sbport.h> /* NOTE: sbport.h must be the first include. */
40 #include <bms/stringbuf.h> /* public declarations */
41 #include <bms/MemoryMgr.h> /* Xe_make_struct ... */
44 static int ExpandStringBuffer (XeStringBuffer buffer);
50 #define EXPANSION_FACTOR 4
53 BUFFER_MIN_SIZE was chosen to be 16 so
54 BUFSIZE % EXPANSION_FACTOR > 0
56 (BUFSIZE + (BUFSIZE % EXPANSION_FACTOR)) % EXPANSION_FACTOR
57 > (BUFSIZE % EXPANSION_FACTOR)
60 #define BUFFER_MIN_SIZE 16
62 /*------------------------------------------------------------------------+*/
64 ExpandStringBuffer(XeStringBuffer buffer)
65 /*------------------------------------------------------------------------+*/
67 int new_size = buffer->size + buffer->increment;
68 XeString new_string = Xe_make_ntype(new_size, XeChar);
71 sprintf(debug_string, "expanding string buffer. current size is: %d", buffer->size);
72 print_debug_info(3, debug_string);
75 memcpy(new_string, buffer->buffer, buffer->last_char + 1);
76 XeFree(buffer->buffer);
77 buffer->size = new_size;
78 buffer->buffer = new_string;
79 buffer->increment += (buffer->increment % EXPANSION_FACTOR);
81 sprintf(debug_string, "new size is: %d", buffer->size);
82 print_debug_info(4, debug_string);
87 /*------------------------------------------------------------------------+*/
89 XeAppendToStringBuffer(XeStringBuffer buffer, XeString string)
90 /*------------------------------------------------------------------------+*/
92 int size = buffer->size;
93 XeString buffer_position; /* so operations are pointer based */
97 sprintf(debug_string, "appending: \"%s\" to: \"%s\"", string, buffer->buffer);
98 print_debug_info(5, debug_string);
101 if (!(string && string[0])) {
105 buffer_position = &(buffer->buffer[buffer->last_char]);
106 for (i = buffer->last_char; *string ; i++) {
108 buffer->last_char = i - 1;
109 if (-1 == ExpandStringBuffer(buffer)) return(-1);
111 buffer_position = &(buffer->buffer[i]);
113 *buffer_position++ = *string++;
116 buffer->last_char = i - 1;
117 if (-1 == ExpandStringBuffer(buffer)) return(-1);
119 buffer->buffer[i] = (XeChar)'\0';
120 buffer->last_char = i;
122 sprintf(debug_string, "new string is: \"%s\"", buffer->buffer);
123 print_debug_info(6, debug_string);
128 /*------------------------------------------------------------------------+*/
130 XeClearStringBuffer(XeStringBuffer buffer)
131 /*------------------------------------------------------------------------+*/
133 XeString string = buffer->buffer;
134 string[0] = (XeChar)'\0';
135 buffer->last_char = 0;
139 /*------------------------------------------------------------------------+*/
141 XeMakeStringBuffer(int increment_size)
142 /*------------------------------------------------------------------------+*/
144 XeStringBuffer new_buffer;
145 increment_size = (increment_size > BUFFER_MIN_SIZE)
149 new_buffer = Xe_make_struct(_XeStringBuffer);
150 new_buffer->buffer = Xe_make_ntype(increment_size, XeChar);
151 new_buffer->increment = increment_size;
152 new_buffer->size = increment_size;
153 XeClearStringBuffer(new_buffer);