'=== Full description for the ParseLine&() function is available
'=== in the separate HTML document.
'=====================================================================
REDIM a$
(3 TO 4) 'result array (at least one element)
'=== e$ = example description
'=== s$ = used separators (max. 5 chars)
'=== q$ = used quotes (max. 2 chars) (empty = regular ")
'=== l$ = test line to parse
'=====================================================================
e$ = "empty lines or those containing defined separators only, won't give a result"
s$ = " ,.": q$ = ""
l$ = " ,. , ., ., . ,.,., .,,,,,. ,., "
e$ = "a simple text line, using space, comma and period as separators and regular quoting"
s$ = " ,.": q$ = ""
l$
= "Hello World, just want to say,greetings " + CHR$(34) + "to all" + CHR$(34) + " from RhoSigma."e$ = "now a complex space separated test line with regular quoting and empty quotes"
s$ = " ": q$ = ""
e$ = "same space separated test line with reodered quoting and empty quotes"
s$ = " ": q$ = ""
e$ = "again the space separated test line with regular quoting and an unfinished (EOL) quote"
s$ = " ": q$ = ""
l$
= " ABC 123" + CHR$(34) + " " + CHR$(34) + " X Y Z " + CHR$(34) + " " + CHR$(34) + CHR$(34) + "345 "e$ = "an opening quote at EOL is in fact an empty quote, it adds another empty array element"
s$ = " ": q$ = ""
l$
= " " + CHR$(34) + "a final open quote is empty" + CHR$(34) + " " + CHR$(34)'-----------------------------
'-----------------------------
e$ = "a SUB declaration line using paranthesis as TWO char quoting"
s$ = " ": q$ = "()"
l$ = "SUB RectFill (lin%, col%, hei%, wid%, fg%, bg%, ch$)"
e$ = "same SUB line with many extra paranthesis, showing that TWO char quoting avoids nesting"
s$ = " ": q$ = "()"
l$ = "SUB RectFill (lin%, col%, ((hei%)), wid%, (fg%), bg%, (ch$))"
'-----------------------------
'-----------------------------
e$ = "space separated command line with regular quoting"
s$ = " ": q$ = ""
l$
= "--testfile " + CHR$(34) + "C:\My Folder\My File.txt" + CHR$(34) + " --testmode --output logfile.txt"e$ = "space and/or equal sign separated command line with regular quoting"
s$ = " =": q$ = ""
l$
= "--testfile=" + CHR$(34) + "C:\My Folder\My File.txt" + CHR$(34) + " --testmode --output=logfile.txt"e$ = "space and/or equal sign separated command line with alternative ONE char quoting"
s$ = " =": q$ = "|"
l$ = "--testfile=|C:\My Folder\My File.txt| --testmode --output=logfile.txt"
e$ = "space and/or equal sign separated command line with alternative TWO char quoting"
s$ = " =": q$ = "{}"
l$ = "--testfile={C:\My Folder\My File.txt} --testmode --output=logfile.txt"
'-----------------------------
'-----------------------------
e$ = "parsing a filename using (back)slashes as separators but NO spaces"
s$ = "\/": q$ = ""
l$ = "C:\My Folder\My File.txt"
e$ = "for quoted filenames the quoting char(s) must be separators instead of quotes (see source)"
'NOTE: a char cannot be used as separator and quote at the same time
s$
= "\/" + CHR$(34): q$
= "*" '* is not allowd in filenames, so it's perfect to knock out the regular quote herel$
= CHR$(34) + "C:\My Folder\My File.txt" + CHR$(34)'=====================================================================
'-- This GOSUB subroutine will execute the examples from above and
'-- print the given inputs and function results.
doFunc:
COLOR 12:
PRINT "square brackets just used to better visualize the start and end of strings ..." PRINT "given input to function:" PRINT "------------------------" res& = ParseLine&(l$, s$, q$, a$(), 0)
PRINT "result of function call (new UBOUND or -1 for nothing to parse):" PRINT "----------------------------------------------------------------"
'--- Full description available in separate HTML document.
'---------------------------------------------------------------------
FUNCTION ParseLine&
(inpLine$
, sepChars$
, quoChars$
, outArray$
(), minUB&
) '--- option _explicit requirements ---
DIM ilen&
, icnt&
, slen%
, s1%
, s2%
, s3%
, s4%
, s5%
, q1%
, q2%
DIM oalb&
, oaub&
, ocnt&
, flag%
, ch%
, nest%
, spos&
, epos&
'--- so far return nothing ---
ParseLine& = -1
'--- init & check some runtime variables ---
ilen&
= LEN(inpLine$
): icnt&
= 1IF slen%
> 5 THEN slen%
= 5 'max. 5 chars, ignore the rest oalb&
= LBOUND(outArray$
): oaub&
= UBOUND(outArray$
): ocnt&
= oalb&
'--- skip preceding separators ---
plSkipSepas:
flag% = 0
ch%
= ASC(inpLine$
, icnt&
) CASE 1: flag%
= ch%
<> s1%
CASE 2: flag%
= ch%
<> s1%
AND ch%
<> s2%
CASE 3: flag%
= ch%
<> s1%
AND ch%
<> s2%
AND ch%
<> s3%
CASE 4: flag%
= ch%
<> s1%
AND ch%
<> s2%
AND ch%
<> s3%
AND ch%
<> s4%
CASE 5: flag%
= ch%
<> s1%
AND ch%
<> s2%
AND ch%
<> s3%
AND ch%
<> s4%
AND ch%
<> s5%
icnt& = icnt& + 1
'--- redim to clear array on 1st word/component ---
'--- expand array, if required ---
plNextWord:
oaub& = oaub& + 10
'--- get current word/component until next separator ---
flag% = 0: nest% = 0: spos& = icnt& - 1
nest% = 1
nest% = nest% + 1
nest% = nest% - 1
ch%
= ASC(inpLine$
, icnt&
) CASE 0: flag%
= (nest%
= 0 AND (ch%
= q1%
)) OR (nest%
= 1 AND ch%
= q2%
) CASE 1: flag%
= (nest%
= 0 AND (ch%
= s1%
OR ch%
= q1%
)) OR (nest%
= 1 AND ch%
= q2%
) CASE 2: flag%
= (nest%
= 0 AND (ch%
= s1%
OR ch%
= s2%
OR ch%
= q1%
)) OR (nest%
= 1 AND ch%
= q2%
) CASE 3: flag%
= (nest%
= 0 AND (ch%
= s1%
OR ch%
= s2%
OR ch%
= s3%
OR ch%
= q1%
)) OR (nest%
= 1 AND ch%
= q2%
) CASE 4: flag%
= (nest%
= 0 AND (ch%
= s1%
OR ch%
= s2%
OR ch%
= s3%
OR ch%
= s4%
OR ch%
= q1%
)) OR (nest%
= 1 AND ch%
= q2%
) CASE 5: flag%
= (nest%
= 0 AND (ch%
= s1%
OR ch%
= s2%
OR ch%
= s3%
OR ch%
= s4%
OR ch%
= s5%
OR ch%
= q1%
)) OR (nest%
= 1 AND ch%
= q2%
) icnt& = icnt& + 1
epos& = icnt& - 1
IF ASC(inpLine$
, spos&
) = q1%
THEN spos&
= spos&
+ 1 outArray$
(ocnt&
) = MID$(inpLine$
, spos&
, epos&
- spos&
)ocnt& = ocnt& + 1
'--- more words/components following? ---
IF (ch%
<> q1%
) AND (ch%
<> q2%
OR nest%
= 0) THEN outArray$
(ocnt&
- 1) = outArray$
(ocnt&
- 1) + CHR$(ch%
) '--- final array size adjustment, then exit ---
plEnd:
IF ocnt&
- 1 < minUB&
THEN ocnt&
= minUB&
+ 1 ParseLine& = ocnt& - 1