Skip to content

Instantly share code, notes, and snippets.

@moriyoshi
Created March 24, 2009 21:44
Show Gist options
  • Save moriyoshi/84381 to your computer and use it in GitHub Desktop.
Save moriyoshi/84381 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <id3tag/id3tag.h>
static const char progname[] = "id3tagfix";
int main(int argc, char **argv)
{
int err = 0;
if (argc <= 1) {
fprintf(stderr, "usage: %s [file]¥n", progname);
return 255;
}
struct id3_file *file = id3_file_open(argv[1], ID3_FILE_MODE_READWRITE);
if (!file) {
fprintf(stderr, "%s: %s cannot be opened", progname, argv[1]);
return 1;
}
struct id3_tag *tag = id3_file_tag(file);
if (!tag) {
fprintf(stderr, "%s: could not read ID3 tags from %s¥n", progname, argv[1]);
err = 1;
goto out;
}
for (unsigned int j = 0, je = tag->nframes; j < je; ++j) {
struct id3_frame *frame = tag->frames[j];
enum id3_field_textencoding enc;
int conversion_needed = 0;
for (unsigned int i = 0, ie = frame->nfields; i < ie; ++i) {
union id3_field *field = id3_frame_field(frame, i);
enum id3_field_type type = id3_field_type(field);
switch (type) {
case ID3_FIELD_TYPE_TEXTENCODING:
{
enc = id3_field_gettextencoding(field);
if (enc == ID3_FIELD_TEXTENCODING_ISO_8859_1) {
conversion_needed = 1;
id3_field_settextencoding(field,
ID3_FIELD_TEXTENCODING_UTF_16);
}
}
break;
case ID3_FIELD_TYPE_STRING:
if (conversion_needed) {
unsigned int len = 0;
id3_ucs4_t const *b = id3_field_getstring(field);
for (id3_ucs4_t const *p = b; *p; ++p)
++len;
id3_utf8_t ub[len + 1];
for (unsigned int i = 0; i <= len; ++i)
ub[i] = b[i];
id3_ucs4_t *ns = id3_utf8_ucs4duplicate(ub);
id3_field_setstring(field, ns);
free(ns);
}
conversion_needed = 0;
break;
case ID3_FIELD_TYPE_STRINGFULL:
if (conversion_needed) {
unsigned int len = 0;
id3_ucs4_t const *b = id3_field_getfullstring(field);
for (id3_ucs4_t const *p = b; *p; ++p)
++len;
id3_utf8_t ub[len + 1];
for (unsigned int i = 0; i <= len; ++i)
ub[i] = b[i];
id3_ucs4_t *ns = id3_utf8_ucs4duplicate(ub);
id3_field_setfullstring(field, ns);
free(ns);
}
conversion_needed = 0;
break;
case ID3_FIELD_TYPE_STRINGLIST:
if (conversion_needed) {
unsigned int ke = id3_field_getnstrings(field);
id3_ucs4_t *items[ke];
for (unsigned int k = 0; k < ke; ++k) {
unsigned int len = 0;
id3_ucs4_t const *b = id3_field_getstrings(field, k);
for (id3_ucs4_t const *p = b; *p; ++p)
++len;
id3_utf8_t ub[len + 1];
for (unsigned int i = 0; i <= len; ++i)
ub[i] = b[i];
items[k] = id3_utf8_ucs4duplicate(ub);
}
id3_field_setstrings(field, ke, items);
for (unsigned int k = 0; k < ke; ++k)
free(items[k]);
}
conversion_needed = 0;
break;
case ID3_FIELD_TYPE_LATIN1:
case ID3_FIELD_TYPE_LATIN1FULL:
case ID3_FIELD_TYPE_LATIN1LIST:
case ID3_FIELD_TYPE_FRAMEID:
case ID3_FIELD_TYPE_DATE:
case ID3_FIELD_TYPE_INT8:
case ID3_FIELD_TYPE_INT16:
case ID3_FIELD_TYPE_INT24:
case ID3_FIELD_TYPE_INT32:
case ID3_FIELD_TYPE_INT32PLUS:
case ID3_FIELD_TYPE_BINARYDATA:
break;
}
}
}
id3_file_update(file);
out:
id3_tag_delete(tag);
id3_file_close(file);
return err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment