File: bonobo-object-io.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 (164 lines) | stat: -rw-r--r-- 4,439 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
/**
 * gnome-object-io.c: Helper routines for loading and saving of objects
 *
 * Author:
 *   Miguel de Icaza (miguel@kernel.org)
 *
 * Copyright 1999, Helix Code, Inc.
 */
#include <config.h>
#include <bonobo/Bonobo.h>
#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-object-io.h>
#include <bonobo/bonobo-object-client.h>
#include <bonobo/bonobo-stream-client.h>

/** 
 * bonobo_persist_stream_save_object_iid:
 * @target: A Bonobo_Stream object where the @object_iid will be written
 * @object_iid: the OBJECT ID to write to the @target stream
 * @ev: Error values are returned here
 *
 * This routine saves the @object_iid in the @target stream.
 */
void
bonobo_persist_stream_save_object_iid (Bonobo_Stream target,
				   const CORBA_char *object_iid,
				   CORBA_Environment *ev)
{
	char *copy;
	int len, slen;
	
	g_return_if_fail (target != CORBA_OBJECT_NIL);
	g_return_if_fail (object_iid != NULL);

	slen = strlen (object_iid) + 1;
	len = sizeof (gint32) + slen;
	copy = g_malloc (len);
	*((gint32 *) copy) = slen;
	strcpy (copy + sizeof (gint32), object_iid);
		
	bonobo_stream_client_write (target, copy, len, ev);

	if (BONOBO_EX (ev)){
		CORBA_exception_free (ev);
		return;
	}
}

/**
 * bonobo_persist_stream_load_object_iid:
 * @source: Stream to load the OBJECT ID from.
 *
 * Loads a OBJECT ID from the @source Bonobo_Stream CORBA object reference.
 *
 * Returns: a pointer to the OBJECT ID retrieved from the @source Bonobo_Stream
 * object, or %NULL if an error happens.
 */
char *
bonobo_persist_stream_load_object_iid (Bonobo_Stream source)
{
	CORBA_Environment ev;
	Bonobo_Stream_iobuf *buf;
	CORBA_long n;
	char      *rval;
	
	g_return_val_if_fail (source != CORBA_OBJECT_NIL, NULL);

	CORBA_exception_init (&ev);
	Bonobo_Stream_read (source, sizeof (gint32), &buf, &ev);
	if (BONOBO_EX (&ev) ||
	    buf->_length != sizeof (gint32)){
		CORBA_exception_free (&ev);
		return NULL;
	}

	n = *((gint32 *) buf->_buffer);
	CORBA_free (buf);
	
	Bonobo_Stream_read (source, n, &buf, &ev);
	if (BONOBO_EX (&ev) ||
	    buf->_length != n) {
		CORBA_exception_free (&ev);
		return NULL;
	}
	
	/*
	 * Sanity check: the object-id should be NULL terminated
	 */
	if (buf->_buffer [n - 1] != 0) {
		CORBA_free (buf);
		return NULL;
	}

	rval = g_strdup (buf->_buffer);
	CORBA_free (buf);
	CORBA_exception_free (&ev);

	return rval;
}

/**
 * bonobo_persiststream_save_to_stream:
 * @pstream: A Bonobo_PersistStream CORBA reference.
 * @stream: A Bonobo_Stream CORBA reference to save object on
 *
 * Queries the object_iid for the @pstream object, and saves this on  @object in the
 * @stream and then the object in @pstream is saved.
 *
 * Returns: The IO status for the operation.  Might return %GNOME_IOERR_PERSIST_NOT_SUPPORTED
 * if @object does not support the IDL:Bonobo/PersistStream:1.0 interface
 */
GnomeIOStatus
bonobo_persiststream_save_to_stream (Bonobo_PersistStream pstream, Bonobo_Stream target,
				    const char *object_iid)
{
	CORBA_Environment ev;
	
	g_return_val_if_fail (pstream != CORBA_OBJECT_NIL, GNOME_IOERR_GENERAL);
	g_return_val_if_fail (target != CORBA_OBJECT_NIL, GNOME_IOERR_GENERAL);
	
	CORBA_exception_init (&ev);

	bonobo_persist_stream_save_object_iid (target, object_iid, &ev);

	Bonobo_PersistStream_save (pstream, target, "", &ev);
	if (BONOBO_EX (&ev)){
		CORBA_exception_free (&ev);
		return GNOME_IOERR_GENERAL;
	}

	return GNOME_IO_OK;
}

/**
 * bonobo_object_save_to_stream:
 * @object: A BonoboObject
 * @stream: A Bonobo_Stream CORBA reference to save object on
 *
 * Saves the BonoboObject @object in the @stream.
 *
 * Returns: The IO status for the operation.  Might return %GNOME_IOERR_PERSIST_NOT_SUPPORTED
 * if @object does not support the IDL:Bonobo/PersistStream:1.0 interface
 */
GnomeIOStatus
bonobo_object_save_to_stream (BonoboObject *object, Bonobo_Stream stream,
			     const char *object_iid)
{
	Bonobo_PersistStream pstream;
	
	g_return_val_if_fail (object != NULL, GNOME_IOERR_GENERAL);
	g_return_val_if_fail (BONOBO_IS_OBJECT (object), GNOME_IOERR_GENERAL);
	g_return_val_if_fail (stream != CORBA_OBJECT_NIL, GNOME_IOERR_GENERAL);

	pstream = bonobo_object_query_interface (
		BONOBO_OBJECT (object), "IDL:Bonobo/PersistStream:1.0");
	
	if (pstream != CORBA_OBJECT_NIL)
		return GNOME_IOERR_PERSIST_NOT_SUPPORTED;

	return bonobo_persiststream_save_to_stream (pstream, stream, object_iid);
	
	return GNOME_IO_OK;
}