summaryrefslogtreecommitdiff
path: root/ADSBDecoder/main.swift
diff options
context:
space:
mode:
Diffstat (limited to 'ADSBDecoder/main.swift')
-rw-r--r--ADSBDecoder/main.swift119
1 files changed, 118 insertions, 1 deletions
diff --git a/ADSBDecoder/main.swift b/ADSBDecoder/main.swift
index fa84962..a324a0f 100644
--- a/ADSBDecoder/main.swift
+++ b/ADSBDecoder/main.swift
@@ -6,6 +6,123 @@
//
import Foundation
+import ArgumentParser
+import RegexBuilder
-print("Hello, World!")
+//return true if file excists
+func checkIfFileExists(_ fname: String) -> Bool {
+ let fm = FileManager.default
+ if fm.fileExists(atPath: fname) {
+ return true
+ }
+ return false
+}
+//get current run directory
+func getCurrentDirPath() -> String {
+ return Process().currentDirectoryPath
+}
+
+struct CommandLineArgs: ParsableCommand {
+ @Option(name: .shortAndLong) var inputfile: String
+ @Flag(name: .shortAndLong) var debug:Bool = false
+ @Flag(name: .shortAndLong) var version:Bool = false
+ @Flag(name: .shortAndLong) var show_stats:Bool = false
+}
+
+let args = CommandLineArgs.parseOrExit()
+
+let fileUrl = URL(fileURLWithPath:args.inputfile)
+print("File location [\(fileUrl.absoluteString)]")
+
+//check if file excists
+if (checkIfFileExists(fileUrl.path) == false) {
+ print("Supplied path \(fileUrl.path) doesnt exists")
+ exit(1)
+}
+
+//load the file with adsb data
+var adsb_source = ""
+do {
+ adsb_source = try String(contentsOfFile: fileUrl.path)
+ print("Loaded \(adsb_source.count) bytes")
+} catch {
+ print("Couldn't load text from a file \(fileUrl.path)")
+ exit(1)
+}
+
+
+
+let matchADSBLong = Regex {
+ Anchor.startOfLine
+ "*"
+
+ Repeat(
+ CharacterClass(
+ ("a"..."f"),
+ ("0"..."9")
+ )
+ ,count:28)
+
+ ";"
+}
+
+let matchADSBShort = Regex {
+ Anchor.startOfLine
+ "*"
+ Repeat(
+ CharacterClass(
+ ("a"..."f"),
+ ("0"..."9")
+ )
+ ,count:14)
+ ";"
+}
+
+//gather stats
+var q_df = QueryDF()
+var q_dftc = QueryDF17TC()
+
+//parse line by line
+var cnt=0
+for line in adsb_source.components(separatedBy: .newlines) {
+ var found=false
+ //print("\(cnt) \(line)")
+ //cnt += 1
+ if let tokenMatch = try matchADSBLong.prefixMatch(in: line) {
+ //print("\(String(tokenMatch.output))")
+ found = true
+ let str = String(tokenMatch.output)
+ let startIndex = str.index(str.startIndex, offsetBy: 1)
+ let endIndex = str.index(str.endIndex, offsetBy: -2)
+ let decoder = Decoder(String(str[startIndex...endIndex]))
+ if decoder.DataFormat == 17 {
+ if let d17 = decoder.getDataFormat17() {
+ //print(d17)
+ q_dftc.addTC(d17.TypeCode)
+ }
+ } else {
+
+ }
+ q_df.addDF(decoder.DataFormat)
+ };
+
+ if let tokenMatch = try matchADSBShort.prefixMatch(in: line) {
+ print("\(tokenMatch.output)")
+ found = true
+ };
+
+ if (found == false) {
+ print("Unknown adsb data line \(line)")
+ }
+
+
+}
+
+
+if args.show_stats {
+ print("----STAT----")
+ //q_df.showStat()
+ print(q_df)
+ print(q_dftc)
+}