Plasma GitLab Archive
Projects Blog Knowledge

Class Netconversion.conversion_pipe

class conversion_pipe : ?subst:int -> string -> in_enc:encoding -> out_enc:encoding -> unit -> Netchannels.io_obj_channel
This pipeline class (see Netchannels for more information) can be used to recode a netchannel while reading or writing. The argument in_enc is the input encoding, and out_enc is the output encoding.

The channel must consist of a whole number of characters. If it ends with an incomplete multi-byte character, however, this is detected, and the exception Malformed_code will be raised. This exception is also raised for other encoding errors in the channel data.

Example. Convert ISO-8859-1 to UTF-8 while writing to the file "output.txt":

    let ch = new output_channel (open_out "output.txt") in
    let encoder = 
      new conversion_pipe ~in_enc:`Enc_iso88591 ~out_enc:`Enc_utf8 () in
    let ch' = new output_filter encoder ch in
    ... (* write to ch' *)
    ch' # close_out();
    ch  # close_out();  (* you must close both channels! *)

If you write as UTF-16, don't forget to output the byte order mark yourself, as the channel does not do this.

Example. Convert UTF-16 to UTF-8 while reading from the file "input.txt":

    let ch = new input_channel (open_in "input.txt") in
    let encoder = 
      new conversion_pipe ~in_enc:`Enc_utf16 ~out_enc:`Enc_utf8 () in
    let ch' = new input_filter ch encoder in
    ... (* read from ch' *)
    ch' # close_in();
    ch  # close_in();  (* you must close both channels! *)

subst : This function is invoked for code points of in_enc that cannot be represented in out_enc, and the result of the function invocation is substituted (directly, without any further conversion). Restriction: The string returned by subst must not be longer than 50 bytes. If subst is missing, Cannot_represent is raised in this case.

This web site is published by Informatikbüro Gerd Stolpmann
Powered by Caml