2.7 <GConverter>

Interface: <GConverter>

<GConverter> is an interface for streaming conversions.

<GConverter> is implemented by objects that convert binary data in various ways. The conversion can be stateful and may fail at any place.

Some example conversions are: character set conversion, compression, decompression and regular expression replace.

Procedures

Method: converter:convert self inbuf outbuf flags ⇒ %return bytes-read bytes-written
Method: convert self inbuf outbuf flags ⇒ %return bytes-read bytes-written

This is the main operation used when converting data. It is to be called multiple times in a loop, and each time it will do some work, i.e. producing some output (in outbuf) or consuming some input (from inbuf) or both. If its not possible to do any work an error is returned.

Note that a single call may not consume all input (or any input at all). Also a call may produce output even if given no input, due to state stored in the converter producing output.

If any data was either produced or consumed, and then an error happens, then only the successful conversion is reported and the error is returned on the next call.

A full conversion loop involves calling this method repeatedly, each time giving it new input and space output space. When there is no more input data after the data in inbuf, the flag input-at-end must be set. The loop will be (unless some error happens) returning converted each time until all data is consumed and all output is produced, then finished is returned instead. Note, that finished may be returned even if input-at-end is not set, for instance in a decompression converter where the end of data is detectable from the data (and there might even be other data after the end of the compressed data).

When some data has successfully been converted bytes-read and is set to the number of bytes read from inbuf, and bytes-written is set to indicate how many bytes was written to outbuf. If there are more data to output or consume (i.e. unless the input-at-end is specified) then converted is returned, and if no more data is to be output then finished is returned.

On error error is returned and error is set accordingly. Some errors need special handling:

no-space is returned if there is not enough space to write the resulting converted data, the application should call the function again with a larger outbuf to continue.

partial-input is returned if there is not enough input to fully determine what the conversion should produce, and the input-at-end flag is not set. This happens for example with an incomplete multibyte sequence when converting text, or when a regexp matches up to the end of the input (and may match further input). It may also happen when inbuf-size is zero and there is no more data to produce.

When this happens the application should read more input and then call the function again. If further input shows that there is no more data call the function again with the same data but with the input-at-end flag set. This may cause the conversion to finish as e.g. in the regexp match case (or, to fail again with partial-input in e.g. a charset conversion where the input is actually partial).

After converter:convert has returned finished the converter object is in an invalid state where its not allowed to call converter:convert anymore. At this time you can only free the object or call converter:reset to reset it to the initial state.

If the flag flush is set then conversion is modified to try to write out all internal state to the output. The application has to call the function multiple times with the flag set, and when the available input has been consumed and all internal state has been produced then flushed (or finished if really at the end) is returned instead of converted. This is somewhat similar to what happens at the end of the input stream, but done in the middle of the data.

This has different meanings for different conversions. For instance in a compression converter it would mean that we flush all the compression state into output such that if you uncompress the compressed data you get back all the input data. Doing this may make the final file larger due to padding though. Another example is a regexp conversion, where if you at the end of the flushed data have a match, but there is also a potential longer match. In the non-flushed case we would ask for more input, but when flushing we treat this as the end of input and do the match.

Flushing is not always possible (like if a charset converter flushes at a partial multibyte sequence). Converters are supposed to try to produce as much output as possible and then return an error (typically partial-input).

self

Passed: a <GConverter>.

a <GConverter>.

inbuf

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.

the buffer containing the data to convert.

outbuf

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.

a buffer to write converted data in.

flags

Passed: a <GConverterFlags>, or a list of its symbols, or one of them.

a <GConverterFlags> controlling the conversion details

%return

Returned: a <GConverterResult>.

a <GConverterResult>, error on error.

bytes-read

Returned: an unsigned 64-bit integer.

will be set to the number of bytes read from inbuf on success

bytes-written

Returned: an unsigned 64-bit integer.

will be set to the number of bytes written to outbuf on success

inbuf-size

Not passed: guile-gi gives the length of inbuf.

the number of bytes in inbuf

outbuf-size

Not passed: guile-gi gives the length of outbuf.

the number of bytes in outbuf, must be at least one

Method: converter:convert-bytes self bytes ⇒ %return
Method: convert-bytes self bytes ⇒ %return

Applies self to the data in bytes.

self

Passed: a <GConverter>.

the <GConverter> to use

bytes

Passed: a <GBytes>.

the data to convert

%return

Returned: a <GBytes>.

A newly-allocated <GBytes> with the converted data, or NULL if an error occurred

Method: converter:reset self
Method: reset self

Resets all internal state in the converter, making it behave as if it was just created. If the converter has any internal state that would produce output then that output is lost.

self

Passed: a <GConverter>.

a <GConverter>.

Virtual methods

Virtual method: converter:do-convert implementor self inbuf outbuf flags ⇒ %return bytes-read bytes-written

This is the main operation used when converting data. It is to be called multiple times in a loop, and each time it will do some work, i.e. producing some output (in outbuf) or consuming some input (from inbuf) or both. If its not possible to do any work an error is returned.

Note that a single call may not consume all input (or any input at all). Also a call may produce output even if given no input, due to state stored in the converter producing output.

If any data was either produced or consumed, and then an error happens, then only the successful conversion is reported and the error is returned on the next call.

A full conversion loop involves calling this method repeatedly, each time giving it new input and space output space. When there is no more input data after the data in inbuf, the flag input-at-end must be set. The loop will be (unless some error happens) returning converted each time until all data is consumed and all output is produced, then finished is returned instead. Note, that finished may be returned even if input-at-end is not set, for instance in a decompression converter where the end of data is detectable from the data (and there might even be other data after the end of the compressed data).

When some data has successfully been converted bytes-read and is set to the number of bytes read from inbuf, and bytes-written is set to indicate how many bytes was written to outbuf. If there are more data to output or consume (i.e. unless the input-at-end is specified) then converted is returned, and if no more data is to be output then finished is returned.

On error error is returned and error is set accordingly. Some errors need special handling:

no-space is returned if there is not enough space to write the resulting converted data, the application should call the function again with a larger outbuf to continue.

partial-input is returned if there is not enough input to fully determine what the conversion should produce, and the input-at-end flag is not set. This happens for example with an incomplete multibyte sequence when converting text, or when a regexp matches up to the end of the input (and may match further input). It may also happen when inbuf-size is zero and there is no more data to produce.

When this happens the application should read more input and then call the function again. If further input shows that there is no more data call the function again with the same data but with the input-at-end flag set. This may cause the conversion to finish as e.g. in the regexp match case (or, to fail again with partial-input in e.g. a charset conversion where the input is actually partial).

After converter:convert has returned finished the converter object is in an invalid state where its not allowed to call converter:convert anymore. At this time you can only free the object or call converter:reset to reset it to the initial state.

If the flag flush is set then conversion is modified to try to write out all internal state to the output. The application has to call the function multiple times with the flag set, and when the available input has been consumed and all internal state has been produced then flushed (or finished if really at the end) is returned instead of converted. This is somewhat similar to what happens at the end of the input stream, but done in the middle of the data.

This has different meanings for different conversions. For instance in a compression converter it would mean that we flush all the compression state into output such that if you uncompress the compressed data you get back all the input data. Doing this may make the final file larger due to padding though. Another example is a regexp conversion, where if you at the end of the flushed data have a match, but there is also a potential longer match. In the non-flushed case we would ask for more input, but when flushing we treat this as the end of input and do the match.

Flushing is not always possible (like if a charset converter flushes at a partial multibyte sequence). Converters are supposed to try to produce as much output as possible and then return an error (typically partial-input).

To override it in a new class, give register-type #:vfuncs `((convert . ,procedure)). The procedure is called as (procedure self inbuf outbuf flags) ⇒ %return bytes-read bytes-written.

implementor

Passed: the class whose implementation to call: a class that implements <GConverter>, or its GType number.

self

Passed: a <GConverter>.

a <GConverter>.

inbuf

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer, or #f.

the buffer containing the data to convert.

outbuf

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.

a buffer to write converted data in.

flags

Passed: a <GConverterFlags>, or a list of its symbols, or one of them.

a <GConverterFlags> controlling the conversion details

%return

Returned: a <GConverterResult>.

a <GConverterResult>, error on error.

bytes-read

Returned: an unsigned 64-bit integer.

will be set to the number of bytes read from inbuf on success

bytes-written

Returned: an unsigned 64-bit integer.

will be set to the number of bytes written to outbuf on success

inbuf-size

Not passed: guile-gi gives the length of inbuf.

the number of bytes in inbuf

outbuf-size

Not passed: guile-gi gives the length of outbuf.

the number of bytes in outbuf, must be at least one

Virtual method: converter:do-reset implementor self

Resets all internal state in the converter, making it behave as if it was just created. If the converter has any internal state that would produce output then that output is lost.

To override it in a new class, give register-type #:vfuncs `((reset . ,procedure)). The procedure is called as (procedure self).

implementor

Passed: the class whose implementation to call: a class that implements <GConverter>, or its GType number.

self

Passed: a <GConverter>.

a <GConverter>.