Enca Library Reference Manual
<<< Previous Page | Home | Up | Next Page >>>

# Analyser

## Name

Analyser -- Basic analyser interface.

## Synopsis

    EncaAnalyser enca_analyser_alloc            (const char *langname);
    void        enca_analyser_free              (EncaAnalyser analyser);
    EncaEncoding enca_analyse                   (EncaAnalyser analyser,
                                                 unsigned char *buffer,
                                                 size_t size);
    EncaEncoding enca_analyse_const             (EncaAnalyser analyser,
                                                 unsigned char *buffer,
                                                 size_t size);
    int         enca_errno                      (EncaAnalyser analyser);
    const char* enca_strerror                   (EncaAnalyser analyser,
                                                 int errnum);
    int         enca_double_utf8_check          (EncaAnalyser analyser,
                                                 unsigned char *buffer,
                                                 size_t size);
    int*        enca_double_utf8_get_candidates (EncaAnalyser analyser);
    void        enca_set_multibyte              (EncaAnalyser analyser,
                                                 int multibyte);
    void        enca_set_interpreted_surfaces   (EncaAnalyser analyser,
                                                 int interpreted_surfaces);
    void        enca_set_ambiguity              (EncaAnalyser analyser,
                                                 int ambiguity);
    void        enca_set_filtering              (EncaAnalyser analyser,
                                                 int filtering);
    void        enca_set_garbage_test           (EncaAnalyser analyser,
                                                 int garabage_test);
    void        enca_set_termination_strictness (EncaAnalyser analyser,
                                                 int termination_strictness);
    int         enca_set_significant            (EncaAnalyser analyser,
                                                 size_t significant);
    int         enca_set_threshold              (EncaAnalyser analyser,
                                                 double threshold);

## Description

A single working example is better than a hundred pages of reference manual, so:

Example 1. A minimal Enca library application – Czech encoding detector.

    include <stdio.h>
    include <enca.h>

    int
    main(void)
    {
      EncaAnalyser analyser;
      EncaEncoding encoding;
      unsigned char buffer[4096];
      size_t buflen;

      buflen = fread(buffer, 1, 4096, stdin);
      analyser = enca_analyser_alloc("cs");
      encoding = enca_analyse(analyser, buffer, buflen);
      printf("Charset: %s\n", enca_charset_name(encoding.charset,
                                                 ENCA_NAME_STYLE_HUMAN));
      enca_analyser_free(analyser);

      return 0;
    }

The analyser has plenty of options, but generally you don't need to fiddle with them, except enca_set_termination_strictness().

## Details

### enca_analyser_alloc ()

    EncaAnalyser enca_analyser_alloc            (const char *langname);

Allocates an analyser and initializes it for language `language`.

The analyser, once crerated, can be used only for language for which it was initialized. If you need to detect encodings of texts in more than one language, you must allocate an analyser for each one.

`langname` is two-letter ISO 639:1989 language code. Locale names in form language_territory and ISO-639 English language names also may be accepted in the future. To be on the safe side, use only names returned by enca_get_languages().

`langname` : | Language for which the analyser should be initialized.
Returns : | The newly created EncaAnalyser on success, NULL on failure (namely when `langname` is unknown or otherwise invalid).

### enca_analyser_free ()

    void        enca_analyser_free              (EncaAnalyser analyser);

Frees memory used by EncaAnalyser `analyser`.

`analyser` : | An analyser to be destroyed.

### enca_analyse ()

    EncaEncoding enca_analyse                   (EncaAnalyser analyser,
                                                 unsigned char *buffer,
                                                 size_t size);

Analyses `buffer` and finds its encoding.

The `buffer` is checked for 8bit encodings of language for which `analyser` was initialized and for multibyte encodings, mostly independent on language (unless disabled with enca_set_multibyte()).

The contents of `buffer` may be (and probably will be) modified during the analyse. Use enca_analyse_const() instead if this discomforts you.

`analyser` : | An analyser initialized for some language.
`buffer` : | Buffer to be analysed.
`size` : | Size of `buffer`.
Returns : | Encoding of `buffer`. When charset part of return value is ENCA_CS_UNKNOWN, encoding was not determined. Check enca_errno() for reason.

### enca_analyse_const ()

    EncaEncoding enca_analyse_const             (EncaAnalyser analyser,
                                                 unsigned char *buffer,
                                                 size_t size);

Analyses `buffer` and finds its encoding.

The `buffer` is checked for 8bit encodings of language for which `analyser` was initialized and for multibyte encodings, mostly independent on language (unless disabled with enca_set_multibyte()).

This function never modifies `buffer` (can be even used with string literal `buffer`) at the expense it's generally slower than enca_analyse().

`analyser` : | An analyser initialized for some language.
`buffer` : | Buffer to be analysed.
`size` : | Size of `buffer`.
Returns : | Encoding of `buffer`. When charset part of return value is ENCA_CS_UNKNOWN, encoding was not determined. Check enca_errno() for reason.

### enca_errno ()

    int         enca_errno                      (EncaAnalyser analyser);

Returns analyser error code.

The error code is not modified. However, any other analyser call i.e. call to a function taking `analyser` as parameter can change the error code.

`analyser` : | An analyser.
Returns : | Error code of reason why last analyser call failed.

### enca_strerror ()

    const char* enca_strerror                   (EncaAnalyser analyser,
                                                 int errnum);

Returns string describing the error code.

The returned string must be considered constant and must NOT be freed. It is however gauranteed not to be modified on invalidated by subsequent calls to any libenca functions, including enca_strerror().

The analyser error code is not changed for a successful call, and it set to ENCA_EINVALUE upon error.

`analyser` : | An analyser.
`errnum` : | An analyser error code.
Returns : | String describing the error code.

### enca_double_utf8_check ()

    int         enca_double_utf8_check          (EncaAnalyser analyser,
                                                 unsigned char *buffer,
                                                 size_t size);

Checks buffer for double-UTF-8 encoding.

Double-UTF-8 encoding is result of converting UTF-8 text to UTF-8 again, as if it was some 8bit charset. This is quite hard to recover from.

The analayser mostly only determines the language, the rest of the test is independent on the main guessing routines. When `buffer` doesn't containing UTF-8 text, the result is undefined.

Calling this function when language is `none' has currently no effect.

`analyser` : | Analyzer state determinig the language for double-UTF-8 check.
`buffer` : | The buffer to be checked [`size`].
`size` : | The size of `buffer`.
Returns : | Nonzero, when `buffer` probably contains doubly-UTF-8 encoded text. More precisely, it returns the number of charsets which are possible candidates for source charset. You can then use enca_double_utf8_get_candidates() to retrieve the charsets.

### enca_double_utf8_get_candidates ()

    int*        enca_double_utf8_get_candidates (EncaAnalyser analyser);

Returns array of Double-UTF-8 source charset candidates from last check.

The size of the array is the return value of enca_double_utf8_check(). It must be freed by caller. Must not be called if no Double-UTF-8 test has been performed yet. When called after unsuccessfull Double-UTF-8 test, it returns NULL, but the result after unsuccessfull call should be considered undefined.

`analyser` : | Analyzer state for which Double-UTF-8 candidates are to be returned.
Returns : | An array containing charset id's of possible source charsets from which the sample was Doubly-UTF-8 encoded. The array may contain only one value, but usually enca is not able to decide between e.g. ISO-8859-2 and Win1250, thus more candidates are returned.

### enca_set_multibyte ()

    void        enca_set_multibyte              (EncaAnalyser analyser,
                                                 int multibyte);

Enables or disables multibyte encoding tests for `analyser`.

This option is enabled by default.

When multibyte encodings are disabled, only 8bit charsets are checked. Disabling them for language with no 8bit charsets leaves only one thing enca_analyse() could test: whether the sample is purely 7bit ASCII or not (the latter leading to analyser failure, of course).

Multibyte encoding detection is also affected by enca_set_termination_strictness().

`analyser` : | An analyser.
`multibyte` : | Whether multibyte encoding tests should be enabled.

### enca_set_interpreted_surfaces ()

    void        enca_set_interpreted_surfaces   (EncaAnalyser analyser,
                                                 int interpreted_surfaces);

Enables or disables interpeted surfaces tests for `analyser`.

This option is enabled by default.

To allow simple applications which care about charset only and don't want to wrestle with surfaces, neglecting surface information should not have serious consequences. While ignoring EOL type surface is feasible, and ignoring UCS byteorders may be acceptable in endian-homogenous environment; ignoring the fact file is Quoted-Printable encoded can have disasterous consequences. By disabling this option you can disable surfaces requiring fundamental reinterpretation of the content, namely ENCA_SURFACE_QP and ENCA_SURFACE_EOL_BIN (thus probably making enca_analyse() to fail on such samples).

`analyser` : | An analyser.
`interpreted_surfaces` : | Whether interpreted surfaces tests should be enabled.

### enca_set_ambiguity ()

    void        enca_set_ambiguity              (EncaAnalyser analyser,
                                                 int ambiguity);

Enables or disables ambiguous mode for `analyser`.

This option is disabled by default.

In ambiguous mode some result is returned even when the charset cannot be determined uniquely, provided that sample contains only characters which have the same meaning in all charsets under consideration.

`analyser` : | An analyser.
`ambiguity` : | Whether result can be ambiguous.

### enca_set_filtering ()

    void        enca_set_filtering              (EncaAnalyser analyser,
                                                 int filtering);

Enables or disables filters for `analyser`.

This option is enabled by default.

Various filters are used to filter out block of binary noise and box-drawing characters which can confuse enca. In cases this is unwanted, you can disable them by setting this option to zero.

`analyser` : | An analyser.
`filtering` : | Whether filters should be allowed.

### enca_set_garbage_test ()

    void        enca_set_garbage_test           (EncaAnalyser analyser,
                                                 int garabage_test);

Enables or disables garbage test for `analyser`.

This option is enabled by default.

To prevent white noise (and almost-white noise) from being accidentally detected as some charset, a garbage test is used. In cases this is unwanted, you can disable is by setting this option to zero.

`analyser` : | An analyser.
`garabage_test` : | Whether garbage test should be allowed.

### enca_set_termination_strictness ()

    void        enca_set_termination_strictness (EncaAnalyser analyser,
                                                 int termination_strictness);

Enables or disables requiring multibyte sequences to be terminated correctly at the end of sample.

This option is enabled by default.

The sample given to enca_analyse() generally may not be whole text (e.g. for efficiency reasons). As a result, it may end in the middle of a multibyte sequence. In this case, you should disable this option to prevent rejecting some charset just because the sample don't terminate correctly. On the other hand, when given sample contains whole text, you should always enable this option to assure correctness of the result.

Note this option doesn't affect fixed character size encodings, like UCS-2 and UCS-4; sample is never assumed to contain UCS-2 text when its size is not even (and similarly for UCS-4).

`analyser` : | An analyser.
`termination_strictness` : | Whether multibyte sequences are required to be terminated correctly at the end of sample.

### enca_set_significant ()

    int         enca_set_significant            (EncaAnalyser analyser,
                                                 size_t significant);

Sets the minimal number of required significant characters.

The default value of this option is 10.

enca_analyse() refuses to make a decision unles at least this number of significant characters is found in sample. You may want to lower this number for very short texts.

`analyser` : | An analyser.
`significant` : | Minimal number of required significant characters.
Returns : | Zero on success, nonzero on failure, i.e. when you passed zero as `significant`. It sets analyser errno to ENCA_EINVALUE on failure.

### enca_set_threshold ()

    int         enca_set_threshold              (EncaAnalyser analyser,
                                                 double threshold);

Sets the minimal ratio between the most probable and the second most probable charsets.

The default value of this option is 1.4142.

enca_analyse() consideres the result known only when there's a clear gap between the most probable and the second most probable charset proabailities. Lower `threshold` values mean larger probability of a mistake and smaller probability of not recognizing a charset; higher `threshold` values the contrary.

`analyser` : | An analyser.
`threshold` : | Minimal ratio between winner and second best guess.
Returns : | Zero on success, nonzero on failure, i.e. when you passed value smaller than 1.0 as `threshold`. It sets analyser errno to ENCA_EINVALUE on failure.

<<< Previous Page | Home | Up | Next Page >>>
Enca Library API | Typedefs and Constants