diff options
Diffstat (limited to 'xdelta1/libedsio/fh.c')
-rwxr-xr-x | xdelta1/libedsio/fh.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/xdelta1/libedsio/fh.c b/xdelta1/libedsio/fh.c new file mode 100755 index 0000000..9135f8b --- /dev/null +++ b/xdelta1/libedsio/fh.c | |||
@@ -0,0 +1,164 @@ | |||
1 | /* -*-Mode: C;-*- | ||
2 | * $Id: fh.c 1.3 Wed, 31 Mar 1999 17:39:58 -0800 jmacd $ | ||
3 | * | ||
4 | * Copyright (C) 1998, 1999, Josh MacDonald. | ||
5 | * All Rights Reserved. | ||
6 | * | ||
7 | * Author: Josh MacDonald <jmacd@CS.Berkeley.EDU> | ||
8 | */ | ||
9 | |||
10 | #include "edsio.h" | ||
11 | |||
12 | /* Handle source/sink impls | ||
13 | */ | ||
14 | typedef struct _HandleSerialSource HandleSerialSource; | ||
15 | typedef struct _HandleSerialSink HandleSerialSink; | ||
16 | |||
17 | struct _HandleSerialSource { | ||
18 | SerialSource source; | ||
19 | |||
20 | FileHandle* fh; | ||
21 | }; | ||
22 | |||
23 | struct _HandleSerialSink { | ||
24 | SerialSink sink; | ||
25 | |||
26 | FileHandle* fh; | ||
27 | gpointer data1; | ||
28 | gpointer data2; | ||
29 | gpointer data3; | ||
30 | gboolean (* cont_onclose) (gpointer data1, gpointer data2, gpointer data3); | ||
31 | }; | ||
32 | |||
33 | static SerialType handle_source_type (SerialSource* source, gboolean set_allocation); | ||
34 | static gboolean handle_source_close (SerialSource* source); | ||
35 | static gboolean handle_source_read (SerialSource* source, guint8 *ptr, guint32 len); | ||
36 | static void handle_source_free (SerialSource* source); | ||
37 | |||
38 | static gboolean handle_sink_type (SerialSink* sink, SerialType type, guint len, gboolean set_allocation); | ||
39 | static gboolean handle_sink_close (SerialSink* sink); | ||
40 | static gboolean handle_sink_write (SerialSink* sink, const guint8 *ptr, guint32 len); | ||
41 | static void handle_sink_free (SerialSink* sink); | ||
42 | |||
43 | SerialSource* | ||
44 | handle_source (FileHandle* fh) | ||
45 | { | ||
46 | HandleSerialSource* it = g_new0 (HandleSerialSource, 1); | ||
47 | |||
48 | serializeio_source_init (&it->source, | ||
49 | handle_source_type, | ||
50 | handle_source_close, | ||
51 | handle_source_read, | ||
52 | handle_source_free, | ||
53 | NULL, | ||
54 | NULL); | ||
55 | |||
56 | it->fh = fh; | ||
57 | |||
58 | return &it->source; | ||
59 | } | ||
60 | |||
61 | SerialSink* | ||
62 | handle_sink (FileHandle* fh, gpointer data1, gpointer data2, gpointer data3, gboolean (* cont_onclose) (gpointer data1, gpointer data2, gpointer data3)) | ||
63 | { | ||
64 | HandleSerialSink* it = g_new0 (HandleSerialSink, 1); | ||
65 | |||
66 | serializeio_sink_init (&it->sink, | ||
67 | handle_sink_type, | ||
68 | handle_sink_close, | ||
69 | handle_sink_write, | ||
70 | handle_sink_free, | ||
71 | NULL); | ||
72 | |||
73 | it->fh = fh; | ||
74 | it->data1 = data1; | ||
75 | it->data2 = data2; | ||
76 | it->data3 = data3; | ||
77 | it->cont_onclose = cont_onclose; | ||
78 | |||
79 | return &it->sink; | ||
80 | } | ||
81 | |||
82 | SerialType | ||
83 | handle_source_type (SerialSource* source, gboolean set_allocation) | ||
84 | { | ||
85 | HandleSerialSource* ssource = (HandleSerialSource*) source; | ||
86 | guint32 x; | ||
87 | |||
88 | if (! ssource->fh->table->table_handle_getui (ssource->fh, &x)) | ||
89 | return ST_Error; | ||
90 | |||
91 | if (set_allocation) | ||
92 | { | ||
93 | if (! ssource->fh->table->table_handle_getui (ssource->fh, &source->alloc_total)) | ||
94 | return ST_Error; | ||
95 | } | ||
96 | |||
97 | return x; | ||
98 | } | ||
99 | |||
100 | gboolean | ||
101 | handle_source_close (SerialSource* source) | ||
102 | { | ||
103 | HandleSerialSource* ssource = (HandleSerialSource*) source; | ||
104 | |||
105 | return ssource->fh->table->table_handle_close (ssource->fh, 0); | ||
106 | } | ||
107 | |||
108 | gboolean | ||
109 | handle_source_read (SerialSource* source, guint8 *ptr, guint32 len) | ||
110 | { | ||
111 | HandleSerialSource* ssource = (HandleSerialSource*) source; | ||
112 | |||
113 | return ssource->fh->table->table_handle_read (ssource->fh, ptr, len) == len; | ||
114 | } | ||
115 | |||
116 | void | ||
117 | handle_source_free (SerialSource* source) | ||
118 | { | ||
119 | g_free (source); | ||
120 | } | ||
121 | |||
122 | gboolean | ||
123 | handle_sink_type (SerialSink* sink, SerialType type, guint len, gboolean set_allocation) | ||
124 | { | ||
125 | HandleSerialSink* ssink = (HandleSerialSink*) sink; | ||
126 | |||
127 | if (! ssink->fh->table->table_handle_putui (ssink->fh, type)) | ||
128 | return FALSE; | ||
129 | |||
130 | if (set_allocation && ! ssink->fh->table->table_handle_putui (ssink->fh, len)) | ||
131 | return FALSE; | ||
132 | |||
133 | return TRUE; | ||
134 | } | ||
135 | |||
136 | gboolean | ||
137 | handle_sink_close (SerialSink* sink) | ||
138 | { | ||
139 | HandleSerialSink* ssink = (HandleSerialSink*) sink; | ||
140 | |||
141 | if (ssink->fh->table->table_handle_close (ssink->fh, 0)) | ||
142 | { | ||
143 | if (ssink->cont_onclose) | ||
144 | return ssink->cont_onclose (ssink->data1, ssink->data2, ssink->data3); | ||
145 | |||
146 | return TRUE; | ||
147 | } | ||
148 | |||
149 | return FALSE; | ||
150 | } | ||
151 | |||
152 | gboolean | ||
153 | handle_sink_write (SerialSink* sink, const guint8 *ptr, guint32 len) | ||
154 | { | ||
155 | HandleSerialSink* ssink = (HandleSerialSink*) sink; | ||
156 | |||
157 | return ssink->fh->table->table_handle_write (ssink->fh, ptr, len); | ||
158 | } | ||
159 | |||
160 | void | ||
161 | handle_sink_free (SerialSink* sink) | ||
162 | { | ||
163 | g_free (sink); | ||
164 | } | ||