detect_handledata(3)
okay bestie, buckle up β this one's from the Library Functions Manual, and it's about libchardet's charset detective duo. π
β¨ NAME β¨
detect_handledata, detect_handledata_r β sniffing out the character set and measuring how confident the charset guess is. ππ¦
β¨ SNOPSYS β¨
(yes, it's spelled like that. iconic. we don't fix it, we respect it π )
#include <chardet.h>
short chardet_handledata (Detect ** handle, const char * inbuf,
DetectObj ** outbuf);
short chardet_handledata_r (Detect ** handle, const char * inbuf, size_t
inlen, DetectObj ** outbuf);
β¨ DESCRIPTION β¨
So basically it stores the charset and the accuracy of inbuf into outbuf. That's the whole vibe. π
β οΈ Lowkey important, highkey a problem: thedetect_handledataAPI is deprecated because this api is not binary safe. Use or replace todetect_handledata_rapi. Like, no cap, just switch. π
πΈ Arguments:
handleβ theDetecthandle resource that was allocated by thedetect_initapi. That's your main character right there. πinbufβ the input string for detecting. Feed it the mystery text. πinlenβ length of the input string for detecting. Know your size, bestie. β¨outbufβ storing information ofinbuf. The structure ofoutbufis as follows. π
typedef struct DetectObject {
char * encoding;
float confidence;
} DetectObj;
The outbuf variable is must initialized by the detect_obj_init API before calling this detect api. Do not skip this step, I am begging. π
β¨ RETURN VALUES β¨
It returns the following condition, case by case. It's giving β¨menu of outcomesβ¨:
CHARDET_SUCCESSβ detecting success. slay! πCHARDET_NO_RESULTβ detection failure. it tried, it didn't know. πCHARDET_NULL_OBJECTβ you didn't initializeoutbufwithchardet_obj_init. bestie. πCHARDET_OUT_OF_MEMORYβ occurring out of memory at internal api. π
β¨ EXAMPLE β¨
Here's the whole thing, verbatim, because code is sacred:
#include <chardet.h>
int main (void) {
Detect * d;
DetectObj * obj;
int i, arrayNum;
char *str[] = {
"this is ascii",
"μ΄κ±΄ euc-kr μ
λλ€."
};
arrayNum = sizeof (str) / sizeof (str[0]);
if ( (d = detect_init ()) == NULL ) {
fprintf (stderr, "chardet handle initialize failed\n");
return CHARDET_MEM_ALLOCATED_FAIL;
}
for ( i=0; i<arrayNum; i++ ) {
detect_reset (&d);
if ( (obj = detect_obj_init ()) == NULL ) {
fprintf (stderr, "Memory Allocation failed\n");
return CHARDET_MEM_ALLOCATED_FAIL;
}
//switch (detect_handledata (&d, str[i], &obj))
switch (detect_handledata (&d, str[i], strlen(str[i]), &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);
}
detect_destroy (&d);
return 0;
}
β¨ AUTHORS β¨
JoungKyun.Kim <http://oops.org> β the actual legend who made this. π«Ά
β¨ BUG REPORTS β¨
Use QnA board on https://github.com/Joungkyun/libchardet/issues β go tell them, they wanna know. π¦
β¨ SEE ALSO β¨
detect_obj_init(3), detect_obj_free(3), detect_init(3), detect_reset(3), detect_destroy(3) β the whole friend group, go read them all. π
and that's a wrap on the libchardet manuals, dated 2019-08-01 π β mwah, go detect something, you're doing amazing, periodt. β¨π
π¦ where this page comes from
- debian trixie: libchardet-dev 1.0.6-1 Β· MPL-1.1 OR GPL-2.0-or-later OR LGPL-2.1-or-later
- ubuntu questing: libchardet-dev 1.0.6-1
- ubuntu resolute: libchardet-dev 1.0.6-2 Β· MPL-1.1 OR GPL-2.0-or-later OR LGPL-2.1-or-later
- ubuntu stonking: libchardet-dev 1.0.6-2 Β· MPL-1.1 OR GPL-2.0-or-later OR LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms