-
Notifications
You must be signed in to change notification settings - Fork 92
/
Common.m
96 lines (81 loc) · 3.12 KB
/
Common.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
/*
* Common.c
* QLColorCode
*
* Created by Nathaniel Gray on 12/6/07.
* Copyright 2007 Nathaniel Gray.
*
*/
#import <CoreFoundation/CoreFoundation.h>
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
#import <limits.h> // PATH_MAX
#include "Common.h"
NSData *runTask(NSString *script, NSDictionary *env, int *exitCode) {
NSTask *task = [[NSTask alloc] init];
[task setCurrentDirectoryPath:@"/tmp"]; /* XXX: Fix this */
[task setEnvironment:env];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:@"-c", script, nil]];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
// Let stderr go to the usual place
//[task setStandardError: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
[task waitUntilExit];
*exitCode = [task terminationStatus];
[task release];
/* The docs claim this isn't needed, but we leak descriptors otherwise */
[file closeFile];
/*[pipe release];*/
return data;
}
NSString *pathOfURL(CFURLRef url)
{
NSString *targetCFS = [[(NSURL *)url absoluteURL] path];
n8log(@"targetCFS = %@", targetCFS);
//return [targetCFS stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return targetCFS;
}
NSData *colorizeURL(CFBundleRef bundle, CFURLRef url, int *status, int thumbnail)
{
NSData *output = NULL;
CFURLRef rsrcDirURL = CFBundleCopyResourcesDirectoryURL(bundle);
//n8log(@"rsrcDirURL = %@", CFURLGetString(rsrcDirURL));
NSString *rsrcEsc = pathOfURL(rsrcDirURL);
CFRelease(rsrcDirURL);
n8log(@"url = %@", url);
NSString *targetEsc = pathOfURL(url);
n8log(@"targetEsc = %@", targetEsc);
// Set up preferences
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *env = [NSMutableDictionary dictionaryWithDictionary:
[[NSProcessInfo processInfo] environment]];
[env addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
#ifdef DEBUG
@"1", @"qlcc_debug",
#endif
@"9", @"fontSizePoints",
@"Monaco", @"font",
@"ide-xcode", @"hlTheme",
@"", @"extraHLFlags",
@"", @"maxFileSize",
@"UTF-8", @"textEncoding",
@"UTF-8", @"webkitTextEncoding", nil]];
[env addEntriesFromDictionary:[defaults persistentDomainForName:myDomain]];
NSString *cmd = [NSString stringWithFormat:
@"'%@/colorize.sh' '%@' '%@' %s",
rsrcEsc, rsrcEsc, targetEsc, thumbnail ? "1" : "0"];
n8log(@"cmd = %@", cmd);
output = runTask(cmd, env, status);
if (*status != 0) {
NSLog(@"QLColorCode: colorize.sh failed with exit code %d. Command was (%@).",
*status, cmd);
}
return output;
}