detect_handledata(3)

πŸ“¦
version 1.0.6 Β· 4 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

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: the detect_handledata API is deprecated because this api is not binary safe. Use or replace to detect_handledata_r api. Like, no cap, just switch. 😭

🌸 Arguments:

  • handle β€” the Detect handle resource that was allocated by the detect_init api. 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 of inbuf. The structure of outbuf is 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 initialize outbuf with chardet_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