The first code section is the file Recall.bi, containing the functions Recall(filename, stringarray) and Cell(row, column, string array).
Note that "normal" text files without tabs can be loaded as well with Recall().
The second code section is a usage example.
Code: Select all
' Recall.fbi, 15.10.2018, jj2007
#include "crt.bi" ' needed for memcpy
#ifndef maxCell
#Define maxCell 100 ' whatever you consider enough for a single cell
#endif
Dim shared retstr As string * maxCell+1
Function Recall(fname As String, locArray() As String) As Integer
Dim As Integer ct=0, cursize=1000 ' locArray is a local representation of a dynamic array
Dim As Long f=Freefile, sBytes
If Open(fname For Binary Access Read As #f) = 0 Then
Dim as ubyte ptr pContent = Allocate(Lof(f)), CurPos=pContent, CrPos
Dim as long flen=Lof(f)
Get #f, 1, *pContent, Lof(f)
Close #f
Do
if ct=0 or ct>cursize then
cursize+=cursize shr 1
ReDim Preserve locArray(cursize)
endif
CrPos=strstr(CurPos, Chr(10))
if CrPos=0 Then CrPos=pContent+flen:flen=0
sBytes=CrPos-CurPos-1
if sBytes>0 then
locArray(ct)=Space(sBytes)
memcpy(StrPtr(locArray(ct)), CurPos, sBytes)
endif
CurPos=CrPos+1
ct+=1 ' inc dword ptr [ebp-20]
Loop until flen=0
While ct>1 and len(trim(locArray(ct-1)))=0 ' get rid of trailing empty strings
ct-=1
Wend
ReDim Preserve locArray(ct)
DeAllocate(pContent)
Else
Print "Error opening file"
End If
Return ct
End Function
Function Cell(row As integer, col As integer, locArray() As String) As string
Dim As integer ct=0, ctTabs=0, posLeft=-1, posRight=0
Dim As ubyte ptr pString
Dim c As ubyte
pString=StrPtr(locArray(row))
if pString then
Do
c=pString[ct]
if c=0 then
if ctTabs>=col then posRight=ct+1
Exit do
endif
if c=9 then
ctTabs=ctTabs+1
if col=0 then
posLeft=0
if ctTabs>col then
posRight=ct+1
Exit do
endif
else
if posLeft=-1 and ctTabs>=col then
posLeft=ct+1
elseif ctTabs>col then
posRight=ct+1
Exit do
endif
endif
endif
ct=ct+1
Loop
endif
if posRight=0 then
retstr[0]=0
else
posRight-=posLeft
if posRight>maxCell then posRight=maxCell
memcpy(StrPtr(retstr), pString+posLeft, posRight)
retstr[posRight-1]=0
endif
return retstr
end function
Code: Select all
Dim MyArray() As string
Dim As Double t=Timer
if Recall(Command$(1), MyArray()) then
Dim as integer timeMs=(Timer-t)*1000
Print "loading";ubound(MyArray);" strings took",timeMs; " ms";
For i As Integer=0 To ubound(MyArray)-1
If i<5 Or i>=ubound(MyArray)-5 Then ' show only the first and last rows
Print i,
For j As Integer=0 To 9 ' 10 columns
Print j;"=[";Cell(i, j, MyArray());"]",
Next
print
ElseIf i=5 Then
Print " ..."
EndIf
Next
Print "-----------"
endif
Sleep