File: bonobo-stream-client.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (310 lines) | stat: -rw-r--r-- 7,573 bytes parent folder | download | duplicates (2)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
 * gnome-stream-client.c: Helper routines to access a Bonobo_Stream CORBA object
 *
 * Authors:
 *   Nat Friedman    (nat@nat.org)
 *   Miguel de Icaza (miguel@kernel.org).
 *   Michael Meekss  (michael@helixcode.com)
 *
 * Copyright 1999,2000 Helix Code, Inc.
 */
#include <config.h>

#include <bonobo/Bonobo.h>
#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo-stream-client.h>

#define CORBA_BLOCK_SIZE 65536

/**
 * bonobo_stream_client_write:
 * @stream: A CORBA Object reference to a Bonobo_Stream
 * @buffer: the buffer to write
 * @size: number of bytes to write
 * @ev: a CORBA environment to return status information.
 *
 * This is a helper routine to write @size bytes from @buffer to the
 * @stream.  It will continue to write bytes until a fatal error
 * occurs. It works around serious problems in ORBit's handling of
 * sequences, and makes for nicer, saner protocol usage for
 * transfering huge chunks of data.
 */
void
bonobo_stream_client_write (const Bonobo_Stream stream,
			    const void *buffer, const size_t size,
			    CORBA_Environment *ev)
{
	Bonobo_Stream_iobuf *buf;
	size_t               pos;
	guint8              *mem = (guint8 *)buffer;

	if (size == 0)
		return;

	g_return_if_fail (ev != NULL);
	
	if (buffer == NULL || stream == CORBA_OBJECT_NIL)
		goto bad_param;

	buf = Bonobo_Stream_iobuf__alloc ();
	if (!buf)
		goto alloc_error;

	for (pos = 0; pos < size;) {
		buf->_buffer = (mem + pos);
		buf->_length = (pos + CORBA_BLOCK_SIZE < size) ?
			CORBA_BLOCK_SIZE : size - pos;
		buf->_maximum = buf->_length;

		Bonobo_Stream_write (stream, buf, ev);
		if (BONOBO_EX (ev)) {
			CORBA_free (buf);
			return;
		}
		pos += buf->_length;
	}

	CORBA_free (buf);
	return;

 alloc_error:
	CORBA_exception_set_system (ev, ex_CORBA_NO_MEMORY,
				    CORBA_COMPLETED_NO);
	return;

 bad_param:
	CORBA_exception_set_system (ev, ex_CORBA_BAD_PARAM,
				    CORBA_COMPLETED_NO);
	return;
}

/**
 * bonobo_stream_client_write_string:
 * @stream: A CORBA object reference to a #Bonobo_Stream.
 * @str: A string.
 * @terminate: Whether or not to write the \0 at the end of the
 * string.
 * @ev: A pointer to a #CORBA_Environment
 *
 * This is a helper routine to write the string in @str to @stream.
 * If @terminate is TRUE, a NULL character will be written out at the
 * end of the string.  This function will not return until the entire
 * string has been written out, unless an exception is raised.  See
 * also bonobo_stream_client_write(). Continues writing until finished
 * or a fatal exception occurs.
 *
 */
void
bonobo_stream_client_write_string (const Bonobo_Stream stream, const char *str,
				   gboolean terminate, CORBA_Environment *ev)
{
	size_t total_length;

	g_return_if_fail (ev != NULL);
	g_return_if_fail (str != NULL);

	total_length = strlen (str) + (terminate ? 1 : 0);

	bonobo_stream_client_write (stream, str, total_length, ev);
}

/**
 * bonobo_stream_client_printf:
 * @stream: A CORBA object reference to a #Bonobo_Stream.
 * @terminate: Whether or not to null-terminate the string when it is
 * written out to the stream.
 * @ev: A CORBA_Environment pointer.
 * @fmt: The printf format string.
 *
 * Processes @fmt and the arguments which follow it to produce a
 * string.  Writes this string out to @stream.  This function will not
 * return until the entire string is written out, unless an exception
 * is raised.  See also bonobo_stream_client_write_string() and
 * bonobo_stream_client_write().
 */
void
bonobo_stream_client_printf (const Bonobo_Stream stream, const gboolean terminate,
			     CORBA_Environment *ev, const char *fmt, ...)
{
	va_list      args;
	char        *str;

	g_return_if_fail (fmt != NULL);

	va_start (args, fmt);
	str = g_strdup_vprintf (fmt, args);
	va_end (args);

	bonobo_stream_client_write_string (stream, str, terminate, ev);

	g_free (str);
}

/**
 * bonobo_stream_client_read_string:
 * @stream: The #Bonobo_Stream from which the string will be read.
 * @str: The string pointer in which the string will be stored.
 * @ev: A pointer to a #CORBA_Environment.
 *
 * Reads a NULL-terminated string from @stream and stores it in a
 * newly-allocated string in @str.
 *
 * Returns: The number of bytes read, or -1 if an error occurs.
 * If an exception occurs, @ev will contain the exception.
 */
CORBA_long
bonobo_stream_client_read_string (const Bonobo_Stream stream, char **str,
				  CORBA_Environment *ev)
{
	Bonobo_Stream_iobuf *buffer;
	GString             *gstr;
	gboolean             all;

	gstr = g_string_sized_new (16);

	for (all = FALSE; !all; ) {

		Bonobo_Stream_read (stream, 1,
				    &buffer, ev);

		if (BONOBO_EX (ev))
			break;

		else if (buffer->_length == 0 ||
			 buffer->_buffer [0] == '\0')
			all = TRUE;
		
		else {
			g_string_append_c (gstr, buffer->_buffer [0]);
			CORBA_free (buffer);
		}
	}

	if (BONOBO_EX (ev)) {
		*str = NULL;
		g_string_free (gstr, TRUE);

		return -1;
	} else {
		CORBA_long l;

		l    = gstr->len;
		*str = gstr->str;
		g_string_free (gstr, FALSE);

		return l;
	}
}

/**
 * bonobo_stream_client_get_length:
 * @stream: The stream.
 * @ev: Exception environment
 * 
 *   Does the grunt work to get the length of a stream,
 * returns -1 if the length is not available. Returns -1
 * on exception.
 * 
 * Return value: Length or -1
 **/
CORBA_long
bonobo_stream_client_get_length (const Bonobo_Stream stream,
				 CORBA_Environment  *ev)
{
	CORBA_long len;
	Bonobo_StorageInfo *info;

	g_return_val_if_fail (ev != NULL, -1);

	info = Bonobo_Stream_getInfo (stream, Bonobo_FIELD_SIZE, ev);

	if (BONOBO_EX (ev) || !info)
		return -1;

	len = info->size;

	CORBA_free (info);
	
	return len;
}

/**
 * bonobo_stream_client_read:
 * @stream: A CORBA Object reference to a Bonobo_Stream
 * @size: number of bytes to read or -1 for whole stream.
 * @length_read: if non NULL will be set to the length read
 * @ev: a CORBA environment to return status information.
 *
 * This is a helper routine to read @size bytes from the @stream into
 * a freshly g_ allocated buffer which is returned. Whilst this
 * routine may seem pointless; it reads the stream in small chunks
 * avoiding possibly massive alloca's inside ORBit's stub/skel code.
 *
 * Returns NULL on any sort of failure & 0 size read.
 */
guint8 *
bonobo_stream_client_read (const Bonobo_Stream stream,
			   const size_t        size,
			   CORBA_long         *length_read,
			   CORBA_Environment  *ev)
{
	size_t  pos;
	guint8 *mem;
	size_t  length;

	g_return_val_if_fail (ev != NULL, NULL);

	if (length_read)
		*length_read = size;

	length = size;

	if (length == -1) {
		length = bonobo_stream_client_get_length (stream, ev);
		if (BONOBO_EX (ev) || length == -1) {
			g_warning ("Exception getting length / FIXME: print "
				   "stream doesn't support length determination");
			return NULL;
		}
	} 

	*length_read = length;

	if (length == 0)
		return NULL;

	mem = g_malloc (length);
	if (!mem) {
		CORBA_exception_set_system (ev, ex_CORBA_NO_MEMORY,
					    CORBA_COMPLETED_NO);
		return NULL;
	}
	
	for (pos = 0; pos < length;) {
		Bonobo_Stream_iobuf *buf;
		CORBA_long           len;

		len = (pos + CORBA_BLOCK_SIZE < length) ?
			CORBA_BLOCK_SIZE : length - pos;

		Bonobo_Stream_read (stream, len, &buf, ev);

		if (BONOBO_EX (ev) || !buf)
			goto io_error;

		if (buf->_length > 0) {
			memcpy (mem + pos, buf->_buffer, buf->_length);
			pos += buf->_length;
		} else {
			g_warning ("Buffer length %d", buf->_length);
			goto io_error;
		}
		CORBA_free (buf);
	}

	return mem;

 io_error:
	return NULL;
}