detect(3)

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

πŸ’– detect(3) β€” the manual, but make it make sense πŸ’–

okay bestie, gather round β€” this is the detect(3) page from the Library Functions Manual, and we're gonna get you fluent in charset detection like it's nothing. no cap, this one's short but it's got ✨lore✨.


✨ NAME ✨

detect, detect_r β€” detecting the character set of stuff, and measuring how confident the charset detection is. it's giving detective energy πŸ”πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <chardet.h>

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

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

🌸 DESCRIPTION 🌸

So what it does: it stores the charset and the accuracy of inbuf into outbuf. that's the whole vibe. πŸŽ€

🚨 bestie, listen β€” the detect API is deprecated because this api is not binary safe. use it or replace it with the detect_r api. like, detect_r is the main character here, detect is lowkey on its way out. πŸ’€

πŸ’œ Arguments:

  • inbuf β€” the input string for detecting. this is the thing you're snooping on πŸ‘€
  • inlen β€” the length of that input string you're detecting. (detect_r said "i need the receipts", and honestly, valid.)
  • outbuf β€” where the information about inbuf gets stored. the structure of outbuf goes like this:
typedef struct DetectObject {
     char * encoding;
     float confidence;
} DetectObj;
⚠️ heads up: the outbuf variable must be initialized by the detect_obj_init API before calling this detect api. do not skip this step, bestie, i am begging πŸ™ˆ

⭐ RETURN VALUE ⭐

it returns the following, case by case, depending on what happened:

  • CHARDET_SUCCESS β€” detecting success πŸ’– slay, it worked
  • CHARDET_NO_RESULT β€” detection failure 😭 it tried and it did not find the vibe
  • CHARDET_NULL_OBJECT β€” you didn't initialize outbuf with chardet_obj_init. bestie. why.
  • CHARDET_OUT_OF_MEMORY β€” out of memory happened at an internal API. the memory said "no πŸ’…"

πŸŽ€ EXAMPLE πŸŽ€

here's the whole thing in action, copy-paste ready, iconic behavior only:

#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;
}

the way that "μ•ˆλ…•ν•˜μ„Έμš”" just sits there being a whole charset detection demo... we love a multilingual moment 🫢✨


πŸ‘‘ AUTHORS πŸ‘‘

JoungKyun.Kim <http://oops.org> β€” the legend themselves. bow down. πŸ‘‘


πŸ“ BUG REPORTS πŸ“

found a bug? use the QnA board on https://github.com/Joungkyun/libchardet/issues β€” that's the spot, don't be shy, they wanna hear it 🌈


πŸ”₯ SEE ALSO πŸ”₯

detect_handledata(3), detect_obj_init(3), detect_obj_free(3) β€” go read these too, they're the supporting cast and they ate fr fr πŸ’…


and that's the page!! this was the libchardet manuals version from 2019-08-01, and it's been a joy. mwah πŸ’‹ go detect some encodings, bestie β€” you've got this. πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from