summaryrefslogtreecommitdiff
path: root/ipl/procs/inbits.icn
blob: 5e4a4d1937a34eb6316afe92dd4375f876b46558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
############################################################################
#
#	File:     inbits.icn
#
#	Subject:  Procedure to read variable-length characters
#
#	Author:   Richard L. Goerwitz
#
#	Date:     November 3, 1991
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#	Version:  1.2
#
############################################################################
#  
#  This procedure, inbits(), re-imports data converted into writable
#  form by outbits().  See the file outbits.icn for all the whys and
#  hows.
#
############################################################################
#
#  See also: outbits.icn
#
############################################################################

procedure inbits(f, len)

    local i, byte, old_byte_mask
    static old_byte, old_len, byte_length
    initial {
	old_byte := old_len := 0
	byte_length := 8
    }

    old_byte_mask := (0 < 2^old_len - 1) | 0
    old_byte := iand(old_byte, old_byte_mask)
    i := ishift(old_byte, len-old_len)

    len -:= (len > old_len) | {
	old_len -:= len
	return i
    }
    
    while byte := ord(reads(f)) do {
	i := ior(i, ishift(byte, len-byte_length))
	len -:= (len > byte_length) | {
	    old_len := byte_length-len
	    old_byte := byte
	    return i
	}
    }

end