summaryrefslogtreecommitdiffabout
path: root/lib/util.cc
blob: 4bbe6f36e6ec7d8c4d13799791d8469784ba1bd8 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <midillo/util.h>
#include <midillo/exception.h>

namespace midillo {

    unsigned long read32(istream& s) {
	unsigned long rv = 0;
	for(int t=0;t<4;++t) {
	    rv<<=8;
	    rv|=0xFF&s.get();
	    if(!s.good())
		throw exception_input_error(CODEPOINT,"Input error");
	}
	return rv;
    }

    unsigned int read16(istream& s) {
	unsigned int rv = 0;
	for(int t=0;t<2;++t) {
	    rv<<=8;
	    rv|=0xFF&s.get();
	    if(!s.good())
		throw exception_input_error(CODEPOINT,"Input error");
	}
	return rv;
    }

    unsigned long readVL(istream& s) {
	unsigned long rv=s.get();
	if(!s.good())
	    throw exception_input_error(CODEPOINT,"Error reading VLQ");
	int b;
	if(rv&0x80) {
	    rv&=0x7F;
	    do {
		rv = (rv<<7)|( (b=s.get())&0x7F );
		if(!s.good())
		    throw exception_input_error(CODEPOINT,"Error reading VLQ");
	    }while(b&0x80);
	}
	return rv;
    }

    void write32(ostream& s,unsigned long d) {
	for(int t=3;t>=0;--t) {
	    s.put(((const char*)&d)[t]);
	    if(!s.good())
		throw exception_output_error(CODEPOINT,"Output error");
	}
    }

    void write16(ostream& s,unsigned int d) {
	for(int t=1;t>=0;--t) {
	    s.put(((const char*)&d)[t]);
	    if(!s.good())
		throw exception_output_error(CODEPOINT,"Output error");
	}
    }

    void writeVL(ostream& s,unsigned long d) {
	// TODO: I don't think this is perfectly written code
	unsigned long tmp = d&0x7F;
	while(d>>=7) {
	    tmp<<=8;
	    tmp |=((d&0x7F)|0x80);
	}
	for(;;) {
	    s.put(tmp&0xFF);
	    if(!s.good())
		throw exception_output_error(CODEPOINT,"Error writing VLQ");
	    if(tmp&0x80)
		tmp>>=8;
	    else
		break;
	}
    }

    unsigned long calcVLsize(unsigned long d) {
	unsigned long rv = 0;
	// TODO: imperfect code revisited
	unsigned long tmp = d&0x7F;
	while(d>>=7) {
	    tmp<<=8;
	    tmp |=((d&0x7F)|0x80);
	}
	for(;;) {
	    ++rv;
	    if(tmp&0x80)
		tmp>>=8;
	    else
		break;
	}
	return rv;
    }

}