forked from MacPaw/universal-detector
-
Notifications
You must be signed in to change notification settings - Fork 2
/
UniversalDetector.m
120 lines (99 loc) · 3.02 KB
/
UniversalDetector.m
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* UniversalDetector.m
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#import "UniversalDetector.h"
#import "WrappedUniversalDetector.h"
@implementation UniversalDetector
+(UniversalDetector *)detector
{
return [[self new] autorelease];
}
+(NSArray *)possibleMIMECharsets
{
static NSArray *array=nil;
if(!array) array=[[NSArray alloc] initWithObjects:
@"UTF-8",@"UTF-16BE",@"UTF-16LE",@"UTF-32BE",@"UTF-32LE",
@"ISO-8859-2",@"ISO-8859-5",@"ISO-8859-7",@"ISO-8859-8",@"ISO-8859-8-I",
@"windows-1250",@"windows-1251",@"windows-1252",@"windows-1253",@"windows-1255",
@"KOI8-R",@"Shift_JIS",@"EUC-JP",@"EUC-KR"/* actually CP949 */,@"x-euc-tw",
@"ISO-2022-JP",@"ISO-2022-CN",@"ISO-2022-KR",
@"Big5",@"GB2312",@"HZ-GB-2312",@"gb18030",@"GB18030",
@"IBM855",@"IBM866",@"TIS-620",@"X-ISO-10646-UCS-4-2143",@"X-ISO-10646-UCS-4-3412",
@"x-mac-cyrillic",@"x-mac-hebrew",
nil];
return array;
}
-(id)init
{
if((self=[super init]))
{
detector=AllocUniversalDetector();
charset=nil;
lastcstring=NULL;
}
return self;
}
-(void)dealloc
{
FreeUniversalDetector(detector);
[charset release];
[super dealloc];
}
-(void)analyzeData:(NSData *)data
{
[self analyzeBytes:(const char *)[data bytes] length:(int)[data length]];
}
-(void)analyzeBytes:(const char *)data length:(int)len
{
UniversalDetectorHandleData(detector,data,len);
}
-(void)reset { UniversalDetectorReset(detector); }
-(BOOL)done { return UniversalDetectorDone(detector); }
-(NSString *)MIMECharset
{
const char *cstr=UniversalDetectorCharset(detector,&confidence);
if(!cstr) return nil;
// nsUniversalDetector detects CP949 but returns "EUC-KR" because CP949
// lacks an IANA name. Kludge the name to make sure decoding succeeds.
if(strcmp(cstr,"EUC-KR")==0) cstr="CP949";
if(cstr!=lastcstring)
{
[charset release];
charset=[[NSString alloc] initWithUTF8String:cstr];
lastcstring=cstr;
}
return charset;
}
-(float)confidence
{
if(!charset) [self MIMECharset];
return confidence;
}
#ifdef __APPLE__
-(NSStringEncoding)encoding
{
NSString *mimecharset=[self MIMECharset];
if(!mimecharset) return 0;
CFStringEncoding cfenc=CFStringConvertIANACharSetNameToEncoding((CFStringRef)mimecharset);
if(cfenc==kCFStringEncodingInvalidId) return 0;
return CFStringConvertEncodingToNSStringEncoding(cfenc);
}
#endif
@end