-
Notifications
You must be signed in to change notification settings - Fork 0
/
aye.c
168 lines (149 loc) · 3.67 KB
/
aye.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
/* AYE
* ZXAYEMUL metadata editor
* (c) Chris Young 2021, 2022
* https://www.unsatisfactorysoftware.co.uk
*/
#define AYECOPYVER "AYE 1.3+git\nZXAYEMUL metadata editor by Chris Young 2021, 2022\n\n"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>
#include <unistd.h>
#include "zxay_load.h"
#include "zxay_dump.h"
#include "zxay_peek.h"
#include "zxay_poke.h"
#include "zxay_save.h"
#include "zxay_merge.h"
static void print_ay_details(void *zxay, uint8_t numsongs)
{
printf("Author: %s\n", zxay_peek(zxay, ZXAY_AUTHOR));
printf("Misc: %s\n", zxay_peek(zxay, ZXAY_MISC));
printf("Tracks: %d\n", numsongs + 1);
for(int i = 0; i <= numsongs; i++) {
printf(" %d: %s\n", i + 1, zxay_peek_song(zxay, i, ZXAY_SONG_NAME));
}
}
int main(int argc, char **argv)
{
int c;
char *outfile = NULL;
char *infile = NULL;
char *joinfile = NULL;
char *newauthor = NULL;
char *newmisc = NULL;
char *newtitle = NULL;
int song = 0;
int debug = 0;
bool quiet = false;
bool updates = false;
void *zxay = NULL;
void *zxayjoin = NULL;
void *zxaymerge = NULL;
uint8_t numsongs;
while ((c = getopt(argc, argv, "hqd:s:a:m:t:j:o:")) != -1) {
switch(c) {
case 'h':
printf(AYECOPYVER);
printf("aye [OPTIONS] [INPUT]\n\n");
printf("Options:\n"
" -h This help\n"
" -q Quiet\n"
" -s Song number\n"
" -a New author\n"
" -m New misc\n"
" -t New title\n"
" -j Join\n"
" -o Output file\n\n");
return 0;
break;
case 'd':
debug = atoi(optarg);
break;
case 'q':
quiet = true;
break;
case 'a':
newauthor = optarg;
break;
case 'm':
newmisc = optarg;
break;
case 's':
song = atoi(optarg);
break;
case 't':
newtitle = optarg;
break;
case 'j':
joinfile = optarg;
break;
case 'o':
outfile = optarg;
break;
}
}
for(c = optind; c < argc; c++) {
infile = argv[c];
}
if(infile == NULL) {
printf("ERROR: No file specified\n\n");
return 0;
}
if(!quiet) {
printf(AYECOPYVER);
printf("File: %s\n", infile);
}
if(newtitle && (song == 0)) {
printf("\nWARNING: New title specified without -s, ignoring.\n\n");
newtitle = NULL;
}
if(newauthor || newmisc || newtitle) updates = true;
if(zxay = zxay_load(infile)) {
numsongs = zxay_peek(zxay, ZXAY_SONGCOUNT)[0];
if(!quiet) print_ay_details(zxay, numsongs);
if(debug) zxay_dump(zxay, debug);
if(updates && (!quiet)) printf("\nNew metadata:\n");
if(newauthor) {
if(!quiet) printf(" Author: %s\n", newauthor);
zxay_poke(zxay, ZXAY_AUTHOR, newauthor);
}
if(newmisc) {
if(!quiet) printf(" Misc: %s\n", newmisc);
zxay_poke(zxay, ZXAY_MISC, newmisc);
}
if(newtitle) {
song -= 1; /* First track is 0 */
if(song <= numsongs) {
if(!quiet) printf(" Track %d: %s\n", song + 1, newtitle);
zxay_poke_track(zxay, song, ZXAY_SONG_NAME, newtitle);
} else {
printf("WARNING: Invalid song (%d/%d) specified.\n", song + 1, numsongs + 1);
}
}
if(joinfile) {
if(zxayjoin = zxay_load(joinfile)) {
if(!quiet) printf("\nJoining %s\n", joinfile);
numsongs = zxay_peek(zxayjoin, ZXAY_SONGCOUNT)[0];
if(!quiet) print_ay_details(zxayjoin, numsongs);
} else {
printf("ERROR: Unable to read file; not joining %s\n", joinfile);
}
zxaymerge = zxay_merge(zxay, zxayjoin);
zxay_free(zxayjoin);
zxay_free(zxay);
zxay = zxaymerge;
}
if(outfile) {
if(!quiet) printf("\nOutput: %s\n", outfile);
zxay_save(zxay, outfile, debug);
} else {
if(updates) {
printf("WARNING: No output file specified; changes have not been saved.\n");
}
}
zxay_free(zxay);
}
return 0;
}