* Convert the CD-Text contents to UTF-8 (always assumes ISO-8859-1 for now).

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38848 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2010-09-29 10:33:04 +00:00
parent 8be096e0f7
commit 77b833b87e

View File

@ -49,6 +49,37 @@ copy_string(const char *string)
}
static char *
to_utf8(const char* string)
{
char buffer[256];
size_t out = 0;
// TODO: assume ISO-8859-1 character set for now
while (uint32 c = (uint8)string[0]) {
if (out == sizeof(buffer) - 1)
break;
if (c < 0x80)
buffer[out++] = c;
else if (c < 0x800) {
buffer[out++] = 0xc0 | (c >> 6);
buffer[out++] = 0x80 | (c & 0x3f);
}
string++;
}
buffer[out++] = '\0';
char *copy = (char *)malloc(out);
if (copy == NULL)
return NULL;
memcpy(copy, buffer, out);
return copy;
}
static bool
is_garbage(char c)
{
@ -317,6 +348,7 @@ parse_pack_data(cdtext_pack_data *&pack, uint32 &packLeft,
id = pack->id;
track = pack->track;
buffer[0] = '\0';
length = 0;
@ -375,7 +407,6 @@ parse_pack_data(cdtext_pack_data *&pack, uint32 &packLeft,
}
}
// TODO: convert text to UTF-8
return true;
}
@ -534,6 +565,8 @@ read_cdtext(int fd, struct cdtext &cdtext)
uint8 id = 0;
char text[256];
// TODO: determine encoding!
while (true) {
size_t length = sizeof(text);
@ -545,10 +578,10 @@ read_cdtext(int fd, struct cdtext &cdtext)
case kTrackID:
if (track == 0) {
if (cdtext.album == NULL)
cdtext.album = copy_string(text);
cdtext.album = to_utf8(text);
} else if (track <= kMaxTracks) {
if (cdtext.titles[track - 1] == NULL)
cdtext.titles[track - 1] = copy_string(text);
cdtext.titles[track - 1] = to_utf8(text);
if (track > cdtext.track_count)
cdtext.track_count = track;
}
@ -557,10 +590,10 @@ read_cdtext(int fd, struct cdtext &cdtext)
case kArtistID:
if (track == 0) {
if (cdtext.artist == NULL)
cdtext.artist = copy_string(text);
cdtext.artist = to_utf8(text);
} else if (track <= kMaxTracks) {
if (cdtext.artists[track - 1] == NULL)
cdtext.artists[track - 1] = copy_string(text);
cdtext.artists[track - 1] = to_utf8(text);
}
break;
@ -573,7 +606,7 @@ read_cdtext(int fd, struct cdtext &cdtext)
free(buffer);
if (cdtext.artist == NULL || cdtext.album == NULL)
if (cdtext.artist == NULL && cdtext.album == NULL)
return B_ERROR;
for (int i = 0; i < cdtext.track_count; i++) {