-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
624 lines (547 loc) · 13.3 KB
/
main.c
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "nsf.h"
#include <popt.h>//TODO: Rewrite to argp because it appears to be GNU standard and more widespread
void nsf_printChipExtensions(const nsf_chipExtensions* ext,FILE* output){
if(*ext & NSF_CHIP_VRCVI) fputs("VRCVI, ",output);
if(*ext & NSF_CHIP_VRCVII) fputs("VRCVII, ",output);
if(*ext & NSF_CHIP_FDS) fputs("FDS sound, ",output);
if(*ext & NSF_CHIP_MMC5) fputs("MMC5 audio, ",output);
if(*ext & NSF_CHIP_NAMCO106) fputs("Namco 106, ",output);
if(*ext & NSF_CHIP_SUNSOFT_FME07)fputs("Sunsoft FME-07, ",output);
}
struct nsf_data nsf;
FILE* in = NULL,
* out = NULL;
const char* filepath_in = NULL,
* filepath_out = NULL;
bool output=false;
const char const EMPTY_STRING[] = "\0";
struct{
const char* title,
* region,
* artist,
* copyright,
* ripper,
* trackTitles,
* trackLengths,
* trackFades;
}write={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
struct{
uint8_t title :1;
uint8_t region :1;
uint8_t artist :1;
uint8_t copyright :1;
uint8_t ripper :1;
uint8_t trackTitles :1;
uint8_t trackLengths :1;
uint8_t trackFades :1;
uint8_t trackcount :1;
uint8_t initialtrack :1;
uint8_t extensions :1;
}show={false,false,false,false,false,false,false,false,false,false,false};
poptContext parameterContext;
enum Show{
SHOW_NULL,
SHOW_TITLE,
SHOW_REGION,
SHOW_ARTIST,
SHOW_COPYRIGHT,
SHOW_RIPPER,
SHOW_TRACKTITLES,
SHOW_TRACKLENGTHS,
SHOW_TRACKFADES,
SHOW_TRACKCOUNT,
SHOW_INITIALTRACK,
SHOW_EXTENSIONS,
SHOW_OUTPUT
};
/**
* Parses a string in a certain format, executing a function for each key-value pair.
* Example of the format: 0:Zero,2,3:Three,4,5,7,8:Eight,100:"One hundred"
*
* @param str Input, to parse from.
* Must be non-null and a null-terminated string.
* @param parsePair Parsing function to execute.
* The second argument is a pointer which may point to invalid data after the execution of parsePair.
* Must be non-null.
* @param closureData Passed to every execution of parsePair.
*/
bool parseOptionalIntStrMap(char* str,bool(*parsePair)(int,const char*,void*),void* closureData){
char* tmp = str;
char* intBegin = NULL;
char* intEnd = NULL;
char* strBegin = NULL;
#define SKIP_SPACES() while(*tmp==' '){++tmp;}
ParseKeyValue:
SKIP_SPACES();
if(*tmp=='\0') return true;
//Parse int
if(*tmp>='0' && *tmp<='9'){
intBegin = tmp;
while(*tmp>='0' && *tmp<='9'){
++tmp;
}
intEnd = tmp;
}else{
return false;
}
SKIP_SPACES();
//Parse separator
switch(*tmp){
//Key-value separator
case ',':
{
char c = *intEnd;
*intEnd = '\0';
parsePair(atoi(intBegin),NULL,closureData);
*intEnd = c;
}
++tmp;
//Reset states, and begin parsing a new key-value
intBegin = NULL;
intEnd = NULL;
goto ParseKeyValue;
//End of string
case '\0':
parsePair(atoi(intBegin),NULL,closureData);
return true;
//Continue to parsing the value
case ':':
++tmp;
break;
//Expected separator or end-of-string
default:
return false;
}
SKIP_SPACES();
//Parse str
strBegin = tmp;
switch(*tmp){
//TODO: String with citation marks
/*case '"':
ParseValue:
switch(*++tmp){
case '\0':
return false;
case '"':
++tmp;
SKIP_SPACES();
if(*tmp=='\0') return true;
break;
default:
goto ParseValue;
}
break;*/
//String without citation marks
default: ParseValueRaw:
switch(*++tmp){
case ',':
{
char c = *intEnd;
*intEnd = '\0';
*tmp = '\0';
parsePair(atoi(intBegin),strBegin,closureData);
*tmp = ',';
*intEnd = c;
}
++tmp;
//Reset states, and begin parsing a new key-value
intBegin = NULL;
intEnd = NULL;
strBegin = NULL;
goto ParseKeyValue;
case '\0':
{
char c = *intEnd;
*intEnd = '\0';
parsePair(atoi(intBegin),strBegin,closureData);
*intEnd = c;
}
return true;
default:
goto ParseValueRaw;
}
break;
}
}
char* strcpyof(const char* str){
char* new = malloc(strlen(str));
if(new==NULL) return NULL;
strcpy(new,str);
return new;
}
struct TrackTitleClosureData{
char** titles;
uint8_t tracks;
};
bool trackTitleClosure(int i,const char* title,void* dat){
struct TrackTitleClosureData* data = dat;
if(0<=i && i<data->tracks){
//Write
if(title!=NULL){
if((data->titles[i] = strcpyof(title))==NULL) return false;
}
//Show
puts(data->titles[i]);
return true;
}else{
return false;
}
}
struct TrackLengthClosureData{
int32_t* lengths;
uint8_t tracks;
};
bool trackLengthClosure(int i,const char* length,void* dat){
struct TrackLengthClosureData* data = dat;
if(0<=i && i<data->tracks){
//Write
if(length!=NULL){
unsigned int min,sec;
//Parsing
char* tmp = length;
while(*tmp>='0' && *tmp<='9'){++tmp;}
if(*tmp=='.'){
*tmp = '\0';
min = atoi(length);
sec = atoi(tmp+1);
*tmp = '.';
}else{
return false;
}
//Writing
data->lengths[i] = min*60*1000 + sec*1000;
}
//Show
printf("%u ms (%u:%02u)\n",
data->lengths[i],
data->lengths[i]/1000/60,
data->lengths[i]/1000%60
);
return true;
}else{
return false;
}
}
struct TrackFadeClosureData{
int32_t* fades;
uint8_t tracks;
};
bool trackFadeClosure(int i,const char* fade,void* dat){
struct TrackFadeClosureData* data = dat;
if(0<=i && i<data->tracks){
//Write
if(fade!=NULL){
data->fades[i] = atoi(fade);
}
//Show
printf("%u ms\n",data->fades[i]);
return true;
}else{
return false;
}
}
int main(int argc,const char* argv[]){
//Initialize variables
memset(&nsf,'\0',sizeof(struct nsf_data));
//popt
struct poptOption parameterTable[] = {
{
"title",
't',
POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL,
&write.title,
SHOW_TITLE,
"Title of nsf. Often used as the album name.",
"<string>"
},
{
"region",
'r',
POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL,
&write.region,
SHOW_REGION,
"Region of nsf.",
"NTSC|PAL|MIX1|MIX2"
},
{
"artist",
'a',
POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL,
&write.artist,
SHOW_ARTIST,
"Artist of nsf.",
"<string>"
},
{
"copyright",
'c',
POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL,
&write.copyright,
SHOW_COPYRIGHT,
"Copyright string of nsf.",
"<string>"
},
{
"ripper",
'p',
POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL,
&write.ripper,
SHOW_RIPPER,
"String of the ripper of the nsf.",
"<string>"
},
{
"tracktitles",
'T',
POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL,
&write.trackTitles,
SHOW_TRACKTITLES,
"Titles of tracks.",
"<int>[:<string>][,..]"
},
{
"tracklengths",
'L',
POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL,
&write.trackLengths,
SHOW_TRACKLENGTHS,
"Length of tracks.",
"<int>[:<minutes>.<seconds>][,..]"
},
{
"trackfades",
'F',
POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL,
&write.trackFades,
SHOW_TRACKFADES,
"Fade length of tracks.",
"<int>[:<milliseconds>][,..]"
},
{
"out",
'o',
POPT_ARG_STRING,
&filepath_out,
SHOW_OUTPUT,
"Output filepath. This must be specified for changes to be written out.",
"<filepath>"
},
{
"trackcount",
'l',
POPT_ARG_NONE,
NULL,
SHOW_TRACKCOUNT,
"Amount of tracks in the nsf.",
NULL
},
{
"initialtrack",
'i',
POPT_ARG_NONE,
NULL,
SHOW_INITIALTRACK,
"The initial track number (Output is a zero based integer).",
NULL
},
{
"extensions",
'e',
POPT_ARG_NONE,
NULL,
SHOW_EXTENSIONS,
"The extension chips needed to play the nsf (Output is comma separated).",
NULL
},
POPT_AUTOHELP
POPT_TABLEEND
};
parameterContext = poptGetContext(NULL,argc,argv,parameterTable,0);
poptSetOtherOptionHelp(parameterContext,"<path to nsf file> [OPTIONS...]");
//Show help if there's no parameters
if(argc<2){
poptPrintUsage(parameterContext,stderr,0);
return 1;
}
//Parse option parameters
OptionLoop:{
char c=poptGetNextOpt(parameterContext);
switch(c){
case POPT_ERROR_BADOPT:
fputs("Error: Invalid parameter option\n",stderr);
return POPT_ERROR_BADOPT;
case POPT_ERROR_BADNUMBER :
fputs("Error: Bad number\n",stderr);
return POPT_ERROR_BADNUMBER ;
case POPT_ERROR_OVERFLOW:
fputs("Error: Number overflow\n",stderr);
return POPT_ERROR_OVERFLOW;
case -1:
break;
case SHOW_TITLE:
show.title=true;
goto OptionLoop;
case SHOW_REGION:
show.region=true;
goto OptionLoop;
case SHOW_ARTIST:
show.artist=true;
goto OptionLoop;
case SHOW_COPYRIGHT:
show.copyright=true;
goto OptionLoop;
case SHOW_RIPPER:
show.ripper=true;
goto OptionLoop;
case SHOW_TRACKTITLES:
show.trackTitles=true;
goto OptionLoop;
case SHOW_TRACKLENGTHS:
show.trackTitles=true;
goto OptionLoop;
case SHOW_TRACKFADES:
show.trackTitles=true;
goto OptionLoop;
case SHOW_TRACKCOUNT:
show.trackcount=true;
goto OptionLoop;
case SHOW_INITIALTRACK:
show.initialtrack=true;
goto OptionLoop;
case SHOW_EXTENSIONS:
show.extensions=true;
goto OptionLoop;
case SHOW_OUTPUT:
output=true;
goto OptionLoop;
default:
fputs("Error: Internal error\n",stderr);
return 3;
}
}
//Parse file parameter and do the thing
if((filepath_in = poptGetArg(parameterContext))==NULL){
poptPrintUsage(parameterContext,stderr,0);
fputs("Specify the path to the nsf file: .e.g., /path/to/the/file.nsf\n",stderr);
return 2;
}else{
in = fopen(filepath_in,"rb");
int fileLoadingReturnCode=nsf_load(&nsf,in,output);
if(fileLoadingReturnCode!=0){
fprintf(stderr,"Error in file loading: %i\n",fileLoadingReturnCode);
return 3;
}
if(argc==2){//If only the filepath is defined (nsfinfo /path/to/file.nsf)
printf("Title: %s\nRegion: %s\nArtist: %s\nCopyright: %s\n\nTracks: %i\nInitial track: %i\nExtension Chips: ",
nsf.gameTitle,
nsf.info.region?"PAL":"NTSC",
nsf.artist,
nsf.copyright,
nsf.info.trackCount,
nsf.info.initialTrack
);
nsf_printChipExtensions(&nsf.info.chipExtensions,stdout);
printf("\nRipper: %s\n\n",nsf.ripper);
puts("Tracks:");
for(int i=0;i<nsf.info.trackCount;++i){
printf(" %03u:\n Title: %s\n Length: %i ms (%u:%02u)\n Fade: %i ms\n",
i,
nsf.trackLabels?nsf.trackLabels[i]:"",
nsf.trackTimes?nsf.trackTimes[i]:-1,
nsf.trackTimes?(nsf.trackTimes[i]/1000/60):0,
nsf.trackTimes?(nsf.trackTimes[i]/1000%60):0,
nsf.trackFades?nsf.trackFades[i]:-1
);
}
if(nsf.playlist){
puts("\nPlaylist Order:");
for(int i=0;i<nsf.playlistSize;++i){
printf(" %03u\n",nsf.playlist[i]);
}
}
}else{
//Write
if(write.title){
if((nsf.gameTitle = strcpyof(write.title))==NULL) return 1;
}
if(write.region){
if(strcmp(write.region,"NTSC")==0)
nsf.info.region=NSF_REGION_NTSC;
else if(strcmp(write.region,"PAL")==0)
nsf.info.region=NSF_REGION_PAL;
else{
printf("Error: Invalid region: `%s`\n",write.region);
return 5;
}
}
if(write.artist){
if((nsf.artist = strcpyof(write.artist))==NULL) return 1;
}
if(write.copyright){
if((nsf.copyright = strcpyof(write.copyright))==NULL) return 1;
}
if(write.ripper){
if((nsf.ripper = strcpyof(write.ripper))==NULL) return 1;
}
if(write.trackTitles){
if(nsf.trackLabels==NULL){
if((nsf.trackLabels = calloc(nsf.info.trackCount,sizeof(nsf.trackLabels[0])))==NULL) return 1;
}
struct TrackTitleClosureData data = {nsf.trackLabels,nsf.info.trackCount};
parseOptionalIntStrMap(write.trackTitles,trackTitleClosure,&data);
}
if(write.trackLengths){
if(nsf.trackTimes==NULL){
if((nsf.trackTimes = calloc(nsf.info.trackCount,sizeof(nsf.trackTimes[0])))==NULL) return 1;
}
struct TrackLengthClosureData data = {nsf.trackTimes,nsf.info.trackCount};
parseOptionalIntStrMap(write.trackLengths,trackLengthClosure,&data);
}
if(write.trackFades){
if(nsf.trackFades==NULL){
if((nsf.trackFades = calloc(nsf.info.trackCount,sizeof(nsf.trackFades[0])))==NULL) return 1;
}
struct TrackFadeClosureData data = {nsf.trackFades,nsf.info.trackCount};
parseOptionalIntStrMap(write.trackFades,trackFadeClosure,&data);
}
//Show
if(show.title)
puts(nsf.gameTitle?:EMPTY_STRING);
if(show.region)
puts(nsf.info.region==1?"PAL":"NTSC");
if(show.artist)
puts(nsf.artist?:EMPTY_STRING);
if(show.copyright)
puts(nsf.copyright?:EMPTY_STRING);
if(show.trackcount)
printf("%i\n",nsf.info.trackCount);
if(show.initialtrack)
printf("%i\n",nsf.info.initialTrack);
if(show.ripper)
puts(nsf.ripper?:EMPTY_STRING);
if(show.extensions){
nsf_printChipExtensions(&nsf.info.chipExtensions,stdout);
putchar('\n');
}
//Output file
if(filepath_out){
FILE* file=fopen(filepath_out,"wb");
//nsf_saveNesm(&nsf,file);
nsf_saveNsfe(&nsf,file);
fclose(file);
}
}
if(in){
nsf_free(&nsf);
fclose(in);
}
return fileLoadingReturnCode;
}
//Free popt context
poptFreeContext(parameterContext);
return EXIT_SUCCESS;
}