Skip to content

Commit 3c126b9

Browse files
committed
Can also handle TAB delimiters
1 parent cd3b67e commit 3c126b9

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

CSVDocument.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
NSString *separator;
1515
NSArray *rows;
1616
NSArray *columnKeys;
17+
18+
BOOL autoDetectSeparator;
1719
}
1820

1921
@property (nonatomic, retain) NSString *separator;
2022
@property (nonatomic, retain) NSArray *rows;
2123
@property (nonatomic, retain) NSArray *columnKeys;
2224

25+
@property (nonatomic, assign) BOOL autoDetectSeparator;
26+
2327
+ (CSVDocument *) csvDoc;
2428
- (NSUInteger) numRowsFromCSVString:(NSString *)string error:(NSError **)error;
2529
- (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NSError **)error;

CSVDocument.m

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@implementation CSVDocument
1515

16-
@synthesize separator, rows, columnKeys;
16+
@synthesize separator, rows, columnKeys, autoDetectSeparator;
1717

1818

1919
- (id) init
@@ -58,12 +58,18 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
5858
NSMutableArray *thisRows = [NSMutableArray array];
5959
NSMutableArray *thisColumnKeys = [NSMutableArray array];
6060

61-
// Check whether the file uses ";" as separator
62-
if(![separator isEqualToString:@";"]) {
63-
NSUInteger testStringLength = ([string length] > 100) ? 100 : [string length];
61+
// Check whether the file uses ";" or TAB as separator by comparing relative occurrences in the first 200 chars
62+
if(autoDetectSeparator) {
63+
self.separator = @",";
64+
65+
NSUInteger testStringLength = ([string length] > 200) ? 200 : [string length];
6466
NSString *testString = [string substringToIndex:testStringLength];
65-
if([[testString componentsSeparatedByString:@";"] count] > [[testString componentsSeparatedByString:separator] count]) {
66-
self.separator = @";";
67+
NSArray *possSeparators = [NSArray arrayWithObjects:@";", @" ", nil];
68+
69+
for(NSString *s in possSeparators) {
70+
if([[testString componentsSeparatedByString:s] count] > [[testString componentsSeparatedByString:separator] count]) {
71+
self.separator = s;
72+
}
6773
}
6874
}
6975

GeneratePreviewForURL.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
5858
// Parse the data if still interested in the preview
5959
if(false == QLPreviewRequestIsCancelled(preview)) {
6060
CSVDocument *csvDoc = [CSVDocument csvDoc];
61+
csvDoc.autoDetectSeparator = YES;
6162
NSUInteger numRowsParsed = [csvDoc numRowsFromCSVString:fileString maxRows:MAX_ROWS error:NULL];
6263

6364

@@ -106,6 +107,7 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
106107
}
107108
[html appendString:@"</html>"];
108109

110+
// feed the HTML
109111
CFDictionaryRef properties = (CFDictionaryRef)[NSDictionary dictionary];
110112
QLPreviewRequestSetDataRepresentation(preview,
111113
(CFDataRef)[html dataUsingEncoding:stringEncoding],
@@ -196,9 +198,10 @@ void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
196198
while(bytes > 1000) {
197199
bytes /= 1024;
198200
i++;
199-
}
200-
if(i > 5) { // we won't end up here anyway, but let's be sure
201-
i = 5;
201+
202+
if(i >= 5) { // we most likely won't end up here, but let's be sure
203+
break;
204+
}
202205
}
203206

204207
char formatString[9];

GenerateThumbnailForURL.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
5555
NSUInteger numRows = ceilf(THUMB_SIZE / rowHeight);
5656

5757
CSVDocument *csvDoc = [CSVDocument csvDoc];
58+
csvDoc.autoDetectSeparator = YES;
5859
NSUInteger gotRows = [csvDoc numRowsFromCSVString:fileString maxRows:numRows error:NULL];
5960

6061

@@ -67,7 +68,7 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
6768
CGContextRef context = createRGBABitmapContext(maxBounds.size);
6869
//CGContextRef context = createVectorContext(maxBounds.size);
6970
if(context) {
70-
CGPDFContextBeginPage(context, NULL);
71+
//CGPDFContextBeginPage(context, NULL);
7172

7273
// Flip CoreGraphics coordinate system
7374
CGContextScaleCTM(context, 1.0, -1.0);
@@ -176,12 +177,13 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
176177
}
177178
}
178179

180+
//CGPDFContextEndPage(context);
181+
179182
CGColorRelease(borderColor);
180183
CGColorRelease(rowBG);
181184
CGColorRelease(altRowBG);
182185

183186
// Create a CGImage
184-
CGPDFContextEndPage(context);
185187
CGImageRef fullImage = CGBitmapContextCreateImage(context);
186188
CGImageRef usedImage = CGImageCreateWithImageInRect(fullImage, usedBounds);
187189
CGImageRelease(fullImage);
@@ -217,17 +219,17 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
217219
badgeColor, NSForegroundColorAttributeName,
218220
badgeShadow, NSShadowAttributeName, nil];
219221
NSSize badgeSize = [badgeString sizeWithAttributes:badgeAttributes];
220-
NSRect badgeRect = NSMakeRect((usedBounds.size.width / 2) - (badgeSize.width / 2), 0.25 * badgeFontSize, 0.0, 0.0);
222+
NSRect badgeRect = NSMakeRect((usedBounds.size.width / 2) - (badgeSize.width / 2), 0.2 * badgeFontSize, 0.0, 0.0);
221223
badgeRect.size = badgeSize;
222224

223225
[badgeString drawWithRect:badgeRect options:NSStringDrawingUsesLineFragmentOrigin attributes:badgeAttributes];
224226
}
225227

226228

227229
// Clean up
228-
CGContextRelease(context);
229230
QLThumbnailRequestFlushContext(thumbnail, thumbContext);
230231
CGContextRelease(thumbContext);
232+
CGContextRelease(context);
231233
}
232234
}
233235
}

0 commit comments

Comments
 (0)