Bulk file I/O with OSGBPB

From BeebWiki
Revision as of 17:28, 5 November 2018 by Jgharston (talk | contribs) (typo)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

   DEFPROCgbpb(A%,chn%,addr%,num%,ptr%)
   ?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%
   REPEAT
     len%=max%
     IF PTR#in%+len%>EXT#in% THEN len%=EXT#in%-PTR#in%
     PROCgbpb(4,in%, data%,len%,0)
     PROCgbpb(2,out%,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%
   num%=EXT#in%-PTR#in%
   REPEAT
     len%=max%
     IF num%<len% THEN len%=num%
     PROCgbpb(4,in%, data%,len%,0)
     PROCgbpb(2,out%,data%,len%,0)
     num%=num%-len%
   UNTIL num%=0

See Also

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