File: sid_.cpp

package info (click to toggle)
libsidplay 1.36.59-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,120 kB
  • ctags: 1,368
  • sloc: cpp: 10,639; sh: 8,422; makefile: 69
file content (253 lines) | stat: -rw-r--r-- 7,569 bytes parent folder | download | duplicates (7)
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
//
// /home/ms/source/sidplay/libsidplay/fformat/RCS/sid_.cpp,v
// 

#include "sid_.h"


const char text_format[] = "Raw plus SIDPLAY ASCII text file (SID)";

const char text_truncatedError[] = "ERROR: SID file is truncated";
const char text_noMemError[] = "ERROR: Not enough free memory";

const char keyword_id[] = "SIDPLAY INFOFILE";

const char keyword_name[] = "NAME=";            // No white-space characters 
const char keyword_author[] = "AUTHOR=";        // in these keywords, because
const char keyword_copyright[] = "COPYRIGHT=";  // we want to use a white-space
const char keyword_address[] = "ADDRESS=";      // eating string stream to
const char keyword_songs[] = "SONGS=";          // parse most of the header.
const char keyword_speed[] = "SPEED=";
const char keyword_musPlayer[] = "SIDSONG=YES";

const uint sidMinFileSize = 1+sizeof(keyword_id);  // Just to avoid a first segm.fault.
const uint parseChunkLen = 80;                     // Enough for all keywords incl. their values.


bool sidTune::SID_fileSupport(const void* dataBuffer, udword dataBufLen,
                              const void* sidBuffer, udword sidBufLen)
{
	// Remove any format description or error string.
	info.formatString = 0;
	
	// Make sure SID buffer pointer is not zero.
	// Check for a minimum file size. If it is smaller, we will not proceed.
	if ((sidBuffer==0) || (sidBufLen<sidMinFileSize))
	{
		return false;
	}

	const char* pParseBuf = (const char*)sidBuffer;
	// First line has to contain the exact identification string.
	if ( myStrNcaseCmp( pParseBuf, keyword_id ) != 0 )
	{
		return false;
	}
	else
	{
		// At least the ID was found, so set a default error message.
		info.formatString = text_truncatedError;
		
		// Defaults.
		fileOffset = 0;                // no header in separate data file
		info.musPlayer = false;
		info.numberOfInfoStrings = 0;
		udword oldStyleSpeed = 0;

		// Flags for required entries.
		bool hasAddress = false,
		    hasName = false,
		    hasAuthor = false,
		    hasCopyright = false,
		    hasSongs = false,
		    hasSpeed = false;
	
		// Using a temporary instance of an input string chunk.
#ifdef SID_HAVE_EXCEPTIONS
		char* pParseChunk = new(std::nothrow) char[parseChunkLen+1];
#else
		char* pParseChunk = new char[parseChunkLen+1];
#endif
		if ( pParseChunk == 0 )
		{
			info.formatString = text_noMemError;
			return false;
		}
		
		// Parse as long we have not collected all ``required'' entries.
		while ( !hasAddress || !hasName || !hasAuthor || !hasCopyright
			    || !hasSongs || !hasSpeed )
		{
			// Skip to next line. Leave loop, if none.
			if (( pParseBuf = returnNextLine( pParseBuf )) == 0 )
			{
				break;
			}
			// And get a second pointer to the following line.
			const char* pNextLine = returnNextLine( pParseBuf );
			udword restLen;
			if ( pNextLine != 0 )
			{
				// Calculate number of chars between current pos and next line.
				restLen = (udword)(pNextLine - pParseBuf);
			}
			else
			{
				// Calculate number of chars between current pos and end of buf.
				restLen = sidBufLen - (udword)(pParseBuf - (char*)sidBuffer);
			}
			// Create whitespace eating (!) input string stream.
#ifdef SID_HAVE_SSTREAM
			string sParse( pParseBuf, restLen );
            istringstream parseStream( sParse );
			// A second one just for copying.
			istringstream parseCopyStream( sParse );
#else
			istrstream parseStream( pParseBuf, restLen );
			istrstream parseCopyStream( pParseBuf, restLen );
#endif
			if ( !parseStream || !parseCopyStream )
			{
				break;
			}
			// Now copy the next X characters except white-spaces.
			for ( uint i = 0; i < parseChunkLen; i++ )
			{
				char c;
				parseCopyStream >> c;
				pParseChunk[i] = c;
			}
			pParseChunk[parseChunkLen]=0;
			// Now check for the possible keywords.
			// ADDRESS
			if ( myStrNcaseCmp( pParseChunk, keyword_address ) == 0 )
			{
				skipToEqu( parseStream );
				info.loadAddr = (uword)readHex( parseStream );
				if ( !parseStream )
				    break;
				info.initAddr = (uword)readHex( parseStream );
				if ( !parseStream )
				    break;
				info.playAddr = (uword)readHex( parseStream );
				hasAddress = true;
			}
			// NAME
			else if ( myStrNcaseCmp( pParseChunk, keyword_name ) == 0 )
			{
				copyStringValueToEOL( pParseBuf, &infoString[0][0], infoStringLen );
				info.nameString = &infoString[0][0];
				info.infoString[0] = &infoString[0][0];
				hasName = true;
			}
			// AUTHOR
			else if ( myStrNcaseCmp( pParseChunk, keyword_author ) == 0 )
			{
				copyStringValueToEOL( pParseBuf, &infoString[1][0], infoStringLen );
				info.authorString = &infoString[1][0];
				info.infoString[1] = &infoString[1][0];
				hasAuthor = true;
			}
			// COPYRIGHT
			else if ( myStrNcaseCmp( pParseChunk, keyword_copyright ) == 0 )
			{
				copyStringValueToEOL( pParseBuf, &infoString[2][0], infoStringLen );
				info.copyrightString = &infoString[2][0];
				info.infoString[2] = &infoString[2][0];
				hasCopyright = true;
			}
			// SONGS
			else if ( myStrNcaseCmp( pParseChunk, keyword_songs ) == 0 )
			{
				skipToEqu( parseStream );
				info.songs = (uword)readDec( parseStream );
				info.startSong = (uword)readDec( parseStream );
				hasSongs = true;
			}
			// SPEED
			else if ( myStrNcaseCmp( pParseChunk, keyword_speed ) == 0 )
			{
				skipToEqu( parseStream );
				oldStyleSpeed = readHex(parseStream);
				hasSpeed = true;
			}
			// SIDSONG
			else if ( myStrNcaseCmp( pParseChunk, keyword_musPlayer ) == 0 )
			{
				info.musPlayer = true;
			}
        };
		
        delete[] pParseChunk;
		
		// Again check for the ``required'' values.
		if ( hasAddress || hasName || hasAuthor || hasCopyright || hasSongs || hasSpeed )
		{
			// Create the speed/clock setting table.
			convertOldStyleSpeedToTables(oldStyleSpeed);
			// loadAddr = 0 means, the address is stored in front of the C64 data.
			// We cannot verify whether the dataBuffer contains valid data.
		    // All we want to know is whether the SID buffer is valid.
			// If data is present, we access it (here to get the C64 load address).
			if (info.loadAddr==0 && (dataBufLen>=(fileOffset+2)) && dataBuffer!=0)
			{
				const ubyte* pDataBufCp = (const ubyte*)dataBuffer + fileOffset;
				info.loadAddr = readEndian( *(pDataBufCp + 1), *pDataBufCp );
				fileOffset += 2;  // begin of data
			}
			// Keep compatibility to PSID/SID.
			if ( info.initAddr == 0 )
			{
				info.initAddr = info.loadAddr;
			}
			info.numberOfInfoStrings = 3;
			// We finally accept the input data.
			info.formatString = text_format;
			return true;
		}
		else
		{
			// Something is missing (or damaged ?).
			// Error string set above.
			return false;
		}
	}
}


bool sidTune::SID_fileSupportSave( ofstream& fMyOut )
{
	fMyOut << keyword_id << endl
		<< keyword_address << hex << setw(4) << setfill('0') << 0 << ','
		<< hex << setw(4) << info.initAddr << ","
		<< hex << setw(4) << info.playAddr << endl
		<< keyword_songs << dec << (int)info.songs << "," << (int)info.startSong << endl;

	udword oldStyleSpeed = 0;
	int maxBugSongs = ((info.songs <= 32) ? info.songs : 32);
	for (int s = 0; s < maxBugSongs; s++)
	{
		if (songSpeed[s] == SIDTUNE_SPEED_CIA_1A)
		{
			oldStyleSpeed |= (1<<s);
		}
	}

	fMyOut
		<< keyword_speed << hex << setw(8) << oldStyleSpeed << endl
		<< keyword_name << info.nameString << endl
		<< keyword_author << info.authorString << endl
		<< keyword_copyright << info.copyrightString << endl;
	if ( info.musPlayer )
	{
		fMyOut << keyword_musPlayer << endl;
	}
	if ( !fMyOut )
	{
		return false;
	}
	else
	{
		return true;
	}
}