bug encoding multibyte?

Ray whatdoineed2do at yahoo.co.uk
Fri Feb 2 01:12:39 CET 2007


hi,

i've been having problem storing multibyte strings in
mp3 tags -- i verify the input going in looks valid,
but when i read the tag out of the mp3 and examine the
data, it looks like garbage.

can someone tell me if this is a mistake on my part?

thanks
ray

 
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <libgen.h>

#include <iostream>

#include <mpegfile.h>
#include <id3v2tag.h>

using namespace std;


int main()
{
    const TagLib::String::Type  enc_ =
TagLib::String::UTF16BE;
    const char*  data_ = "foo&#21894;bar";

    const char*  l;
    if ( (l = setlocale(LC_ALL, "en_US.UTF-8")) ==
NULL) {
        cout << "locale set" << endl;
    }

    const size_t  n = strlen(data_)*sizeof(wchar_t);
    wchar_t*      w = new wchar_t[n];
    memset(w, 0, n);
    const size_t  len = mbstowcs(w, data_, n);
    cout << "input=" << n << " bytes, len=" << len <<
endl;

    char  p[MB_CUR_MAX+1];
    for (int c=0; c<len; c++)
    {
	/* re-convert from wide character to multibyte
character */
	int x = wctomb(p, w[c]);

	/* One multibyte character may be two or more bytes.
	 * Thus "%s" is used instead of "%c".
	 */
	if (x>0) p[x]=0;
	cout << "multibyte char[" << c << "] = '" << p << "' 
(" << x << " bytes)" << endl;
    }

    const TagLib::String  s(w, enc_);
    cout << "encoded input as [" << enc_ << "]  '" <<
data_ << "', unicode='" << s.toCString(true) << "',
latin1='" << s.toCString() << "'" << endl;
    delete []  w;


    if (access("test.mp3", R_OK | W_OK) != 0) {
       cerr << "test file unavailable for write" <<
endl;
       return 1;
    }

    TagLib::MPEG::File  f("test.mp3");
    TagLib::ID3v2::Tag*  tag = f.ID3v2Tag(true);
    tag->setArtist(s);

    if (!f.save()) {
        cerr << "unable to save tag to mp3!" << endl;
	return 2;
    }
    cout << "saved tag to file, re-opening" << endl;


    TagLib::MPEG::File  f1("test.mp3");
    if ( (tag = f1.ID3v2Tag(false)) == NULL) {
        cerr << "no tag on mp3 even tho we just save
it out?" << endl;
	return 3;
    }

    cout << "artist='";
    const TagLib::String  artist = tag->artist();
    if (artist  == TagLib::String::null) {
         cout << "<null>";
    }
    else {
        cout << artist.toCString(true);
    }
    cout << "'" << endl;
    return 0;
}



		
___________________________________________________________ 
What kind of emailer are you? Find out today - get a free analysis of your email personality. Take the quiz at the Yahoo! Mail Championship. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk 


More information about the taglib-devel mailing list