File: sample1.c

package info (click to toggle)
libchardet 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,444 kB
  • sloc: sh: 11,197; cpp: 5,181; ansic: 205; makefile: 108
file content (67 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * sample code with libchardet
 * author: JoungKyun.Kim <http://oops.org>
 */
#include <chardet.h>
//#include "../src/chardet.h"

#ifdef CHARDET_BINARY_SAFE
    #define detect_str(x,y) detect_r(x, strlen(x), y)
#else
    #define detect_str(x,y) detect(x, y)
#endif

int main (int argc, char ** argv) {
	DetectObj *obj;
	int i, ret = 0;

	char *str[] = {
		"ȳ",
		"ȳϼ",
		"ȳϼ ?",
		"׷  Ǵ ɱ?",
		"    󸶳  c氢(CP949) ؾ!",
		"׷ ׷         ߾ ...",
		"12345 abcde"
	};

	char *expect[] = {
		"EUC-KR",
		"EUC-KR",
		"EUC-KR",
		"EUC-KR",
		"EUC-KR",
		"EUC-KR",
		"ASCII"
	};

	short arrayNum;
	arrayNum = sizeof (str) / sizeof (str[0]);

	for ( i=0; i<arrayNum; i++ ) {
		obj = detect_obj_init ();
		if ( obj == NULL ) {
			fprintf (stderr, "On attemped \"%s\", memory allocation failed\n", str[i]);
			continue;
		}

		if ( detect_str (str[i], &obj) == CHARDET_OUT_OF_MEMORY )
		{
			fprintf (stderr, "On handle processing, occured out of memory\n");
			return CHARDET_OUT_OF_MEMORY;
		}

		if ( argc > 1 )
			printf ("#2 %s : %s : %f : %d\n", str[i], obj->encoding, obj->confidence, obj->bom);
		else {
			if ( strcmp(obj->encoding, expect[i]) != 0 || obj->confidence < 0.3 )
				ret = 1;
		}
		detect_obj_free (&obj);

		if ( ret > 0 )
			break;
	}

	return ret;
}