PDA

View Full Version : CSV Parsing Module using OOP



Gary
08-09-2023, 01:05
Testing OOP in a CSV parsing Module






Uses "console"


'testing oop to create a CSV parsing module




Type ParseCsv
Private
mainstring as string
Delimiter As String
posString as long



Public
Function _Create(ByVal txt_in As String) As Long
'---------------------------------------------------
me.mainstring=txt_in
me.Delimiter=","

End Function


Function Chg_ParseCVS(ByVal txt_in As String)
'---------------------------------------------------
me.mainstring=txt_in

End Function


Function ParseInsert (index As Long ) As String
'add delimiter position inside string ParseInsert("aa,bb,cc", "," ,2) would give aa,,bb,cc



if (index > 0) then
if (index > parsecount(me.mainstring,me.delimiter) + 1) Then
function =me.mainstring
exit function
end if
index= index -1
if index = 0 then
function = me.delimiter + me.mainstring
exit function
end if

me.posString = instr(1,me.mainstring, me.delimiter,index)

if me.posString=0 Then
me.mainString = me.mainString+me.delimiter
'? me.mainString
else
me.mainString=mid$(me.mainstring,1,me.posString-1) + me.delimiter + mid$(me.mainstring,me.posString )
'? me.mainString

end if
Else
me.mainstring
'? ? me.mainString
exit Function
end if


End Function


Function ParseDelete(index AS Long) AS String
'deletes a indexed position inside string
dim Cnt AS Long
dim cntMinusDelimiter as long
Local Txt AS String
If Len(me.MainString) = 0 Then
exit function
'Function = me.MainString
Else
Cnt = instr(1,me.MainString,me.delimiter,index)
Txt = Parse$(me.MainString, me.delimiter, index)
cntMinusDelimiter=cnt-len(txt)-1
if cnt=0 then
me.MainString =mid$(me.MainString,1,cntMinusDelimiter)
else
me.MainString =mid$(me.MainString,1,cntMinusDelimiter) + mid$(me.MainString,Cnt+len(me.delimiter))
end if
End If




End Function


Function ParseReplace( ByVal iPos AS Long, ByVal sNewString AS String) AS String
'from William Burns


Local iCount AS Long
Local sTextOut AS String
If Len(me.mainstring) = 0 Then
Function = me.mainstring 'just return same string on errors
Else
For iCount = 1 To ParseCount(me.mainstring, me.delimiter)
If iCount = iPos Then 'is this the one we want to switch?
sTextOut = sTextOut + sNewString + me.delimiter
Else
sTextOut = sTextOut + Parse$(me.mainstring, me.delimiter, iCount) + me.delimiter
End If
Next iCount
me.mainstring = Left$(sTextOut,Len(sTextOut)-Len(me.delimiter)) 'remove traling delimiter
End If
End Function






function PrintOut()
'---------------------------------------------------
PrintL " mainstring :", Me.mainstring

End function


function ToVariable()as string
'---------------------------------------------------
function = Me.mainstring

End function


function parseNew(index as long) as string
function = Parse$(me.mainstring,me.delimiter,index)
end function




function parseCnt() as long
function = parsecount(me.mainstring,me.delimiter)


end function
end type


dim main as string
Dim c1 As ParseCsv("aa,bb,cc") 'CSV string (comma delimited)


c1.ParseInsert(2) 'insert delimiter into aa,bb,cc like aa,,bb,cc
c1.PrintOut()


c1.ParseReplace(2,"dfa") 'puts dfa into aa,,bb,cc like aa,dfa,bb,cc
c1.PrintOut()


c1.ParseDelete(2) 'deletes the second delimited field
c1.PrintOut()


main = c1.ToVariable() 'retrieves mainstring and puts in variable


Printl "main variable:", main


'can not use parse$ reserve word as function name
printl "ParseNew: " + c1.ParseNew(3) ' the parse$ statement


printl "ParseCnt: " + str$(c1.parseCnt()) 'the parsecount statement


c1.Chg_ParseCVS("1,2,3")
c1.PrintOut()


Console_WaitKey

ErosOlmi
08-09-2023, 16:55
Thanks a lot for this example!
Considering the very basic thinBasic OOP programming ... is a great example of OOP encapsulation

Caindaf
13-02-2024, 06:15
How effective has the CSV parsing module using OOP been in your testing, and have you encountered any specific challenges or limitations while implementing it?