2 * AdPlug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (c) 1999 - 2006 Simon Peter <dn.tlp@gmx.net>, et al.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * database.cpp - AdPlug database class
20 * Copyright (c) 2002 Riven the Mage <riven@ok.ru>
21 * Copyright (c) 2002, 2003, 2006 Simon Peter <dn.tlp@gmx.net>
30 #define DB_FILEID_V10 "AdPlug Module Information Database 1.0\x10"
32 /***** CAdPlugDatabase *****/
34 const unsigned short CAdPlugDatabase::hash_radix = 0xfff1; // should be prime
36 CAdPlugDatabase::CAdPlugDatabase()
37 : linear_index(0), linear_logic_length(0), linear_length(0)
39 db_linear = new DB_Bucket * [hash_radix];
40 db_hashed = new DB_Bucket * [hash_radix];
41 memset(db_linear, 0, sizeof(DB_Bucket *) * hash_radix);
42 memset(db_hashed, 0, sizeof(DB_Bucket *) * hash_radix);
45 CAdPlugDatabase::~CAdPlugDatabase()
49 for(i = 0; i < linear_length; i++)
56 bool CAdPlugDatabase::load(std::string db_name)
58 binifstream f(db_name);
59 if(f.error()) return false;
63 bool CAdPlugDatabase::load(binistream &f)
65 unsigned int idlen = strlen(DB_FILEID_V10);
66 char *id = new char [idlen];
69 // Open database as little endian with IEEE floats
70 f.setFlag(binio::BigEndian, false); f.setFlag(binio::FloatIEEE);
72 f.readString(id,idlen);
73 if(memcmp(id,DB_FILEID_V10,idlen)) {
78 length = f.readInt(4);
81 for(unsigned long i = 0; i < length; i++)
82 insert(CRecord::factory(f));
87 bool CAdPlugDatabase::save(std::string db_name)
89 binofstream f(db_name);
90 if(f.error()) return false;
94 bool CAdPlugDatabase::save(binostream &f)
98 // Save database as little endian with IEEE floats
99 f.setFlag(binio::BigEndian, false); f.setFlag(binio::FloatIEEE);
101 f.writeString(DB_FILEID_V10);
102 f.writeInt(linear_logic_length, 4);
105 for(i = 0; i < linear_length; i++)
106 if(!db_linear[i]->deleted)
107 db_linear[i]->record->write(f);
112 CAdPlugDatabase::CRecord *CAdPlugDatabase::search(CKey const &key)
114 if(lookup(key)) return get_record(); else return 0;
117 bool CAdPlugDatabase::lookup(CKey const &key)
119 unsigned long index = make_hash(key);
120 if(!db_hashed[index]) return false;
123 DB_Bucket *bucket = db_hashed[index];
125 if(!bucket->deleted && bucket->record->key == key) {
126 linear_index = bucket->index;
131 bucket = db_hashed[index]->chain;
134 if(!bucket->deleted && bucket->record->key == key) {
135 linear_index = bucket->index;
139 bucket = bucket->chain;
145 bool CAdPlugDatabase::insert(CRecord *record)
150 if(!record) return false; // null-pointer given
151 if(linear_length == hash_radix) return false; // max. db size exceeded
152 if(lookup(record->key)) return false; // record already in db
155 DB_Bucket *bucket = new DB_Bucket(linear_length, record);
156 if(!bucket) return false;
158 // add to linear list
159 db_linear[linear_length] = bucket;
160 linear_logic_length++; linear_length++;
162 // add to hashed list
163 index = make_hash(record->key);
165 if(!db_hashed[index]) // First entry in hashtable
166 db_hashed[index] = bucket;
167 else { // Add entry in chained list
168 DB_Bucket *chain = db_hashed[index];
170 while(chain->chain) chain = chain->chain;
171 chain->chain = bucket;
177 void CAdPlugDatabase::wipe(CRecord *record)
179 if(!lookup(record->key)) return;
183 void CAdPlugDatabase::wipe()
185 if(!linear_length) return;
187 DB_Bucket *bucket = db_linear[linear_index];
189 if(!bucket->deleted) {
190 delete bucket->record;
191 linear_logic_length--;
192 bucket->deleted = true;
196 CAdPlugDatabase::CRecord *CAdPlugDatabase::get_record()
198 if(!linear_length) return 0;
199 return db_linear[linear_index]->record;
202 bool CAdPlugDatabase::go_forward()
204 if(linear_index + 1 < linear_length) {
211 bool CAdPlugDatabase::go_backward()
213 if(!linear_index) return false;
218 void CAdPlugDatabase::goto_begin()
220 if(linear_length) linear_index = 0;
223 void CAdPlugDatabase::goto_end()
225 if(linear_length) linear_index = linear_length - 1;
228 inline unsigned long CAdPlugDatabase::make_hash(CKey const &key)
230 return (key.crc32 + key.crc16) % hash_radix;
233 /***** CAdPlugDatabase::DB_Bucket *****/
235 CAdPlugDatabase::DB_Bucket::DB_Bucket(unsigned long nindex, CRecord *newrecord, DB_Bucket *newchain)
236 : index(nindex), deleted(false), chain(newchain), record(newrecord)
240 CAdPlugDatabase::DB_Bucket::~DB_Bucket()
242 if(!deleted) delete record;
245 /***** CAdPlugDatabase::CRecord *****/
247 CAdPlugDatabase::CRecord *CAdPlugDatabase::CRecord::factory(RecordType type)
250 case Plain: return new CPlainRecord;
251 case SongInfo: return new CInfoRecord;
252 case ClockSpeed: return new CClockRecord;
257 CAdPlugDatabase::CRecord *CAdPlugDatabase::CRecord::factory(binistream &in)
263 type = (RecordType)in.readInt(1); size = in.readInt(4);
267 rec->key.crc16 = in.readInt(2); rec->key.crc32 = in.readInt(4);
268 rec->filetype = in.readString('\0'); rec->comment = in.readString('\0');
272 // skip this record, cause we don't know about it
273 in.seek(size, binio::Add);
278 void CAdPlugDatabase::CRecord::write(binostream &out)
280 out.writeInt(type, 1);
281 out.writeInt(get_size() + filetype.length() + comment.length() + 8, 4);
282 out.writeInt(key.crc16, 2); out.writeInt(key.crc32, 4);
283 out.writeString(filetype); out.writeInt('\0', 1);
284 out.writeString(comment); out.writeInt('\0', 1);
289 bool CAdPlugDatabase::CRecord::user_read(std::istream &in, std::ostream &out)
291 return user_read_own(in, out);
294 bool CAdPlugDatabase::CRecord::user_write(std::ostream &out)
296 out << "Record type: ";
298 case Plain: out << "Plain"; break;
299 case SongInfo: out << "SongInfo"; break;
300 case ClockSpeed: out << "ClockSpeed"; break;
301 default: out << "*** Unknown ***"; break;
304 out << "Key: " << std::hex << key.crc16 << ":" << key.crc32 << std::dec << std::endl;
305 out << "File type: " << filetype << std::endl;
306 out << "Comment: " << comment << std::endl;
308 return user_write_own(out);
311 /***** CAdPlugDatabase::CRecord::CKey *****/
313 CAdPlugDatabase::CKey::CKey(binistream &buf)
318 bool CAdPlugDatabase::CKey::operator==(const CKey &key)
320 return ((crc16 == key.crc16) && (crc32 == key.crc32));
323 void CAdPlugDatabase::CKey::make(binistream &buf)
324 // Key is CRC16:CRC32 pair. CRC16 and CRC32 calculation routines (c) Zhengxi
326 static const unsigned short magic16 = 0xa001;
327 static const unsigned long magic32 = 0xedb88320;
329 crc16 = 0; crc32 = ~0;
333 unsigned char byte = buf.readInt(1);
335 for (int j=0;j<8;j++)
337 if ((crc16 ^ byte) & 1)
338 crc16 = (crc16 >> 1) ^ magic16;
342 if ((crc32 ^ byte) & 1)
343 crc32 = (crc32 >> 1) ^ magic32;
355 /***** CInfoRecord *****/
357 CInfoRecord::CInfoRecord()
362 void CInfoRecord::read_own(binistream &in)
364 title = in.readString('\0');
365 author = in.readString('\0');
368 void CInfoRecord::write_own(binostream &out)
370 out.writeString(title); out.writeInt('\0', 1);
371 out.writeString(author); out.writeInt('\0', 1);
374 unsigned long CInfoRecord::get_size()
376 return title.length() + author.length() + 2;
379 bool CInfoRecord::user_read_own(std::istream &in, std::ostream &out)
381 out << "Title: "; in >> title;
382 out << "Author: "; in >> author;
386 bool CInfoRecord::user_write_own(std::ostream &out)
388 out << "Title: " << title << std::endl;
389 out << "Author: " << author << std::endl;
393 /***** CClockRecord *****/
395 CClockRecord::CClockRecord()
401 void CClockRecord::read_own(binistream &in)
403 clock = in.readFloat(binio::Single);
406 void CClockRecord::write_own(binostream &out)
408 out.writeFloat(clock, binio::Single);
411 unsigned long CClockRecord::get_size()
416 bool CClockRecord::user_read_own(std::istream &in, std::ostream &out)
418 out << "Clockspeed: "; in >> clock;
422 bool CClockRecord::user_write_own(std::ostream &out)
424 out << "Clock speed: " << clock << " Hz" << std::endl;