Bulk file I/O with OSGBPB

From BeebWiki
Jump to: navigation, search

OSGBPB lets you read or write blocks of data from or to an open file. Using OSGBPB makes program code more compact than using BGET and BPUT, and can also can make data transfer a lot faster - particularly on an unbuffered network filing system. It can also be faster on disk as disk files are buffered to 256 bytes and this is not necessarily the number you want. The following procedure is from the FileIO library.

   ?X%=chn%:X%!1=addr%:X%!5=num%:X%!9=ptr%:CALL &FFD1:ENDPROC

A global control block needs to be set up beforehand with X% and Y% pointing to it with:

   DIM ctrl% 31:X%=ctrl%:Y%=X%DIV256

The procedure is called as

   PROCgbpb(action, handle, address, number, pointer)

The action parameter is one of the following:

  • 1 - Write bytes to file using the supplied pointer
  • 2 - Write bytes to file at the current PTR
  • 3 - Read bytes from file using the supplied pointer
  • 4 - Read bytes from file at the current PTR

You can check whether you went past the end of the file by checking X%!5 after the call. This contains the number of bytes which were not transfered. This will be non-zero if you went outside the file.

Sample code

The following code snippet will copy all the data from in% to out% using a buffer in memory at data%.

   REM max%=maximum buffer size at data%
     IF PTR#in%+len%>EXT#in% THEN len%=EXT#in%-PTR#in%
     PROCgbpb(4,in%, data%,len%,0)
   UNTIL PTR#in%>=EXT#in%

It can be made more efficient by not repeatedly reading PTR and EXT. It is faster to access variables than it is to make filing system calls, especially for values that don't change.

   REM max%=maximum buffer size at data%
     IF num%<len% THEN len%=num%
     PROCgbpb(4,in%, data%,len%,0)
   UNTIL num%=0

See Also

WikiSysop (talk) 13:17, 8 March 2015 (UTC) Jgharston (talk) 17:28, 5 November 2018 (CET)