forked from n8gray/QLColorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.m
More file actions
79 lines (66 loc) · 2.2 KB
/
Common.m
File metadata and controls
79 lines (66 loc) · 2.2 KB
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
/*
* Common.c
* QLColorCode
*
* Created by Nathaniel Gray on 12/6/07.
* Copyright 2007 Nathaniel Gray. All rights reserved.
*
*/
#import <CoreFoundation/CoreFoundation.h>
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
#import <limits.h> // PATH_MAX
#include "Common.h"
NSData *runTask(NSString *script, 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];
[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;
}
NSData *colorizeURL(CFBundleRef bundle, CFURLRef url, int *status, int thumbnail)
{
NSData *output = NULL;
unsigned char *targetBuf = malloc(PATH_MAX);
unsigned char *rsrcDirBuf = malloc(PATH_MAX);
char *thumbString;
CFURLRef rsrcDirURL = CFBundleCopyResourcesDirectoryURL(bundle);
if (!CFURLGetFileSystemRepresentation(url, YES, targetBuf, PATH_MAX)
|| !CFURLGetFileSystemRepresentation(rsrcDirURL, YES, rsrcDirBuf, PATH_MAX))
{
NSLog(@"QLColorCode: CFURLGetFileSystemRepresentation failed");
*status = 1;
goto done;
}
if (thumbnail)
thumbString = "1";
else
thumbString = "0";
NSString *cmd = [NSString stringWithFormat:
@"PYTHONPATH=\"%s:%s/pygments\" \"%s/colorize.py\" \"%s\" \"%s\" %s",
rsrcDirBuf, rsrcDirBuf, rsrcDirBuf, rsrcDirBuf, targetBuf, thumbString];
output = runTask(cmd, status);
if (*status != 0) {
NSLog(@"QLColorCode: colorize.py failed with exit code %d", *status);
}
done:
free(targetBuf);
free(rsrcDirBuf);
return output;
}