Debian Codegraph @

src/spctag.c

raw symbols SCIP
1 /*
2 spctag.c : spctag library
3 v0.2 - 2011-09-18
4
5 Copyright (C) 2011 Jérôme SONRIER <[email protected]g>
6
7 This file is part of libspctag.
8
9 libspctag is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 libspctag is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with libspctag. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "spctag.h"
29 #include "constants.h"
30
31 // Returned variables
32 char song_title[SPCTAG_SONGTITLE_LENGTH+1];
33 char game_title[SPCTAG_GAMETITLE_LENGTH+1];
34 char dumper_name[SPCTAG_DUMPERNAME_LENGTH+1];
35 char comments[SPCTAG_COMMENTS_LENGTH+1];
36 char dump_date[SPCTAG_DUMPDATE_TXT_LENGTH+1];
37 char length[SPCTAG_LENGTH_LENGTH+1];
38 char fade_length[SPCTAG_FADELENGTH_TXT_LENGTH+1];
39 char artist[SPCTAG_ARTIST_LENGTH+1];
40 char channels[9];
41 char *emulator;
42
43 /*
44 * get_formated_channels
45 * Transform a byte into string
46 * param:
47 * channels: channels encoded on a byte
48 * formated_channels: string composed of 0 and 1 that represent channels states
49 */
50 void get_formated_channels( char channels, char* formated_channels )
51 {
52 unsigned char masque = 0x1 << 7;
53 int i;
54
55 for ( i=0; i<=7; i++ ) {
56 if ( channels & masque )
57 formated_channels[i] = '1';
58 else
59 formated_channels[i] = '0';
60
61 masque >>= 1;
62 }
63
64 formated_channels[i] = '\0';
65 }
66
67 /*
68 * set_formated_channels
69 * Transform a string composed of 0 and 1 into a byte
70 * param:
71 * formated_channels: string composed of 0 and 1 that represent channels states
72 * channels: channels encoded on a byte
73 */
74 void set_formated_channels( char* formated_channels, char* channels )
75 {
76 unsigned char masque;
77 int i;
78
79 for ( i=0; i<=7; i++ ) {
80 masque = 0x1 << 7-i;
81
82 if ( formated_channels[i] == '1' )
83 *channels |= masque;
84 else {
85 masque = ~masque;
86 *channels &= masque;
87 }
88 }
89 }
90
91 /*
92 * spctag_is_text
93 * Read data and tells if it is only text
94 * param:
95 * data: data wich must be test
96 * return:
97 * 0: at least one byte of data is not considered as text
98 * 1: all bytes are considered as text
99 */
100 int spctag_is_text( const char *data )
101 {
102 int i;
103
104 for ( i=0; i<SPCTAG_TEST_LENGTH; i++ ) {
105 if ( data[i] != 0 && ( data[i] < 44 || data[i] > 58 ) )
106 return 0;
107 }
108
109 return 1;
110 }
111
112 int spctag_init( FILE* file )
113 {
114 char header[SPCTAG_FILE_HEADER_LENGTH+1]; // SPC file header
115 char has_tag;
116 char test_tag[SPCTAG_TEST_LENGTH+1];
117
118 // Return if the file is not a SCP file
119 fseek( file, 0, SEEK_SET );
120 fread( &header, sizeof( header ) - 1, 1, file );
121 header[SPCTAG_FILE_HEADER_LENGTH] = '\0';
122 if ( strcmp( header, SPCTAG_FILE_HEADER ) != 0 ) {
123 fprintf( stderr, "Not an %s\n", SPCTAG_FILE_HEADER );
124 return SPCTAG_E_NOT_SPC;
125 }
126
127 // Return if the file header do not contain ID666 tags
128 fseek( file, SPCTAG_HAS_TAGS, SEEK_SET );
129 has_tag = fgetc( file );
130 if ( has_tag != SPCTAG_TAGS_PRESENTS ) {
131 printf( "No ID666 tag.\n" );
132 return SPCTAG_E_NO_TAGS;
133 }
134
135 // Test if we have text or binary tags
136 fseek( file, SPCTAG_TEST_START, SEEK_SET );
137 fread( &test_tag, SPCTAG_TEST_LENGTH, 1, file );
138 spctag_txt_tag = spctag_is_text( test_tag );
139
140 // Init tag struct
141 fseek( file, SPCTAG_TAGS_START, SEEK_SET );
142 if ( spctag_txt_tag ) {
143 spctag_tags_txt = (struct TAGS_TXT *)malloc( sizeof( struct TAGS_TXT ) );
144 fread( spctag_tags_txt, sizeof( struct TAGS_TXT ), 1, file );
145 } else {
146 spctag_tags_bin = (struct TAGS_BIN *)malloc( sizeof( struct TAGS_BIN ) );
147 fread( spctag_tags_bin, sizeof( struct TAGS_BIN ), 1, file );
148 }
149
150 spctag_is_init = 1;
151
152 return SPCTAG_SUCCESS;
153 }
154
155 char* spctag_get_songtitle()
156 {
157 if ( spctag_txt_tag )
158 strncpy( song_title, spctag_tags_txt->song_title, SPCTAG_SONGTITLE_LENGTH );
159 else
160 strncpy( song_title, spctag_tags_bin->song_title, SPCTAG_SONGTITLE_LENGTH );
161
162 song_title[SPCTAG_SONGTITLE_LENGTH] = '\0';
163
164 return song_title;
165 }
166
167 char* spctag_get_gametitle()
168 {
169 if ( spctag_txt_tag )
170 strncpy( game_title, spctag_tags_txt->game_title, SPCTAG_GAMETITLE_LENGTH );
171 else
172 strncpy( game_title, spctag_tags_bin->game_title, SPCTAG_GAMETITLE_LENGTH );
173
174 game_title[SPCTAG_GAMETITLE_LENGTH] = '\0';
175
176 return game_title;
177 }
178
179 char* spctag_get_dumpername()
180 {
181 if ( spctag_txt_tag )
182 strncpy( dumper_name, spctag_tags_txt->dumper_name, SPCTAG_DUMPERNAME_LENGTH );
183 else
184 strncpy( dumper_name, spctag_tags_bin->dumper_name, SPCTAG_DUMPERNAME_LENGTH );
185
186 dumper_name[SPCTAG_DUMPERNAME_LENGTH] = '\0';
187
188 return dumper_name;
189 }
190
191 char* spctag_get_comments()
192 {
193 if ( spctag_txt_tag )
194 strncpy( comments, spctag_tags_txt->comments, SPCTAG_COMMENTS_LENGTH );
195 else
196 strncpy( comments, spctag_tags_bin->comments, SPCTAG_COMMENTS_LENGTH );
197
198 comments[SPCTAG_COMMENTS_LENGTH] = '\0';
199
200 return comments;
201 }
202
203 char* spctag_get_dumpdate()
204 {
205 if ( spctag_txt_tag )
206 strncpy( dump_date, spctag_tags_txt->dump_date, SPCTAG_DUMPDATE_TXT_LENGTH );
207 else {
208 if ( spctag_tags_bin->dump_day != 0 && spctag_tags_bin->dump_month != 0 && spctag_tags_bin->dump_year != 0 )
209 sprintf( dump_date, "%.2d/%.2d/%.4u", spctag_tags_bin->dump_month, spctag_tags_bin->dump_day, *(unsigned*)spctag_tags_bin->dump_year );
210 else
211 dump_date[0] = '\0';
212 }
213
214 dump_date[SPCTAG_DUMPDATE_TXT_LENGTH] = '\0';
215
216 return dump_date;
217 }
218
219 char* spctag_get_length()
220 {
221 if ( spctag_txt_tag )
222 strncpy( length, spctag_tags_txt->length, SPCTAG_LENGTH_LENGTH );
223 else {
224 unsigned bin_length = 0;
225 strncpy( (char*)&bin_length, spctag_tags_bin->length, SPCTAG_LENGTH_LENGTH );
226 sprintf( length, "%u", bin_length );
227 }
228
229 length[SPCTAG_LENGTH_LENGTH] = '\0';
230
231 return length;
232 }
233
234 char* spctag_get_fadelength()
235 {
236 if ( spctag_txt_tag )
237 strncpy( fade_length, spctag_tags_txt->fade_length, SPCTAG_FADELENGTH_TXT_LENGTH );
238 else {
239 unsigned bin_fadelength = 0;
240 strncpy( (char*)&bin_fadelength, spctag_tags_bin->fade_length, SPCTAG_FADELENGTH_BIN_LENGTH );
241 sprintf( fade_length, "%u", bin_fadelength );
242 }
243
244 fade_length[SPCTAG_FADELENGTH_TXT_LENGTH] = '\0';
245
246 return fade_length;
247 }
248
249 char* spctag_get_artist()
250 {
251 if ( spctag_txt_tag )
252 strncpy( artist, spctag_tags_txt->artist, SPCTAG_ARTIST_LENGTH );
253 else
254 strncpy( artist, spctag_tags_bin->artist, SPCTAG_ARTIST_LENGTH );
255
256 artist[SPCTAG_ARTIST_LENGTH] = '\0';
257
258 return artist;
259 }
260
261 char* spctag_get_defaultchannels()
262 {
263 if ( spctag_txt_tag )
264 get_formated_channels( spctag_tags_txt->default_channels, channels );
265 else
266 get_formated_channels( spctag_tags_bin->default_channels, channels );
267
268 return channels;
269 }
270
271 char* spctag_get_emulator()
272 {
273 if ( spctag_txt_tag ) {
274 switch ( spctag_tags_txt->emulator ) {
275 case '1' : emulator = "ZSNES"; break;
276 case '2' : emulator = "Snes9x"; break;
277 default : emulator = "Unknown";
278 }
279 } else {
280 switch ( (int)spctag_tags_bin->emulator ) {
281 case 1 : emulator = "ZSNES"; break;
282 case 2 : emulator = "Snes9x"; break;
283 default : emulator = "Unknown";
284 }
285 }
286
287 return emulator;
288 }
289
290 int spctag_set_songtitle( char* new_songtitle )
291 {
292 if ( spctag_txt_tag )
293 strncpy( spctag_tags_txt->song_title, new_songtitle, SPCTAG_SONGTITLE_LENGTH );
294 else
295 strncpy( spctag_tags_bin->song_title, new_songtitle, SPCTAG_SONGTITLE_LENGTH );
296
297 return SPCTAG_SUCCESS;
298 }
299
300 int spctag_set_gametitle( char* new_gametitle )
301 {
302 if ( spctag_txt_tag )
303 strncpy( spctag_tags_txt->game_title, new_gametitle, SPCTAG_GAMETITLE_LENGTH );
304 else
305 strncpy( spctag_tags_bin->game_title, new_gametitle, SPCTAG_GAMETITLE_LENGTH );
306
307 return SPCTAG_SUCCESS;
308 }
309
310 int spctag_set_dumpername( char* new_dumpername )
311 {
312 if ( spctag_txt_tag )
313 strncpy( spctag_tags_txt->dumper_name, new_dumpername, SPCTAG_DUMPERNAME_LENGTH );
314 else
315 strncpy( spctag_tags_bin->dumper_name, new_dumpername, SPCTAG_DUMPERNAME_LENGTH );
316
317 return SPCTAG_SUCCESS;
318 }
319
320 int spctag_set_comments( char* new_comments )
321 {
322 if ( spctag_txt_tag )
323 strncpy( spctag_tags_txt->comments, new_comments, SPCTAG_COMMENTS_LENGTH );
324 else
325 strncpy( spctag_tags_bin->comments, new_comments, SPCTAG_COMMENTS_LENGTH );
326
327 return SPCTAG_SUCCESS;
328 }
329
330 int spctag_set_dumpdate( char* new_dumdate )
331 {
332 if ( spctag_txt_tag )
333 strncpy( spctag_tags_txt->dump_date, new_dumdate, SPCTAG_DUMPDATE_TXT_LENGTH );
334 else {
335 char day_string[3];
336 char month_string[3];
337 char year_string[5];
338 char day, month;
339 unsigned year;
340
341 strncpy( day_string, new_dumdate+3, 2 );
342 strncpy( month_string, new_dumdate, 2 );
343 strncpy( year_string, new_dumdate+6, 4 );
344
345 day = atoi( day_string ) & 0xFF;
346 month = atoi( month_string ) & 0xFF;
347 year = atoi( year_string );
348
349 spctag_tags_bin->dump_day = day;
350 spctag_tags_bin->dump_month = month;
351 strncpy( spctag_tags_bin->dump_year, (char*)&year, SPCTAG_DUMPYEAR_BIN_LENGTH );
352 }
353
354 return SPCTAG_SUCCESS;
355 }
356
357 int spctag_set_length( char* new_length )
358 {
359 if ( spctag_txt_tag )
360 strncpy( spctag_tags_txt->length, new_length, SPCTAG_LENGTH_LENGTH );
361 else {
362 unsigned length = (unsigned)atoi( new_length );
363
364 if ( length > 0xFFFFFF )
365 length = 0xFFFFFF;
366
367 strncpy( spctag_tags_bin->length, (char*)&length, SPCTAG_LENGTH_LENGTH );
368 }
369
370 return SPCTAG_SUCCESS;
371 }
372
373 int spctag_set_fadelength( char* new_fadelength )
374 {
375 if ( spctag_txt_tag )
376 strncpy( spctag_tags_txt->fade_length, new_fadelength, SPCTAG_FADELENGTH_TXT_LENGTH );
377 else {
378 unsigned fadelength = (unsigned)atoi( new_fadelength );
379
380 if ( fadelength > 0xFFFFFFFF )
381 fadelength = 0xFFFFFFFF;
382
383 strncpy( spctag_tags_bin->fade_length, (char*)&fadelength, SPCTAG_FADELENGTH_BIN_LENGTH);
384 }
385
386 return SPCTAG_SUCCESS;
387 }
388
389 int spctag_set_artist( char* new_artist )
390 {
391 if ( spctag_txt_tag )
392 strncpy( spctag_tags_txt->artist, new_artist, SPCTAG_ARTIST_LENGTH );
393 else
394 strncpy( spctag_tags_bin->artist, new_artist, SPCTAG_ARTIST_LENGTH );
395
396 return SPCTAG_SUCCESS;
397 }
398
399 int spctag_set_defaultchannels( char* new_defaultchannels )
400 {
401 if ( spctag_txt_tag ) {
402 set_formated_channels( new_defaultchannels, &spctag_tags_txt->default_channels );
403 } else {
404 set_formated_channels( new_defaultchannels, &spctag_tags_bin->default_channels );
405 }
406 }
407
408 int spctag_set_emulator( char* new_emulator )
409 {
410 if ( spctag_txt_tag ) {
411 if ( strcasecmp( new_emulator, "ZSNES") == 0 )
412 spctag_tags_txt->emulator = '1';
413 else if ( strcasecmp( new_emulator, "Snes9x") == 0 )
414 spctag_tags_txt->emulator = '2';
415 else
416 spctag_tags_txt->emulator = '0';
417 } else {
418 if ( strcasecmp( new_emulator, "ZSNES") == 0 )
419 spctag_tags_bin->emulator = 1;
420 else if ( strcasecmp( new_emulator, "Snes9x") == 0 )
421 spctag_tags_bin->emulator = 2;
422 else
423 spctag_tags_bin->emulator = 0;
424 }
425 }
426
427 int spctag_save( FILE* file )
428 {
429 // Move to the start of ID666 tags
430 fseek( file, SPCTAG_TAGS_START, SEEK_SET );
431
432 // Write structures to file
433 if ( spctag_txt_tag )
434 fwrite( spctag_tags_txt, sizeof(*spctag_tags_txt), 1, file );
435 else
436 fwrite( spctag_tags_bin, sizeof(*spctag_tags_bin), 1, file );
437 }
438
439 void spctag_free()
440 {
441 if ( spctag_txt_tag )
442 free( spctag_tags_txt );
443 else
444 free( spctag_tags_bin );
445 }