forked from larsbs/id3v2lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_utils.c
169 lines (139 loc) · 3.55 KB
/
test_utils.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
/*
* This file is part of id3v2lib library
*
* Copyright (c) Lars Ruiz
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "id3v2lib.h"
#include "test_utils.h"
bool has_bom(const char* string)
{
if (string == NULL) return false;
return memcmp("\xFF\xFE", string, 2) == 0 || memcmp("\xFE\xFF", string, 2) == 0;
}
uint16_t* char_to_utf16(const char* string, int size)
{
size = size < 0 ? ID3v2_strlent(string) : size;
uint16_t* result = (uint16_t*) malloc(size * sizeof(uint16_t));
memcpy(result, string, size);
return result;
}
char* to_unicode(char* string)
{
if (has_bom(string))
{
return string;
}
int size = (strlen(string) * 2) + 4; // +4 to take the BOM and the termination into account;
char* result = (char*) malloc(size * sizeof(char));
result[0] = 0xFF;
result[1] = 0xFE;
for (int i = 1; i <= strlen(string); i++)
{
result[i * 2] = string[i - 1];
result[(i * 2) + 1] = 0x00;
}
result[size - 2] = 0x00;
result[size - 1] = 0x00;
return result;
}
void println_utf16(uint16_t* string, int size)
{
int i = 1; // Skip the BOM
// If size < 0, then keep iterating until we find the termination marker,
// otherwise, use size as a safe limit.
while (i < size || size < 0)
{
// break if we reach the termination marker '0000'
if (string[i] == 0x0000)
{
break;
}
printf("%lc", string[i]);
i++;
}
printf("\n");
}
void print_text_frame(ID3v2_TextFrame* frame)
{
if (frame == NULL)
{
printf("None\n");
return;
}
print_text_frame_text(frame->data->text, frame->data->size);
}
void print_text_frame_text(const char* text, const int size)
{
if (text == NULL)
{
printf("None\n");
}
else if (has_bom(text))
{
uint16_t* text_utf = char_to_utf16(text, size);
println_utf16(text_utf, size);
}
else
{
printf("%s\n", text);
}
}
void print_comment_frame(ID3v2_CommentFrame* frame)
{
if (frame == NULL)
{
printf("None\n");
return;
}
print_text_frame_text(frame->data->comment, frame->data->size);
}
void print_comment_frames(ID3v2_FrameList* frames)
{
if (frames == NULL)
{
printf("None\n");
return;
}
while (frames != NULL)
{
ID3v2_CommentFrame* comment = (ID3v2_CommentFrame*) frames->frame;
print_comment_frame(comment);
frames = frames->next;
}
}
void save_apic_frame(ID3v2_ApicFrame* frame, char* dir_path)
{
if (frame == NULL)
{
printf("None\n");
return;
}
char* extension = strcmp(frame->data->mime_type, ID3v2_MIME_TYPE_PNG) == 0 ? ".png" : ".jpeg";
int file_name_size = strlen(dir_path) + strlen("/album_cover") + strlen(extension);
char* file_name = (char*) malloc((file_name_size) * sizeof(char));
strcpy(file_name, dir_path);
strcat(file_name, "/album_cover");
strcat(file_name, extension);
FILE* file = fopen(file_name, "wb");
fwrite(frame->data->data, 1, frame->data->picture_size, file);
fclose(file);
printf("Saved in %s", file_name);
}
void clone_file(const char* src, const char* dest)
{
FILE* src_fp = fopen(src, "rb");
FILE* dest_fp = fopen(dest, "wb");
int c = 0;
while ((c = getc(src_fp)) != EOF)
{
putc(c, dest_fp);
}
fclose(src_fp);
fclose(dest_fp);
}