Bulk file I/O with OSGBPB

From BeebWiki
Revision as of 23:58, 28 August 2013 by WikiSysop (talk | contribs) (1 revision)
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 read 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%
   ext%=EXT#in%
   num%=ext%-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

Jgharston 11:30, 27 August 2008 (BST)