| 1 |
#import "UniversalDetector.h" |
|---|
| 2 |
|
|---|
| 3 |
#import "nscore.h" |
|---|
| 4 |
#import "nsUniversalDetector.h" |
|---|
| 5 |
#import "nsCharSetProber.h" |
|---|
| 6 |
|
|---|
| 7 |
class wrappedUniversalDetector:public nsUniversalDetector |
|---|
| 8 |
{ |
|---|
| 9 |
public: |
|---|
| 10 |
void Report(const char* aCharset) {} |
|---|
| 11 |
|
|---|
| 12 |
const char *charset(float &confidence) |
|---|
| 13 |
{ |
|---|
| 14 |
if(!mGotData) |
|---|
| 15 |
{ |
|---|
| 16 |
confidence=0; |
|---|
| 17 |
return 0; |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
if(mDetectedCharset) |
|---|
| 21 |
{ |
|---|
| 22 |
confidence=1; |
|---|
| 23 |
return mDetectedCharset; |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
switch(mInputState) |
|---|
| 27 |
{ |
|---|
| 28 |
case eHighbyte: |
|---|
| 29 |
{ |
|---|
| 30 |
float proberConfidence; |
|---|
| 31 |
float maxProberConfidence = (float)0.0; |
|---|
| 32 |
PRInt32 maxProber = 0; |
|---|
| 33 |
|
|---|
| 34 |
for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++) |
|---|
| 35 |
{ |
|---|
| 36 |
proberConfidence = mCharSetProbers[i]->GetConfidence(); |
|---|
| 37 |
if (proberConfidence > maxProberConfidence) |
|---|
| 38 |
{ |
|---|
| 39 |
maxProberConfidence = proberConfidence; |
|---|
| 40 |
maxProber = i; |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
confidence=maxProberConfidence; |
|---|
| 45 |
return mCharSetProbers[maxProber]->GetCharSetName(); |
|---|
| 46 |
} |
|---|
| 47 |
break; |
|---|
| 48 |
|
|---|
| 49 |
case ePureAscii: |
|---|
| 50 |
confidence=0; |
|---|
| 51 |
return "US-ASCII"; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
confidence=0; |
|---|
| 55 |
return 0; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
bool done() |
|---|
| 59 |
{ |
|---|
| 60 |
if(mDetectedCharset) return true; |
|---|
| 61 |
return false; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
void reset() { Reset(); } |
|---|
| 65 |
}; |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
@implementation UniversalDetector |
|---|
| 70 |
|
|---|
| 71 |
-(id)init |
|---|
| 72 |
{ |
|---|
| 73 |
if(self=[super init]) |
|---|
| 74 |
{ |
|---|
| 75 |
detectorptr=(void *)new wrappedUniversalDetector; |
|---|
| 76 |
charset=nil; |
|---|
| 77 |
} |
|---|
| 78 |
return self; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
-(void)dealloc |
|---|
| 82 |
{ |
|---|
| 83 |
delete (wrappedUniversalDetector *)detectorptr; |
|---|
| 84 |
[charset release]; |
|---|
| 85 |
[super dealloc]; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
-(void)finalize |
|---|
| 89 |
{ |
|---|
| 90 |
delete (wrappedUniversalDetector *)detectorptr; |
|---|
| 91 |
[super finalize]; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
-(void)analyzeData:(NSData *)data |
|---|
| 95 |
{ |
|---|
| 96 |
[self analyzeBytes:(const char *)[data bytes] length:[data length]]; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
-(void)analyzeBytes:(const char *)data length:(int)len |
|---|
| 100 |
{ |
|---|
| 101 |
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorptr; |
|---|
| 102 |
|
|---|
| 103 |
if(detector->done()) return; |
|---|
| 104 |
|
|---|
| 105 |
detector->HandleData(data,len); |
|---|
| 106 |
[charset release]; |
|---|
| 107 |
charset=nil; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
-(void)reset |
|---|
| 111 |
{ |
|---|
| 112 |
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorptr; |
|---|
| 113 |
detector->reset(); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
-(BOOL)done |
|---|
| 117 |
{ |
|---|
| 118 |
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorptr; |
|---|
| 119 |
return detector->done()?YES:NO; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
-(NSString *)MIMECharset |
|---|
| 123 |
{ |
|---|
| 124 |
if(!charset) |
|---|
| 125 |
{ |
|---|
| 126 |
wrappedUniversalDetector *detector=(wrappedUniversalDetector *)detectorptr; |
|---|
| 127 |
const char *cstr=detector->charset(confidence); |
|---|
| 128 |
if(!cstr) return nil; |
|---|
| 129 |
charset=[[NSString alloc] initWithUTF8String:cstr]; |
|---|
| 130 |
} |
|---|
| 131 |
return charset; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
-(NSStringEncoding)encoding |
|---|
| 135 |
{ |
|---|
| 136 |
NSString *mimecharset=[self MIMECharset]; |
|---|
| 137 |
if(!mimecharset) return 0; |
|---|
| 138 |
CFStringEncoding cfenc=CFStringConvertIANACharSetNameToEncoding((CFStringRef)mimecharset); |
|---|
| 139 |
if(cfenc==kCFStringEncodingInvalidId) return 0; |
|---|
| 140 |
return CFStringConvertEncodingToNSStringEncoding(cfenc); |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
-(float)confidence |
|---|
| 144 |
{ |
|---|
| 145 |
if(!charset) [self MIMECharset]; |
|---|
| 146 |
return confidence; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
+(UniversalDetector *)detector |
|---|
| 150 |
{ |
|---|
| 151 |
return [[[UniversalDetector alloc] init] autorelease]; |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
@end |
|---|