3 #include <msp/io/zlibcompressed.h>
4 #include "collection.h"
13 const char RawData::signature[4] = { 'M', 'D', 'R', 1 };
23 bool RawData::detect_signature(const std::string &sig)
25 return !sig.compare(0, string::npos, signature, sizeof(signature));
28 void RawData::open_file(Collection &coll, const string &fn)
31 throw logic_error("input already exists");
33 RefPtr<IO::Base> opened = coll.open_raw(fn);
35 throw IO::file_not_found(fn);
42 void RawData::open_io(IO::Base &i, const string &fn)
45 throw logic_error("input already exists");
48 unsigned len = i.read(header, sizeof(header));
49 if(len!=sizeof(header))
50 throw data_error(fn, 0, "Missing header");
51 if(memcmp(header, signature, sizeof(signature)))
52 throw data_error(fn, 0, "Bad header");
55 for(unsigned j=0; j<8; ++j)
56 size = (size<<8) | static_cast<unsigned char>(header[4+j]);
58 uint16_t flags = (static_cast<unsigned char>(header[12])<<8) | static_cast<unsigned char>(header[13]);
63 compressed = new IO::ZlibCompressed(*in, IO::M_READ);
69 throw logic_error("no input");
71 throw logic_error("already loaded");
73 owned_data = new char[size];
74 load_into(owned_data);
77 void RawData::load_into(void *buffer)
80 throw logic_error("no input");
82 data = static_cast<char *>(buffer);
84 IO::Base *src = (compressed ? compressed : in);
88 size_t len = src->read(data+pos, size-pos);
90 throw data_error(src_name, 0, "Truncated data");
99 void RawData::write_io(IO::Base &io, bool compress)
102 throw logic_error("no data");
104 io.write(signature, sizeof(signature));
106 for(unsigned i=56; i<64; i-=8)
107 io.put((size>>i)&0xFF);
112 io.put((flags>>8)&0xFF);
117 IO::ZlibCompressed z(io, IO::M_WRITE);
121 io.write(data, size);
124 } // namespace DataFile