2 Copyright (C) 2010, Parrot Foundation.
7 src/dynpmc/gziphandle.pmc - GzipHandle PMC
11 The GzipHandle PMC performs I/O operations on a source or destination file.
13 This PMC wraps the zlib.
15 =head2 Vtable Functions
24 #include "parrot/extend.h"
26 /* HEADERIZER HFILE: none */
27 /* HEADERIZER BEGIN: static */
28 /* HEADERIZER END: static */
30 pmclass GzipHandle extends Handle dynpmc auto_attrs {
37 Initializes a newly created FileHandle object.
43 PARROT_GZIPHANDLE(SELF)->file = NULL;
48 =item C<INTVAL get_bool()>
50 Returns whether the GzipHandle has reached the end of the file.
55 VTABLE INTVAL get_bool() {
57 GET_ATTR_file(INTERP, SELF, file);
65 =head2 Stream-Oriented Methods
69 =item C<METHOD open(STRING *filename, STRING *mode :optional)>
71 Opens the file at the given filename (including path) with the given mode. The
72 invocant is modified and becomes an open filehandle.
77 METHOD open(STRING *filename, STRING *mode :optional,
78 INTVAL has_mode :opt_flag) {
79 char * const path = Parrot_str_to_cstring(INTERP, filename);
83 char * const mod = Parrot_str_to_cstring(INTERP, mode);
84 file = gzopen(path, mod);
85 Parrot_str_free_cstring(mod);
88 file = gzopen(path, "rb");
90 Parrot_str_free_cstring(path);
93 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
96 SET_ATTR_file(INTERP, SELF, file);
102 =item C<METHOD close()>
104 Close the gziphandle.
112 GET_ATTR_file(INTERP, SELF, file);
113 status = gzclose(file);
114 RETURN(INTVAL status);
119 =item C<METHOD eof()>
121 Returns true if the filehandle is at end-of-file, returns false otherwise.
129 GET_ATTR_file(INTERP, SELF, file);
130 status = gzeof(file);
131 RETURN(INTVAL status);
136 =item C<METHOD flush()>
138 Flushes the gziphandle.
146 GET_ATTR_file(INTERP, SELF, file);
147 status = gzflush(file, Z_SYNC_FLUSH);
148 RETURN(INTVAL status);
153 =item C<METHOD print([INTVAL|FLOATVAL|STRING *|PMC*] value)>
155 Print the passed in integer, number, string, or PMC to the gziphandle.
156 (Integers, numbers, and strings are auto-boxed as PMCs.)
161 METHOD print(PMC *value) {
163 STRING * const str = VTABLE_get_string(INTERP, value);
164 char * const buf = Parrot_str_to_cstring(INTERP, str);
165 const UINTVAL len = Parrot_str_byte_length(INTERP, str);
167 GET_ATTR_file(INTERP, SELF, file);
168 (void)gzwrite(file, buf, len);
169 Parrot_str_free_cstring(buf);
175 =item C<METHOD puts(STRING *value)>
177 Print the string to the gziphandle.
182 METHOD puts(STRING *value) {
185 char * const buf = Parrot_str_to_cstring(INTERP, value);
186 const UINTVAL len = Parrot_str_byte_length(INTERP, value);
188 GET_ATTR_file(INTERP, SELF, file);
189 status = gzwrite(file, buf, len);
190 Parrot_str_free_cstring(buf);
191 RETURN(INTVAL status);
196 =item C<METHOD read(INTVAL bytes)>
198 Read the given number of bytes from the gziphandle and return them in a string.
203 METHOD read(INTVAL length) {
206 STRING *str = STRINGNULL;
207 char * const buf = mem_allocate_n_zeroed_typed(length, char);
208 GET_ATTR_file(INTERP, SELF, file);
209 result = gzread(file, buf, length);
211 str = Parrot_str_new(INTERP, buf, result);
225 =item C<METHOD version()>
227 Returns a string with the zlib version.
236 STRING *version = Parrot_str_new_constant(INTERP, zlibVersion());
237 RETURN(STRING *version);
240 METHOD compress(STRING *str) {
243 STRING *dst = STRINGNULL;
244 UINTVAL srcLen, dstLen;
245 char * const src = Parrot_str_to_cstring(INTERP, str);
248 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
249 "failed to allocate");
251 srcLen = Parrot_str_byte_length(INTERP, str);
252 dstLen = 12 + srcLen + srcLen / 1000;
253 buf = mem_allocate_n_zeroed_typed(dstLen, char);
256 Parrot_str_free_cstring(src);
257 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
258 "failed to allocate");
261 rc = compress((Bytef *)buf, &dstLen, (const Bytef *)src, srcLen);
262 Parrot_str_free_cstring(src);
266 dst = Parrot_str_new(INTERP, buf, dstLen);
272 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
273 "not enough memory");
278 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
279 "output buffer error");
283 /* these are the only three documented return values */
290 METHOD uncompress(STRING *str) {
293 STRING *dst = STRINGNULL;
294 UINTVAL srcLen, dstLen;
295 char * const src = Parrot_str_to_cstring(INTERP, str);
298 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
299 "failed to allocate");
301 srcLen = Parrot_str_byte_length(INTERP, str);
304 buf = mem_allocate_n_zeroed_typed(dstLen, char);
307 Parrot_str_free_cstring(src);
308 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
309 "failed to allocate");
312 rc = uncompress((Bytef *)buf, &dstLen, (const Bytef *)src, srcLen);
316 Parrot_str_free_cstring(src);
317 dst = Parrot_str_new(INTERP, buf, dstLen);
322 Parrot_str_free_cstring(src);
324 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
325 "not enough memory");
334 Parrot_str_free_cstring(src);
336 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
337 "input data corrupted");
341 /* these are the only four documented return values */
348 METHOD crc32(INTVAL crc, STRING *str) {
350 char * const src = Parrot_str_to_cstring(INTERP, str);
353 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
354 "failed to allocate");
356 srcLen = Parrot_str_byte_length(INTERP, str);
358 crc = crc32(crc, (const Bytef *)src, srcLen);
365 * c-file-style: "parrot"
367 * vim: expandtab shiftwidth=4: