1 #include "dl-loader.hh"
5 using namespace DviLib;
10 /* unsigned integers */
12 AbstractLoader::get_uint16 ()
14 return (get_uint8() << 8) | get_uint8();
18 AbstractLoader::get_uint24 ()
20 return (get_uint16() << 8) | get_uint8();
23 AbstractLoader::get_uint32 ()
25 return (get_uint16() << 16) | get_uint16();
30 AbstractLoader::get_int16 ()
32 return (get_int8() << 8) | get_uint8();
36 AbstractLoader::get_int24 ()
38 return (get_int16() << 8) | get_uint8();
42 AbstractLoader::get_int32 ()
44 return (get_int16() << 16) | get_uint16();
47 /* (Pascal) strings */
49 AbstractLoader::get_string8 ()
51 return get_n (get_uint8());
55 AbstractLoader::get_string16 ()
57 return get_n (get_uint16());
61 AbstractLoader::get_string24 ()
63 return get_n (get_uint24());
67 AbstractLoader::get_string32 ()
69 return get_n (get_uint32());
73 AbstractLoader::skip_string8 ()
79 AbstractLoader::skip_string16 ()
85 AbstractLoader::skip_string24 ()
91 AbstractLoader::skip_string32 ()
99 AbstractLoader::skip_n (int n)
105 AbstractLoader::get_n (int n)
116 AbstractLoader::get_n (int n, unsigned char *v)
119 *v++ = (unsigned char)get_uint8 ();
126 * do not use C style files (?)
127 * what exceptions should we throw?
130 FileLoader::FileLoader (const string &name)
133 f = fopen (filename.c_str(), "r");
136 string s (strerror (errno));
137 throw string ("Could not open " + filename + ": " + s);
141 FileLoader::~FileLoader ()
143 std::cout << "hej" << std::endl;
144 if (fclose (f) == EOF)
145 throw string ("Error closing " + filename);
149 FileLoader::get_int8 ()
153 if ((c = fgetc (f)) == EOF)
154 throw string ("Unexpected end of file");
156 return (signed char)c;
160 FileLoader::get_uint8 ()
162 return (unsigned char)get_int8();
166 FileLoader::goto_from_start (int n)
168 if (fseek (f, n, SEEK_SET) < 0)
170 string error = "fseek failed: ";
171 error += strerror (errno);
177 FileLoader::goto_from_current (int n)
179 if (fseek (f, n, SEEK_CUR) < 0)
181 string error = "fseek failed: ";
182 error += strerror (errno);
188 FileLoader::goto_from_end (int n)
190 if (fseek (f, n, SEEK_END) < 0)
192 string error = "fseek failed: ";
193 error += strerror (errno);