That looks possible, but I extended to remove the "tabular" or "\t" also:
Code: Select all
Sub Test_String
Print parse_string("""a b c"" ""\"""" c")(1)
Print parse_string("a b c")(2)
Print erf
ev=parse_string(" ")
Print lbound(ev),":",ubound(ev)
Print erf
ev=parse_string("a b 'c")
Print erf
If erf=True Then
Print ers
End If
End Sub
REM Function error return is defined at the global variable level.
REM This could be possible method for all the functions.
REM The user function can not stop into the error automatically or detect error line number.
REM Errors must be checked from the erf-flag. erf is operated by all commands, while
REM erc, ers and erm are filled in the function only in case erf=TRUE
REM If erc, ers and erm would be arrays, then all the errors in the program could be logged
REM into memory or a file for later inspection. Also I can not fill the "erl" and "err"
REM variables for user defined functions. That is if I have my own error message outside
REM the openoffice errors the program will not halt for that case.
REM Error handling should be available for the user defined functions also...
REM erf : Flag TRUE/FALSE (error occurred in last command or not)
REM erc : Command that produced the error
REM ers : Error name as string
REM erm : Error message as string
Global erf as Boolean
Global erc as String
Global ers as String
Global erm as String
REM The above parameters could be initialized globally, but how?
Function parse_string(stri as String) as Array
If Not (TypeName(stri)="String") Then
erc="parse_string()"
erm="Input argument can be only string type!"
ers="@wrong_argument_type"
erf=TRUE
parse_string=""
Print erm
Exit Function
End If
lst=Len(stri)
Dim ssv(30) as String
Dim ssvr() as String
i=1 : n=0
Do While i<=lst
Do While (Mid(stri,i,1)=" " Or Mid(stri,i,1)=Chr$(9))
i=i+1
If i>lst Then
Exit Do
End If
Loop
If i>lst Then
Exit Do
End If
If Mid(stri,i,1)="""" Then
f=0 : i=i+1
ssv(n)=""
Do While i<=lst
If Mid(stri,i,1)="""" Then
If Mid(stri,i-1,1)="\" Then
ssv(n)=Left(ssv(n),Len(ssv(n))-1)
Else
f=1 : i=i+1
Exit Do
End If
End If
ssv(n)=ssv(n)+Mid(stri,i,1)
i=i+1
Loop
If f=0 Then
erc="parse_string()"
erm="String has no closing quotation!"
ers="@closing_quotation_missing"
erf=TRUE
parse_string=ssvr
Print erm
Exit Function
End If
If i<=lst Then
If Not(Mid(stri,i,1)=" " Or Mid(stri,i,1)=Chr$(9)) Then
erc="parse_string()"
erm="After closing quotation at column "+Str(i)+" string separator is required!"
ers="@string_sepator_missing"
erf=TRUE
parse_string=ssvr
Print erm
Exit Function
End If
i=i+1
End If
Else
ssv(n)=Mid(stri,i,1)
i=i+1
Do While i<=lst
If Mid(stri,i,1)=" " Or Mid(stri,i,1)=Chr$(9) Then
i=i+1
Exit Do
End If
If Mid(stri,i,1)="""" Then
If Mid(stri,i-1,1)="\" Then
ssv(n)=Left(ssv(n),Len(ssv(n))-1)
Else
erc="parse_string()"
erm="Quotation character at column "+Str(i)+" not allowed in middle of string!"
ers="@wrong_quotation_placement"
erf=TRUE
parse_string=ssvr
Print erm
Exit Function
End If
End If
ssv(n)=ssv(n)+Mid(stri,i,1)
i=i+1
Loop
End If
n=n+1
Loop
REM redim does not seem to operate with negative value (to make empty string)!
If (n>0) Then
Redim preserve ssv(n-1) as String
parse_string=ssv
Else
parse_string=ssvr
End If
erf=FALSE
End Function
Here also even input arguments are defined as "String" -type the User should check that they come as "string"-type into the function...
Edit: Please use the CODE tags around program code for readability and easy copying |