-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileparser.swift
71 lines (61 loc) · 1.46 KB
/
fileparser.swift
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
//
// fileparser.swift
// fileparser
//
// Created by Adam on 2018-02-26.
// Copyright © 2018 Adam Bell. All rights reserved.
//
import Foundation
if CommandLine.arguments.count < 4
{
print( "Usage: fileparser [inputfile] [firstsearchterm] [secondsearchterm]" )
print( " fileparser [-verbose] [inputfile] [firstsearchterm] [secondsearchterm]" )
exit(1)
}
var inputFileIndex = 1
var mainParamStartIndex = 2
var verbose = false
if CommandLine.arguments.count > 2 && CommandLine.arguments[1] == "-verbose"
{
mainParamStartIndex += 1
inputFileIndex += 1
verbose = true
}
var searches = [String]()
for searchStr in mainParamStartIndex..<CommandLine.arguments.count
{
searches.append( CommandLine.arguments[searchStr])
}
let fileURL = URL( fileURLWithPath: CommandLine.arguments[inputFileIndex])
var inBuff = ""
do
{
inBuff = try String( contentsOf: fileURL )
}
catch
{
if verbose
{
print( "Error reading: " + CommandLine.arguments[inputFileIndex] )
}
exit(1)
}
let splitString = inBuff.split( separator: "\n")
var currSearchIndex = 0
for currString in splitString
{
let asString = String(currString)
if asString.range( of: searches[currSearchIndex] ) != nil
{
if verbose
{
print( "Found " + searches[currSearchIndex] );
}
if currSearchIndex + 1 == searches.count
{
exit(0)
}
currSearchIndex = currSearchIndex + 1
}
}
exit(1)