### BOM Detection Support in libchardet Source: https://github.com/joungkyun/libchardet/blob/master/README.md Demonstrates how to check for Byte Order Mark (BOM) detection support in libchardet. This feature is available if the CHARDET_BOM_CHECK constant is defined. ```c #ifdef CHARDET_BOM_CHECK if (detected_obj->bom == 1) { // Detected as BOM } #endif ``` -------------------------------- ### Basic Character Encoding Detection Source: https://github.com/joungkyun/libchardet/blob/master/README.md Initializes a detection object, detects the encoding of a given string, and prints the results. This is suitable for single-pass detection. Ensure memory allocation is handled correctly. ```c #include int main (void) { DetectObj *obj; char * str = "안녕하세요"; if ( (obj = detect_obj_init ()) == NULL ) { fprintf (stderr, "Memory Allocation failed\n"); return CHARDET_MEM_ALLOCATED_FAIL; } #ifndef CHARDET_BINARY_SAFE // before 1.0.5. This API is deprecated on 1.0.5 switch (detect (str, &obj)) #else // from 1.0.5 switch (detect_r (str, strlen (str), &obj)) #endif { 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; } #ifndef CHARDET_BOM_CHECK printf ("encoding: %s, confidence: %f\n", obj->encoding, obj->confidence); #else // from 1.0.6 support return whether exists BOM printf ( "encoding: %s, confidence: %f, exist BOM: %d\n", obj->encoding, obj->confidence, obj->bom ); #endif detect_obj_free (&obj); return 0; } ``` -------------------------------- ### Looping Character Encoding Detection Source: https://github.com/joungkyun/libchardet/blob/master/README.md Initializes a detection handle and processes data within a loop to detect character encoding. This is useful for streaming data or repeated detections. It includes reset and cleanup steps for the detection handle and object. ```c #include int main (void) { Detect * d; DetectObj * obj; char * str = "안녕하세요"; if ( (d = detect_init ()) == NULL ) { fprintf (stderr, "chardet handle initialize failed\n"); return CHARDET_MEM_ALLOCATED_FAIL; } while ( 1 ) { detect_reset (&d); if ( (obj = detect_obj_init ()) == NULL ) { fprintf (stderr, "Memory Allocation failed\n"); return CHARDET_MEM_ALLOCATED_FAIL; } #ifndef CHARDET_BINARY_SAFE // before 1.0.5. This API is deprecated on 1.0.5 switch (detect_handledata (&d, str,, &obj)) #else // from 1.0.5 switch (detect_handledata_r (&d, str, strlen (str), &obj)) #endif { 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; } #ifndef CHARDET_BOM_CHECK printf ("encoding: %s, confidence: %f\n", obj->encoding, obj->confidence); #else // from 1.0.6 support return whether exists BOM printf ( "encoding: %s, confidence: %f, exist BOM: %d\n", obj->encoding, obj->confidence, obj->bom ); #endif detect_obj_free (&obj); if ( 1 ) break; } detect_destroy (&d); return 0; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.