LArray, SArray, StringPointer (Update)
Verfasst: 29.08.2007 00:45
Hier eine kleine Hilfe für alle, die dynamisch Array erstellen wollen. Ich
denke es ist vieles selbsterklärend.
LArray.pbi (inkl. Beispiel)
SArray.pbi (Analog zu LArray.pbi)
StringPointer.pbi (wird von SArray.pbi genutzt, inkl. Beispiel)
denke es ist vieles selbsterklärend.
LArray.pbi (inkl. Beispiel)
Code: Alles auswählen
Structure LArray
*arr
c.l
EndStructure
Procedure LA_Add(*LArray.LArray, Long.l, Elements.l = 1) ;Fügt einen Long-Wert ans Ende hinzu
Protected *tmp.Long
*tmp = ReAllocateMemory(*LArray\arr, (*LArray\c + Elements) * SizeOf(Long))
If *tmp
*LArray\arr = *tmp
*tmp + *LArray\c * SizeOf(Long)
*tmp\l = Long
*LArray\c + Elements
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure LA_Del(*LArray.LArray, pos.l) ;Löscht einen Long-Wert
Protected *tmp
If pos >= 0 And pos < *LArray\c
*tmp = *LArray\arr + pos * SizeOf(Long)
MoveMemory(*tmp + SizeOf(Long), *tmp, (*LArray\c - pos - 1) * SizeOf(Long))
*LArray\c - 1
If *LArray\c
*LArray\arr = ReAllocateMemory(*LArray\arr, *LArray\c * SizeOf(Long))
Else
FreeMemory(*LArray\arr)
*LArray\arr = 0
EndIf
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure LA_Set(*LArray.LArray, pos.l, Long.l) ;Setzt einen Long-Wert
Protected *tmp.Long
If pos >= 0 And pos < *LArray\c
*tmp = *LArray\arr + pos * SizeOf(Long)
*tmp\l = Long
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure.l LA_Get(*LArray.LArray, pos.l) ;Gibt einen Long-Wert zurück
Protected *tmp.Long
If pos >= 0 And pos < *LArray\c
*tmp = *LArray\arr + pos * SizeOf(Long)
ProcedureReturn *tmp\l
EndIf
ProcedureReturn 0
EndProcedure
Procedure LA_Move(*LArray.LArray, pos.l, newpos.l) ;Verschiebt ein Long an eine andere Position
Protected Long.l, *tmp.Long
If pos = newpos : ProcedureReturn #False : EndIf
If pos >= 0 And newpos >= 0 And pos < *LArray\c And newpos < *LArray\c
*tmp = *LArray\arr + pos * SizeOf(Long)
Long = *tmp\l
If pos < newpos
MoveMemory(*tmp + SizeOf(Long), *tmp, (newpos - pos) * SizeOf(Long))
*tmp = *LArray\arr + newpos * SizeOf(Long)
*tmp\l = Long
Else
*tmp = *LArray\arr + newpos * SizeOf(Long)
MoveMemory(*tmp, *tmp + SizeOf(Long), (pos - newpos) * SizeOf(Long))
*tmp\l = Long
EndIf
EndIf
EndProcedure
Procedure LA_Clear(*LArray.LArray) ;Löscht alle Longs im Array
Protected a.l
If *LArray\arr : FreeMemory(*LArray\arr) : EndIf
*LArray\arr = 0
*LArray\c = 0
EndProcedure
Structure Arrays
a.LArray
b.LArray
EndStructure
Define var.Arrays
For i = 1 To 10
LA_Add(var\a, i)
Next
For i = 1 To 4
LA_Add(var\b, 100 + i)
Next
Debug "Array a"
For i = 0 To var\a\c - 1
Debug LA_Get(var\a, i)
Next
Debug "Array b"
For i = 0 To var\b\c - 1
Debug LA_Get(var\b, i)
Next
Code: Alles auswählen
XIncludeFile "StringPointer.pbi"
Structure SArray
*arr
c.l
EndStructure
Procedure SA_Add(*SArray.SArray, String.s, Elements.l = 1)
Protected *tmp
*tmp = ReAllocateMemory(*SArray\arr, (*SArray\c + Elements) * SizeOf(*tmp))
If *tmp
*SArray\arr = *tmp
*tmp + *SArray\c * SizeOf(*tmp)
SetString(*tmp, String)
*SArray\c + Elements
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure SA_Del(*SArray.SArray, pos.l)
Protected *tmp
If pos >= 0 And pos < *SArray\c
*tmp = *SArray\arr + pos * SizeOf(*tmp)
SetString(*tmp)
MoveMemory(*tmp + SizeOf(*tmp), *tmp, (*SArray\c - pos - 1) * SizeOf(*tmp))
*SArray\c - 1
If *SArray\c
*SArray\arr = ReAllocateMemory(*SArray\arr, *SArray\c * SizeOf(*tmp))
Else
FreeMemory(*SArray\arr)
*SArray\arr = 0
EndIf
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure SA_Set(*SArray.SArray, pos.l, String.s)
Protected *tmp
If pos >= 0 And pos < *SArray\c
*tmp = *SArray\arr + pos * SizeOf(*tmp)
SetString(*tmp, String)
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure.s SA_Get(*SArray.SArray, pos.l)
Protected *tmp
If pos >= 0 And pos < *SArray\c
*tmp = *SArray\arr + pos * SizeOf(*tmp)
ProcedureReturn GetString(*tmp)
EndIf
ProcedureReturn ""
EndProcedure
Procedure SA_Move(*SArray.SArray, pos.l, newpos.l)
Protected *String, *tmp.SArray
If pos = newpos : ProcedureReturn #False : EndIf
If pos >= 0 And newpos >= 0 And pos < *SArray\c And newpos < *SArray\c
*tmp = *SArray\arr + pos * SizeOf(Long)
*String = *tmp\arr
If pos < newpos
MoveMemory(*tmp + SizeOf(*String), *tmp, (newpos - pos) * SizeOf(*String))
*tmp = *SArray\arr + newpos * SizeOf(*String)
*tmp\arr = *String
Else
*tmp = *SArray\arr + newpos * SizeOf(*String)
MoveMemory(*tmp, *tmp + SizeOf(*String), (pos - newpos) * SizeOf(*String))
*tmp\arr = *String
EndIf
EndIf
EndProcedure
Procedure SA_Clear(*SArray.SArray)
Protected a.l, *tmp = *SArray\arr
For a = 1 To *SArray\c
SetString(*tmp)
*tmp + SizeOf(String)
Next
If *SArray\arr : FreeMemory(*SArray\arr) : EndIf
*SArray\arr = 0
*SArray\c = 0
EndProcedure
Code: Alles auswählen
;Info: SetString, GetString, StringLength
Procedure SetString(*pPtr.Long, String.s = "") ;Setzt den String. "" löscht den String
Protected Size.l = Len(String)
If Size = 0
If *pPtr\l
FreeMemory(*pPtr\l)
*pPtr\l = 0
ProcedureReturn #True
Else
ProcedureReturn #True
EndIf
EndIf
*pPtr\l = ReAllocateMemory(*pPtr\l, Size + SizeOf(Long) + 1)
If *pPtr\l
PokeL(*pPtr\l, Size)
PokeS(*pPtr\l + SizeOf(Long), String)
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure.s GetString(*pPtr.Long) ;Gibt den String zurück
If *pPtr\l
ProcedureReturn PeekS(*pPtr\l + SizeOf(Long), PeekL(*pPtr\l))
EndIf
ProcedureReturn ""
EndProcedure
Procedure.l StringLength(*pPtr.Long) ;Gibt die Stringlänge zurück
If *pPtr\l
ProcedureReturn PeekL(*pPtr\l)
EndIf
ProcedureReturn 0
EndProcedure
Define *String
SetString(@*String, "Hallo")
Debug StringLength(@*String)
Debug GetString(@*String)
SetString(@*String)
Debug StringLength(@*String)
Debug GetString(@*String)