forked from planbnet/QuickNFO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicklooknfo.c
104 lines (85 loc) · 2.54 KB
/
quicklooknfo.c
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
#include "quicklooknfo.h"
#include <iconv.h>
#include <unistd.h>
#define BUFFERSIZE 1024 * 16
#define PRE_NFO_HTML "<html style='margin: 0; padding: 0; background-color: black;'><body style='margin: 0; padding: 0;'><pre style='font-family: Andale Mono, Menlo, monospace; font-size: 10px; line-height: 1; color: white;'>"
#define POST_NFO_HTML "</pre></body></html>"
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Specify nfo file");
return 1;
}
CFStringRef path = CFStringCreateWithCString(NULL, argv[1], kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, false);
CFRelease(path);
CFDataRef text = createDataFromURL(url);
CFRelease(url);
if (!text) {
printf("File does not exist");
return 2;
}
CFDataRef result = createNFOString(text);
CFRelease(text);
printf("%s", CFDataGetBytePtr(result));
return 0;
}
CFDataRef createHTMLPreview( CFURLRef url )
{
CFDataRef text = createDataFromURL(url);
if (!text) return NULL;
CFDataRef page = createNFOString(text);
CFRelease(text);
if (!page) return NULL;
CFDataRef result = createHTMLData(page);
CFRelease(page);
return result;
}
CFDataRef createDataFromURL( CFURLRef url )
{
SInt32 errorCode = 0;
CFDataRef fileContent;
CFDictionaryRef dict;
CFArrayRef arr = CFArrayCreate(NULL, NULL, 0, NULL);
Boolean success = CFURLCreateDataAndPropertiesFromResource (NULL,
url,
&fileContent,
&dict,
arr,
&errorCode);
CFRelease(arr);
CFRelease(dict);
if (!success) {
return NULL;
}
return fileContent;
}
CFDataRef createNFOString( CFDataRef text )
{
iconv_t cd;
cd = iconv_open ("UTF8", "437");
if (cd == (iconv_t) -1)
{
return NULL;
}
CFMutableDataRef result = CFDataCreateMutable(NULL, 0);
char *inPtr = (char*) CFDataGetBytePtr(text);
size_t inBytesLeft = CFDataGetLength(text);
char buffer[BUFFERSIZE];
do {
size_t outBytesLeft = BUFFERSIZE;
char *outPtr = (char*)buffer;
iconv(cd, &inPtr, &inBytesLeft, &outPtr , &outBytesLeft);
CFIndex converted = BUFFERSIZE - outBytesLeft;
CFDataAppendBytes(result, (UInt8*) &buffer, converted);
} while (inBytesLeft > 0);
iconv_close( cd );
return result;
}
CFDataRef createHTMLData(CFDataRef nfo )
{
CFMutableDataRef page = CFDataCreateMutable(NULL, 0);
CFDataAppendBytes(page, (const UInt8*) PRE_NFO_HTML, sizeof(PRE_NFO_HTML));
CFDataAppendBytes(page, CFDataGetBytePtr(nfo), CFDataGetLength(nfo));
CFDataAppendBytes(page, (const UInt8*) POST_NFO_HTML, sizeof(POST_NFO_HTML));
return page;
}