]> www.fi.muni.cz Git - evince.git/blob - dvi/dvilib/dl-loader.cc
Updated Swedish translation.
[evince.git] / dvi / dvilib / dl-loader.cc
1 #include "dl-loader.hh"
2 #include <errno.h>
3 #include <iostream>
4
5 using namespace DviLib;
6
7 /* Abstract loader */
8 /* =============== */
9
10 /* unsigned integers */
11 int 
12 AbstractLoader::get_uint16 ()
13 {
14     return (get_uint8() << 8) | get_uint8();
15 }
16
17 int 
18 AbstractLoader::get_uint24 ()
19 {
20     return (get_uint16() << 8) | get_uint8();
21 }
22 int 
23 AbstractLoader::get_uint32 ()
24 {
25     return (get_uint16() << 16) | get_uint16();
26 }
27
28 /* signed integers */
29 int 
30 AbstractLoader::get_int16 ()
31 {
32     return (get_int8() << 8) | get_uint8();
33 }
34
35 int 
36 AbstractLoader::get_int24 ()
37 {
38     return (get_int16() << 8) | get_uint8();
39 }
40
41 int 
42 AbstractLoader::get_int32 ()
43 {
44     return (get_int16() << 16) | get_uint16();
45 }
46
47 /* (Pascal) strings */
48 string 
49 AbstractLoader::get_string8 ()
50 {
51     return get_n (get_uint8());
52 }
53
54 string 
55 AbstractLoader::get_string16 ()
56 {
57     return get_n (get_uint16());
58 }
59
60 string
61 AbstractLoader::get_string24 ()
62 {
63     return get_n (get_uint24());
64 }
65
66 string 
67 AbstractLoader::get_string32 ()
68 {
69     return get_n (get_uint32());
70 }
71
72 void 
73 AbstractLoader::skip_string8 ()
74 {
75     get_string8();
76 }
77
78 void 
79 AbstractLoader::skip_string16 ()
80 {
81     get_string16();
82 }
83
84 void 
85 AbstractLoader::skip_string24 ()
86 {
87     get_string24();
88 }
89
90 void 
91 AbstractLoader::skip_string32 ()
92 {
93     get_string32();
94 }
95
96
97 /* "n" */
98 void 
99 AbstractLoader::skip_n (int n)
100 {
101     get_n(n);
102 }
103
104 string 
105 AbstractLoader::get_n (int n)
106 {
107     string r;
108     
109     while (n--)
110         r += get_uint8 ();
111     
112     return r;
113 }
114
115 void
116 AbstractLoader::get_n (int n, unsigned char *v)
117 {
118     while (n--)
119         *v++ = (unsigned char)get_uint8 ();
120 }
121
122 /* File loader */
123
124 /* FIXME
125  * 
126  * do not use C style files (?)
127  * what exceptions should we throw?
128  */
129
130 FileLoader::FileLoader (const string &name)
131 {
132     filename = name;
133     f = fopen (filename.c_str(), "r");
134     if (!f)
135     {
136         string s (strerror (errno));
137         throw string ("Could not open " + filename + ": " + s);
138     }
139 }
140
141 FileLoader::~FileLoader ()
142 {
143     std::cout << "hej" << std::endl;
144     if (fclose (f) == EOF)
145         throw string ("Error closing " + filename);
146 }
147
148 int
149 FileLoader::get_int8 ()
150 {
151     int c; 
152     
153     if ((c = fgetc (f)) == EOF)
154         throw string ("Unexpected end of file");
155     else
156         return (signed char)c;
157 }
158
159 int
160 FileLoader::get_uint8 ()
161 {
162     return (unsigned char)get_int8();
163 }
164
165 void
166 FileLoader::goto_from_start (int n)
167 {
168     if (fseek (f, n, SEEK_SET) < 0)
169     {
170         string error = "fseek failed: ";
171         error += strerror (errno);
172         throw error;
173     }
174 }
175
176 void
177 FileLoader::goto_from_current (int n)
178 {
179     if (fseek (f, n, SEEK_CUR) < 0)
180     {
181         string error = "fseek failed: ";
182         error += strerror (errno);
183         throw error;
184     }
185 }
186
187 void
188 FileLoader::goto_from_end (int n)
189 {
190     if (fseek (f, n, SEEK_END) < 0)
191     {
192         string error = "fseek failed: ";
193         error += strerror (errno);
194         throw error;
195     }    
196 }