From e750cb491d4b4de609f6339d6cceb11ab0d4bfa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Beauz=C3=A9e-Luyssen?= Date: Tue, 28 Apr 2015 11:56:07 +0200 Subject: [PATCH] FileRef: Allow an IOStream to be used --- taglib/fileref.cpp | 50 ++++++++++++++++++++++++++++-------------- taglib/fileref.h | 16 +++++++++++++- tests/test_fileref.cpp | 12 ++++++++++ 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/taglib/fileref.cpp b/taglib/fileref.cpp index 2b5d5f23..c2cd31d1 100644 --- a/taglib/fileref.cpp +++ b/taglib/fileref.cpp @@ -54,6 +54,10 @@ using namespace TagLib; +template +static File* createInternal(T arg, const String& filename, bool readAudioProperties, + AudioProperties::ReadStyle audioPropertiesStyle); + class FileRef::FileRefPrivate : public RefCounter { public: @@ -83,6 +87,17 @@ FileRef::FileRef(FileName fileName, bool readAudioProperties, d = new FileRefPrivate(create(fileName, readAudioProperties, audioPropertiesStyle)); } +FileRef::FileRef(IOStream* stream, bool readAudioProperties, AudioProperties::ReadStyle audioPropertiesStyle) +{ + d = new FileRefPrivate(createInternal(stream, +#ifdef _WIN32 + stream->name().toString() +#else + stream->name() +#endif + , readAudioProperties, audioPropertiesStyle)); +} + FileRef::FileRef(File *file) { d = new FileRefPrivate(file); @@ -213,24 +228,25 @@ File *FileRef::create(FileName fileName, bool readAudioProperties, return file; } + return createInternal(fileName, +#ifdef _WIN32 + fileName.toString() +#else + fileName +#endif + , readAudioProperties, audioPropertiesStyle); +} + +template +static File* createInternal(T fileName, const String& s, bool readAudioProperties, + AudioProperties::ReadStyle audioPropertiesStyle) +{ // Ok, this is really dumb for now, but it works for testing. String ext; - { -#ifdef _WIN32 - - String s = fileName.toString(); - -#else - - String s = fileName; - - #endif - - const int pos = s.rfind("."); - if(pos != -1) - ext = s.substr(pos + 1).upper(); - } + const int pos = s.rfind("."); + if(pos != -1) + ext = s.substr(pos + 1).upper(); // If this list is updated, the method defaultFileExtensions() should also be // updated. However at some point that list should be created at the same time @@ -238,7 +254,7 @@ File *FileRef::create(FileName fileName, bool readAudioProperties, if(!ext.isEmpty()) { if(ext == "MP3") - return new MPEG::File(fileName, readAudioProperties, audioPropertiesStyle); + return new MPEG::File(fileName, ID3v2::FrameFactory::instance(), readAudioProperties, audioPropertiesStyle); if(ext == "OGG") return new Ogg::Vorbis::File(fileName, readAudioProperties, audioPropertiesStyle); if(ext == "OGA") { @@ -250,7 +266,7 @@ File *FileRef::create(FileName fileName, bool readAudioProperties, return new Ogg::Vorbis::File(fileName, readAudioProperties, audioPropertiesStyle); } if(ext == "FLAC") - return new FLAC::File(fileName, readAudioProperties, audioPropertiesStyle); + return new FLAC::File(fileName, ID3v2::FrameFactory::instance(), readAudioProperties, audioPropertiesStyle); if(ext == "MPC") return new MPC::File(fileName, readAudioProperties, audioPropertiesStyle); if(ext == "WV") diff --git a/taglib/fileref.h b/taglib/fileref.h index 1e965229..abeb0af9 100644 --- a/taglib/fileref.h +++ b/taglib/fileref.h @@ -128,7 +128,21 @@ namespace TagLib { audioPropertiesStyle = AudioProperties::Average); /*! - * Construct a FileRef using \a file. The FileRef now takes ownership of the + * Construct a FileRef from an opened \a IOStream. If \a readAudioProperties is true then + * the audio properties will be read using \a audioPropertiesStyle. If + * \a readAudioProperties is false then \a audioPropertiesStyle will be + * ignored. + * + * Also see the note in the class documentation about why you may not want to + * use this method in your application. + */ + explicit FileRef(IOStream* stream, + bool readAudioProperties = true, + AudioProperties::ReadStyle + audioPropertiesStyle = AudioProperties::Average); + + /*! + * Contruct a FileRef using \a file. The FileRef now takes ownership of the * pointer and will delete the File when it passes out of scope. */ explicit FileRef(File *file); diff --git a/tests/test_fileref.cpp b/tests/test_fileref.cpp index f13eafaa..a51b868c 100644 --- a/tests/test_fileref.cpp +++ b/tests/test_fileref.cpp @@ -6,6 +6,7 @@ #include #include #include "utils.h" +#include using namespace std; using namespace TagLib; @@ -74,6 +75,17 @@ public: CPPUNIT_ASSERT_EQUAL(f->tag()->track(), TagLib::uint(7)); CPPUNIT_ASSERT_EQUAL(f->tag()->year(), TagLib::uint(2080)); delete f; + + FileStream fs(newname.c_str()); + f = new FileRef(&fs); + CPPUNIT_ASSERT(!f->isNull()); + CPPUNIT_ASSERT_EQUAL(f->tag()->artist(), String("ttest artist")); + CPPUNIT_ASSERT_EQUAL(f->tag()->title(), String("ytest title")); + CPPUNIT_ASSERT_EQUAL(f->tag()->genre(), String("uTest!")); + CPPUNIT_ASSERT_EQUAL(f->tag()->album(), String("ialbummmm")); + CPPUNIT_ASSERT_EQUAL(f->tag()->track(), TagLib::uint(7)); + CPPUNIT_ASSERT_EQUAL(f->tag()->year(), TagLib::uint(2080)); + delete f; } void testMusepack()