File: bonobo-stream.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 (303 lines) | stat: -rw-r--r-- 7,404 bytes parent folder | download
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**
 * bonobo-stream.c: Stream manipulation, abstract class
 *
 * Author:
 *     Miguel de Icaza (miguel@gnu.org).
 *
 * Copyright 1999, 2000 Helix Code, Inc.
 */
#include <config.h>

#include <bonobo/bonobo-stream.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-storage-plugin.h>

static BonoboObjectClass *bonobo_stream_parent_class;

POA_Bonobo_Stream__vepv bonobo_stream_vepv;

#define CLASS(o) BONOBO_STREAM_CLASS(GTK_OBJECT(o)->klass)

static inline BonoboStream *
bonobo_stream_from_servant (PortableServer_Servant servant)
{
	return BONOBO_STREAM (bonobo_object_from_servant (servant));
}

static Bonobo_StorageInfo*
impl_Bonobo_Stream_getInfo (PortableServer_Servant servant,
			    const Bonobo_StorageInfoFields mask,
			    CORBA_Environment *ev)
{
	BonoboStream *stream = bonobo_stream_from_servant (servant);

	return CLASS (stream)->get_info (stream, mask, ev);
}

static void          
impl_Bonobo_Stream_setInfo (PortableServer_Servant servant,
			    const Bonobo_StorageInfo *info,
			    const Bonobo_StorageInfoFields mask,
			    CORBA_Environment *ev)
{
	BonoboStream *stream = bonobo_stream_from_servant (servant);

	CLASS (stream)->set_info (stream, info, mask, ev);
}

static void
impl_Bonobo_Stream_read (PortableServer_Servant servant,
			 CORBA_long             count,
			 Bonobo_Stream_iobuf  **buffer,
			 CORBA_Environment     *ev)
{
	BonoboStream *stream = bonobo_stream_from_servant (servant);

	CLASS (stream)->read (stream, count, buffer, ev);
}

static void
impl_Bonobo_Stream_write (PortableServer_Servant servant,
			  const Bonobo_Stream_iobuf *buffer,
			  CORBA_Environment *ev)
{
	BonoboStream *stream = bonobo_stream_from_servant (servant);

	CLASS (stream)->write (stream, buffer, ev);
}

static CORBA_long
impl_Bonobo_Stream_seek (PortableServer_Servant servant,
			 CORBA_long offset,
			 Bonobo_Stream_SeekType whence,
			 CORBA_Environment *ev)
{
	BonoboStream *stream = bonobo_stream_from_servant (servant);

	return CLASS (stream)->seek (stream, offset, whence, ev);
}

static void
impl_Bonobo_Stream_truncate (PortableServer_Servant servant,
			     CORBA_long length,
			     CORBA_Environment *ev)
{
	BonoboStream *stream = bonobo_stream_from_servant (servant);

	CLASS (stream)->truncate (stream, length, ev);
}

static void
impl_Bonobo_Stream_copyTo (PortableServer_Servant servant,
			   const CORBA_char *dest,
			   CORBA_long bytes,
			   CORBA_long *read,
			   CORBA_long *written,
			   CORBA_Environment *ev)
{
	BonoboStream *stream = bonobo_stream_from_servant (servant);

	CLASS (stream)->copy_to (stream, dest, bytes, read, written, ev);
}

static void
impl_Bonobo_Stream_commit (PortableServer_Servant servant,
			   CORBA_Environment * ev)
{
       BonoboStream *stream = bonobo_stream_from_servant (servant);

       CLASS (stream)->commit (stream, ev);
}

static void
impl_Bonobo_Stream_revert (PortableServer_Servant servant,
			   CORBA_Environment * ev)
{
	BonoboStream *stream = bonobo_stream_from_servant (servant);

	CLASS (stream)->revert (stream, ev);
}

/**
 * bonobo_stream_get_epv:
 */
POA_Bonobo_Stream__epv *
bonobo_stream_get_epv (void)
{
	POA_Bonobo_Stream__epv *epv;

	epv = g_new0 (POA_Bonobo_Stream__epv, 1);

	epv->getInfo  = impl_Bonobo_Stream_getInfo;
	epv->setInfo  = impl_Bonobo_Stream_setInfo;
	epv->read     = impl_Bonobo_Stream_read;
	epv->write    = impl_Bonobo_Stream_write;
	epv->seek     = impl_Bonobo_Stream_seek;
	epv->truncate = impl_Bonobo_Stream_truncate;
	epv->copyTo   = impl_Bonobo_Stream_copyTo;
	epv->commit   = impl_Bonobo_Stream_commit;
	epv->revert   = impl_Bonobo_Stream_revert;

	return epv;
}

static void
init_stream_corba_class (void)
{
	/* The VEPV */
	bonobo_stream_vepv.Bonobo_Unknown_epv = bonobo_object_get_epv ();
	bonobo_stream_vepv.Bonobo_Stream_epv = bonobo_stream_get_epv ();
}

static void
bonobo_stream_class_init (BonoboStreamClass *klass)
{
	bonobo_stream_parent_class = gtk_type_class (bonobo_object_get_type ());

	init_stream_corba_class ();
}

/**
 * bonobo_stream_get_type:
 *
 * Returns: the GtkType for a BonoboStream.
 */
GtkType
bonobo_stream_get_type (void)
{
	static GtkType type = 0;

	if (!type){
		GtkTypeInfo info = {
			"BonoboStream",
			sizeof (BonoboStream),
			sizeof (BonoboStreamClass),
			(GtkClassInitFunc) bonobo_stream_class_init,
			(GtkObjectInitFunc) NULL,
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

		type = gtk_type_unique (bonobo_object_get_type (), &info);
	}

	return type;
}

/**
 * bonobo_stream_open:
 * @driver: driver to use for opening.
 * @path: path where the base file resides
 * @flags: Bonobo Storage OpenMode
 * @mode: Unix open(2) mode
 *
 * Opens or creates the file named at @path with the stream driver @driver.
 *
 * @driver is one of: "fs" or "vfs" for now.
 *
 * Returns: a created BonoboStream object.
 */
BonoboStream *
bonobo_stream_open_full (const char *driver, const char *path, gint flags, 
			 gint mode, CORBA_Environment *opt_ev)
{
	BonoboStream  *stream = NULL;
	StoragePlugin *p;
	CORBA_Environment ev, *my_ev;
	
	if (!opt_ev) {
		CORBA_exception_init (&ev);
		my_ev = &ev;
	} else
		my_ev = opt_ev;

	if (!driver || !path)
		CORBA_exception_set (my_ev, CORBA_USER_EXCEPTION, 
				     ex_Bonobo_Storage_IOError, NULL);
	else if (!(p = bonobo_storage_plugin_find (driver)) ||
		 !p->stream_open)
		CORBA_exception_set (my_ev, CORBA_USER_EXCEPTION, 
				     ex_Bonobo_Storage_NotSupported, NULL);
	else 
		stream = p->stream_open (path, flags, mode, my_ev);

	if (!opt_ev) {
		if (BONOBO_EX (my_ev)) {
			char *text;
			
			text = bonobo_exception_get_text (my_ev);
			g_warning ("bonobo_stream_open failed '%s'", text);
			g_free (text);
		}
		
		CORBA_exception_free (&ev);
	}

	return stream;
}

BonoboStream *
bonobo_stream_open (const char *driver, const char *path,
		    gint flags, gint mode)
{
	return bonobo_stream_open_full (driver, path, flags, mode, NULL);
}

/**
 * bonobo_stream_corba_object_create:
 * @object: the GtkObject that will wrap the CORBA object
 *
 * Creates and activates the CORBA object that is wrapped by the
 * @object BonoboObject.
 *
 * Returns: An activated object reference to the created object
 * or %CORBA_OBJECT_NIL in case of failure.
 */
Bonobo_Stream
bonobo_stream_corba_object_create (BonoboObject *object)
{
	POA_Bonobo_Stream *servant;
	CORBA_Environment ev;

	servant = (POA_Bonobo_Stream *) g_new0 (BonoboObjectServant, 1);
	servant->vepv = &bonobo_stream_vepv;

	CORBA_exception_init (&ev);

	POA_Bonobo_Stream__init ((PortableServer_Servant) servant, &ev);
	if (BONOBO_EX (&ev)){
                g_free (servant);
		CORBA_exception_free (&ev);
                return CORBA_OBJECT_NIL;
        }

	CORBA_exception_free (&ev);

	return bonobo_object_activate_servant (object, servant);
}

/**
 * bonobo_internal_get_major_mime_type:
 *
 * This is not the function you are looking for. In fact, you've never
 * even heard of this function.
 * But when you wake up, you will remember to g_free the result.
 */
gchar *
bonobo_internal_get_major_mime_type (const char *mime_type)
{
	char *major_end;
	char *major;
	int   major_length;

	major_end = strchr (mime_type, '/');
	if (!major_end)
		return g_strdup (mime_type);

	major_length = major_end - mime_type;
	major = g_strndup (mime_type, major_length);
	
	return major;
}