diff options
Diffstat (limited to 'cmd/cmd.swift')
-rw-r--r-- | cmd/cmd.swift | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/cmd/cmd.swift b/cmd/cmd.swift new file mode 100644 index 0000000..ec1ca44 --- /dev/null +++ b/cmd/cmd.swift @@ -0,0 +1,78 @@ +// +// cmd.swift +// cmd +// +// Created by Jacky Jack on 12/06/2023. +// + +import Foundation + +public class CmdToken { + public var type:Int = 0 + public var val="" + init(type: Int, val: String = "") { + self.type = type + self.val = val + } +} + + +public class CmdParser { + + public init() {} + + public func parse() { + var tok:cmd_tok_t = cmd_tok_t() + //var pointer = UnsafeMutablePointer(&tok) + var c="hello 123 0x0" + parse_cmd(&tok, c, c.count) + + + var iter = tok + while (iter.next != nil) { + iter = iter.next.pointee + //print("\(iter.type) \(iter.s) \(iter.e) \(iter.next) \(iter.sz)") + } + } + + public func parse(_ cmd: String) -> Array<CmdToken> { + var token_list = Array<CmdToken>() + var tok:cmd_tok_t = cmd_tok_t() + parse_cmd(&tok, cmd, cmd.count ) + + var iter = tok + var start_str = UnsafeRawPointer(cmd) + //print("cmd \(start_str)") + while (iter.next != nil) { + iter = iter.next.pointee + //print("\(iter.type) \(iter.s) \(iter.e) \(iter.next) \(iter.sz)") + if let start = iter.s { + //print("yes") + } else { + //print("Cant unwrap") + } + let addr_s = UnsafeRawPointer(&iter.s.pointee) + let addr_e = UnsafeRawPointer(&iter.e.pointee) + //let addr_len = addr_e - addr_s + //print("s \(addr_s) e \(addr_e)") + + //let length = iter.e - iter.s + + let sub_st = cmd.index(cmd.startIndex, offsetBy: addr_s - start_str ) + let sub_en = cmd.index(cmd.startIndex, offsetBy: addr_e - start_str ) + let subs = cmd[sub_st..<sub_en] + token_list.append(CmdToken(type: Int(iter.type), val: String(subs))) + } + + + return token_list + } +} +/* +public func parse() { + var tok:cmd_tok_t = cmd_tok_t() + var c="asd 123" + parse_cmd(&tok, c, c.count) + +}*/ + |