#include #include #include extern "C" { #include #include static PyObject *taglib_error; static PyObject * taglib_apic(const char *fn, PyObject *list) { TagLib::MPEG::File *f; TagLib::ID3v2::Tag *t; PyObject *tuple, *description, *description_utf8; int i, type, width, height, depth, colors, len; const char *mime_type, *description_utf8_s, *image; TagLib::ByteVector *bv; TagLib::ID3v2::AttachedPictureFrame *p; /* GAH! Opening the file is entangled with allocating the object; * if this fails, am i out of memory, or is there a problem with * the file? Hate C++... */ f = new TagLib::MPEG::File (fn); if (f == 0) { return PyErr_NoMemory(); } t = f->ID3v2Tag(true); if (t == 0) { PyErr_SetString(taglib_error, "taglib failed to return ID3v2"); return 0; } for (i = 0; i < PyList_Size(list); i++) { tuple = PyList_GetItem(list, i); if (!PyArg_ParseTuple(tuple, "isUiiiis#", &type, &mime_type, &description, &width, &height, &depth, &colors, &image, &len)) { return 0; } description_utf8 = PyUnicode_AsEncodedString(description, "utf8", "strict"); if (description_utf8 == 0) { return 0; } description_utf8_s = PyString_AsString(description_utf8); if (description_utf8_s == 0) { return 0; } bv = new TagLib::ByteVector (image, len); if (bv == 0) { return PyErr_NoMemory(); } p = new TagLib::ID3v2::AttachedPictureFrame; if (p == 0) { return PyErr_NoMemory(); } /* Um, look at all these void-returning functions. I'm sure * they can fail, i just have no way to detect it... */ p->setType((TagLib::ID3v2::AttachedPictureFrame::Type)type); p->setMimeType(mime_type); p->setDescription(description_utf8_s); p->setTextEncoding(TagLib::String::UTF8); // taglib doesn't support width, height, depth, colors. p->setPicture(*bv); // another void-returning function that i bet fails t->addFrame(p); } errno = 0; if (!f->save()) { if (errno != 0) { PyErr_SetFromErrnoWithFilename(PyExc_IOError, (char *)fn); } else { PyErr_SetString(taglib_error, "taglib failed to save"); } return 0; } Py_INCREF(Py_None); return Py_None; } static PyObject * add_apic_frame_to_mp3(PyObject *self, PyObject *args) { const char *fn; PyObject *list; if (!PyArg_ParseTuple(args, "sO", &fn, &list)) { return 0; } return taglib_apic(fn, list); } static PyMethodDef methods[] = { {"add_apic_frame_to_mp3", add_apic_frame_to_mp3, METH_VARARGS, "document me"}, {0, 0, 0, 0} }; PyMODINIT_FUNC inittaglib(void) { PyObject *m = Py_InitModule("taglib", methods); if (m == NULL) { return; } taglib_error = PyErr_NewException("flac_archive.taglib.error", 0, 0); if (taglib_error == 0) { return; } PyModule_AddObject(m, "error", taglib_error); } }