Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dvi/dvilib/dl-loader.cc
blob: ce29a43cd04f9869098cf5c018e6ba1a240bb92d (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "dl-loader.hh"
#include <errno.h>
#include <iostream>

using namespace DviLib;

/* Abstract loader */
/* =============== */

/* unsigned integers */
int 
AbstractLoader::get_uint16 ()
{
    return (get_uint8() << 8) | get_uint8();
}

int 
AbstractLoader::get_uint24 ()
{
    return (get_uint16() << 8) | get_uint8();
}
int 
AbstractLoader::get_uint32 ()
{
    return (get_uint16() << 16) | get_uint16();
}

/* signed integers */
int 
AbstractLoader::get_int16 ()
{
    return (get_int8() << 8) | get_uint8();
}

int 
AbstractLoader::get_int24 ()
{
    return (get_int16() << 8) | get_uint8();
}

int 
AbstractLoader::get_int32 ()
{
    return (get_int16() << 16) | get_uint16();
}

/* (Pascal) strings */
string 
AbstractLoader::get_string8 ()
{
    return get_n (get_uint8());
}

string 
AbstractLoader::get_string16 ()
{
    return get_n (get_uint16());
}

string
AbstractLoader::get_string24 ()
{
    return get_n (get_uint24());
}

string 
AbstractLoader::get_string32 ()
{
    return get_n (get_uint32());
}

void 
AbstractLoader::skip_string8 ()
{
    get_string8();
}

void 
AbstractLoader::skip_string16 ()
{
    get_string16();
}

void 
AbstractLoader::skip_string24 ()
{
    get_string24();
}

void 
AbstractLoader::skip_string32 ()
{
    get_string32();
}


/* "n" */
void 
AbstractLoader::skip_n (int n)
{
    get_n(n);
}

string 
AbstractLoader::get_n (int n)
{
    string r;
    
    while (n--)
	r += get_uint8 ();
    
    return r;
}

void
AbstractLoader::get_n (int n, unsigned char *v)
{
    while (n--)
	*v++ = (unsigned char)get_uint8 ();
}

/* File loader */

/* FIXME
 * 
 * do not use C style files (?)
 * what exceptions should we throw?
 */

FileLoader::FileLoader (const string &name)
{
    filename = name;
    f = fopen (filename.c_str(), "r");
    if (!f)
    {
	string s (strerror (errno));
	throw string ("Could not open " + filename + ": " + s);
    }
}

FileLoader::~FileLoader ()
{
    std::cout << "hej" << std::endl;
    if (fclose (f) == EOF)
	throw string ("Error closing " + filename);
}

int
FileLoader::get_int8 ()
{
    int c; 
    
    if ((c = fgetc (f)) == EOF)
	throw string ("Unexpected end of file");
    else
	return (signed char)c;
}

int
FileLoader::get_uint8 ()
{
    return (unsigned char)get_int8();
}

void
FileLoader::goto_from_start (int n)
{
    if (fseek (f, n, SEEK_SET) < 0)
    {
	string error = "fseek failed: ";
	error += strerror (errno);
	throw error;
    }
}

void
FileLoader::goto_from_current (int n)
{
    if (fseek (f, n, SEEK_CUR) < 0)
    {
	string error = "fseek failed: ";
	error += strerror (errno);
	throw error;
    }
}

void
FileLoader::goto_from_end (int n)
{
    if (fseek (f, n, SEEK_END) < 0)
    {
	string error = "fseek failed: ";
	error += strerror (errno);
	throw error;
    }    
}