COMPRESS/DECOMPRESS Example


Example:

 

INNAME

DIM

120

 

OUTNAME

DIM

120

 

BLOCK

DIM

65531

 

NEXTREC

INTEGER

4

 

LENGTH

INTEGER

2

 

MODE

DIM

1

 

SEQ

FORM

"-1"

 

INSIZE

FORM

10

 

OUTSIZE

FORM

10

 

.

 

 

 

IN

FILE

 

 

OUT

FILE

 

 

.

 

 

 

COMPREC

RECORD

 

 

SRCSIZE

INTEGER

2

 

CMPSIZE

INTEGER

2

 

DATA

DIM

65531

 

  

RECORDEND

 

 

*.........................................................................

.

.Get the file names

.

 

 

 

  

KEYIN

*HD,*R,"Enter the input file name: ",INNAME

  

TYPE

INNAME

 

  

STOP

IF EOS

 

.

 

 

 

  

KEYIN

*HD,*R,"Enter the output file name: ",OUTNAME

  

TYPE

OUTNAME

 

  

STOP

IF EOS

 

*

.Get the direction

.

  

KEYIN

*HD,*R,"(C)ompress or (U)ncompress? ",MODE

  

CMATCH

"U",MODE

 

  

GOTO

UNCOMPRESS IF EQUAL

  

CMATCH

"C",MODE

 

  

STOP

IF NOT EQUAL

 

*............................................................................

.

.Compress a file

.

 

 

 

  

OPEN

IN,INNAME,EXCLUSIVE

  

PREP

OUT,OUTNAME,EXCLUSIVE

.

 

 

 

  

LOOP

 

 

  

READ

IN,SEQ;*ABSON,BLOCK;

// read some data

  

TYPE

BLOCK

// if there was something read,

  

WHILE

NOT EOS

 

  

MOVELPTR

BLOCK,COMPREC.SRCSIZE

// note the original size

  

COMPRESS

BLOCK,COMPREC.DATA

// compress the data

  

MOVELPTR

COMPREC.DATA,COMPREC.CMPSIZE

// note the compressed size

  

WRITE

OUT,SEQ;*ABSON,*LL,COMPREC;

// write the data

  

REPEAT

 

// continue

.

 

 

 

  

GOTO

END

 

*.............................................................................

.

.Uncompress a File

.

UNCOMPRESS

 

 

 

  

OPEN

IN,INNAME,EXCLUSIVE

 

  

PREP

OUT,OUTNAME,EXCLUSIVE

 

.

 

 

 

  

LOOP

 

 

  

REPOSIT

IN,NEXTREC

// position to the next input record

  

READ

IN,SEQ;*ABSON,COMPREC;

// read a compressed record

  

TYPE

COMPREC.DATA

// did we find one ?

  

WHILE

NOT EOS

// stop if nothing read

  

ADD

(COMPREC.CMPSIZE + 4),NEXTREC

// compute the next compressed record start

  

SETLPTR

COMPREC.DATA,COMPREC.CMPSIZE

// set the logical length of the block

  

DECOMPRESS

COMPREC.DATA,BLOCK

// decompress it

  

MOVELPTR

BLOCK,LENGTH

// get the decompressed length

  

IF

(LENGTH != COMPREC.SRCSIZE)

// does it match the original size ?

  

KEYIN

*HD,*R,"Uncompressed data size incorrect.",INNAME

  

STOP

 

// die

  

ENDIF

 

// otherwise,

  

WRITE

OUT,SEQ;*ABSON,*LL,BLOCK;

// output the data

  

REPEAT

 

// continue

*.................................................................................

.

.Show the results

.

END

 

 

 

  

CLOSE

IN

 

  

CLOSE

OUT

 

.

 

 

 

  

FINDFILE

INNAME,FILESIZE=INSIZE

 

  

FINDFILE

OUTNAME,FILESIZE=OUTSIZE

 

.

 

 

 

  

KEYIN

*HD,*R,"Input file size: ",*DV,INSIZE:

  

  

 " Output file size: ",*DV,OUTSIZE:

  

  

 " ",INNAME

.

 

 

 

  

STOP

 

 

 

This example program accepts an input and an output file name. Depending upon the mode selected, the input file is compressed or uncompressed into the output file. Upon completion, both file sizes are shown

 



PL/B Language Reference COMPARE Examples CONST Example