]> granicus.if.org Git - taglib/commitdiff
FileRef: Allow an IOStream to be used
authorHugo Beauzée-Luyssen <hugo@beauzee.fr>
Tue, 28 Apr 2015 09:56:07 +0000 (11:56 +0200)
committerHugo Beauzée-Luyssen <hugo@beauzee.fr>
Tue, 15 Sep 2015 13:01:40 +0000 (15:01 +0200)
taglib/fileref.cpp
taglib/fileref.h
tests/test_fileref.cpp

index 2b5d5f2352558b0e1efd8563012d1b1ad7916715..c2cd31d1cde54e30f6585e5ea6ad7de325b71378 100644 (file)
 
 using namespace TagLib;
 
+template <typename T>
+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;
   }
 
-  // Ok, this is really dumb for now, but it works for testing.
-
-  String ext;
-  {
+  return createInternal(fileName,
 #ifdef _WIN32
-
-    String s = fileName.toString();
-
+  fileName.toString()
 #else
+  fileName
+#endif
+  , readAudioProperties, audioPropertiesStyle);
+}
 
-    String s = fileName;
-
- #endif
+template <typename T>
+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.
 
-    const int pos = s.rfind(".");
-    if(pos != -1)
-      ext = s.substr(pos + 1).upper();
-  }
+  String ext;
+  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")
index 1e965229e13c33cc4fe2a3a6233db9502fce0cb7..abeb0af9c9f0632a8a36cb39f5d92c35516e0ef9 100644 (file)
@@ -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);
index f13eafaaf5744ebbc1bf00257a3806da2b91e5a9..a51b868c33026ef87fb8d86f640ac230baf7aeba 100644 (file)
@@ -6,6 +6,7 @@
 #include <vorbisfile.h>
 #include <cppunit/extensions/HelperMacros.h>
 #include "utils.h"
+#include <tfilestream.h>
 
 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()