Ooops
November 25, 2020, 05:19:06 AM

Author Topic: [bb] Bank IO commands by RifRaf [ 1+ years ago ]  (Read 653 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Bank IO commands by RifRaf [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Bank IO commands
Author : RifRaf
Posted : 1+ years ago

Description : These functions will allow you to treat a bank like any other stream.. such as a file.

Code :
Code: BlitzBasic
  1. ;;//// BANK IO COMMANDS BY Jeff Frazier / Rifraf
  2.  
  3.  
  4. ;;;-------------Example---------------;;....................
  5. ;                                                          .
  6. ;                                                          .
  7. ;SeedRnd(MilliSecs())  
  8. ;f%=Bank_Openfile()  
  9. ;For i=1 To 5                                              
  10. ;       Bank_writeint(f%,Rand(1,32))
  11. ;Next                      
  12. ;bank_writestring(f%,"Hello there")
  13. ;For i=1 To 5                    
  14. ;       Bank_writefloat(f%,Rnd(1,32))    
  15. ;Next                            
  16. ;bank_writestring(f%,"Hello again")
  17. ;For i=1 To 5                    
  18. ;       Bank_writebyte(f%,Rnd(1,32))      
  19. ;Next                            
  20. ;bank_seekfile(f%,bank_filepos(f)-4)
  21. ;bank_writestring(f%,"Final hello ")
  22. ;                                
  23. ;;; write data to a file using normal file functions       .
  24. ;                                
  25. ;m$="testme.dat"
  26. ;                                
  27. ;bank_dump(f,m$)
  28. ;                                
  29. ;;; cleanup the bank            
  30. ;                                
  31. ;Bank_CLOSEFILE(f)                
  32. ;                                
  33. ;;;---------------------------------------------------------
  34. ;F=OpenFile("TESTME.DAT")
  35. ;For I=1 To 5
  36. ;       DebugLog ReadInt(f)
  37. ;       Next
  38. ;DebugLog ReadString$(f)
  39. ;For I=1 To 5
  40. ;       DebugLog ReadFloat(f)
  41. ;       Next
  42. ;DebugLog ReadString$(f)
  43. ;For I=1 To 1
  44. ;       DebugLog ReadByte(f)
  45. ;       Next
  46. ;DebugLog ReadString$(f)
  47. ;While Not Eof(f)
  48. ;DebugLog ReadByte(f)
  49. ;Wend
  50. ;CloseFile f
  51. ;WaitKey
  52. ;End
  53.  
  54. Type Bankoffset
  55.  Field Name$
  56.  Field offset
  57.  Field bankid
  58. End Type
  59.  
  60. Function BANK_OPENFILE(name$="Bank",sizeoverride=0)
  61.  bo.bankoffset=New bankoffset
  62.  If sizeoverride<>0 Then
  63.          boankid=CreateBank(size)
  64.  Else
  65.          boankid=CreateBank(1)
  66.  EndIf
  67.  bo
  68. ame$=name$
  69.  Return Handle(bo)
  70.  
  71. End Function
  72.  
  73.  
  74. Function BANK_OPENFILE_exists(originalbank,name$="banker")
  75.  bo.bankoffset=New bankoffset
  76.  boankid=originalbank
  77.  bo
  78. ame$=name$
  79.  Return Handle(bo)
  80. End Function
  81.  
  82. Function BANK_CLOSEFILE(bid%)
  83. Thisbank.bankoffset=Object.bankoffset(bid%)
  84. FreeBank THISBANKBANKID
  85. Delete thisbank
  86. End Function
  87.  
  88. Function Bank_EOF(bid%)
  89. Thisbank.bankoffset=Object.bankoffset(bid%)
  90. If thisbankoffset=>BankSize(bid%) Then Return True
  91. Return False
  92. End Function  
  93.  
  94. Function Bank_SeekFile(bid%,offset)
  95. Thisbank.bankoffset=Object.bankoffset(bid%)
  96. thisbankoffset=offset
  97. End Function
  98.  
  99. Function Bank_FILEPOS(bid%)
  100. Thisbank.bankoffset=Object.bankoffset(bid%)
  101. Return thisbankoffset
  102. End Function
  103.  
  104. Function Bank_FILELEN(bid%)
  105. Return BankSize(bid%)
  106. End Function
  107. ;;;;;;;;;;;;;;;;;;;WRITE FUNCTIONS;;;;;;;;;;;;;;;;;;;;;;;;
  108.  
  109. Function Bank_Writeint(bid%,intdata%)
  110. Thisbank.bankoffset=Object.bankoffset(bid%)
  111. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteInt"
  112. If thisbankoffset+4>(BankSize(thisbankankid)) Then
  113.    neededsize=4-((BankSize(thisbankankid))-thisbankoffset)
  114.    Bankresize(thisbank.bankoffset,neededsize)
  115. EndIf
  116. PokeInt(thisbankankid,thisbankoffset,intdata)
  117. thisbankoffset=thisbankoffset+4
  118. End Function
  119.  
  120.  
  121. Function Bank_WriteFloat(bid%,floatData#)
  122. Thisbank.bankoffset=Object.bankoffset(bid%)
  123. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteFloat"
  124. If thisbankoffset+4>(BankSize(thisbankankid)) Then
  125.    neededsize=4-((BankSize(thisbankankid))-thisbankoffset)
  126.    Bankresize(thisbank.bankoffset,neededsize)
  127. EndIf
  128. PokeFloat(thisbankankid,thisbankoffset,floatData#)
  129. thisbankoffset=thisbankoffset+4
  130. End Function
  131.  
  132. Function Bank_WriteBytes(bid%,bytebank)
  133. Local s=BankSize (bytebank)
  134. Local i
  135. For i=1 To s
  136.  bank_writebyte(bid%,PeekFloat(bytebank,i))
  137. Next
  138. End Function
  139.  
  140.  
  141. Function Bank_WriteByte(bid%,bytedata)
  142. Thisbank.bankoffset=Object.bankoffset(bid%)
  143. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteByte"
  144.  
  145. If thisbankoffset+1>(BankSize(thisbankankid)) Then
  146.    neededsize=1-((BankSize(thisbankankid))-thisbankoffset)
  147.    Bankresize(thisbank.bankoffset,neededsize)
  148. EndIf
  149.  
  150. PokeByte(thisbankankid,thisbankoffset,byteData)
  151. thisbankoffset=thisbankoffset+1
  152. End Function
  153.  
  154. Function Bank_WriteString(bid%,stringdata$)
  155. Thisbank.bankoffset=Object.bankoffset(bid%)
  156. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteString"
  157. Length=Len(Stringdata$)
  158. Bank_Writeint(bid,length)
  159. For I=1 To Length
  160.         bank_Writebyte(bid,Asc(Mid$(stringdata$,I,1)))
  161. Next
  162. End Function
  163.  
  164. Function Bank_WriteLine(bid%,stringdata$)
  165. stringdata$=stringdata$+Chr$(13)+Chr$(10)
  166. Thisbank.bankoffset=Object.bankoffset(bid%)
  167. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteString"
  168. Length=Len(Stringdata$)
  169. ;Bank_Writeint(thisbankankid,length)
  170. For I=1 To Length
  171.         bank_Writebyte(bid,Asc(Mid$(stringdata$,I,1)))
  172. Next
  173. End Function
  174.  
  175.  
  176.  
  177. ;;;;;;;;;;;;;;;;;;;READ FUNCTIONS;;;;;;;;;;;;;;;;;;;;;;;;
  178.  
  179. Function Bank_ReadInt(bid%)
  180. Local THISV
  181. Thisbank.bankoffset=Object.bankoffset(bid%)
  182. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_ReadInt"
  183. If thisbankoffset+4>(BankSize(thisbankankid)) Then RuntimeError "Bank io read, past eof: Bank_ReadInt"
  184. THISV = PeekInt(thisbankankid,thisbankoffset)
  185. thisbankoffset=thisbankoffset+4
  186. Return THISV
  187. End Function
  188.  
  189. Function Bank_ReadFloat#(bid%)
  190. Local THISV#
  191. Thisbank.bankoffset=Object.bankoffset(bid%)
  192. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_Readfloat"
  193. If thisbankoffset+4>(BankSize(thisbankankid)) Then  RuntimeError "Bank io read, past eof: Bank_ReadFloat"
  194. THISV#= PeekFloat(thisbankankid,thisbankoffset)
  195. thisbankoffset=thisbankoffset+4
  196. Return THISV#
  197. End Function
  198.  
  199. ;ank
  200. Function Bank_ReadByte(bid%)
  201. Local THISV
  202. Thisbank.bankoffset=Object.bankoffset(bid%)
  203. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_ReadByte"
  204. If thisbankoffset+1>(BankSize(thisbankankid)) Then  RuntimeError "Bank io read, past eof: Bank_ReadByte"
  205. THISV= PeekByte(thisbankankid,thisbankoffset)
  206. thisbankoffset=thisbankoffset+1
  207. Return THISV
  208. End Function
  209.  
  210. Function Bank_ReadString$(bid%)
  211. Local StringV$=""
  212. Local THISV
  213. THISV=Bank_ReadInt(bid%)
  214. For I=1 To thisv
  215.         Stringv$=stringv$+Chr$(bank_readbyte(bid%))
  216. Next
  217. Return Stringv$
  218. End Function
  219.  
  220. Function Bank_ReadLine$(bid%)
  221. Local StringV$=""
  222. Local THISV
  223. While Not Bank_EOF(bid%)
  224.     Temp$=Chr$(bank_readbyte(bid%))
  225.          If temp$=Chr$(13)  Then
  226.              bank_readbyte(bid%)
  227.          Return stringv$
  228.     EndIf
  229.         Stringv$=stringv$+temp$
  230. Wend
  231. Return Stringv$
  232. End Function
  233.  
  234.  
  235.  
  236. ;;;;;;;;;;;;;;;;;;;MISC;;;;;;;;;;;;;;;;;;;;;;;;
  237. Function Findbank.bankoffset(b_id)
  238. For bo.bankoffset=Each bankoffset
  239.  If boankid=b_id Then Return bo.bankoffset
  240. Next
  241. Return Null
  242. End Function
  243.  
  244. Function Findbankname.bankoffset(name$)
  245. For bo.bankoffset=Each bankoffset
  246.  If bo
  247. ame$=name$ Then Return bo.bankoffset
  248. Next
  249. Return Null
  250. End Function
  251.  
  252. ; BankSize, ResizeBank, CopyBank Example
  253. Function BankResize(bo.bankoffset,size)
  254.         This=BankSize(boankid)
  255.         ResizeBank boankid,(this+size)
  256. End Function
  257.  
  258. Function BANK_DUMP(b_id,fn$)
  259. Thisbank.bankoffset=Object.bankoffset(b_id)
  260. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_Dump"
  261. sz=BankSize(thisbankankid)
  262. f=WriteFile(fn$)
  263. WriteBytes (thisbankankid,f,0,sz)
  264. CloseFile f
  265. End Function
  266.  
  267. Function BANK_DUMPbyID(b_id,fn$)
  268. Thisbank.bankoffset=FINDBANK(b_id)
  269. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_Dump"
  270. sz=BankSize(thisbankankid)
  271. f=WriteFile(fn$)
  272. WriteBytes (thisbankankid,f,0,sz)
  273. CloseFile f
  274. End Function
  275.  
  276.  
  277. Function BANK_DUMPbyName(name$,fn$)
  278. Thisbank.bankoffset=Findbankname(name$)
  279. If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_Dump"
  280. sz=BankSize(thisbankankid)
  281. f=WriteFile(fn$)
  282. WriteBytes (thisbankankid,f,0,sz)
  283. CloseFile f
  284. End Function


Comments :


RifRaf(Posted 1+ years ago)

 Updated.. using object() and Handle() now instead of manually finding objects
Code: [Select]
;;//// BANK IO COMMANDS BY Jeff Frazier / Rifraf


;;;-------------Example---------------;;....................
;                                                          .
;                                                          .
;SeedRnd(MilliSecs())  
;f%=Bank_Openfile()  
;For i=1 To 5                                              
; Bank_writeint(f%,Rand(1,32))
;Next                      
;bank_writestring(f%,"Hello there")
;For i=1 To 5                    
; Bank_writefloat(f%,Rnd(1,32))    
;Next                            
;bank_writestring(f%,"Hello again")
;For i=1 To 5                    
; Bank_writebyte(f%,Rnd(1,32))      
;Next                            
;bank_seekfile(f%,bank_filepos(f)-4)
;bank_writestring(f%,"Final hello ")
;                                
;;; write data to a file using normal file functions       .
;                                
;m$="testme.dat"
;                                
;bank_dump(f,m$)
;                                
;;; cleanup the bank            
;                                
;Bank_CLOSEFILE(f)                
;                                
;;;---------------------------------------------------------
;F=OpenFile("TESTME.DAT")
;For I=1 To 5
; DebugLog ReadInt(f)
; Next
;DebugLog ReadString$(f)
;For I=1 To 5
; DebugLog ReadFloat(f)
; Next
;DebugLog ReadString$(f)
;For I=1 To 1
; DebugLog ReadByte(f)
; Next
;DebugLog ReadString$(f)
;While Not Eof(f)
;DebugLog ReadByte(f)
;Wend
;CloseFile f
;WaitKey
;End

Type Bankoffset
 Field Name$
 Field offset
 Field bankid
End Type

Function BANK_OPENFILE(name$="Bank",sizeoverride=0)
 bo.bankoffset=New bankoffset
 If sizeoverride<>0 Then
boankid=CreateBank(size)
 Else
boankid=CreateBank(1)
 EndIf
 bo
ame$=name$
 Return Handle(bo)
 
End Function


Function BANK_OPENFILE_exists(originalbank,name$="banker")
 bo.bankoffset=New bankoffset
 boankid=originalbank
 bo
ame$=name$
 Return Handle(bo)
End Function

Function BANK_CLOSEFILE(bid%)
Thisbank.bankoffset=Object.bankoffset(bid%)
FreeBank THISBANKBANKID
Delete thisbank
End Function

Function Bank_EOF(bid%)
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbankoffset=>BankSize(bid%) Then Return True
Return False
End Function  

Function Bank_SeekFile(bid%,offset)
Thisbank.bankoffset=Object.bankoffset(bid%)
thisbankoffset=offset
End Function

Function Bank_FILEPOS(bid%)
Thisbank.bankoffset=Object.bankoffset(bid%)
Return thisbankoffset
End Function

Function Bank_FILELEN(bid%)
Return BankSize(bid%)
End Function
;;;;;;;;;;;;;;;;;;;WRITE FUNCTIONS;;;;;;;;;;;;;;;;;;;;;;;;

Function Bank_Writeint(bid%,intdata%)
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteInt"
If thisbankoffset+4>(BankSize(thisbankankid)) Then
   neededsize=4-((BankSize(thisbankankid))-thisbankoffset)
   Bankresize(thisbank.bankoffset,neededsize)
EndIf
PokeInt(thisbankankid,thisbankoffset,intdata)
thisbankoffset=thisbankoffset+4
End Function


Function Bank_WriteFloat(bid%,floatData#)
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteFloat"
If thisbankoffset+4>(BankSize(thisbankankid)) Then
   neededsize=4-((BankSize(thisbankankid))-thisbankoffset)
   Bankresize(thisbank.bankoffset,neededsize)
EndIf
PokeFloat(thisbankankid,thisbankoffset,floatData#)
thisbankoffset=thisbankoffset+4
End Function

Function Bank_WriteBytes(bid%,bytebank)
Local s=BankSize (bytebank)
Local i
For i=1 To s
 bank_writebyte(bid%,PeekFloat(bytebank,i))
Next
End Function


Function Bank_WriteByte(bid%,bytedata)
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteByte"

If thisbankoffset+1>(BankSize(thisbankankid)) Then
   neededsize=1-((BankSize(thisbankankid))-thisbankoffset)
   Bankresize(thisbank.bankoffset,neededsize)
EndIf

PokeByte(thisbankankid,thisbankoffset,byteData)
thisbankoffset=thisbankoffset+1
End Function

Function Bank_WriteString(bid%,stringdata$)
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteString"
Length=Len(Stringdata$)
Bank_Writeint(bid,length)
For I=1 To Length
bank_Writebyte(bid,Asc(Mid$(stringdata$,I,1)))
Next
End Function

Function Bank_WriteLine(bid%,stringdata$)
stringdata$=stringdata$+Chr$(13)+Chr$(10)
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_WriteString"
Length=Len(Stringdata$)
;Bank_Writeint(thisbankankid,length)
For I=1 To Length
bank_Writebyte(bid,Asc(Mid$(stringdata$,I,1)))
Next
End Function



;;;;;;;;;;;;;;;;;;;READ FUNCTIONS;;;;;;;;;;;;;;;;;;;;;;;;

Function Bank_ReadInt(bid%)
Local THISV
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_ReadInt"
If thisbankoffset+4>(BankSize(thisbankankid)) Then RuntimeError "Bank io read, past eof: Bank_ReadInt"
THISV = PeekInt(thisbankankid,thisbankoffset)
thisbankoffset=thisbankoffset+4
Return THISV
End Function

Function Bank_ReadFloat#(bid%)
Local THISV#
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_Readfloat"
If thisbankoffset+4>(BankSize(thisbankankid)) Then  RuntimeError "Bank io read, past eof: Bank_ReadFloat"
THISV#= PeekFloat(thisbankankid,thisbankoffset)
thisbankoffset=thisbankoffset+4
Return THISV#
End Function

;ank
Function Bank_ReadByte(bid%)
Local THISV
Thisbank.bankoffset=Object.bankoffset(bid%)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_ReadByte"
If thisbankoffset+1>(BankSize(thisbankankid)) Then  RuntimeError "Bank io read, past eof: Bank_ReadByte"
THISV= PeekByte(thisbankankid,thisbankoffset)
thisbankoffset=thisbankoffset+1
Return THISV
End Function

Function Bank_ReadString$(bid%)
Local StringV$=""
Local THISV
THISV=Bank_ReadInt(bid%)
For I=1 To thisv
Stringv$=stringv$+Chr$(bank_readbyte(bid%))
Next
Return Stringv$
End Function

Function Bank_ReadLine$(bid%)
Local StringV$=""
Local THISV
While Not Bank_EOF(bid%)
    Temp$=Chr$(bank_readbyte(bid%))
  If temp$=Chr$(13)  Then
    bank_readbyte(bid%)
    Return stringv$
    EndIf
Stringv$=stringv$+temp$
Wend
Return Stringv$
End Function



;;;;;;;;;;;;;;;;;;;MISC;;;;;;;;;;;;;;;;;;;;;;;;
Function Findbank.bankoffset(b_id)
For bo.bankoffset=Each bankoffset
 If boankid=b_id Then Return bo.bankoffset
Next
Return Null
End Function

Function Findbankname.bankoffset(name$)
For bo.bankoffset=Each bankoffset
 If bo
ame$=name$ Then Return bo.bankoffset
Next
Return Null
End Function

; BankSize, ResizeBank, CopyBank Example
Function BankResize(bo.bankoffset,size)
This=BankSize(boankid)
ResizeBank boankid,(this+size)
End Function

Function BANK_DUMP(b_id,fn$)
Thisbank.bankoffset=Object.bankoffset(b_id)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_Dump"
sz=BankSize(thisbankankid)
f=WriteFile(fn$)
WriteBytes (thisbankankid,f,0,sz)
CloseFile f
End Function

Function BANK_DUMPbyID(b_id,fn$)
Thisbank.bankoffset=FINDBANK(b_id)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_Dump"
sz=BankSize(thisbankankid)
f=WriteFile(fn$)
WriteBytes (thisbankankid,f,0,sz)
CloseFile f
End Function


Function BANK_DUMPbyName(name$,fn$)
Thisbank.bankoffset=Findbankname(name$)
If thisbank.bankoffset=Null Then RuntimeError "I/O bank does not exist : Bank_Dump"
sz=BankSize(thisbankankid)
f=WriteFile(fn$)
WriteBytes (thisbankankid,f,0,sz)
CloseFile f
End Function

Updated.. using object() and Handle() now instead of manually finding objects


Yasha(Posted 1+ years ago)

 This is a nice library. Good stuff.However, I find myself wondering... why do the functions all use integer IDs at all? Wouldn't it just be faster to pass the This pointer directly?


RifRaf(Posted 1+ years ago)

 probably would be faster, I just tossed the handle / object in this morning and reposted..  ill edit it again laterI like the integer id though, my simple brain likes to see smaller IDs [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal