Byte-stream File Handling Routines - Example

The following example program contains routines to open and close files to be processed as byte-stream files, one for input, one for output and to read and write individual bytes using buffering.

Source Code

$SET ANS85
copy "cblproto.cpy".
program-id.
*==========================================================*

 working-storage section
 78 fals value 0
 78 tru  value 1.

*----------------------------------------------------------*
* WS for byte-stream handling

 78 in-buff-len      value 4096.

 01 in-buff          pic x(in-buff-len).

 01 infilename       pic x(64).
 01 infile-handle    cblt-bytestream-handle.
 01 infile-offset    cblt-x8-compx value 0.
 01 infile-len       cblt-x8-compx.

 01 in-buff-ptr      pic x(2) comp-x.
 01 in-buff-end      cblt-x4 comp-x value in-buff-len. 
 
 78 out-buff-len     value 4096.

 01 out-buff         pic x(out-buff-len).

 01 outfilename      pic x(64).
 01 outfile-handle   cblt-bytestream-handle.
 01 outfile-offset   cblt-x8-compx value 0.
 01 outfile-len      cblt-x8-compx.

 01 out-buff-ptr     pic x(2) comp-x.
 01 out-buff-end     cblt-x4-compx value in-buff-len.

 01 bs-misc.
     03 eof-flag         pic x comp-x value fals.
         88 eof              value tru.
     03 last-block-flag  pic x comp-x value fals.
         88 last-block       value tru.
     03 display-put-flag pic x comp-x value fals.
         88 display-put      value tru false fals.

     03 next-byte.
         05 next-byte-n  cblt-x1-compx
     03 save-byte        pic x.

 01 k-0              cblt-x1-compx value 0.
 01 k-1              cblt-x1-compx value 1.
 01 k-2              cblt-x1-compx value 2.
 01 k-128            cblt-x1-compx value 128.

*----------------------------------------------------------*

*==========================================================*
 procedure division.
 aa-control section.
     perform ca-initial.
     perform cc-process.
     perform ce-final
     .

 aa-990-exit.
     exit program
     stop run
     .

 ca-initial section.
     move "inbyte.dat" to infilename
     move "outbyte.dat" to outfilename
     perform open-in-file
     perform open-out-file
     .

 cc-process section.

* Examine each byte, and process as required when the 
* appropriate byte is found (as identified by the when 
* clause)

     perform get-byte
     perform until eof
         evaluate next-byte
          when .......


          when other
             perform put-byte
             perform get-byte
         end-evaluate
     end-perform
     .

 ce-final section.
     perform close-in-file
     perform close-out-file
     .

*----------------------------------------------------------*
* Input byte-stream file code

 open-in-file section.
     call "CBL_OPEN_FILE" using infilename
                                k-1
                                k-0
                                k-0
                                infile-handle
     if return-code not = 0
         display "Open failed: "
                 infilename
         stop run
     end-if

* Find length of input file.
     call "CBL_READ_FILE" using infile-handle
                                infile-offset
                                in-buff-end
                                k-128
                                in-buff
     move infile-offset to infile-len
     move 0 to infile-offset
     move in-buff-len to in-buff-ptr
     add 1 to in-buff-ptr
     move fals to last-block-flag
     move fals to eof-flag
     .

 get-byte section.
     if in-buff-ptr = in-buff-end
         if last-block
             set eof to true
             move x"ff" to next-byte
         else
             perform get-next-block
             if in-buff-end = 0
                 set eof to true
                 move x"ff" to next-byte
             end-if
         end-if
     end-if

     if not eof
         move in-buff (in-buff-ptr : 1) to next-byte
         add 1 to in-buff-ptr
*        display next-byte
*            with no advancing
     end-if
     .

 get-previous-byte section.
     if in-buff-ptr = 2
         subtract 2 from in-buff-ptr
     else
         perform get-previous-block
     end-if
     perform get-byte
     .

 get-next-block section.
     call "CBL_READ_FILE" using infile-handle
                                infile-offset
                                in-buff-end
                                k-0
                                in-buff
     end-call
     move 1 to in-buff-ptr
     add in-buff-end to infile-offset
     if infile-offset = infile-len
         subtract infile-len from infile-offset
         subtract infile-offset from in-buff-end
         move tru to last-block-flag
     end-if
     .

 get-previous-block section.
     if last-block
         move fals to last-block-flag
         move in-buff-len to in-buff-end
         add infile-len to infile-offset
     end-if
     subtract in-buff-end from infile-offset
     call "CBL_READ_FILE" using infile-handle
                         infile-offset
                         in-buff-end
                         k-0
                         in-buff
     end-call
     move in-buff-len to in-buff-ptr
     .
 close-in-file section.
     call "CBL_CLOSE_FILE" using infile-handle
     .

*==========================================================*
* Output byte-stream file code

 open-out-file section.
     call "CBL_CREATE_FILE" using outfilename
                                k-2
                                k-0
                                k-0
                                outfile-handle

     if return-code not = 0
         display "Open failed: "
                 outfilename
         stop run
     end-if
     move 1 to out-buff-ptr
     .

 put-byte section.
     if display-put
         display next-byte
             with no advancing
     end-if
     move next-byte to out-buff (out-buff-ptr:1)
     add 1 to out-buff-ptr
     if out-buff-ptr = out-buff-len
         perform write-block
         move 1 to out-buff-ptr
     end-if
     .

 write-block section.
     call "CBL_WRITE_FILE" using outfile-handle
                                 outfile-offset
                                 out-buff-end
                                 k-0
                                 out-buff
     end-call
     add out-buff-end to outfile-offset
     .

 close-out-file section.
     if out-buff-ptr = 1
         move out-buff-ptr to out-buff-end
         subtract 1 from out-buff-end
         perform write-block
     end-if
     call "CBL_CLOSE_FILE" using outfile-handle
     .