## Analyser 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);
    int                 enca_get_multibyte                  (EncaAnalyser analyser);
    void                enca_set_interpreted_surfaces       (EncaAnalyser analyser,
                                                             int interpreted_surfaces);
    int                 enca_get_interpreted_surfaces       (EncaAnalyser analyser);
    void                enca_set_ambiguity                  (EncaAnalyser analyser,
                                                             int ambiguity);
    int                 enca_get_ambiguity                  (EncaAnalyser analyser);
    void                enca_set_filtering                  (EncaAnalyser analyser,
                                                             int filtering);
    int                 enca_get_filtering                  (EncaAnalyser analyser);
    void                enca_set_garbage_test               (EncaAnalyser analyser,
                                                             int garabage_test);
    int                 enca_get_garbage_test               (EncaAnalyser analyser);
    void                enca_set_termination_strictness     (EncaAnalyser analyser,
                                                             int termination_strictness);
    int                 enca_get_termination_strictness     (EncaAnalyser analyser);
    int                 enca_set_significant                (EncaAnalyser analyser,
                                                             size_t significant);
    size_t              enca_get_significant                (EncaAnalyser analyser);
    int                 enca_set_threshold                  (EncaAnalyser analyser,
                                                             double threshold);
    double              enca_get_threshold                  (EncaAnalyser analyser);

## Description

Basically you want to allocate an analyser with `enca_analyser_alloc()` for some language, use `enca_analyse()` (or `enca_analyse_const()`) on a buffer to find its encoding, and interpret the results with something like `enca_charset_name()`. The analyser then can be used for another buffer. Once you no longer need it, call `enca_analyser_free()` to release it.

A single working example is better than a hundred pages of reference manual.

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()`.

All names prefixed with `ENCA_`, `Enca`, `_Enca`, or `enca_` should be treated as reserved and not used for application function/variable/type/macro names.

## 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. Note however, an analyser may occupy a considerable amount of memory (a few hundreds of kB), so it's generally not a good idea to have several hundreds of them floating around.

`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 the result of [errorneous] conversion of UTF-8 text to UTF-8 again, as if it was in some 8bit charset. This is quite hard to recover from.

The analayser mostly only determines what language will be assumed, the rest of this test is independent on the main guessing routines. When `buffer` doesn't containing UTF-8 text, the result is undefined (namely, false positives are possible).

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 the last check.

The returned array should be freed by caller then no longer needed. Its is the return value of the preceding `enca_double_utf8_check()` call.

When called before any double-UTF-8 test has been performed yet or after and unsuccessfull double-UTF-8 test, it returns NULL, but the result after an unsuccessfull check 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 (nonzero to enable, zero to disable).

### enca_get_multibyte ()

    int                 enca_get_multibyte                  (EncaAnalyser analyser);

Returns whether `analyser` can return multibyte encodings.

See `enca_set_multibyte()` for more detailed description of multibyte encoding checking.

`analyser` : | An analyser.
Returns : | Nonzero when multibyte encoding are possible, zero otherwise.

Since 1.3.

### 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 (nonzero to allow, zero to disallow).

### enca_get_interpreted_surfaces ()

    int                 enca_get_interpreted_surfaces       (EncaAnalyser analyser);

Returns whether `analyser` allows interpreted surfaces.

See `enca_set_interpreted_surfaces()` for more detailed description of interpreted surfaces.

`analyser` : | An analyser.
Returns : | Nonzero when interpreted surfaces are possible, zero otherwise.

Since 1.3.

### 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 (nonzero to allow, zero to disallow).

### enca_get_ambiguity ()

    int                 enca_get_ambiguity                  (EncaAnalyser analyser);

Returns whether `analyser` can return ambiguous results.

See `enca_set_ambiguity()` for more detailed description of ambiguous results.

`analyser` : | An analyser.
Returns : | Nonzero when ambiguous results are allowed, zero otherwise.

Since 1.3.

### 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 that could otherwise 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 enabled (nonzero to enable, zero to disable).

### enca_get_filtering ()

    int                 enca_get_filtering                  (EncaAnalyser analyser);

Returns whether `analyser` has filtering enabled.

See `enca_set_filtering()` for more detailed description of filtering.

`analyser` : | An analyser.
Returns : | Nonzero when filtering is enabled, zero otherwise.

Since 1.3.

### 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 (nonzero to enable, nzero to disable).

### enca_get_garbage_test ()

    int                 enca_get_garbage_test               (EncaAnalyser analyser);

Returns whether `analyser` has garbage test enabled.

See `enca_set_garbage_test()` for more detailed description of garbage test.

`analyser` : | An analyser.
Returns : | Nonzero when garbage test is enabled, zero otherwise.

Since 1.3.

### 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 a complete 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 does NOT 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 (nonzero to require, zero to relax).

### enca_get_termination_strictness ()

    int                 enca_get_termination_strictness     (EncaAnalyser analyser);

Returns whether `analyser` requires strict termination.

See `enca_set_termination_strictness()` for more detailed description of termination strictness.

`analyser` : | An analyser.
Returns : | Nonzero when strict termination is required, zero otherwise.

Since 1.3.

### 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_get_significant ()

    size_t              enca_get_significant                (EncaAnalyser analyser);

Returns the minimum number of significant characters required by `analyser`.

See `enca_set_significant()` for more detailed description of required significant characters.

`analyser` : | An analyser.
Returns : | The minimum number of significant characters.

Since 1.3.

### 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 proababilities. Lower `threshold` values mean larger probability of a mistake and smaller probability of not recognizing a charset; higher `threshold` values the contrary. Threshold value of 2 is almost infinity.

`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.

### enca_get_threshold ()

    double              enca_get_threshold                  (EncaAnalyser analyser);

Returns the threshold value used by `analyser`.

See `enca_set_threshold()` for more detailed threshold description.

`analyser` : | An analyser.
Returns : | The threshold value.

Since 1.3.