Difference between revisions of "Bulk file I/O with OSGBPB"

From BeebWiki
Jump to: navigation, search
m (Fixed broken formatting.)
 
m (typo)
 
(5 intermediate revisions by one other user not shown)
Line 44: Line 44:
 
     UNTIL PTR#in%>=EXT#in%
 
     UNTIL PTR#in%>=EXT#in%
  
It can be made more efficient by not repeatedly read <code>'''PTR'''</code> and
+
It can be made more efficient by not repeatedly reading <code>'''PTR'''</code>
<code>'''EXT</code>'''. It is faster to access variables than it is to make
+
and <code>'''EXT</code>'''. It is faster to access variables than it is to make
 
filing system calls, especially for values that don't change.
 
filing system calls, especially for values that don't change.
  
 
     REM max%=maximum buffer size at data%
 
     REM max%=maximum buffer size at data%
     ext%=EXT#in%
+
     num%=EXT#in%-PTR#in%
    num%=ext%-PTR#in%
 
 
     REPEAT
 
     REPEAT
 
       len%=max%
 
       len%=max%
Line 57: Line 56:
 
       PROCgbpb(2,out%,data%,len%,0)
 
       PROCgbpb(2,out%,data%,len%,0)
 
       num%=num%-len%
 
       num%=num%-len%
     UNTIL num%<0
+
     UNTIL num%=0
  
[[User:Jgharston|Jgharston]] 11:30, 27 August 2008 (BST)
+
==See Also==
 +
* [[OSGBPB]]
 +
 
 +
[[User:WikiSysop|WikiSysop]] ([[User talk:WikiSysop|talk]]) 13:17, 8 March 2015 (UTC)
 +
[[User:Jgharston|Jgharston]] ([[User talk:Jgharston|talk]]) 17:28, 5 November 2018 (CET)

Latest revision as of 18:28, 5 November 2018

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)