[haiku-commits] r38848 - haiku/trunk/src/add-ons/kernel/file_systems/cdda

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Wed, 29 Sep 2010 12:33:04 +0200 (CEST)

Author: axeld
Date: 2010-09-29 12:33:04 +0200 (Wed, 29 Sep 2010)
New Revision: 38848
Changeset: http://dev.haiku-os.org/changeset/38848

Modified:
   haiku/trunk/src/add-ons/kernel/file_systems/cdda/cdda.cpp
Log:
* Convert the CD-Text contents to UTF-8 (always assumes ISO-8859-1 for now).


Modified: haiku/trunk/src/add-ons/kernel/file_systems/cdda/cdda.cpp
===================================================================
--- haiku/trunk/src/add-ons/kernel/file_systems/cdda/cdda.cpp   2010-09-29 
10:07:14 UTC (rev 38847)
+++ haiku/trunk/src/add-ons/kernel/file_systems/cdda/cdda.cpp   2010-09-29 
10:33:04 UTC (rev 38848)
@@ -49,6 +49,37 @@
 }
 
 
+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 @@
 
        id = pack->id;
        track = pack->track;
+
        buffer[0] = '\0';
        length = 0;
 
@@ -375,7 +407,6 @@
                }
        }
 
-       // TODO: convert text to UTF-8
        return true;
 }
 
@@ -534,6 +565,8 @@
        uint8 id = 0;
        char text[256];
 
+       // TODO: determine encoding!
+
        while (true) {
                size_t length = sizeof(text);
 
@@ -545,10 +578,10 @@
                        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 @@
                        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 @@
 
        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++) {


Other related posts:

  • » [haiku-commits] r38848 - haiku/trunk/src/add-ons/kernel/file_systems/cdda - axeld