Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[oweals/cde.git] / cde / lib / DtSvc / DtEncap / stringbuf.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /*
24  * File:         stringbuf.c $XConsortium: stringbuf.c /main/3 1995/10/26 15:41:59 rswiston $
25  * Language:     C
26  *
27  * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
28  *
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.                                *
33  */
34
35 #include <bms/sbport.h>       /* NOTE: sbport.h must be the first include. */
36
37 #include <stdarg.h>
38
39 #include <bms/bms.h>
40 #include <bms/stringbuf.h>    /* public declarations */
41 #include <bms/MemoryMgr.h>    /* Xe_make_struct ... */
42
43 /* Forwards */
44 static int ExpandStringBuffer (XeStringBuffer buffer);
45
46 #ifndef DEBUG
47 #   define DEBUG 0
48 #endif
49
50 #define EXPANSION_FACTOR 4
51
52   /** 
53      BUFFER_MIN_SIZE  was chosen to be 16 so 
54         BUFSIZE % EXPANSION_FACTOR > 0
55         and 
56         (BUFSIZE + (BUFSIZE % EXPANSION_FACTOR)) % EXPANSION_FACTOR 
57         > (BUFSIZE % EXPANSION_FACTOR)
58    **/  
59      
60 #define BUFFER_MIN_SIZE 16
61
62 /*------------------------------------------------------------------------+*/
63 static int
64 ExpandStringBuffer(XeStringBuffer buffer)
65 /*------------------------------------------------------------------------+*/
66 {
67    int new_size = buffer->size + buffer->increment;
68    XeString new_string = Xe_make_ntype(new_size, XeChar);
69
70 #  if DEBUG >= 3
71       sprintf(debug_string, "expanding string buffer. current size is: %d", buffer->size);
72       print_debug_info(3, debug_string);
73 #  endif
74
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);
80 #  if DEBUG >= 3
81       sprintf(debug_string, "new size is: %d", buffer->size);
82       print_debug_info(4, debug_string);
83 #  endif
84    return(0);
85 }
86
87 /*------------------------------------------------------------------------+*/
88 int
89 XeAppendToStringBuffer(XeStringBuffer buffer, XeString string)
90 /*------------------------------------------------------------------------+*/
91 {
92    int size = buffer->size;
93    XeString buffer_position;  /* so operations are pointer based */
94    int i;
95    
96 #  if DEBUG >= 5
97       sprintf(debug_string, "appending: \"%s\" to: \"%s\"", string, buffer->buffer);
98       print_debug_info(5, debug_string);
99 #  endif
100
101    if (!(string && string[0])) {
102       return(0);
103    }
104    
105    buffer_position = &(buffer->buffer[buffer->last_char]);
106    for (i = buffer->last_char; *string ; i++) {
107       if (i == size) {
108           buffer->last_char = i - 1;
109           if (-1 == ExpandStringBuffer(buffer)) return(-1);
110           size = buffer->size;
111           buffer_position = &(buffer->buffer[i]);
112       }
113       *buffer_position++ = *string++;
114    }
115    if (i == size) {
116        buffer->last_char = i - 1;
117        if (-1 == ExpandStringBuffer(buffer)) return(-1);
118    }
119    buffer->buffer[i] = (XeChar)'\0';
120    buffer->last_char = i;
121 #  if DEBUG >= 5
122       sprintf(debug_string, "new string is: \"%s\"", buffer->buffer);
123       print_debug_info(6, debug_string);
124 #  endif
125    return(0);
126 }
127
128 /*------------------------------------------------------------------------+*/
129 void
130 XeClearStringBuffer(XeStringBuffer buffer)
131 /*------------------------------------------------------------------------+*/
132 {
133    XeString string = buffer->buffer;
134    string[0] = (XeChar)'\0';
135    buffer->last_char = 0;
136 }
137
138
139 /*------------------------------------------------------------------------+*/
140 XeStringBuffer
141 XeMakeStringBuffer(int increment_size)
142 /*------------------------------------------------------------------------+*/
143 {
144    XeStringBuffer new_buffer;
145    increment_size = (increment_size > BUFFER_MIN_SIZE)
146                         ? increment_size 
147                         : BUFFER_MIN_SIZE;
148
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);
154    return(new_buffer);
155 }