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
|
/* HBChapterTitlesController.m $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import "HBChapterTitlesController.h"
@import HandBrakeKit.HBChapter;
@import HandBrakeKit.HBJob;
@interface NSArray (HBCSVAdditions)
+ (nullable NSArray<NSArray<NSString *> *> *)HB_arrayWithContentsOfCSVURL:(NSURL *)url;
@end
@implementation NSArray (HBCSVAdditions)
// CSV parsing examples
// CSV Record:
// one,two,three
// Fields:
// <one>
// <two>
// <three>
// CSV Record:
// one, two, three
// Fields:
// <one>
// < two>
// < three>
// CSV Record:
// one,"2,345",three
// Fields:
// <one>
// <2,345>
// <three>
// CSV record:
// one,"John said, ""Hello there.""",three
// Explanation: inside a quoted field, two double quotes in a row count
// as an escaped double quote in the field data.
// Fields:
// <one>
// <John said, "Hello there.">
// <three>
+ (nullable NSArray<NSArray<NSString *> *> *)HB_arrayWithContentsOfCSVURL:(NSURL *)url;
{
NSString *str = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
if (str == nil)
{
return nil;
}
NSMutableString *csvString = [str mutableCopy];
[csvString replaceOccurrencesOfString:@"\r\n" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, csvString.length)];
[csvString replaceOccurrencesOfString:@"\r" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, csvString.length)];
if (!csvString)
{
return 0;
}
if ([csvString characterAtIndex:0] == 0xFEFF)
{
[csvString deleteCharactersInRange:NSMakeRange(0,1)];
}
if ([csvString characterAtIndex:[csvString length]-1] != '\n')
{
[csvString appendFormat:@"%c",'\n'];
}
NSScanner *sc = [NSScanner scannerWithString:csvString];
sc.charactersToBeSkipped = nil;
NSMutableArray *csvArray = [NSMutableArray array];
[csvArray addObject:[NSMutableArray array]];
NSCharacterSet *commaNewlineCS = [NSCharacterSet characterSetWithCharactersInString:@",\n"];
while (sc.scanLocation < csvString.length)
{
if ([sc scanString:@"\"" intoString:NULL])
{
// Quoted field
NSMutableString *field = [NSMutableString string];
BOOL done = NO;
NSString *quotedString;
// Scan until we get to the end double quote or the EOF.
while (!done && sc.scanLocation < csvString.length)
{
if ([sc scanUpToString:@"\"" intoString:"edString])
{
[field appendString:quotedString];
}
if ([sc scanString:@"\"\"" intoString:NULL])
{
// Escaped double quote inside the quoted string.
[field appendString:@"\""];
}
else
{
done = YES;
}
}
if (sc.scanLocation < csvString.length)
{
++sc.scanLocation;
BOOL nextIsNewline = [sc scanString:@"\n" intoString:NULL];
BOOL nextIsComma = NO;
if (!nextIsNewline)
{
nextIsComma = [sc scanString:@"," intoString:NULL];
}
if (nextIsNewline || nextIsComma)
{
[[csvArray lastObject] addObject:field];
if (nextIsNewline && sc.scanLocation < csvString.length)
{
[csvArray addObject:[NSMutableArray array]];
}
}
else
{
// Quoted fields must be immediately followed by a comma or newline.
return nil;
}
}
else
{
// No close quote found before EOF, so file is invalid CSV.
return nil;
}
}
else
{
NSString *field;
[sc scanUpToCharactersFromSet:commaNewlineCS intoString:&field];
BOOL nextIsNewline = [sc scanString:@"\n" intoString:NULL];
BOOL nextIsComma = NO;
if (!nextIsNewline)
{
nextIsComma = [sc scanString:@"," intoString:NULL];
}
if (nextIsNewline || nextIsComma)
{
[[csvArray lastObject] addObject:field];
if (nextIsNewline && sc.scanLocation < csvString.length)
{
[csvArray addObject:[NSMutableArray array]];
}
}
}
}
return csvArray;
}
@end
@interface HBChapterTitlesController () <NSTableViewDataSource, NSTableViewDelegate>
@property (weak) IBOutlet NSTableView *table;
@property (nonatomic, readwrite, strong) NSArray<HBChapter *> *chapterTitles;
@end
@implementation HBChapterTitlesController
- (instancetype)init
{
self = [super initWithNibName:@"ChaptersTitles" bundle:nil];
if (self)
{
_chapterTitles = [[NSMutableArray alloc] init];
}
return self;
}
- (void)setJob:(HBJob *)job
{
_job = job;
self.chapterTitles = job.chapterTitles;
}
/**
* Method to edit the next chapter when the user presses Return.
* We queue the action on the runloop to avoid interfering
* with the chain of events that handles the edit.
*/
- (void)controlTextDidEndEditing:(NSNotification *)notification
{
NSTableView *chapterTable = self.table;
NSInteger column = 2;
NSInteger row = [self.table rowForView:[notification object]];
NSInteger textMovement;
// Edit the cell in the next row, same column
row++;
textMovement = [[notification userInfo][@"NSTextMovement"] integerValue];
if (textMovement == NSReturnTextMovement && row < chapterTable.numberOfRows)
{
NSArray *info = @[chapterTable, @(column), @(row)];
// The delay is unimportant; editNextRow: won't be called until the responder
// chain finishes because the event loop containing the timer is on this thread
[self performSelector:@selector(editNextRow:) withObject:info afterDelay:0.0];
}
}
- (void)editNextRow:(id)objects
{
NSTableView *chapterTable = objects[0];
NSInteger column = [objects[1] integerValue];
NSInteger row = [objects[2] integerValue];
if (row >= 0 && row < chapterTable.numberOfRows)
{
[chapterTable selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
[chapterTable editColumn:column row:row withEvent:nil select:YES];
}
}
#pragma mark - Chapter Files Import / Export
- (BOOL)importChaptersFromURL:(NSURL *)URL error:(NSError **)outError
{
NSArray<NSArray<NSString *> *> *csvData = [NSArray HB_arrayWithContentsOfCSVURL:URL];
if (csvData.count == self.chapterTitles.count)
{
NSUInteger i = 0;
for (NSArray<NSString *> *lineFields in csvData)
{
if (lineFields.count < 2 || [lineFields[0] integerValue] != i + 1)
{
if (NULL != outError)
{
*outError = [NSError errorWithDomain:@"HBError" code:0 userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid chapters CSV file", nil),
NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString(@"The CSV file is not a valid chapters CSV file.", nil)}];
}
return NO;
}
i++;
}
NSUInteger j = 0;
for (NSArray<NSString *> *lineFields in csvData)
{
[self.chapterTitles[j] setTitle:lineFields[1]];
j++;
}
return YES;
}
if (NULL != outError)
{
*outError = [NSError errorWithDomain:@"HBError" code:0 userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"Incorrect line count", nil),
NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString(@"The line count in the chapters CSV file does not match the number of chapters in the movie.", nil)}];
}
return NO;
}
- (IBAction)browseForChapterFile:(id)sender
{
// We get the current file name and path from the destination field here
NSURL *sourceDirectory = [[NSUserDefaults standardUserDefaults] URLForKey:@"HBLastDestinationDirectory"];
// Open a panel to let the user choose the file
NSOpenPanel *panel = [NSOpenPanel openPanel];
panel.allowedFileTypes = @[@"csv", @"txt"];
panel.directoryURL = sourceDirectory;
[panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
{
if (result == NSFileHandlingPanelOKButton)
{
NSError *error;
if ([self importChaptersFromURL:panel.URL error:&error] == NO)
{
[self presentError:error];
}
}
}];
}
- (IBAction)browseForChapterFileSave:(id)sender
{
NSURL *destinationDirectory = [[NSUserDefaults standardUserDefaults] URLForKey:@"HBLastDestinationDirectory"];
NSSavePanel *panel = [NSSavePanel savePanel];
panel.allowedFileTypes = @[@"csv"];
panel.directoryURL = destinationDirectory;
panel.nameFieldStringValue = self.job.destURL.lastPathComponent.stringByDeletingPathExtension;
[panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
{
if (result == NSFileHandlingPanelOKButton)
{
NSError *saveError;
NSMutableString *csv = [NSMutableString string];
NSInteger idx = 0;
for (HBChapter *chapter in self.chapterTitles)
{
// put each chapter title from the table into the array
[csv appendFormat:@"%ld,",idx + 1];
idx++;
NSString *sanatizedTitle = [chapter.title stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
// If the title contains any commas or quotes, add quotes
if ([sanatizedTitle containsString:@","] || [sanatizedTitle containsString:@"\""])
{
[csv appendString:@"\""];
[csv appendString:sanatizedTitle];
[csv appendString:@"\""];
}
else
{
[csv appendString:sanatizedTitle];
}
[csv appendString:@"\n"];
}
[csv deleteCharactersInRange:NSMakeRange(csv.length - 1, 1)];
// try to write it to where the user wanted
if (![csv writeToURL:panel.URL
atomically:YES
encoding:NSUTF8StringEncoding
error:&saveError])
{
[panel close];
[[NSAlert alertWithError:saveError] runModal];
}
}
}];
}
@end
|