Here's a procedure that will take a CL-style string (delimited by spaces, defining multi-word tokens with quotation marks) and split it into tokens.
Code (Contains usage example)
Code: Select all
; ----------------------------------------------------------------------------------------------------
; Title: Commandline String Tokeniser (Procedural)
; Description: Procedure that provides a String Tokeniser for Commandlines.
; Author(s): Michael R. King (mrking2910@gmail.com)
; Revision: 1
; Support: Cross-Platform
;
; Notes: Will use spaces as delimiters, and consider strings encapsulated
; by double quotes as a single token.
; ----------------------------------------------------------------------------------------------------
EnableExplicit
CompilerIf Defined(_PBI_CLTOK_, #PB_Constant) = #False
#_PBI_CLTOK_ = #True
Procedure.l CLTok(String$, List OutputList.s())
Protected cCnt.l, cIx.l, c.s, tQuot.a, t.s
ClearList(OutputList())
cCnt = Len(String$)
For cIx = 1 To (cCnt + 1)
c = Mid(String$, cIx, 1)
If c = #DQUOTE$
tQuot = 1 - tQuot
If tQuot = 0
If t <> ""
AddElement(OutputList()) : OutputList() = t
EndIf
t = ""
EndIf
EndIf
If tQuot = #False
If c = " " Or cIx = (cCnt + 1)
If t <> ""
AddElement(OutputList()) : OutputList() = t
EndIf
t = ""
Else
If c <> #DQUOTE$
t = t + c
EndIf
EndIf
Else
If c <> #DQUOTE$
t = t + c
EndIf
EndIf
Next
If tQuot = 1
If t <> ""
AddElement(OutputList()) : OutputList() = t
EndIf
EndIf
ProcedureReturn ListSize(OutputList())
EndProcedure
CompilerEndIf ;_PBI_CLTOK_
; -------------------------------------------------------------------------------------
; - DEMONSTRATION CODE - DEMONSTRATION CODE - DEMONSTRATION CODE - DEMONSTRATION CODE -
; -------------------------------------------------------------------------------------
; - Define our test string -
Define TestString$ = "hello world " + #DQUOTE$ + "this is" + #DQUOTE$ + "a test"
; - Create an Output List -
NewList Output.s()
; - Tokenise the Test String -
CLTok(TestString$, Output())
; - Display results in the Debug Window -
ForEach Output()
Debug Output()
Next
; - Free the List -
FreeList(Output())
End

Thanks
