1 | /** |
---|
2 | * gnome-object-io.c: Helper routines for loading and saving of objects |
---|
3 | * |
---|
4 | * Author: |
---|
5 | * Miguel de Icaza (miguel@kernel.org) |
---|
6 | * |
---|
7 | * Copyright 1999, Helix Code, Inc. |
---|
8 | */ |
---|
9 | #include <config.h> |
---|
10 | #include <bonobo/Bonobo.h> |
---|
11 | #include <bonobo/bonobo-object.h> |
---|
12 | #include <bonobo/bonobo-exception.h> |
---|
13 | #include <bonobo/bonobo-object-io.h> |
---|
14 | #include <bonobo/bonobo-object-client.h> |
---|
15 | #include <bonobo/bonobo-stream-client.h> |
---|
16 | |
---|
17 | /** |
---|
18 | * bonobo_persist_stream_save_object_iid: |
---|
19 | * @target: A Bonobo_Stream object where the @object_iid will be written |
---|
20 | * @object_iid: the OBJECT ID to write to the @target stream |
---|
21 | * @ev: Error values are returned here |
---|
22 | * |
---|
23 | * This routine saves the @object_iid in the @target stream. |
---|
24 | */ |
---|
25 | void |
---|
26 | bonobo_persist_stream_save_object_iid (Bonobo_Stream target, |
---|
27 | const CORBA_char *object_iid, |
---|
28 | CORBA_Environment *ev) |
---|
29 | { |
---|
30 | char *copy; |
---|
31 | int len, slen; |
---|
32 | |
---|
33 | g_return_if_fail (target != CORBA_OBJECT_NIL); |
---|
34 | g_return_if_fail (object_iid != NULL); |
---|
35 | |
---|
36 | slen = strlen (object_iid) + 1; |
---|
37 | len = sizeof (gint32) + slen; |
---|
38 | copy = g_malloc (len); |
---|
39 | *((gint32 *) copy) = slen; |
---|
40 | strcpy (copy + sizeof (gint32), object_iid); |
---|
41 | |
---|
42 | bonobo_stream_client_write (target, copy, len, ev); |
---|
43 | |
---|
44 | if (BONOBO_EX (ev)){ |
---|
45 | CORBA_exception_free (ev); |
---|
46 | return; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * bonobo_persist_stream_load_object_iid: |
---|
52 | * @source: Stream to load the OBJECT ID from. |
---|
53 | * |
---|
54 | * Loads a OBJECT ID from the @source Bonobo_Stream CORBA object reference. |
---|
55 | * |
---|
56 | * Returns: a pointer to the OBJECT ID retrieved from the @source Bonobo_Stream |
---|
57 | * object, or %NULL if an error happens. |
---|
58 | */ |
---|
59 | char * |
---|
60 | bonobo_persist_stream_load_object_iid (Bonobo_Stream source) |
---|
61 | { |
---|
62 | CORBA_Environment ev; |
---|
63 | Bonobo_Stream_iobuf *buf; |
---|
64 | CORBA_long n; |
---|
65 | char *rval; |
---|
66 | |
---|
67 | g_return_val_if_fail (source != CORBA_OBJECT_NIL, NULL); |
---|
68 | |
---|
69 | CORBA_exception_init (&ev); |
---|
70 | Bonobo_Stream_read (source, sizeof (gint32), &buf, &ev); |
---|
71 | if (BONOBO_EX (&ev) || |
---|
72 | buf->_length != sizeof (gint32)){ |
---|
73 | CORBA_exception_free (&ev); |
---|
74 | return NULL; |
---|
75 | } |
---|
76 | |
---|
77 | n = *((gint32 *) buf->_buffer); |
---|
78 | CORBA_free (buf); |
---|
79 | |
---|
80 | Bonobo_Stream_read (source, n, &buf, &ev); |
---|
81 | if (BONOBO_EX (&ev) || |
---|
82 | buf->_length != n) { |
---|
83 | CORBA_exception_free (&ev); |
---|
84 | return NULL; |
---|
85 | } |
---|
86 | |
---|
87 | /* |
---|
88 | * Sanity check: the object-id should be NULL terminated |
---|
89 | */ |
---|
90 | if (buf->_buffer [n - 1] != 0) { |
---|
91 | CORBA_free (buf); |
---|
92 | return NULL; |
---|
93 | } |
---|
94 | |
---|
95 | rval = g_strdup (buf->_buffer); |
---|
96 | CORBA_free (buf); |
---|
97 | CORBA_exception_free (&ev); |
---|
98 | |
---|
99 | return rval; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * bonobo_persiststream_save_to_stream: |
---|
104 | * @pstream: A Bonobo_PersistStream CORBA reference. |
---|
105 | * @stream: A Bonobo_Stream CORBA reference to save object on |
---|
106 | * |
---|
107 | * Queries the object_iid for the @pstream object, and saves this on @object in the |
---|
108 | * @stream and then the object in @pstream is saved. |
---|
109 | * |
---|
110 | * Returns: The IO status for the operation. Might return %GNOME_IOERR_PERSIST_NOT_SUPPORTED |
---|
111 | * if @object does not support the IDL:Bonobo/PersistStream:1.0 interface |
---|
112 | */ |
---|
113 | GnomeIOStatus |
---|
114 | bonobo_persiststream_save_to_stream (Bonobo_PersistStream pstream, Bonobo_Stream target, |
---|
115 | const char *object_iid) |
---|
116 | { |
---|
117 | CORBA_Environment ev; |
---|
118 | |
---|
119 | g_return_val_if_fail (pstream != CORBA_OBJECT_NIL, GNOME_IOERR_GENERAL); |
---|
120 | g_return_val_if_fail (target != CORBA_OBJECT_NIL, GNOME_IOERR_GENERAL); |
---|
121 | |
---|
122 | CORBA_exception_init (&ev); |
---|
123 | |
---|
124 | bonobo_persist_stream_save_object_iid (target, object_iid, &ev); |
---|
125 | |
---|
126 | Bonobo_PersistStream_save (pstream, target, "", &ev); |
---|
127 | if (BONOBO_EX (&ev)){ |
---|
128 | CORBA_exception_free (&ev); |
---|
129 | return GNOME_IOERR_GENERAL; |
---|
130 | } |
---|
131 | |
---|
132 | return GNOME_IO_OK; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * bonobo_object_save_to_stream: |
---|
137 | * @object: A BonoboObject |
---|
138 | * @stream: A Bonobo_Stream CORBA reference to save object on |
---|
139 | * |
---|
140 | * Saves the BonoboObject @object in the @stream. |
---|
141 | * |
---|
142 | * Returns: The IO status for the operation. Might return %GNOME_IOERR_PERSIST_NOT_SUPPORTED |
---|
143 | * if @object does not support the IDL:Bonobo/PersistStream:1.0 interface |
---|
144 | */ |
---|
145 | GnomeIOStatus |
---|
146 | bonobo_object_save_to_stream (BonoboObject *object, Bonobo_Stream stream, |
---|
147 | const char *object_iid) |
---|
148 | { |
---|
149 | Bonobo_PersistStream pstream; |
---|
150 | |
---|
151 | g_return_val_if_fail (object != NULL, GNOME_IOERR_GENERAL); |
---|
152 | g_return_val_if_fail (BONOBO_IS_OBJECT (object), GNOME_IOERR_GENERAL); |
---|
153 | g_return_val_if_fail (stream != CORBA_OBJECT_NIL, GNOME_IOERR_GENERAL); |
---|
154 | |
---|
155 | pstream = bonobo_object_query_interface ( |
---|
156 | BONOBO_OBJECT (object), "IDL:Bonobo/PersistStream:1.0"); |
---|
157 | |
---|
158 | if (pstream != CORBA_OBJECT_NIL) |
---|
159 | return GNOME_IOERR_PERSIST_NOT_SUPPORTED; |
---|
160 | |
---|
161 | return bonobo_persiststream_save_to_stream (pstream, stream, object_iid); |
---|
162 | |
---|
163 | return GNOME_IO_OK; |
---|
164 | } |
---|