summaryrefslogtreecommitdiff
path: root/plugins/adplug/adplug/database.cpp
blob: de5cad3c67ebc329b028e4734cf185f6af319469 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * AdPlug - Replayer for many OPL2/OPL3 audio file formats.
 * Copyright (c) 1999 - 2006 Simon Peter <dn.tlp@gmx.net>, et al.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * database.cpp - AdPlug database class
 * Copyright (c) 2002 Riven the Mage <riven@ok.ru>
 * Copyright (c) 2002, 2003, 2006 Simon Peter <dn.tlp@gmx.net>
 */

#include <binio.h>
#include <binfile.h>
#include <string.h>

#include "database.h"

#define DB_FILEID_V10	"AdPlug Module Information Database 1.0\x10"

/***** CAdPlugDatabase *****/

const unsigned short CAdPlugDatabase::hash_radix = 0xfff1;	// should be prime

CAdPlugDatabase::CAdPlugDatabase()
  : linear_index(0), linear_logic_length(0), linear_length(0)
{
  db_linear = new DB_Bucket * [hash_radix];
  db_hashed = new DB_Bucket * [hash_radix];
  memset(db_linear, 0, sizeof(DB_Bucket *) * hash_radix);
  memset(db_hashed, 0, sizeof(DB_Bucket *) * hash_radix);
}

CAdPlugDatabase::~CAdPlugDatabase()
{
  unsigned long i;

  for(i = 0; i < linear_length; i++)
    delete db_linear[i];

  delete [] db_linear;
  delete [] db_hashed;
}

bool CAdPlugDatabase::load(std::string db_name)
{
  binifstream f(db_name);
  if(f.error()) return false;
  return load(f);
}

bool CAdPlugDatabase::load(binistream &f)
{
  unsigned int idlen = strlen(DB_FILEID_V10);
  char *id = new char [idlen];
  unsigned long length;

  // Open database as little endian with IEEE floats
  f.setFlag(binio::BigEndian, false); f.setFlag(binio::FloatIEEE);

  f.readString(id,idlen);
  if(memcmp(id,DB_FILEID_V10,idlen)) {
    delete [] id;
    return false;
  }
  delete [] id;
  length = f.readInt(4);

  // read records
  for(unsigned long i = 0; i < length; i++)
    insert(CRecord::factory(f));

  return true;
}

bool CAdPlugDatabase::save(std::string db_name)
{
  binofstream f(db_name);
  if(f.error()) return false;
  return save(f);
}

bool CAdPlugDatabase::save(binostream &f)
{
  unsigned long i;

  // Save database as little endian with IEEE floats
  f.setFlag(binio::BigEndian, false); f.setFlag(binio::FloatIEEE);

  f.writeString(DB_FILEID_V10);
  f.writeInt(linear_logic_length, 4);

  // write records
  for(i = 0; i < linear_length; i++)
    if(!db_linear[i]->deleted)
      db_linear[i]->record->write(f);

  return true;
}

CAdPlugDatabase::CRecord *CAdPlugDatabase::search(CKey const &key)
{
  if(lookup(key)) return get_record(); else return 0;
}

bool CAdPlugDatabase::lookup(CKey const &key)
{
  unsigned long index = make_hash(key);
  if(!db_hashed[index]) return false;

  // immediate hit ?
  DB_Bucket *bucket = db_hashed[index];

  if(!bucket->deleted && bucket->record->key == key) {
    linear_index = bucket->index;
    return true;
  }

  // in-chain hit ?
  bucket = db_hashed[index]->chain;

  while(bucket) {
    if(!bucket->deleted && bucket->record->key == key) {
      linear_index = bucket->index;
      return true;
    }

    bucket = bucket->chain;
  }

  return false;
}

bool CAdPlugDatabase::insert(CRecord *record)
{
  long index;

  // sanity checks
  if(!record) return false;			// null-pointer given
  if(linear_length == hash_radix) return false;	// max. db size exceeded
  if(lookup(record->key)) return false;		// record already in db

  // make bucket
  DB_Bucket *bucket = new DB_Bucket(linear_length, record);
  if(!bucket) return false;

  // add to linear list
  db_linear[linear_length] = bucket;
  linear_logic_length++; linear_length++;

  // add to hashed list
  index = make_hash(record->key);

  if(!db_hashed[index])	// First entry in hashtable
    db_hashed[index] = bucket;
  else {		// Add entry in chained list
    DB_Bucket *chain = db_hashed[index];

    while(chain->chain) chain = chain->chain;
    chain->chain = bucket;
  }

  return true;
}

void CAdPlugDatabase::wipe(CRecord *record)
{
  if(!lookup(record->key)) return;
  wipe();
}

void CAdPlugDatabase::wipe()
{
  if(!linear_length) return;

  DB_Bucket *bucket = db_linear[linear_index];

  if(!bucket->deleted) {
    delete bucket->record;
    linear_logic_length--;
    bucket->deleted = true;
  }
}

CAdPlugDatabase::CRecord *CAdPlugDatabase::get_record()
{
  if(!linear_length) return 0;
  return db_linear[linear_index]->record;
}

bool CAdPlugDatabase::go_forward()
{
  if(linear_index + 1 < linear_length) {
    linear_index++;
    return true;
  } else
    return false;
}

bool CAdPlugDatabase::go_backward()
{
  if(!linear_index) return false;
  linear_index--;
  return true;
}

void CAdPlugDatabase::goto_begin()
{	
  if(linear_length) linear_index = 0;
}

void CAdPlugDatabase::goto_end()
{
  if(linear_length) linear_index = linear_length - 1;
}

inline unsigned long CAdPlugDatabase::make_hash(CKey const &key)
{
  return (key.crc32 + key.crc16) % hash_radix;
}

/***** CAdPlugDatabase::DB_Bucket *****/

CAdPlugDatabase::DB_Bucket::DB_Bucket(unsigned long nindex, CRecord *newrecord, DB_Bucket *newchain)
  : index(nindex), deleted(false), chain(newchain), record(newrecord)
{
}

CAdPlugDatabase::DB_Bucket::~DB_Bucket()
{
  if(!deleted) delete record;
}

/***** CAdPlugDatabase::CRecord *****/

CAdPlugDatabase::CRecord *CAdPlugDatabase::CRecord::factory(RecordType type)
{
  switch(type) {
  case Plain: return new CPlainRecord;
  case SongInfo: return new CInfoRecord;
  case ClockSpeed: return new CClockRecord;
  default: return 0;
  }
}

CAdPlugDatabase::CRecord *CAdPlugDatabase::CRecord::factory(binistream &in)
{
  RecordType	type;
  unsigned long	size;
  CRecord	*rec;

  type = (RecordType)in.readInt(1); size = in.readInt(4);
  rec = factory(type);

  if(rec) {
    rec->key.crc16 = in.readInt(2); rec->key.crc32 = in.readInt(4);
    rec->filetype = in.readString('\0'); rec->comment = in.readString('\0');
    rec->read_own(in);
    return rec;
  } else {
    // skip this record, cause we don't know about it
    in.seek(size, binio::Add);
    return 0;
  }
}

void CAdPlugDatabase::CRecord::write(binostream &out)
{
  out.writeInt(type, 1);
  out.writeInt(get_size() + filetype.length() + comment.length() + 8, 4);
  out.writeInt(key.crc16, 2); out.writeInt(key.crc32, 4);
  out.writeString(filetype); out.writeInt('\0', 1);
  out.writeString(comment); out.writeInt('\0', 1);

  write_own(out);
}

//bool CAdPlugDatabase::CRecord::user_read(std::istream &in, std::ostream &out)
//{
//  return user_read_own(in, out);
//}

//bool CAdPlugDatabase::CRecord::user_write(std::ostream &out)
//{
//  out << "Record type: ";
//  switch(type) {
//  case Plain: out << "Plain"; break;
//  case SongInfo: out << "SongInfo"; break;
//  case ClockSpeed: out << "ClockSpeed"; break;
//  default: out << "*** Unknown ***"; break;
//  }
//  out << std::endl;
//  out << "Key: " << std::hex << key.crc16 << ":" << key.crc32 << std::dec << std::endl;
//  out << "File type: " << filetype << std::endl;
//  out << "Comment: " << comment << std::endl;
//
//  return user_write_own(out);
//}

/***** CAdPlugDatabase::CRecord::CKey *****/

CAdPlugDatabase::CKey::CKey(binistream &buf)
{
  make(buf);
}

bool CAdPlugDatabase::CKey::operator==(const CKey &key)
{
  return ((crc16 == key.crc16) && (crc32 == key.crc32));
}

void CAdPlugDatabase::CKey::make(binistream &buf)
// Key is CRC16:CRC32 pair. CRC16 and CRC32 calculation routines (c) Zhengxi
{
  static const unsigned short magic16 = 0xa001;
  static const unsigned long  magic32 = 0xedb88320;

  crc16 = 0; crc32 = ~0;

  while(!buf.eof())
    {
      unsigned char byte = buf.readInt(1);

      for (int j=0;j<8;j++)
	{
	  if ((crc16 ^ byte) & 1)
	    crc16 = (crc16 >> 1) ^ magic16;
	  else
	    crc16 >>= 1;

	  if ((crc32 ^ byte) & 1)
	    crc32 = (crc32 >> 1) ^ magic32;
	  else
	    crc32 >>= 1;

	  byte >>= 1;
	}
    }

  crc16 &= 0xffff;
  crc32  = ~crc32;
}

/***** CInfoRecord *****/

CInfoRecord::CInfoRecord()
{
  type = SongInfo;
}

void CInfoRecord::read_own(binistream &in)
{
  title = in.readString('\0');
  author = in.readString('\0');
}

void CInfoRecord::write_own(binostream &out)
{
  out.writeString(title); out.writeInt('\0', 1);
  out.writeString(author); out.writeInt('\0', 1);
}

unsigned long CInfoRecord::get_size()
{
  return title.length() + author.length() + 2;
}

bool CInfoRecord::user_read_own(std::istream &in, std::ostream &out)
{
  out << "Title: "; in >> title;
  out << "Author: "; in >> author;
  return true;
}

bool CInfoRecord::user_write_own(std::ostream &out)
{
  out << "Title: " << title << std::endl;
  out << "Author: " << author << std::endl;
  return true;
}

/***** CClockRecord *****/

CClockRecord::CClockRecord()
  : clock(0.0f)
{
  type = ClockSpeed;
}

void CClockRecord::read_own(binistream &in)
{
  clock = in.readFloat(binio::Single);
}

void CClockRecord::write_own(binostream &out)
{
  out.writeFloat(clock, binio::Single);
}

unsigned long CClockRecord::get_size()
{
  return 4;
}

bool CClockRecord::user_read_own(std::istream &in, std::ostream &out)
{
  out << "Clockspeed: "; in >> clock;
  return true;
}

bool CClockRecord::user_write_own(std::ostream &out)
{
  out << "Clock speed: " << clock << " Hz" << std::endl;
  return true;
}