-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3_tagreader.c
343 lines (306 loc) · 8.3 KB
/
mp3_tagreader.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
/*****************************************************HEADER FILES***********************************************************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include"types.h"
#include"mp3.h"
/****************************************************************************************************************************************/
/*****************************************************MACROS*****************************************************************************/
#define TAG "ID3"
#define OFFSET 128
#define SONG 10
#define ARTIST 67
#define ALBUM 110
#define YEAR 129
#define COMMENT 185
/****************************************************************************************************************************************/
/*****************************************************FUNCTION PROTOTYPES****************************************************************/
OperationType check_operation_status(char *argv[]);
Status read_and_validate_args(char *argv[], Mp3Info *mp3Info);
Status open_file(Mp3Info *mp3Info);
Status start_operation(Mp3Info *mp3Info);
Status edit_info(Mp3Info *mp3Info);
Status version_id(Mp3Info *mp3Info);
Status get_details(Mp3Info *mp3Info);
uint verify_flag(Mp3Info *mp3Info);
void move_ptr(Mp3Info *mp3Info);
void print_tag(Mp3Info *mp3Info);
void close_file(Mp3Info *mp3Info);
void song(Mp3Info *mp3Info);
/****************************************************************************************************************************************/
int main(int argc,char *argv[])
{
Mp3Info mp3Info;
switch(argc)
{
case 1:
printf("NOT ENOUGH ARGUMENTS \n");
printf("Format : ./a.out [flag] [file.mp3]\n");
printf("INFO : ./a.out -h for help\n");
exit(222);
case 2:
printf(".mp3 FILE NOT MENTIONED \n");
if(strcmp(argv[1],"-h") == 0)
goto here;
else
{
printf("Format : ./a.out [flag] [file.mp3]\n");
printf("INFO : ./a.out -h for help\n");
exit(111);
}
case 3 ... 5:
goto here;
default:
printf("You have entered more arguments \n");
printf("Format : ./a.out [flag] [file.mp3]\n");
printf("INFO : ./a.out -h for help\n");
exit(333);
}
here: switch(check_operation_status(argv))
{
case mp3_display:
mp3Info.fname = argv[2];
if(read_and_validate_args(argv , &mp3Info) == mp3_success)
{
if(open_file(&mp3Info) == mp3_success)
{
if(start_operation(&mp3Info) == mp3_success)
{
close_file(&mp3Info);
}
else
{
printf("Opeartion failed \n");
exit(1);
}
}
else
{
printf("Opeartion failed \n");
exit(2);
}
}
else
{
printf("Opeartion failed \n");
exit(3);
}
break;
case mp3_edit:
mp3Info.fname = argv[2];
mp3Info.flag = argv[3];
mp3Info.data = argv[4];
if(read_and_validate_args(argv,&mp3Info) == mp3_success)
{
if(open_file(&mp3Info) == mp3_success)
{
if(edit_info(&mp3Info) == mp3_success)
{
close_file(&mp3Info);
}
else
{
printf("operation failed\n");
exit(4);
}
}
else
{
printf("operation failed\n");
exit(5);
}
}
else
{
printf("operation failed\n");
exit(6);
}
break;
case mp3_help:
printf("Format : ./a.out [flag] [file.mp3]\n");
printf("use the following flags :\n-t --------> change title\n-a --------> change artist\n-A --------> change album\n-y --------> change year\n-c --------> change comment\n");
break;
}
}
OperationType check_operation_status(char *argv[])
{
if((strcmp(argv[1],"-v")) == mp3_success)
return mp3_display;
else if(strcmp(argv[1],"-e") == mp3_success)
return mp3_edit;
else if(strcmp(argv[1],"-h") == mp3_success)
return mp3_help;
else
return mp3_unsupported;
}
Status read_and_validate_args(char *argv[] , Mp3Info *mp3Info)
{
char ext[5];
strcpy(ext,strstr(mp3Info->fname,".mp3"));
if(strcmp(ext,".mp3") == mp3_success)
return mp3_success;
else
return mp3_failure;
}
Status open_file(Mp3Info *mp3Info)
{
mp3Info->fptr = fopen(mp3Info->fname,"rw+");
if(mp3Info->fptr == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n", mp3Info->fname);
return mp3_failure;
}
return mp3_success;
}
Status start_operation(Mp3Info *mp3Info)
{
if(version_id(mp3Info) == mp3_success)
{
if(get_details(mp3Info) == mp3_success)
{
print_tag(mp3Info);
return mp3_success;
}
else
{
return mp3_failure;
exit(4);
}
}
else
{
return mp3_failure;
exit(5);
}
}
Status version_id(Mp3Info *mp3Info)
{
fseek(mp3Info->fptr,0,SEEK_SET);
fread(mp3Info->ver_id,3,1,mp3Info->fptr);
mp3Info->ver_id[4] = '\0';
if(strcmp(mp3Info->ver_id,TAG) == mp3_success)
{
printf("Version: %s\n",mp3Info->ver_id);
return mp3_success;
}
else
return mp3_failure;
}
Status get_details(Mp3Info *mp3Info)
{
fseek(mp3Info->fptr,7,SEEK_CUR);
move_ptr(mp3Info);
fread(mp3Info->song_title,mp3Info->buffer-1,1,mp3Info->fptr);
mp3Info->song_title[mp3Info->buffer-1] = '\0';
song(mp3Info);
move_ptr(mp3Info);
fread(mp3Info->artist,mp3Info->buffer-1,1,mp3Info->fptr);
mp3Info->artist[mp3Info->buffer-1] = '\0';
move_ptr(mp3Info);
fread(mp3Info->album,mp3Info->buffer-1,1,mp3Info->fptr);
mp3Info->album[mp3Info->buffer-1] = '\0';
move_ptr(mp3Info);
fread(mp3Info->year,mp3Info->buffer-1,1,mp3Info->fptr);
mp3Info->year[mp3Info->buffer-1] = '\0';
fseek(mp3Info->fptr,41,SEEK_CUR);
move_ptr(mp3Info);
fseek(mp3Info->fptr,4,SEEK_CUR);
fread(mp3Info->comment,mp3Info->buffer-1,1,mp3Info->fptr);
mp3Info->comment[mp3Info->buffer-1] = '\0';
return mp3_success;
}
void move_ptr(Mp3Info *mp3Info)
{
fseek(mp3Info->fptr,4,SEEK_CUR);
fseek(mp3Info->fptr,3,SEEK_CUR);
fread(&mp3Info->buffer,1,1,mp3Info->fptr);
fseek(mp3Info->fptr,3,SEEK_CUR);
}
void song(Mp3Info *mp3Info)
{
printf("Title : %s\n",mp3Info->song_title);
}
void print_tag(Mp3Info *mp3Info)
{
printf("Artist : %s\n",mp3Info->artist);
printf("Album : %s\n",mp3Info->album);
printf("Year : %s\n",mp3Info->year);
printf("Comment: %s\n",mp3Info->comment);
}
uint verify_flag(Mp3Info *mp3Info)
{
if(strcmp(mp3Info->flag,"-t") == mp3_success)
return 1;
else if(strcmp(mp3Info->flag,"-a") == mp3_success)
return 2;
else if(strcmp(mp3Info->flag,"-A") == mp3_success)
return 3;
else if(strcmp(mp3Info->flag,"-y") == mp3_success)
return 4;
else if(strcmp(mp3Info->flag,"-c") == mp3_success)
return 5;
else
return 0;
}
Status edit_info(Mp3Info *mp3Info)
{
rewind(mp3Info->fptr);
if(version_id(mp3Info) == mp3_success)
{
switch(verify_flag(mp3Info))
{
case 1:
fseek(mp3Info->fptr,SONG,SEEK_SET);
move_ptr(mp3Info);
strcpy(mp3Info->song_title,mp3Info->data);
mp3Info->song_title[mp3Info->buffer] = '\0';
fwrite(mp3Info->song_title,mp3Info->buffer,1,mp3Info->fptr);
return mp3_success;
case 2:
fseek(mp3Info->fptr,ARTIST,SEEK_SET);
move_ptr(mp3Info);
strcpy(mp3Info->artist,mp3Info->data);
mp3Info->artist[mp3Info->buffer] = '\0';
fwrite(mp3Info->artist,mp3Info->buffer,1,mp3Info->fptr);
return mp3_success;
case 3:
fseek(mp3Info->fptr,ALBUM,SEEK_SET);
move_ptr(mp3Info);
strcpy(mp3Info->album,mp3Info->data);
mp3Info->album[mp3Info->buffer] = '\0';
fwrite(mp3Info->album,mp3Info->buffer,1,mp3Info->fptr);
return mp3_success;
case 4:
fseek(mp3Info->fptr,YEAR,SEEK_SET);
move_ptr(mp3Info);
strcpy(mp3Info->year,mp3Info->data);
mp3Info->year[mp3Info->buffer] = '\0';
fwrite(mp3Info->year,mp3Info->buffer,1,mp3Info->fptr);
return mp3_success;
case 5:
fseek(mp3Info->fptr,COMMENT,SEEK_SET);
move_ptr(mp3Info);
fseek(mp3Info->fptr,4,SEEK_CUR);
strcpy(mp3Info->comment,mp3Info->data);
mp3Info->comment[mp3Info->buffer] = '\0';
fwrite(mp3Info->comment,mp3Info->buffer,1,mp3Info->fptr);
return mp3_success;
default:
printf("Format : ./a.out [flag] [file.mp3]\n");
printf("INFO : ./a.out -h for help\n");
return mp3_failure;
}
}
else
{
return mp3_failure;
}
}
void close_file(Mp3Info *mp3Info)
{
fclose(mp3Info->fptr);
mp3Info->fptr = NULL;
exit(0);
}