1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * INTERFACE: C Rappture Buffer Interface Source |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Derrick Kearney, Purdue University |
---|
7 | * Copyright (c) 2004-2007 Purdue Research Foundation |
---|
8 | * |
---|
9 | * See the file "license.terms" for information on usage and |
---|
10 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
11 | * ====================================================================== |
---|
12 | */ |
---|
13 | |
---|
14 | #include "RpBuffer.h" |
---|
15 | #include "RpBufferCInterface.h" |
---|
16 | #include "RpOutcomeCHelper.h" |
---|
17 | |
---|
18 | #ifdef __cplusplus |
---|
19 | extern "C" { |
---|
20 | #endif |
---|
21 | |
---|
22 | int |
---|
23 | RapptureBufferInit(RapptureBuffer* buf) |
---|
24 | { |
---|
25 | if (buf == NULL) { |
---|
26 | return -1; |
---|
27 | } |
---|
28 | |
---|
29 | buf->_buf = NULL; |
---|
30 | |
---|
31 | return 0; |
---|
32 | } |
---|
33 | |
---|
34 | int |
---|
35 | RapptureBufferNew(RapptureBuffer* buf) |
---|
36 | { |
---|
37 | if (buf == NULL) { |
---|
38 | return -1; |
---|
39 | } |
---|
40 | |
---|
41 | RapptureBufferFree(buf); |
---|
42 | buf->_buf = (void*) new Rappture::Buffer(); |
---|
43 | |
---|
44 | return 0; |
---|
45 | } |
---|
46 | |
---|
47 | int |
---|
48 | RapptureBufferFree(RapptureBuffer* buf) |
---|
49 | { |
---|
50 | if (buf->_buf != NULL) { |
---|
51 | delete ((Rappture::Buffer*)buf->_buf); |
---|
52 | buf->_buf = NULL; |
---|
53 | } |
---|
54 | |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | int |
---|
59 | RpBufferToCBuffer(Rappture::Buffer* rpbuf, RapptureBuffer* buf) |
---|
60 | { |
---|
61 | if (rpbuf == NULL) { |
---|
62 | return -1; |
---|
63 | } |
---|
64 | |
---|
65 | if (buf == NULL) { |
---|
66 | return -1; |
---|
67 | } |
---|
68 | |
---|
69 | RapptureBufferFree(buf); |
---|
70 | buf->_buf = (void*) new Rappture::Buffer(*rpbuf); |
---|
71 | |
---|
72 | return 0; |
---|
73 | } |
---|
74 | |
---|
75 | const char* |
---|
76 | RapptureBufferBytes(RapptureBuffer* buf) |
---|
77 | { |
---|
78 | const char* retVal = NULL; |
---|
79 | |
---|
80 | if (buf == NULL) { |
---|
81 | return retVal; |
---|
82 | } |
---|
83 | |
---|
84 | if (buf->_buf == NULL) { |
---|
85 | return retVal; |
---|
86 | } |
---|
87 | |
---|
88 | return ((Rappture::Buffer*)buf->_buf)->bytes(); |
---|
89 | } |
---|
90 | |
---|
91 | unsigned int |
---|
92 | RapptureBufferSize(RapptureBuffer* buf) |
---|
93 | { |
---|
94 | unsigned int retVal = 1; |
---|
95 | |
---|
96 | if (buf == NULL) { |
---|
97 | return retVal; |
---|
98 | } |
---|
99 | |
---|
100 | if (buf->_buf == NULL) { |
---|
101 | return retVal; |
---|
102 | } |
---|
103 | |
---|
104 | return ((Rappture::Buffer*)buf->_buf)->size(); |
---|
105 | } |
---|
106 | |
---|
107 | int |
---|
108 | RapptureBufferAppend(RapptureBuffer* buf, const char* bytes, int size) |
---|
109 | { |
---|
110 | int retVal = 1; |
---|
111 | |
---|
112 | if (buf == NULL) { |
---|
113 | return retVal; |
---|
114 | } |
---|
115 | |
---|
116 | if (buf->_buf == NULL) { |
---|
117 | return retVal; |
---|
118 | } |
---|
119 | |
---|
120 | return ((Rappture::Buffer*)buf->_buf)->append(bytes,size); |
---|
121 | } |
---|
122 | |
---|
123 | int |
---|
124 | RapptureBufferRead(RapptureBuffer* buf, const char* bytes, int size) |
---|
125 | { |
---|
126 | int retVal = 1; |
---|
127 | |
---|
128 | if (buf == NULL) { |
---|
129 | return retVal; |
---|
130 | } |
---|
131 | |
---|
132 | if (buf->_buf == NULL) { |
---|
133 | return retVal; |
---|
134 | } |
---|
135 | |
---|
136 | return ((Rappture::Buffer*)buf->_buf)->read(bytes,size); |
---|
137 | } |
---|
138 | |
---|
139 | int |
---|
140 | RapptureBufferSeek(RapptureBuffer* buf, int offset, int whence) |
---|
141 | { |
---|
142 | int retVal = 1; |
---|
143 | |
---|
144 | if (buf == NULL) { |
---|
145 | return retVal; |
---|
146 | } |
---|
147 | |
---|
148 | if (buf->_buf == NULL) { |
---|
149 | return retVal; |
---|
150 | } |
---|
151 | |
---|
152 | return ((Rappture::Buffer*)buf->_buf)->seek(offset,whence); |
---|
153 | } |
---|
154 | |
---|
155 | int |
---|
156 | RapptureBufferTell(RapptureBuffer* buf) |
---|
157 | { |
---|
158 | int retVal = 1; |
---|
159 | |
---|
160 | if (buf == NULL) { |
---|
161 | return retVal; |
---|
162 | } |
---|
163 | |
---|
164 | if (buf->_buf == NULL) { |
---|
165 | return retVal; |
---|
166 | } |
---|
167 | |
---|
168 | return ((Rappture::Buffer*)buf->_buf)->tell(); |
---|
169 | } |
---|
170 | |
---|
171 | RapptureOutcome |
---|
172 | RapptureBufferLoad(RapptureBuffer* buf, const char* filename) |
---|
173 | { |
---|
174 | Rappture::Outcome s; |
---|
175 | RapptureOutcome status; |
---|
176 | |
---|
177 | RapptureOutcomeInit(&status); |
---|
178 | |
---|
179 | if (buf == NULL) { |
---|
180 | s.error("invalid parameter: buf == NULL"); |
---|
181 | s.addContext("while in RapptureBufferLoad()"); |
---|
182 | RpOutcomeToCOutcome(&s,&status); |
---|
183 | return status; |
---|
184 | } |
---|
185 | |
---|
186 | if (buf->_buf == NULL) { |
---|
187 | s.error("uninitialized parameter: buf, did you call RapptureBufferInit()?"); |
---|
188 | s.addContext("while in RapptureBufferLoad()"); |
---|
189 | RpOutcomeToCOutcome(&s,&status); |
---|
190 | return status; |
---|
191 | } |
---|
192 | |
---|
193 | ((Rappture::Buffer*)buf->_buf)->load(s, filename); |
---|
194 | RpOutcomeToCOutcome(&s,&status); |
---|
195 | return status; |
---|
196 | } |
---|
197 | |
---|
198 | RapptureOutcome |
---|
199 | RapptureBufferDump(RapptureBuffer* buf, const char* filename) |
---|
200 | { |
---|
201 | Rappture::Outcome s; |
---|
202 | RapptureOutcome status; |
---|
203 | |
---|
204 | s.addContext("while in RapptureBufferLoad()"); |
---|
205 | RapptureOutcomeInit(&status); |
---|
206 | |
---|
207 | if (buf == NULL) { |
---|
208 | s.error("invalid parameter: buf == NULL"); |
---|
209 | RpOutcomeToCOutcome(&s,&status); |
---|
210 | return status; |
---|
211 | } |
---|
212 | |
---|
213 | if (buf->_buf == NULL) { |
---|
214 | s.error("uninitialized parameter: buf, did you call RapptureBufferInit()?"); |
---|
215 | RpOutcomeToCOutcome(&s,&status); |
---|
216 | return status; |
---|
217 | } |
---|
218 | |
---|
219 | ((Rappture::Buffer*)buf->_buf)->dump(s, filename); |
---|
220 | RpOutcomeToCOutcome(&s,&status); |
---|
221 | return status; |
---|
222 | } |
---|
223 | |
---|
224 | RapptureOutcome |
---|
225 | RapptureBufferEncode(RapptureBuffer* buf, int compress, int base64) |
---|
226 | { |
---|
227 | Rappture::Outcome s; |
---|
228 | RapptureOutcome status; |
---|
229 | |
---|
230 | RapptureOutcomeInit(&status); |
---|
231 | s.addContext("while in RapptureBufferLoad()"); |
---|
232 | if (buf == NULL) { |
---|
233 | s.error("invalid parameter: buf == NULL"); |
---|
234 | RpOutcomeToCOutcome(&s,&status); |
---|
235 | return status; |
---|
236 | } |
---|
237 | |
---|
238 | if (buf->_buf == NULL) { |
---|
239 | s.error("uninitialized parameter: buf, did you call RapptureBufferInit()?"); |
---|
240 | RpOutcomeToCOutcome(&s, &status); |
---|
241 | return status; |
---|
242 | } |
---|
243 | unsigned int flags; |
---|
244 | flags = RPENC_HDR; |
---|
245 | if (compress) { |
---|
246 | flags |= RPENC_Z; |
---|
247 | } |
---|
248 | if (base64) { |
---|
249 | flags |= RPENC_B64; |
---|
250 | } |
---|
251 | ((Rappture::Buffer*)buf->_buf)->encode(s, flags); |
---|
252 | RpOutcomeToCOutcome(&s,&status); |
---|
253 | return status; |
---|
254 | } |
---|
255 | |
---|
256 | RapptureOutcome |
---|
257 | RapptureBufferDecode(RapptureBuffer* buf, int decompress, int base64 ) |
---|
258 | { |
---|
259 | Rappture::Outcome s; |
---|
260 | RapptureOutcome status; |
---|
261 | |
---|
262 | RapptureOutcomeInit(&status); |
---|
263 | |
---|
264 | if (buf == NULL) { |
---|
265 | s.error("invalid parameter: buf == NULL"); |
---|
266 | s.addContext("while in RapptureBufferLoad()"); |
---|
267 | RpOutcomeToCOutcome(&s,&status); |
---|
268 | return status; |
---|
269 | } |
---|
270 | |
---|
271 | if (buf->_buf == NULL) { |
---|
272 | s.error("uninitialized parameter: buf, did you call RapptureBufferInit()?"); |
---|
273 | s.addContext("while in RapptureBufferLoad()"); |
---|
274 | RpOutcomeToCOutcome(&s,&status); |
---|
275 | return status; |
---|
276 | } |
---|
277 | unsigned int flags; |
---|
278 | flags = 0; |
---|
279 | if (decompress) { |
---|
280 | flags |= RPENC_Z; |
---|
281 | } |
---|
282 | if (base64) { |
---|
283 | flags |= RPENC_B64; |
---|
284 | } |
---|
285 | ((Rappture::Buffer*)buf->_buf)->decode(s, flags); |
---|
286 | RpOutcomeToCOutcome(&s,&status); |
---|
287 | return status; |
---|
288 | } |
---|
289 | |
---|
290 | #ifdef __cplusplus |
---|
291 | } |
---|
292 | #endif // ifdef __cplusplus |
---|