Gary
07-10-2023, 20:43
OOP code that extends OOP module
parseCSVFile extends parseCSV
Uses "console"
uses "File"
'Extends CSV parsing module with file handling'
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( IndxStart As Long, NewFields As String) As String
dim separator as string, textline as string
separator = me.Delimiter
textline = me.mainstring
Local Pos1 As Long, Pos2 As Long, I As Long, J As Long
dim astrings as string,allstr as string
j = ParseCount(TextLine, Separator)
For I = 1 To ParseCount(TextLine, Separator)
astrings=parse$(me.mainstring,me.Delimiter,i)
if indxstart = i then
astrings = newFields
end if
if j = i then
allstr += astrings
else
allstr += astrings + ","
end if
Next
me.mainstring = allstr
'function =allstr
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
'=================================parseCSVFile=======================================
type parseCSVFile extends parseCSV
private
mainfile as string
fileline as string
fileDelimiter as string
public
allLinesfile as string
Function _Create(ByVal file_in As String) As Long
'---------------------------------------------------
me.mainfile=file_in
'me.Delimiter=","
me.fileDelimiter = $crlf
End Function
Function File2Text()
Dim st As String
Dim n As Long
Dim FileHandle As DWORD,s as string
FileHandle= FILE_OPEN(me.mainfile, "INPUT")
while not FILE_EOF(FileHandle)
s=FILE_LineInput(FileHandle)
me.allLinesfile=me.allLinesfile+s+$crlf
wend
s = FILE_Close(FileHandle)
'Function=st
End Function
function parseCntFile() as long
function = ParseCount(me.allLinesfile,me.fileDelimiter)
end function
function Text2File(sfn As String)
Dim FileHandle As DWORD,s as string
FileHandle= FILE_OPEN(sfn, "OUTPUT")
s = FILE_LinePrint(FileHandle, me.allLinesfile)
s = FILE_Close(FileHandle)
end function
end type
'=====================================================================
dim main as string
dim i as long
'change the file to directory
Dim c1 As parseCSVFile("C:\thinBasic\SampleScripts\csvBills2023.txt") 'CSV string (comma delimited)
'puts all lines in file in allLinesfile
c1.File2Text()
dim a as string,allLines as string
'ParseCntFile counts lines in file
for i=1 to c1.ParseCntFile()
c1.mainstring= parse$(c1.allLinesfile,$crlf,i)
a = c1.parseNew(3)
if a<> "" and instr(a,"$") =0 then
a=trim$(a)
c1.parseReplace(3,"$" + a)
sleep 500
printl c1.mainstring
end if
'c1.allLinesfile = ""
allLines += c1.mainstring + $crlf
next
c1.allLinesfile = alllines
c1.Text2File("C:\thinBasic\SampleScripts\temp.txt")
printl("")
Printl("======All file lines=======================")
printl(c1.allLinesFile)
Console_WaitKey
Text file for testing
Save text below as file for the above code:
01 , CC , $34.42 , 408892
01 , CB , $110.22 , 108287803
01 , DK , 405.72 , 16552914011823
01 , MRC , 386.47 , 894264795
01 , GECU , 210.78 , 056960456
01 , SD1 , $45.91 , 2829157706
01 , PNC , $59.79 , 569805
parseCSVFile extends parseCSV
Uses "console"
uses "File"
'Extends CSV parsing module with file handling'
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( IndxStart As Long, NewFields As String) As String
dim separator as string, textline as string
separator = me.Delimiter
textline = me.mainstring
Local Pos1 As Long, Pos2 As Long, I As Long, J As Long
dim astrings as string,allstr as string
j = ParseCount(TextLine, Separator)
For I = 1 To ParseCount(TextLine, Separator)
astrings=parse$(me.mainstring,me.Delimiter,i)
if indxstart = i then
astrings = newFields
end if
if j = i then
allstr += astrings
else
allstr += astrings + ","
end if
Next
me.mainstring = allstr
'function =allstr
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
'=================================parseCSVFile=======================================
type parseCSVFile extends parseCSV
private
mainfile as string
fileline as string
fileDelimiter as string
public
allLinesfile as string
Function _Create(ByVal file_in As String) As Long
'---------------------------------------------------
me.mainfile=file_in
'me.Delimiter=","
me.fileDelimiter = $crlf
End Function
Function File2Text()
Dim st As String
Dim n As Long
Dim FileHandle As DWORD,s as string
FileHandle= FILE_OPEN(me.mainfile, "INPUT")
while not FILE_EOF(FileHandle)
s=FILE_LineInput(FileHandle)
me.allLinesfile=me.allLinesfile+s+$crlf
wend
s = FILE_Close(FileHandle)
'Function=st
End Function
function parseCntFile() as long
function = ParseCount(me.allLinesfile,me.fileDelimiter)
end function
function Text2File(sfn As String)
Dim FileHandle As DWORD,s as string
FileHandle= FILE_OPEN(sfn, "OUTPUT")
s = FILE_LinePrint(FileHandle, me.allLinesfile)
s = FILE_Close(FileHandle)
end function
end type
'=====================================================================
dim main as string
dim i as long
'change the file to directory
Dim c1 As parseCSVFile("C:\thinBasic\SampleScripts\csvBills2023.txt") 'CSV string (comma delimited)
'puts all lines in file in allLinesfile
c1.File2Text()
dim a as string,allLines as string
'ParseCntFile counts lines in file
for i=1 to c1.ParseCntFile()
c1.mainstring= parse$(c1.allLinesfile,$crlf,i)
a = c1.parseNew(3)
if a<> "" and instr(a,"$") =0 then
a=trim$(a)
c1.parseReplace(3,"$" + a)
sleep 500
printl c1.mainstring
end if
'c1.allLinesfile = ""
allLines += c1.mainstring + $crlf
next
c1.allLinesfile = alllines
c1.Text2File("C:\thinBasic\SampleScripts\temp.txt")
printl("")
Printl("======All file lines=======================")
printl(c1.allLinesFile)
Console_WaitKey
Text file for testing
Save text below as file for the above code:
01 , CC , $34.42 , 408892
01 , CB , $110.22 , 108287803
01 , DK , 405.72 , 16552914011823
01 , MRC , 386.47 , 894264795
01 , GECU , 210.78 , 056960456
01 , SD1 , $45.91 , 2829157706
01 , PNC , $59.79 , 569805