-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.m
116 lines (93 loc) · 2.78 KB
/
Utilities.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
//
// Utilities.m
// iTunesImport
//
// Created by Alexander Strange on 1/25/08.
//
#import "Utilities.h"
#import "UniversalDetector/UniversalDetector.h"
#import <ImageKit/ImageKit.h>
#include <sys/stat.h>
@implementation NSString (IIAdditions)
-(BOOL) isValidFilename
{
struct stat st;
int err = stat([self UTF8String], &st);
return !err || errno != ENOENT;
}
@end
@implementation XADArchive (FileIteration)
-(NSArray *) allEntryNames
{
int numEntries = [self numberOfEntries];
NSString *entries[numEntries];
for (int i = 0; i < numEntries; i++) {
NSString *entry = [self nameOfEntry:i];
entries[i] = entry;
//NSRange range = [entry rangeOfString:@"/"];
//entries[i] = range.length ? [entry substringFromIndex:range.location+1] : @"";
//NSLog(@"entry: \"%@\" to \"%@\"", entry, entries[i]);
}
return [NSArray arrayWithObjects:entries count:numEntries];
}
@end
@implementation NSImage (IKImageBrowserItem)
- (NSString *) imageUID
{
return [NSString stringWithFormat:@"%p", self];
}
- (NSString *) imageRepresentationType
{
return IKImageBrowserNSImageRepresentationType;
}
- (id) imageRepresentation
{
return self;
}
@end
NSMutableString *STStandardizeStringNewlines(NSString *str)
{
NSMutableString *ms = [NSMutableString stringWithString:str];
[ms replaceOccurrencesOfString:@"\r\n" withString:@"\n" options:0 range:NSMakeRange(0,[ms length])];
[ms replaceOccurrencesOfString:@"\r" withString:@"\n" options:0 range:NSMakeRange(0,[ms length])];
return ms;
}
void STSortMutableArrayStably(NSMutableArray *array, int (*compare)(const void *, const void *))
{
int count = [array count];
id objs[count];
[array getObjects:objs];
mergesort(objs, count, sizeof(void*), compare);
[array setArray:[NSArray arrayWithObjects:objs count:count]];
}
NSString *STGetStringWithUnknownEncodingFromData(NSData *data, NSStringEncoding *enc_)
{
UniversalDetector *ud = [[UniversalDetector alloc] init];
NSString *res = nil;
NSStringEncoding enc;
float conf;
int unsure;
NSString *enc_str;
[ud analyzeData:data];
enc = [ud encoding];
conf = [ud confidence];
enc_str = [ud MIMECharset];
unsure = conf < .6 && enc != NSASCIIStringEncoding;
if (unsure) {
NSLog(@"Guessed encoding \"%s\", but not sure (confidence %f%%).\n",[enc_str UTF8String],conf*100.);
}
res = [[[NSString alloc] initWithData:data encoding:enc] autorelease];
if (!res) {
NSLog(@"Failed to load file as guessed encoding %s. (data %@)\n",[enc_str UTF8String], data);
} else if (unsure) {
NSLog(@"With guessed encoding: \"%@\"", res);
}
[ud release];
if (enc_) *enc_ = enc;
return res;
}
extern NSString *STLoadFileWithUnknownEncoding(NSString *path)
{
NSData *data = [NSData dataWithContentsOfMappedFile:path];
return STGetStringWithUnknownEncodingFromData(data, NULL);
}