Spreadsheets: Reading and accessing a two-dimensional string array

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Spreadsheets: Reading and accessing a two-dimensional string array

Post by jj2007 »

The program opens a database in tab-delimited format and reads the content into a string array.

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
Example how to use Recall.fbi (requires a tab-delimited file as commandline argument):

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
Recall.bi is optimised for speed - only MasmBasic Recall is faster (by roughly a factor 3). Use at your own risk, and please report any problems, especially with malformed files.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Spreadsheets: Reading and accessing a two-dimensional string array

Post by Tourist Trap »

Hello,

I've no special remark. I'm not with strings those days. However, wouldn't this great piece of work be better located at the tips and tricks section with the others? Otherwise I'm afraid it will be very less visible than it should in my point of view.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Spreadsheets: Reading and accessing a two-dimensional string array

Post by jj2007 »

Thanks for your nice remarks. Yes, it would be nice if an admin could move the post to the Tips & Tricks section. Happy New Year ;-)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Spreadsheets: Reading and accessing a two-dimensional string array

Post by Tourist Trap »

jj2007 wrote:Thanks for your nice remarks. Yes, it would be nice if an admin could move the post to the Tips & Tricks section. Happy New Year ;-)
Damn 2019 is already at the door! Happy new year dude :)
Post Reply