fb:porticula NoPaste
Function Text File to Array very fast
Uploader: | marpon |
Datum/Zeit: | 23.01.2013 10:37:39 |
Hinweis: Dieser Quelltext ist Bestandteil des Projekts CSED_FB multi-language Windows IDE for FreeBasic, zu dem es auf FreeBASIC-Portal.de eine Projektseite gibt.
'Function Text File to Array very fast
'
'
'
'Copy text file content in array , 1 element by text line ( separator CrLf )
'
'Dim File_Nom As String ' File name
'Dim RET()As String ' Array base(0)
'
'Dim Nb as Integer ' Nb of elements
' the array will have nb + 1 elements because
' RET(0) for the number of elements/lines (as string)
'
'Usage :
'
' Nb = Fil_Tabl_Str ( File_Nom , RET() )
'
'
Function Fil_Tabl_Str( File_Nom As String, RET() As String) As Integer
'' note that this function assignes the memory for the array to the first pointer
'' so freeing this pointer frees the full array.
Dim As Integer DMAX=0
Dim RES() As ZString Ptr
Dim As Integer I1 , fini
Dim As ZString Ptr p , p1 , p2, p3
Dim As Integer LDelimit = 2
Dim As Integer result
Dim As Integer Posi()
Dim As String DELIMIT= Chr$(13) & Chr$(10)
Dim fileData As UByte Ptr
Dim As Integer fileSize
Dim As Integer myHandle
Dim As String Cont, TEXT
myHandle = Freefile()
result = Open (File_Nom For Binary As #myHandle )
If result <> 0 Then
ReDim RET(1)
RET(0) = "0"
Function= 0
Exit Function
EndIf
fileSize = LOF(myHandle)
If fileSize=0 Then
ReDim RET(1)
RET(0) = "0"
Function= 0
Close #myHandle
Exit Function
EndIf
fileData = Allocate(fileSize)
Get #myHandle, 0, *fileData, fileSize
Close #myHandle
Cont= *fileData
'Deallocate(fileData)
' count the delimiters
p = fileData
p1=p
Do While *p
If *p = DELIMIT[0] Then
p3=p
p+=1
If *p = DELIMIT[1] Then
If p= fileData +fileSize-1 Then fini=1 ' fini avec le délimiter
DMAX+=1
ReDim Preserve Posi(0 To DMAX-1)
Posi(DMAX-1)=p3 - p1 +1
EndIf
EndIf
p+=1
Loop
If DMAX=0 Then ' aucun délimiter trouvé
DMAX=1
ReDim RET(2)
RET(0) = "1"
RET(1) =Left$(Cont,fileSize) ' copy the full text
Return DMAX
Exit Function
EndIf
' dimention the array and assign memory to first element
If fini=0 Then DMAX+=1
ReDim RET(0 To DMAX)
ReDim RES(0 To DMAX-1)
RES(0) = fileData
'*RES(0) = TEXT ' copy the full text
' step through the string, setting pointers for each element and null terminating
p = RES(0)
For I1 = 0 To DMAX-2
p2= p + Posi(I1) -1
*p2 = 0 ' null terminate each element
RES(I1+1) = p2 + LDelimit ' set pointer to next element
RET(I1+1)=*RES(I1)
Next
p2= p+Posi(DMAX-1) -1
*p2 = 0
RET(DMAX)=*RES(DMAX-1)
RET(0)= Str$(DMAX)
'Deallocate RES(0)
Deallocate(fileData)
Return DMAX
End Function