Text converters

Text converters — Streaming plain and flowed text conversion

Functions

Types and Values

Description

String APIs accept and produce UTF-8. Stream APIs use the selected input and output charsets, defaulting to UTF-8. Invalid or unrepresentable text is an error, not silently replaced. BOM detection and emission are opt-in.

Plain text copying preserves newlines. Flowed conversion recognizes CRLF, LF and bare CR as input line endings and writes LF, independently of the host platform. Wrapping counts Unicode scalar values rather than UTF-16 code units; it never splits a UTF-8 character.

Converters may be reused after success or failure, but must not be used concurrently or reentrantly. Conversion reads from the current input position and leaves both streams open. On failure, stream output may be partial. The string convenience API returns no partial result.

Functions

g_mime_text_converter_get_input_format ()

GMimeTextFormat
g_mime_text_converter_get_input_format
                               (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

the input format.


g_mime_text_converter_get_output_format ()

GMimeTextFormat
g_mime_text_converter_get_output_format
                               (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

the output format.


g_mime_text_converter_get_input_charset ()

const char *
g_mime_text_converter_get_input_charset
                               (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

the stream input charset, initially "utf-8".

[transfer none]


g_mime_text_converter_set_input_charset ()

gboolean
g_mime_text_converter_set_input_charset
                               (GMimeTextConverter *converter,
                                const char *charset,
                                GError **error);

Selects strict stream decoding. Generic UTF-16/32 names select little-endian input. Strings are already UTF-8 and are not affected by this setting.

Parameters

converter

a text converter

 

charset

a MIME charset name

 

error

return location for a GError, or NULL

 

Returns

TRUE on success; unsupported charsets leave the setting unchanged.


g_mime_text_converter_get_output_charset ()

const char *
g_mime_text_converter_get_output_charset
                               (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

the stream output charset, initially "utf-8".

[transfer none]


g_mime_text_converter_set_output_charset ()

gboolean
g_mime_text_converter_set_output_charset
                               (GMimeTextConverter *converter,
                                const char *charset,
                                GError **error);

Selects strict stream encoding. Unrepresentable characters are errors. Generic UTF-16/32 names select little-endian bytes. String results are always UTF-8. Output has no BOM unless explicitly enabled.

Parameters

converter

a text converter

 

charset

a MIME charset name

 

error

return location for a GError, or NULL

 

Returns

TRUE on success; unsupported charsets leave the setting unchanged.


g_mime_text_converter_get_detect_encoding_from_byte_order_mark ()

gboolean
g_mime_text_converter_get_detect_encoding_from_byte_order_mark
                               (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

whether stream BOM detection is enabled, initially FALSE.


g_mime_text_converter_set_detect_encoding_from_byte_order_mark ()

void
g_mime_text_converter_set_detect_encoding_from_byte_order_mark
                               (GMimeTextConverter *converter,
                                gboolean detect);

When enabled, consumes an initial UTF-8, UTF-16LE/BE or UTF-32LE/BE BOM and overrides the input charset. Without a BOM the configured charset is used. When disabled, BOMs are ordinary text. Does not affect string input.

Parameters

converter

a text converter

 

detect

whether to detect the stream encoding from its BOM

 

g_mime_text_converter_get_write_bom ()

gboolean
g_mime_text_converter_get_write_bom (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

whether stream BOM emission is enabled, initially FALSE.


g_mime_text_converter_set_write_bom ()

void
g_mime_text_converter_set_write_bom (GMimeTextConverter *converter,
                                     gboolean write_bom);

Writes a BOM at the current output position for UTF-8, UTF-16 or UTF-32, including for empty output. Other charsets have no BOM. String output is unaffected. Defaults to FALSE rather than .NET's encoding-specific preamble.

Parameters

converter

a text converter

 

write_bom

whether to emit a Unicode byte-order mark

 

g_mime_text_converter_get_input_stream_buffer_size ()

gsize
g_mime_text_converter_get_input_stream_buffer_size
                               (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

the stream decoding buffer size in bytes, initially 4096.


g_mime_text_converter_set_input_stream_buffer_size ()

void
g_mime_text_converter_set_input_stream_buffer_size
                               (GMimeTextConverter *converter,
                                gsize size);

Sets the decoder read size. Additional space retains incomplete characters.

Parameters

converter

a text converter

 

size

a positive byte count, at most G_MAXSSIZE

 

g_mime_text_converter_get_output_stream_buffer_size ()

gsize
g_mime_text_converter_get_output_stream_buffer_size
                               (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

the stream encoding buffer size in bytes, initially 4096.


g_mime_text_converter_set_output_stream_buffer_size ()

void
g_mime_text_converter_set_output_stream_buffer_size
                               (GMimeTextConverter *converter,
                                gsize size);

Sets the encoder buffer size. At least 32 bytes are reserved for characters and shift sequences.

Parameters

converter

a text converter

 

size

a positive byte count, at most G_MAXSSIZE

 

g_mime_text_converter_get_header ()

const char *
g_mime_text_converter_get_header (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

borrowed UTF-8 text prepended to the converted body.

[nullable]


g_mime_text_converter_set_header ()

void
g_mime_text_converter_set_header (GMimeTextConverter *converter,
                                  const char *header);

Copies header . Set before conversion; no newline is added automatically.

Parameters

converter

a text converter

 

header

UTF-8 text, or NULL.

[nullable]

g_mime_text_converter_get_footer ()

const char *
g_mime_text_converter_get_footer (GMimeTextConverter *converter);

Parameters

converter

a text converter

 

Returns

borrowed UTF-8 text appended to the converted body.

[nullable]


g_mime_text_converter_set_footer ()

void
g_mime_text_converter_set_footer (GMimeTextConverter *converter,
                                  const char *footer);

Copies footer . Set before conversion; no newline is added automatically.

Parameters

converter

a text converter

 

footer

UTF-8 text, or NULL.

[nullable]

g_mime_text_converter_convert ()

gboolean
g_mime_text_converter_convert (GMimeTextConverter *converter,
                               GMimeStream *input,
                               GMimeStream *output,
                               GError **error);

Converts from the current input position, prepending/appending the configured header/footer. Flushes output on success, without closing or rewinding either stream. Output can be partial on failure. Shared backing storage must not overlap. Invalid or unrepresentable characters are reported in G_CONVERT_ERROR; I/O errors use GMIME_ERROR with errno, or GMIME_ERROR_GENERAL when unavailable. Encoding defaults to UTF-8 with no BOM detection or emission, except that RTF converters default to Latin-1 input to preserve wire bytes.

Parameters

converter

a text converter

 

input

a readable stream in the configured input charset

 

output

a writable stream in the configured output charset, distinct from input

 

error

return location for a GError, or NULL

 

Returns

TRUE on success.


g_mime_text_converter_convert_to_string ()

char *
g_mime_text_converter_convert_to_string
                               (GMimeTextConverter *converter,
                                const char *text,
                                gssize length,
                                GError **error);

Converts a bounded string. Input need not be NUL-terminated when length is nonnegative. Malformed UTF-8 is rejected. Input and output are always UTF-8, independent of stream charset and BOM settings. Plain-text converters reject embedded NUL; HTML converters apply their tokenizer's NUL handling. If the converted output contains a NUL, returns NULL with G_CONVERT_ERROR_EMBEDDED_NUL.

Parameters

converter

a text converter

 

text

UTF-8 input

 

length

input length in bytes, or -1 for a terminated string

 

error

return location for a GError, or NULL

 

Returns

converted UTF-8 text; free with g_free().

[transfer full][nullable]


g_mime_text_to_text_new ()

GMimeTextConverter *
g_mime_text_to_text_new (void);

Returns

a plain text copier.

[transfer full]


g_mime_text_to_flowed_new ()

GMimeTextConverter *
g_mime_text_to_flowed_new (void);

Converts plain text to flowed text. Label the output MIME part text/plain; format=flowed; delsp=yes. Does not change MIME headers.

Returns

a plain-to-flowed converter.

[transfer full]


g_mime_flowed_to_text_new ()

GMimeTextConverter *
g_mime_flowed_to_text_new (void);

Returns

a flowed-to-plain converter with delsp=no.

[transfer full]


g_mime_flowed_to_text_get_delete_space ()

gboolean
g_mime_flowed_to_text_get_delete_space
                               (GMimeFlowedToText *converter);

Parameters

converter

a flowed-to-plain converter

 

Returns

whether trailing soft-break spaces are removed.


g_mime_flowed_to_text_set_delete_space ()

void
g_mime_flowed_to_text_set_delete_space
                               (GMimeFlowedToText *converter,
                                gboolean delete_space);

Set before conversion. Defaults to FALSE, corresponding to delsp=no.

Parameters

converter

a flowed-to-plain converter

 

delete_space

whether the input MIME part has delsp=yes

 

Types and Values

GMimeTextConverter

typedef struct _GMimeTextConverter GMimeTextConverter;

An abstract, synchronous text converter with UTF-8 string APIs and selectable stream charsets.


struct GMimeTextConverterClass

struct GMimeTextConverterClass {
	GObjectClass parent_class;

	GMimeTextFormat input_format;
	GMimeTextFormat output_format;

	gboolean (*convert) (GMimeTextConverter *converter, GMimeStream *input, GMimeStream *output, GError **error);
};

Base class for text converters. Implementations must not retain stream arguments after returning, and must leave the converter reusable on failure.

Members

GMimeTextFormat input_format;

the input text format

 

GMimeTextFormat output_format;

the output text format

 

convert ()

converts UTF-8 streams, including the configured header/footer, reporting invalid input and I/O failures through error ; does not flush or close streams

 

GMimeTextToText

typedef struct _GMimeTextToText GMimeTextToText;

Copies UTF-8 text without changing its newlines.


GMimeTextToFlowed

typedef struct _GMimeTextToFlowed GMimeTextToFlowed;

Converts plain text to format=flowed; delsp=yes.


GMimeFlowedToText

typedef struct _GMimeFlowedToText GMimeFlowedToText;

Unwraps format=flowed text.