-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSVFileParser.m
903 lines (815 loc) · 28.9 KB
/
CSVFileParser.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
//
// CSVFileParser.m
// CSV Touch
//
// Created by Simon Wigzell on 03/06/2008.
// Copyright 2008 Ozymandias. All rights reserved.
//
#import "CSVFileParser.h"
#import "CSVPreferencesController.h"
#import "FilesViewController.h"
#import "csv.h"
#import "parseCSV.h"
#import "OzymandiasAdditions.h"
#import "ItemsViewController.h"
#define FILEPARSER_RAW_DATA @"rawData"
#define FILEPARSER_URL @"URL"
#define FILEPARSER_DOWNLOAD_DATE @"downloadDate"
#define FILEPARSER_HIDE_ADDRESS @"hideAdress"
#define ITEM_ICON_COLUMN_NAME @"CSV Touch icon"
#define DEFS_ENCODING_FOR_FILES @"encodingForFiles"
#define MAX_AUTO_WIDTH 24
#define MAX_PREDEFINED_WIDTH 256
@interface CSVFileParser ()
@property (nonatomic, copy) NSData *rawData;
@property (nonatomic, strong) NSMutableArray<NSString *> *shownColumnNames;
@property (nonatomic) unichar usedDelimiter;
@property NSMutableArray<NSNumber *> *maxLengths;
@end
@implementation CSVFileParser
static NSMutableDictionary *encodingForFileName;
static NSArray *_allowedEncodings = nil;
static NSArray *_allowedEncodingNames = nil;
static NSMutableArray<CSVFileParser *> *_files;
+ (NSMutableArray *) files
{
return _files;
}
+ (void) removeAllFiles
{
[_files removeAllObjects];
}
+ (NSArray *) allowedFileEncodings
{
return _allowedEncodings;
}
+ (NSArray *) allowedFileEncodingNames
{
return _allowedEncodingNames;
}
+ (void) addParser:(CSVFileParser *)parser
{
[_files addObject:parser];
[_files sortUsingSelector:@selector(compareFileName:)];
}
+ (void) removeFile:(CSVFileParser *)parser
{
[_files removeObject:parser];
}
+ (CSVFileParser *) existingParserForName:(NSString *)name
{
for( CSVFileParser *file in _files )
{
// Do not use -isEqualToString! This gives wrong results due to using literal Unicode compare which is bad for us since strings might come from both file system names & URL paths
// Also, file name contains .csvtouch
if( [name localizedCompare:[[file fileName] stringByDeletingPathExtension]] == NSOrderedSame )
{
return file;
}
}
return nil;
}
+ (void) removeFileWithName:(NSString *)name
{
for( CSVFileParser *oldFile in _files )
{
// Do not use -isEqualToString! This gives wrong results due to using literal Unicode compare which is bad for us since strings might come from both file system names & URL paths
if( [name localizedCompare:[oldFile fileName]] == NSOrderedSame )
{
[_files removeObject:oldFile];
return;
}
}
}
+ (BOOL) fileExistsWithURL:(NSString *)URL
{
for( CSVFileParser *fp in _files )
{
if( [[fp.URL decomposedStringWithCanonicalMapping] isEqualToString:[URL decomposedStringWithCanonicalMapping]] )
return YES;
}
return NO;
}
+ (void) fixedWidthSettingsChangedUsingUI
{
CSVFileParser *current = [[ItemsViewController sharedInstance] file];
[current reparseIfParsed];
for( CSVFileParser *parser in _files)
{
if( parser != current)
{
parser.hasBeenParsed = NO;
}
}
}
static NSTimer *_resetDownloadFlagsTimer;
+ (void) resetClearingOfDownloadFlagsTimer
{
[_resetDownloadFlagsTimer invalidate];
_resetDownloadFlagsTimer = [NSTimer timerWithTimeInterval:60*60 // 1h
repeats:NO
block:^(NSTimer *timer)
{
for( CSVFileParser *file in _files)
{
file.hasFailedToDownload = FALSE;
file.hasBeenDownloaded = FALSE;
}
[[FilesViewController sharedInstance].tableView reloadData];
}];
[[NSRunLoop currentRunLoop] addTimer:_resetDownloadFlagsTimer forMode:NSDefaultRunLoopMode];
}
+ (void) clearAllDownloadFlags
{
for( CSVFileParser *fp in [CSVFileParser files] )
{
fp.hasBeenDownloaded = NO;
fp.hasFailedToDownload = NO;
}
}
#define DEFS_COLUMN_NAMES @"defaultColumnNames"
+ (void) saveColumnNames
{
NSMutableDictionary *d = [NSMutableDictionary dictionary];
for( CSVFileParser *parser in self.files)
{
// Note that parsers don't have shown column names configured unless they've been shown in UI ->
// We must take from parser if exists, otherwise from old defaults
if( [parser.shownColumnNames count] > 0){
[d setObject:parser.shownColumnNames forKey:parser.fileName];
}
else{
[d setObject:[self shownColumnsFromDefaults:parser] forKey:parser.fileName];
}
}
[[NSUserDefaults standardUserDefaults] setObject:d forKey:DEFS_COLUMN_NAMES];
}
+ (NSMutableArray *) shownColumnsFromDefaults:(CSVFileParser *)parser
{
NSDictionary *d = [[NSUserDefaults standardUserDefaults] objectForKey:DEFS_COLUMN_NAMES];
if( d && [d isKindOfClass:[NSDictionary class]])
{
NSArray *cols = [d objectForKey:parser.fileName];
if( cols && [cols isKindOfClass:[NSArray class]])
{
return [NSMutableArray arrayWithArray:cols];
}
}
return [NSMutableArray array];
}
+ (NSArray *) allowedDelimiters
{
static NSArray *delimiters = nil;
if( !delimiters )
delimiters = [NSArray arrayWithObjects:@",", @";", @".", @"|", @" ", @"\t", nil];
return delimiters;
}
- (BOOL) downloadedLocally
{
return !self.URL || [self.URL isEqualToString:@""];
}
- (void) loadFile
{
NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:self.filePath];
if( !d || ![d isKindOfClass:[NSDictionary class]])
return;
self.URL = [d objectForKey:FILEPARSER_URL];
self.downloadDate = [d objectForKey:FILEPARSER_DOWNLOAD_DATE];
if( [d objectForKey:FILEPARSER_HIDE_ADDRESS] )
self.hideAddress = [[d objectForKey:FILEPARSER_HIDE_ADDRESS] boolValue];
else
self.hideAddress = NO;
self.rawData = [d objectForKey:FILEPARSER_RAW_DATA];
if( self.rawData )
{
_rawString = [[NSString alloc] initWithData:self.rawData
encoding:[CSVFileParser getEncodingForFile:[self fileName]]];
}
}
- (void) invalidateShortDescriptions
{
for( CSVRow *row in self.parsedItems )
{
row.shortDescription = nil;
row.lowercaseShortDescription = nil;
}
}
// Returns FALSE if error is encountered
- (BOOL) parse:(NSString *)s
delimiter:(int)delimiter
testing:(BOOL)testing
foundColumns:(int *)foundColumns
useCorrect:(BOOL)useCorrect
{
NSUInteger encoding = NSUTF8StringEncoding; // Use an encoding which works fine w c-string
*foundColumns = -1;
[self.parsedItems removeAllObjects];
[self.columnNames removeAllObjects];
self.problematicRow = nil;
self.iconIndex = NSNotFound;
if( useCorrect )
{
if( testing)
{
self.problematicRow = @"Internal error; tried to automatically find delimiter using invald parsing logic. Please contact the developer.";
return FALSE;
}
NSUInteger numberOfRows = 0;
CSVParser *parser = [[CSVParser alloc] init];
[parser setEncoding:encoding];
[parser setDelimiter:delimiter];
parser.string = s;
NSMutableArray *tmp = [parser parseFile];
if( [tmp count] == 0)
{
self.problematicRow = @"No rows at all found in the file. Try toggling the setting for 'Alternative parsing', 'Keep quotes', and/or 'Encoding' in the previous 'Files' view";
return FALSE;
}
else if( [tmp count] == 1)
{
self.problematicRow = @"Only 1 row found in the file. Try toggling the setting for 'Alternative parsing', 'Keep quotes', and/or 'Encoding' in the previous 'Files' view";
return FALSE;
}
// First, the column names should not be saved as a CSVRow so we read in the names, and then remove the row
[self.columnNames addObjectsFromArray:[tmp objectAtIndex:0]];
NSUInteger numberOfColumns = [self.columnNames count];
if( (self.iconIndex = [self.columnNames indexOfObject:ITEM_ICON_COLUMN_NAME]) != NSNotFound )
[self.columnNames removeObjectAtIndex:self.iconIndex];
[tmp removeObjectAtIndex:0];
numberOfRows = [tmp count];
NSMutableArray *words;
for( NSUInteger rawrow = 0 ; rawrow < numberOfRows ; rawrow++ )
{
words = [tmp objectAtIndex:rawrow];
if( [words count] != numberOfColumns )
{
if( !self.problematicRow){
self.problematicRow = [NSString stringWithFormat:@"Found %lu columns but in row %lu %lu values were found; row values:\n%@",
(unsigned long)numberOfColumns,
(unsigned long)rawrow+1,
(unsigned long)[words count],
words];
}
}
else
{
CSVRow *csvrow = [[CSVRow alloc] initWithItemCapacity:[self.columnNames count]];
csvrow.fileParser = self;
if( self.iconIndex != NSNotFound )
{
csvrow.imageName = [words objectAtIndex:self.iconIndex];
[words removeObjectAtIndex:self.iconIndex];
}
csvrow.items = words;
[self.parsedItems addObject:csvrow];
}
}
}
else
{
NSRange lineRange;
int numberOfRows = 0;
NSUInteger lineStart, lineEnd, nextLineStart;
NSString *line;
int maxWords = 64;
unsigned char buf[1000000];
unsigned char *row[maxWords];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:(testing ? 2 : 5000)];
int maxNumberOfRows = (testing ? 3 : 1000000);
NSUInteger length = [s length];
int csvParseFlags = CSV_TRIM | ([CSVPreferencesController keepQuotes] ? 0 : CSV_QUOTES);
lineStart = lineEnd = nextLineStart = 0;
while( nextLineStart < length && numberOfRows < maxNumberOfRows )
{
[s getLineStart:&lineStart end:&nextLineStart
contentsEnd:&lineEnd forRange:NSMakeRange(nextLineStart, 0)];
lineRange = NSMakeRange(lineStart, lineEnd - lineStart);
line = [s substringWithRange:lineRange];
if( csv_row_parse((const unsigned char *)[line cStringUsingEncoding:encoding],
65536, buf, 65536, row, maxWords, delimiter, csvParseFlags) != -1 )
{
int wordNumber = 0;
NSString *word;
NSMutableArray *words = [[NSMutableArray alloc] init];
while( row[wordNumber] != NULL && wordNumber < maxWords)
{
if( [result count] < wordNumber+1 )
[result addObject:[NSMutableArray array]];
word = [[NSString alloc] initWithBytes:row[wordNumber]
length:strlen((const char *)row[wordNumber])
encoding:encoding];
[words addObject:word];
wordNumber++;
}
// Was this the
if( *foundColumns == -1 )
{
*foundColumns = wordNumber;
}
else if( *foundColumns != wordNumber && wordNumber != 0 )
{
if(!testing && wordNumber < *foundColumns )
{
// Adding empty values for the last columns, i.e. handling cases like some Excel exports where nothing is exported in case there is nothing in the last columns
for( int i = 0 ; i < *foundColumns - wordNumber ; i++ )
[words addObject:@""];
}
else
{
if( !testing )
{
if( !self.problematicRow)
{
self.problematicRow = [NSString stringWithFormat:@"Found %d columns but in row %d %d values were found; row values:\n%@",
*foundColumns,
numberOfRows,
wordNumber,
line];
}
}
return FALSE;
}
}
// Add data if not testing
if( !testing )
{
// So columns are here; store them but do not create a CSVRow boject, of course
if( numberOfRows == 0 )
{
[self.columnNames addObjectsFromArray:words];
if( (self.iconIndex = [self.columnNames indexOfObject:ITEM_ICON_COLUMN_NAME]) != NSNotFound )
{
[self.columnNames removeObjectAtIndex:self.iconIndex];
}
}
else
{
CSVRow *csvrow = [[CSVRow alloc] initWithItemCapacity:[self.columnNames count]];
csvrow.fileParser = self;
if( self.iconIndex != NSNotFound )
{
csvrow.imageName = [words objectAtIndex:self.iconIndex];
[words removeObjectAtIndex:self.iconIndex];
}
csvrow.items = words;
[self.parsedItems addObject:csvrow];
}
}
}
else
{
if( !self.problematicRow){ // Pick first problematic row
self.problematicRow = [NSString stringWithFormat:@"Problem parsing row %d:\n%@", numberOfRows, line];
}
}
numberOfRows++;
}
}
if( !testing &&
[CSVPreferencesController useMonospacedFont] &&
[self.parsedItems count] > 0) // Last should always be true since we return otherwise but in case the code changes, best to check to avoid exception below.
{
// So now we have all CSVRows with the words and we need to create the fixed width words as well.
//
// First, find the widths.
CSVRow *firstRow = [self.parsedItems objectAtIndex:0];
size_t numberOfWidths = [[firstRow items] count];
int *columnWidths = NULL;
columnWidths = calloc(numberOfWidths, sizeof(int)); // Code belows relies on 0-initialised.
switch([CSVPreferencesController fixedWidthsAlternative])
{
case PREDEFINED_FIXED_WIDTHS:
{
int col = 0;
for( NSString *width in [firstRow items])
{
columnWidths[col++] = MIN([width intValue], MAX_PREDEFINED_WIDTH);
}
[self.parsedItems removeObjectAtIndex:0];
break;
}
case AUTOMATIC_FIXED_WIDTHS:
{
// So find the max word length for each column. Using calloc we already have 0 for all entries.
for( CSVRow *row in self.parsedItems){
for( int col = 0 ; col < MIN([row.items count], [self.columnNames count]) ; ++col)
{
if( columnWidths[col] == MAX_AUTO_WIDTH)
{
continue; // No point in checking length of string here
}
NSUInteger actualLength = [(NSString *)[row.items objectAtIndex:col] characterCount];
int wordLength = MIN((int)actualLength, MAX_AUTO_WIDTH);
if( wordLength > columnWidths[col])
{
columnWidths[col] = wordLength;
}
}
}
break;
}
case NO_FIXED_WIDTHS:
free(columnWidths);
columnWidths = NULL;
break;
}
if( columnWidths != NULL )
{
for( CSVRow *row in self.parsedItems)
{
[row createFixedWidthItemsUsingWidths:columnWidths];
}
free(columnWidths);
}
}
return TRUE;
}
- (unichar) delimiter
{
if( [CSVPreferencesController smartDelimiter] )
{
int foundColumns;
int bestResult = 0;
unichar bestDelimiter = ',';
for( NSString *testDelimiter in [CSVFileParser allowedDelimiters] )
{
if([self parse:_rawString
delimiter:[testDelimiter characterAtIndex:0]
testing:YES
foundColumns:&foundColumns
useCorrect:NO] &&
foundColumns > 0 &&
foundColumns > bestResult )
{
bestResult = foundColumns;
bestDelimiter = [testDelimiter characterAtIndex:0];
}
}
return bestDelimiter;
}
else
{
return [[CSVPreferencesController delimiter] characterAtIndex:0];
}
}
- (void)parseString
{
int foundColumns;
self.usedDelimiter = [self delimiter];
// Parse file
[self parse:_rawString
delimiter:self.usedDelimiter
testing:NO
foundColumns:&foundColumns
useCorrect:[CSVPreferencesController useCorrectParsing]];
}
- (void) parseIfNecessary
{
if( !_hasBeenParsed )
{
[self parseString];
_hasBeenParsed = YES;
}
}
- (void) reparseIfParsed
{
if( _hasBeenParsed )
{
[self parseString];
}
}
- (void) encodingUpdated
{
_rawString = nil;
if( self.rawData)
{
_rawString = [[NSString alloc] initWithData:self.rawData
encoding:[CSVFileParser getEncodingForFile:[self fileName]]];
}
[self reparseIfParsed];
[self resetColumnsInfo];
}
- (id) init
{
self = [super init];
self.parsedItems = [[NSMutableArray alloc] init];
self.columnNames = [[NSMutableArray alloc] init];
self.hasBeenParsed = NO;
self.rawShownColumnIndexes = NULL;
return self;
}
- (id) initWithRawData:(NSData *)d filePath:(NSString *)path fileURL:(NSString *)fileURL
{
self = [self init];
self.filePath = path;
self.rawData = [NSData dataWithData:d];
self.URL = fileURL;
if( self.rawData )
{
_rawString = [[NSString alloc] initWithData:self.rawData
encoding:[CSVFileParser getEncodingForFile:[self fileName]]];
}
return self;
}
- (id) initWithFilePath:(NSString *)path
{
self = [self init];
self.filePath = path;
[self loadFile];
return self;
}
- (void) dealloc
{
self.parsedItems = nil;
self.columnNames = nil;
_rawString = nil;
self.rawData = nil;
self.problematicRow = nil;
self.URL = nil;
self.downloadDate = nil;
self.filePath = nil;
[CSVFileParser removeFile:self];
}
- (NSString *) fileName
{
return [[self.filePath lastPathComponent] decomposedStringWithCanonicalMapping];
}
- (NSUInteger) stringLength
{
return [_rawString length];
}
+ (void) loadParserFromFilePath:(NSString *)path
{
CSVFileParser *cfp = [[self alloc] initWithFilePath:path];
[self addParser:cfp];
}
+ (CSVFileParser *) addParserWithRawData:(NSData *)data forFilePath:(NSString *)path fileURL:(NSString *)URL
{
CSVFileParser *cfp = [[self alloc] initWithRawData:data filePath:path fileURL:URL];
[self addParser:cfp];
return cfp;
}
- (NSData *) fileRawData
{
return self.rawData;
}
- (void) saveToFile
{
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:self.rawData, FILEPARSER_RAW_DATA,
self.URL, FILEPARSER_URL,
self.downloadDate, FILEPARSER_DOWNLOAD_DATE,
[NSNumber numberWithBool:self.hideAddress], FILEPARSER_HIDE_ADDRESS,
nil];
if( [d writeToFile:self.filePath atomically:YES])
{
// Set the file path to the actual string you get back when looking at the file in the file system. Sounds weird, but if you e.g. used an http URL with file name containing "Ö" and write using this string as path to the file system and you then read the file name, you'll still get "Ö" (naturally) but it won't compare equal to the original "Ö"...
}
}
- (NSString *) readableUsedDelimiter
{
if( self.usedDelimiter == '\t')
return @"<tab>";
else if( self.usedDelimiter == ' ' )
return @"<space>";
else
return [NSString stringWithFormat:@"%C", self.usedDelimiter];
}
- (NSString *) parseErrorString
{
NSMutableString *s = [NSMutableString string];
// What type of problem?
if( [self.rawString length] == 0 )
{
if( !self.rawData )
[s appendString:@"No data found in file; please delete it and download/import it again.\n\nNote that version 4.3 unfortunately had a bug which caused the app to lose the data about the downloaded file in some cases, resulting in this exact problem."];
else
[s appendString:@"Couldn't read the file using the currently selected encoding; please try another one in the settings available in the previous 'Files' view.\n\nNote that if you want to override the selected encoding for a single file, long-click on the file instead and select encoding in the shown view instead."];
}
else
{
[s appendFormat:@"Used separator: %@\nFound number of columns using separator: %lu\nRead rows using separator: %lu",
[self readableUsedDelimiter],
(unsigned long)[[self columnNames] count],
(unsigned long)[self.parsedItems count]];
if( [[self columnNames] count] == 1){
[s appendString:@"\n\nThe problem is likely that the correct separator was not found, since only having one column is unusual (but possible). Please specify the correct separator manually in the 'Files' view settings."];
return s;
}
[s appendString:@"\n\nThe problem is either due to an error in the file itself, or to current app settings. Potential fixes using app settings available from the 'Files' view:\n\n- If above separator is wrong, specify separator manually\n- If the text read from the file shown below looks weird, change the encoding (most common is UTF8, followed by ISOLatin1)\n- Change the \"Alternative parsing\" setting\n- Change the \"Keep Quotes\" setting"];
if( self.problematicRow && ![self.problematicRow isEqualToString:@""] ){
[s appendFormat:@"\n\nPotential problem:\n\n%@\n\n",
self.problematicRow];
}
else
{
[s appendFormat:@"\n\nFile read when using the currently selected encoding:\n\n%@", self.rawString];
}
}
return s;
}
- (void) updateRawColumnIndexes
{
if( self.rawShownColumnIndexes)
{
free(self.rawShownColumnIndexes);
self.rawShownColumnIndexes = NULL;
}
self.shownColumnIndexes = [NSMutableArray array];
for( NSString *usedColumn in self.shownColumnNames )
{
for( NSUInteger i = 0 ; i < [self.columnNames count] ; i++ )
{
if( [usedColumn isEqualToString:[self.columnNames objectAtIndex:i]] )
{
[self.shownColumnIndexes addObject:[NSNumber numberWithUnsignedInteger:i]];
break;
}
}
}
// Here we can run into a problem: Normally sizeof(shownColumnNames) = sizeof(shownColumnIndexes).
// But if we e.g. have changed encoding used, the previously saved column names to show might no longer
// exist among the available column names. If this happens, let's just redo
if( [self.shownColumnIndexes count] != [self.shownColumnNames count])
{
self.shownColumnNames = [self.columnNames mutableCopy];
[self updateRawColumnIndexes];
}
else
{
self.rawShownColumnIndexes = malloc(sizeof(int) * [self.shownColumnIndexes count]);
for( int i = 0 ; i < [self.shownColumnIndexes count] ; i++ )
{
self.rawShownColumnIndexes[i] = [[self.shownColumnIndexes objectAtIndex:i] intValue];
}
}
}
- (void) updateColumnsInfoWithShownColumns:(NSArray *)shown
{
// If we have something in shown, use that for shown columns
// If not, if we have hidden stuff, use that
// If not, take shown from defaults
// If no defaults, shown = all
if( [shown count] > 0 )
{
self.shownColumnNames = [NSMutableArray arrayWithArray:shown];
}
else if( [self.predefineHiddenColumns count] > 0){
self.shownColumnNames = [NSMutableArray array];
for( NSUInteger index = 0 ; index < [self.columnNames count] ; index++)
{
if( ![self.predefineHiddenColumns containsIndex:index] )
[self.shownColumnNames addObject:[self.columnNames objectAtIndex:index]];
}
// So we've used these. Save result, and remove the cached ones.
if( [self.shownColumnNames count] > 0)
{
[CSVFileParser saveColumnNames];
[self.predefineHiddenColumns removeAllIndexes];
}
}
else
{
self.shownColumnNames = [CSVFileParser shownColumnsFromDefaults:self];
if( [self.shownColumnNames count] == 0 ){
self.shownColumnNames = [self.columnNames mutableCopy];
}
}
[self updateRawColumnIndexes];
}
- (void) updateColumnsInfo
{
[self updateColumnsInfoWithShownColumns:nil];
}
- (void) resetColumnsInfo
{
self.shownColumnNames = [self.columnNames mutableCopy];
[self updateRawColumnIndexes];
}
- (BOOL) hiddenColumnsExist
{
return [self.shownColumnIndexes count] < [self.columnNames count];
}
@end
@implementation CSVFileParser (OzyTableViewProtocol)
- (NSString *) defaultTableViewDescription
{
NSString *s = [[self filePath] lastPathComponent];
// First remove the .csvtouch extension. Should always be there, but we need to be careful about
// backwards compatibility
if( [[[s pathExtension] lowercaseString] isEqualToString:@"csvtouch"] )
s = [s stringByDeletingPathExtension];
// Then, if file is from URL, lastPathComponent might be something like pub.csv?download=csv&style=3. For example links to Google docs look like that. So, remove everything after first '?'.
if( !self.downloadedLocally )
{
NSArray *array = [s componentsSeparatedByString:@"?"];
if( [array count] > 1 )
s = [array objectAtIndex:0];
}
// Then we remove standard csv file extensions
if( [[[s pathExtension] lowercaseString] isEqualToString:@"csv"] ||
[[[s pathExtension] lowercaseString] isEqualToString:@"tsv"] ||
[[[s pathExtension] lowercaseString] isEqualToString:@"txt"] )
return [s stringByDeletingPathExtension];
else
return s;
}
- (NSString *) tableViewDescription
{
if( self.hasFailedToDownload)
return [NSString stringWithFormat:@"‒ %@", [self defaultTableViewDescription]];
else if( self.hasBeenDownloaded )
return [NSString stringWithFormat:@"✓ %@", [self defaultTableViewDescription]];
else
return [self defaultTableViewDescription];
}
- (NSComparisonResult) compareFileName:(CSVFileParser *)fp
{
return [[self defaultTableViewDescription] compare:[fp defaultTableViewDescription] options:NSNumericSearch];
}
- (NSString *) imageName
{
NSDate *nextDownload = [CSVPreferencesController nextDownload];
if(self.downloadDate && nextDownload &&
[nextDownload timeIntervalSinceDate:self.downloadDate] > 24*60*60 )
return @"alert.png";
return nil;
}
- (NSString *) emptyImageName
{
return nil;
}
@end
@implementation CSVFileParser (Preferences)
+ (void) initialize
{
if( self == [CSVFileParser class])
{
NSDictionary *encodings = [[NSUserDefaults standardUserDefaults] objectForKey:DEFS_ENCODING_FOR_FILES];
if( encodings && [encodings isKindOfClass:[NSDictionary class]] )
{
encodingForFileName = [[NSMutableDictionary alloc] initWithDictionary:encodings];
}
else
{
encodingForFileName = [[NSMutableDictionary alloc] init];
}
_allowedEncodings = [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInteger:DEFAULT_ENCODING],
[NSNumber numberWithUnsignedInteger:NSUTF8StringEncoding],
[NSNumber numberWithUnsignedInteger:NSUnicodeStringEncoding],
[NSNumber numberWithUnsignedInteger:NSISOLatin1StringEncoding],
[NSNumber numberWithUnsignedInteger:NSMacOSRomanStringEncoding], nil];
_allowedEncodingNames = [NSArray arrayWithObjects:@"default", @"UTF8", @"Unicode", @"Latin1", @"Mac", nil];
_files = [NSMutableArray array];
}
}
+ (void) saveEncodingForFiles
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if( encodingForFileName )
{
[defaults setObject:encodingForFileName forKey:DEFS_ENCODING_FOR_FILES];
[defaults synchronize];
}
}
+ (void) setFileEncoding:(NSStringEncoding)encoding forFile:(NSString *)fileName
{
if( fileName != nil)
{
[encodingForFileName setObject:[NSNumber numberWithUnsignedInteger:encoding]
forKey:fileName];
[self saveEncodingForFiles];
}
}
+ (void) removeFileEncodingForFile:(NSString *) fileName
{
[encodingForFileName removeObjectForKey:fileName];
[self saveEncodingForFiles];
}
+ (NSStringEncoding) getEncodingForFile:(NSString *)fileName
{
NSNumber *encoding = [encodingForFileName objectForKey:fileName];
if( encoding && [encoding unsignedIntegerValue] != DEFAULT_ENCODING)
{
return [encoding unsignedIntegerValue];
}
else
{
return [CSVPreferencesController encoding];
}
}
+ (NSUInteger) getEncodingSettingForFile:(NSString *)fileName
{
if( !fileName || [fileName isEqualToString:@""]){
return DEFAULT_ENCODING;
}
NSNumber *encoding = [encodingForFileName objectForKey:fileName];
if( encoding )
{
return [encoding unsignedIntegerValue];
}
else
{
return DEFAULT_ENCODING;
}
}
@end