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
|
/***************************************************************************
dbget.cpp - Get time from database
-------------------
begin : Fri Jun 2 2000
copyright : (C) 2000 by Simon White
email : s_a_white@email.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "SidDatabase.h"
#include "MD5/MD5.h"
const char *SidDatabase::ERR_DATABASE_CORRUPT = "SID DATABASE ERROR: Database seems to be corrupt.";
const char *SidDatabase::ERR_NO_DATABASE_LOADED = "SID DATABASE ERROR: Songlength database not loaded.";
const char *SidDatabase::ERR_NO_SELECTED_SONG = "SID DATABASE ERROR: No song selected for retrieving song length.";
const char *SidDatabase::ERR_MEM_ALLOC = "SID DATABASE ERROR: Memory Allocation Failure.";
const char *SidDatabase::ERR_UNABLE_TO_LOAD_DATABASE = "SID DATABASE ERROR: Unable to load the songlegnth database.";
SidDatabase::~SidDatabase ()
{
close ();
}
int_least32_t SidDatabase::parseTimeStamp(const char* arg)
{
/* Read in m:s format at most.
* Could use a system function if available.
*/
int_least32_t seconds = 0;
int passes = 2; // minutes, seconds
bool gotDigits = false;
while ( passes-- )
{
if ( isdigit(*arg) )
{
int t = atoi(arg);
seconds += t;
gotDigits = true;
}
while ( *arg && isdigit(*arg) )
{
++arg;
}
if ( *arg && *arg==':' )
{
seconds *= 60;
++arg;
}
}
// Handle -:-- time stamps and old 0:00 entries which
// need to be rounded up by one second.
if ( !gotDigits )
seconds = 0;
else if ( seconds==0 )
++seconds;
return seconds;
}
uint_least8_t SidDatabase::timesFound (char *str)
{
/* Try and determine the number of times read back.
* Used to check validility of times in database.
*/
uint_least8_t count = 0;
while (*str)
{
if (*str++ == ':')
count++;
}
return count;
}
int SidDatabase::open (const char *filename)
{
close ();
// @FIXME@: Libini should be changed
database = ini_open ((char *) filename, "r", ";");
if (!database)
{
errorString = ERR_UNABLE_TO_LOAD_DATABASE;
return -1;
}
return 0;
}
void SidDatabase::close ()
{
if (database)
ini_close (database);
}
int_least32_t SidDatabase::length (SidTuneMod &tune)
{
SidTuneInfo tuneInfo;
int_least32_t time = 0;
char timeStamp[10];
MD5 myMD5;
char digest[33];
uint_least16_t selectedSong = 0;
if (!database)
{
errorString = ERR_NO_DATABASE_LOADED;
return -1;
}
tune.getInfo(tuneInfo);
selectedSong = tuneInfo.currentSong;
if (!selectedSong)
{
errorString = ERR_NO_SELECTED_SONG;
return -1;
}
// Restart fingerprint
myMD5.reset();
tune.createMD5(myMD5);
myMD5.finish();
// Construct fingerprint.
digest[0] = '\0';
for (int di = 0; di < 16; ++di)
sprintf (digest, "%s%02x", digest, (int) myMD5.getDigest()[di]);
// Now set up array access
if (ini_listDelims (database, " ") == -1)
{
errorString = ERR_MEM_ALLOC;
return -1;
}
// Read Time (and check times before hand)
(void) ini_locateHeading (database, "Database");
(void) ini_locateKey (database, digest);
// If length return is -1 then no entry found in database
if (ini_dataLength (database) != -1)
{
selectedSong = 0;
while (selectedSong < tuneInfo.currentSong)
{
selectedSong++;
if (ini_readString (database, timeStamp, sizeof (timeStamp)) == -1)
{ // No time found
errorString = ERR_DATABASE_CORRUPT;
return -1;
}
// Validate Time
if (timesFound (timeStamp) != 1)
{
errorString = ERR_DATABASE_CORRUPT;
return -1;
}
}
// Parse timestamp
time = parseTimeStamp (timeStamp);
}
return time;
}
|