12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #import "NCUchardet.h"
- #import "uchardet.h"
- @interface NCUchardet ()
- {
- uchardet_t _detector;
- }
- @end
- @implementation NCUchardet
- + (NCUchardet *)sharedNUCharDet {
- static NCUchardet *nuCharDet;
- @synchronized(self) {
- if (!nuCharDet) {
- nuCharDet = [NCUchardet new];
- }
- return nuCharDet;
- }
- }
- - (id)init
- {
- self = [super init];
-
- if (self) {
- _detector = uchardet_new();
- }
-
- return self;
- }
- - (void)dealloc
- {
- uchardet_delete(_detector);
- }
- - (NSString *)encodingStringDetectWithData:(NSData *)data
- {
- uchardet_handle_data(_detector, [data bytes], [data length]);
- uchardet_data_end(_detector);
-
- const char *charset = uchardet_get_charset(_detector);
- NSString *encodingName = [NSString stringWithCString:charset encoding:NSASCIIStringEncoding];
-
- uchardet_reset(_detector);
-
-
- if ([encodingName isEqualToString:@""])
- encodingName = @"UTF-8";
-
- return encodingName;
- }
- - (CFStringEncoding)encodingCFStringDetectWithData:(NSData *)data
- {
- NSString *encodingName = [self encodingStringDetectWithData:data];
-
- if ([encodingName isEqualToString:@""]) {
- return kCFStringEncodingUTF8;
- }
-
- CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)encodingName);
-
- return encoding;
- }
- @end
|