-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFilesParsingPreferencesViewController.m
144 lines (136 loc) · 5.72 KB
/
FilesParsingPreferencesViewController.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
//
// FilesParsingPreferencesViewController.m
// CSV Touch
//
// Created by Simon Wigzell on 2018-02-26.
//
#import "FilesParsingPreferencesViewController.h"
#import "CSVPreferencesController.h"
#import "CSVFileParser.h"
@implementation FilesParsingPreferencesViewController
#define AUTO_DELIMITER @"<auto>"
#define TAB_DELIMITER @"tab"
#define SPACE_DELIMITER @"space"
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[delimiterControl setTitle:AUTO_DELIMITER forSegmentAtIndex:0];
[delimiterControl setTitle:@";" forSegmentAtIndex:1];
[delimiterControl setTitle:@"," forSegmentAtIndex:2];
[delimiterControl setTitle:@"." forSegmentAtIndex:3];
[delimiterControl setTitle:@"|" forSegmentAtIndex:4];
[delimiterControl setTitle:TAB_DELIMITER forSegmentAtIndex:5];
[delimiterControl setTitle:SPACE_DELIMITER forSegmentAtIndex:6];
[delimiterControl setWidth:20 forSegmentAtIndex:1];
[delimiterControl setWidth:20 forSegmentAtIndex:2];
[delimiterControl setWidth:20 forSegmentAtIndex:3];
[delimiterControl setWidth:20 forSegmentAtIndex:4];
[delimiterControl sizeToFit];
[encodingControl setTitle:@"IsoLatin1" forSegmentAtIndex:0];
[encodingControl setTitle:@"UTF8" forSegmentAtIndex:1];
[encodingControl setTitle:@"Unicode" forSegmentAtIndex:2];
[encodingControl setTitle:@"Mac" forSegmentAtIndex:3];
[self synchUI];
}
- (void) synchUI
{
alternativeParsing.on = [CSVPreferencesController useCorrectParsing];
keepQuotes.on = [CSVPreferencesController keepQuotes];
BOOL foundMatch = false;
NSString *delimiter = [CSVPreferencesController delimiter];
for( NSUInteger index = 0; index < delimiterControl.numberOfSegments; ++index){
if( [[delimiterControl titleForSegmentAtIndex:index] isEqualToString:delimiter]){
delimiterControl.selectedSegmentIndex = index;
foundMatch = YES;
break;
}
else if( [[delimiterControl titleForSegmentAtIndex:index] isEqualToString:AUTO_DELIMITER] &&
delimiter == nil){
delimiterControl.selectedSegmentIndex = index;
foundMatch = YES;
break;
}
else if( [[delimiterControl titleForSegmentAtIndex:index] isEqualToString:TAB_DELIMITER] &&
[delimiter isEqualToString:@"\t"] ){
delimiterControl.selectedSegmentIndex = index;
foundMatch = YES;
break;
}
else if( [[delimiterControl titleForSegmentAtIndex:index] isEqualToString:SPACE_DELIMITER] &&
[delimiter isEqualToString:@" "] ){
delimiterControl.selectedSegmentIndex = index;
foundMatch = YES;
break;
}
}
if( !foundMatch){
delimiterControl.selectedSegmentIndex = 0;
}
foundMatch = NO;
NSStringEncoding encoding = [CSVPreferencesController encoding];
for( NSUInteger index = 0; index < encodingControl.numberOfSegments; ++index){
if( [[encodingControl titleForSegmentAtIndex:index] isEqualToString:@"UTF8"] &&
encoding == NSUTF8StringEncoding){
encodingControl.selectedSegmentIndex = index;
foundMatch = YES;
break;
}
else if( [[encodingControl titleForSegmentAtIndex:index] isEqualToString:@"Unicode"] &&
encoding == NSUnicodeStringEncoding){
encodingControl.selectedSegmentIndex = index;
foundMatch = YES;
break;
}
else if( [[encodingControl titleForSegmentAtIndex:index] isEqualToString:@"Latin1"] &&
encoding == NSISOLatin1StringEncoding){
encodingControl.selectedSegmentIndex = index;
foundMatch = YES;
break;
}
if( [[encodingControl titleForSegmentAtIndex:index] isEqualToString:@"Mac"] &&
encoding == NSMacOSRomanStringEncoding){
encodingControl.selectedSegmentIndex = index;
foundMatch = YES;
break;
}
}
if( !foundMatch){
encodingControl.selectedSegmentIndex = 0;
}
}
- (IBAction)somethingChanged:(id)sender
{
if( sender == delimiterControl){
NSString *title = [delimiterControl titleForSegmentAtIndex:delimiterControl.selectedSegmentIndex];
if( [title isEqualToString:AUTO_DELIMITER] )
[CSVPreferencesController setDelimiter:nil];
else if( [title isEqualToString:TAB_DELIMITER] )
[CSVPreferencesController setDelimiter:@"\t"];
else if( [title isEqualToString:SPACE_DELIMITER] )
[CSVPreferencesController setDelimiter:@" "];
else
[CSVPreferencesController setDelimiter:title];
}
else if( sender == encodingControl){
NSString *title = [encodingControl titleForSegmentAtIndex:encodingControl.selectedSegmentIndex];
if( [title isEqualToString:@"UTF8"] )
[CSVPreferencesController setStringEncoding:NSUTF8StringEncoding];
else if( [title isEqualToString:@"IsoLatin1"] )
[CSVPreferencesController setStringEncoding:NSISOLatin1StringEncoding];
else if( [title isEqualToString:@"Unicode"] )
[CSVPreferencesController setStringEncoding:NSUnicodeStringEncoding];
else if( [title isEqualToString:@"Mac"] )
[CSVPreferencesController setStringEncoding:NSMacOSRomanStringEncoding];
}
else if( sender == keepQuotes){
[CSVPreferencesController setKeepQuotes:keepQuotes.on];
}
else if( sender == alternativeParsing){
[CSVPreferencesController setUseCorrectParsing:alternativeParsing.on];
}
// We need to reparse & save results
[[CSVFileParser files] makeObjectsPerformSelector:@selector(encodingUpdated)];
[CSVFileParser saveColumnNames];
[self synchUI];
}
@end