forked from mccraigmccraig/colloquy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSURLAdditions.m
93 lines (74 loc) · 3.62 KB
/
NSURLAdditions.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
#import "NSURLAdditions.h"
@implementation NSURL (NSURLAdditions)
+ (id) URLWithInternetLocationFile:(NSString *) path {
const char *fileSystemPath = [[NSFileManager defaultManager] fileSystemRepresentationWithPath:path];
FSRef ref;
if( FSPathMakeRef( (UInt8 *)fileSystemPath, &ref, NULL ) ) {
if( NSDebugEnabled ) NSLog( @"Couldn't make FSRef from: %@", path );
return nil;
}
short fileRefNum = 0;
if( ( fileRefNum = FSOpenResFile( &ref, fsRdPerm ) ) == -1 ) {
if( NSDebugEnabled ) NSLog(@"Couldn't open inetloc file at: %@", path);
return nil;
}
if( ! Count1Resources('url ') ) {
if( NSDebugEnabled ) NSLog(@"Inetloc file '%@' contains no 'url ' resources", path);
CloseResFile( fileRefNum );
return nil;
}
Handle res = Get1IndResource( 'url ', 1 );
NSString *urlString = [[[NSString alloc] initWithBytes:*res length:GetHandleSize( res ) encoding:NSUTF8StringEncoding] autorelease];
NSURL *url = [NSURL URLWithString:urlString];
ReleaseResource( res );
CloseResFile( fileRefNum );
return url;
}
- (void) writeToInternetLocationFile:(NSString *) path {
if( ! ( [[path pathExtension] isEqualToString:@"inetloc"] || [[path pathExtension] isEqualToString:@"webloc"] || [[path pathExtension] isEqualToString:@"ftploc"] || [[path pathExtension] isEqualToString:@"mailloc"] || [[path pathExtension] isEqualToString:@"afploc"] ) )
path = [path stringByAppendingPathExtension:@"inetloc"];
[[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
NSString *parentPath = [path stringByDeletingLastPathComponent];
NSString *pathName = [path lastPathComponent];
const char *fileSystemPath = [[NSFileManager defaultManager] fileSystemRepresentationWithPath:parentPath];
FSRef ref, parentRef;
if( FSPathMakeRef( (unsigned char *) fileSystemPath, &parentRef, FALSE ) ) {
if( NSDebugEnabled ) NSLog( @"Couldn't make FSRef from: %@", parentPath );
return;
}
short fileRefNum = 0;
unichar *buffer = (unichar *)calloc( [pathName length], sizeof( unichar ) );
[pathName getCharacters:buffer];
FSCreateResFile( &parentRef, [pathName length], buffer, 0, NULL, &ref, NULL );
free( buffer );
if( ( fileRefNum = FSOpenResFile( &ref, fsWrPerm ) ) == -1 ) {
if( NSDebugEnabled ) NSLog( @"Couldn't open inetloc at: %@", path );
[[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
return;
}
// Create the 'drag' resource first
Handle res = NewHandle( 48 );
Byte dragBuffer[48] = {
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
0x54, 0x45, 0x58, 0x54, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x75, 0x72, 0x6C, 0x20, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
memcpy( *res, &dragBuffer, sizeof( dragBuffer ) );
AddResource( res, 'drag', 128, (unsigned char *) "" );
// Create the 'TEXT' and 'url ' resources
NSString *urlString = [self absoluteString];
const char *utf8string = [urlString UTF8String];
res = NewHandle( strlen( utf8string ) );
memcpy( *res, utf8string, strlen( utf8string ) );
AddResource( res, 'TEXT', 256, (unsigned char *) "" ); // This takes over the Handle - don't dispose it
res = NewHandle( strlen( utf8string ) );
memcpy( *res, utf8string, strlen( utf8string ) );
AddResource( res, 'url ', 256, (unsigned char *) "" ); // This takes over the Handle - don't dispose it
CloseResFile( fileRefNum );
// Set the file type/creator
[[NSFileManager defaultManager] changeFileAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedLong:'ilge'], NSFileHFSTypeCode, [NSNumber numberWithUnsignedLong:'MACS'], NSFileHFSCreatorCode, nil] atPath:path];
}
@end