detect(3) Library Functions Manual detect(3)

detect, detect_r - Detecting character set and measuring accuracy of charset

#include <chardet.h>

short detect (char * inbuf, DetectObj ** outbuf);

short detect_r (char * inbuf, size_t inlen, DetectObj ** outbuf);

Storing charset and accuracy of inbuf to outbuf

The detect API is deprecated becase this api is not binary safe. Use or replace to detect_r api.


input string for detecting

length of input string for detecting

Stroing inforamtion of inbuf. The structure of outbuf is follows.
typedef struct DetectObject {
	char * encoding;
	float confidence;
} DetectObj;

The outbuf variable is must initialized by detect_obj_init API before calling this detect api.

Returns following condition as case by case.


Detecting success

Detection failure

Don't initializing outbuf with chardet_obj_init

Occuring out of memory at internal API

#include <chardet.h>
int main (void) {
	DetectObj *obj;
	char * checkstr = "안녕하세요";
	if ( (obj = detect_obj_init ()) == NULL ) {
		fprintf (stderr, "Memory Allocation failed\n");
		return CHARDET_MEM_ALLOCATED_FAIL;
	}
	//switch (detect (checkstr, &obj))
	switch (detect_r (checkstr, strlen (checkstr), &obj))
	{
		case CHARDET_OUT_OF_MEMORY :
			fprintf (stderr, "On handle processing, occured out of memory\n");
			detect_obj_free (&obj);
			return CHARDET_OUT_OF_MEMORY;
		case CHARDET_NULL_OBJECT :
			fprintf (stderr,
					"2st argument of chardet() is must memory allocation "
					"with detect_obj_init API\n");
			return CHARDET_NULL_OBJECT;
	}
	# check support obj->bom with CHARDET_BOM_CHECK constant
	printf ("encoding: %s, confidence: %f, exists bom: %d\n", obj->encoding, obj->confidence, obj->bom);
	detect_obj_free (&obj);
    return 0;
}

JoungKyun.Kim http://oops.org

Use QnA board on https://github.com/Joungkyun/libchardet/issues

detect_handledata(3), detect_obj_init(3), detect_obj_free(3)

2019-08-01 libchardet manuals