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 librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 // $XConsortium: UAS_Buffer.cc /main/3 1996/06/11 16:36:44 cde-hal $
26 #include "UAS_Buffer.hh"
28 // If necessary in the future, we can rework UAS_Buffer class to provide
29 // global buffer by default and private buffers on request. SWM
31 char *UAS_Buffer::f_start;
32 char *UAS_Buffer::f_end;
33 int UAS_Buffer::f_reference_count;
35 // /////////////////////////////////////////////////////////////////
37 // /////////////////////////////////////////////////////////////////
40 UAS_Buffer::init (unsigned int initial_size)
42 f_start = new char [initial_size];
43 f_end = f_start + initial_size;
46 UAS_Buffer::UAS_Buffer (unsigned int initial_size)
48 // printf ("Constructing a %d byte buffer\n", initial_size);
52 f_end_of_data = f_point;
57 // /////////////////////////////////////////////////////////////////
59 // /////////////////////////////////////////////////////////////////
61 UAS_Buffer::~UAS_Buffer()
65 if (f_reference_count == 0)
76 // /////////////////////////////////////////////////////////////////
77 // check_space - make sure the is space for additional data
78 // /////////////////////////////////////////////////////////////////
81 UAS_Buffer::check_space (unsigned int size)
83 if (f_point + size > f_end)
85 // Allocate new, larger space.
86 char *new_block = new char [f_point + size - f_start];
87 // Copy bytes in use up to (but not including) point.
88 if (f_point - f_start > 0)
89 memcpy (new_block, f_start, f_point - f_start);
90 // Compute the point in the new buffer.
91 f_point = new_block + (f_point - f_start);
93 // Update start and end relative to new buffer.
95 f_end = f_start + size;
100 // /////////////////////////////////////////////////////////////////
101 // read/write for integers
102 // /////////////////////////////////////////////////////////////////
105 static unsigned int g_ordering = 0x01020304;
107 static unsigned int g_ordering = 0x01020304;
109 static const unsigned char *const g_byte_pos =
110 (const unsigned char *const) &g_ordering;
112 static char *const g_intbuf = (char *) &g_int;
115 UAS_Buffer::write (const ssize_t integer)
117 char *inbuf = (char *) &integer;
119 // Convert bytes from native to MSB first ordering.
121 for (int i = 0; i < sizeof (int); i++)
122 if (g_byte_pos[i] != 0)
123 g_intbuf[g_byte_pos[i]-1] = inbuf[i];
125 write (g_intbuf, 4, 1);
129 UAS_Buffer::read (int *integer)
133 // Convert from MSB first ordering to native.
134 for (int i = 0; i < sizeof (int); i++)
135 if (g_byte_pos[i] != 0)
136 ((char *) integer)[i] = f_point[g_byte_pos[i]-1];
142 // /////////////////////////////////////////////////////////////////
143 // read/write for strings
144 // /////////////////////////////////////////////////////////////////
147 UAS_Buffer::write (const char *string)
149 ssize_t length = strlen (string);
151 write (string, sizeof (char), length + 1);
154 // NOTE: The caller is responsible for deleting the string that is
155 // returned by this function!
158 UAS_Buffer::read (char **string)
162 *string = new char [length + 1];
163 read (*string, sizeof (char), length + 1);
168 UAS_Buffer::read (char *const string)
172 read (string, sizeof (char), length + 1);
177 // /////////////////////////////////////////////////////////////////
178 // read/write for bytes
179 // /////////////////////////////////////////////////////////////////
182 UAS_Buffer::write (const char *bytes, unsigned int size, unsigned int length)
184 int num_bytes = size * length;
185 check_space (num_bytes);
186 for (int i = 0; i < size * length; i++)
187 memcpy (f_point, bytes, num_bytes);
188 f_point += num_bytes;
189 f_end_of_data = f_point;
193 UAS_Buffer::read (char **bytes, unsigned int size, unsigned int length)
195 int num_bytes = size * length;
197 for (int i = 0; i < size * length; i++)
198 f_point += num_bytes;
202 UAS_Buffer::read (char *const bytes, unsigned int size, unsigned int length)
205 read (&b, size, length);
206 memcpy (bytes, b, size * length);