forked from JanX2/UniversalDetector
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDetectorTest.m
56 lines (41 loc) · 1.4 KB
/
DetectorTest.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
#import <Foundation/Foundation.h>
#import <UniversalDetector/UniversalDetector.h>
int main(int argc,char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
for (int i = 1; i < argc; i++)
{
// You need a new detector for each piece of data you want to examine!
UniversalDetector *detector = [UniversalDetector new];
NSString *filePath = [NSString stringWithUTF8String:argv[i]];
NSString *fileName = [filePath lastPathComponent];
NSData *data = [NSData dataWithContentsOfFile:filePath
options:0
error:&error];
NSString *str = nil;
if (data == nil) {
str = [NSString stringWithFormat:@"%@\n\t%@", fileName, error];
}
if (data.length == 0) {
str = [NSString stringWithFormat:@"%@\n\t%@", fileName, @"Error: empty file!"];
}
if (str) {
printf("%s\n\n", [str UTF8String]);
continue;
}
[detector analyzeData:data];
NSString *MIMECharsetName = [detector MIMECharset];
NSStringEncoding encoding = [detector encoding];
str = [NSString stringWithFormat:@"%@\n\t\"%@\" (%@) confidence: %.1f%%",
fileName,
(encoding != 0) ? [NSString localizedNameOfStringEncoding:encoding] : @"UNKNOWN",
(MIMECharsetName != nil) ? MIMECharsetName : @"UNKNOWN",
([detector confidence] * 100.0f)
];
printf("%s\n\n", [str UTF8String]);
[detector release];
}
[pool release];
return 0;
}