add virtual baseclass for data sources (iDataSource)
authorghost <andreas.monzner@multimedia-labs.de>
Tue, 9 Nov 2010 22:21:28 +0000 (23:21 +0100)
committerghost <andreas.monzner@multimedia-labs.de>
Tue, 9 Nov 2010 22:21:28 +0000 (23:21 +0100)
let eRawFile inherit from iDataSource

lib/base/rawfile.cpp
lib/base/rawfile.h

index c7e11feb308632d876b706105361ac3d81fd3081..1552203ae358952b9b324eb280b5440f5bcae4e3 100644 (file)
@@ -4,7 +4,10 @@
 #include <lib/base/rawfile.h>
 #include <lib/base/eerror.h>
 
+DEFINE_REF(eRawFile);
+
 eRawFile::eRawFile()
+       :m_lock(true)
 {
        m_fd = -1;
        m_file = 0;
@@ -232,3 +235,20 @@ off_t eRawFile::length()
 {
        return m_totallength;
 }
+
+off_t eRawFile::position()
+{
+       if (m_nrfiles < 2)
+       {
+               if (!m_cached)
+                       return ::lseek(m_fd, 0, SEEK_CUR);
+               else
+                       return ::fseeko(m_file, 0, SEEK_CUR);
+       }
+       return m_current_offset;
+}
+
+eSingleLock &eRawFile::getLock()
+{
+       return m_lock;
+}
index a1c73d6ad6acae3001b4822a97fcbf207559f1c0..bb39dc6e82a0363797fa4b0a156145cf7268887b 100644 (file)
@@ -2,24 +2,58 @@
 #define __lib_base_rawfile_h
 
 #include <string>
+#include <lib/base/object.h>
 
-class eRawFile
+class iDataSource: public iObject
 {
+public:
+       virtual off_t lseek(off_t offset, int whence)=0;
+       virtual ssize_t read(void *buf, size_t count)=0; /* NOTE: you must be able to handle short reads! */
+       virtual off_t length()=0;
+       virtual off_t position()=0;
+       virtual int valid()=0;
+       virtual eSingleLock &getLock()=0;
+       virtual bool is_shared()=0;
+};
+
+class iDataSourcePositionRestorer
+{
+       ePtr<iDataSource> &m_source;
+       off_t m_position;
+public:
+       iDataSourcePositionRestorer(ePtr<iDataSource> &source)
+               :m_source(source)
+       {
+               if (m_source->is_shared())
+                       m_position = m_source->position();
+       }
+       ~iDataSourcePositionRestorer()
+       {
+               if (m_source->is_shared())
+                       m_source->lseek(m_position, SEEK_SET);
+       }
+};
+
+class eRawFile: public iDataSource
+{
+       DECLARE_REF(eRawFile);
+       eSingleLock m_lock;
 public:
        eRawFile();
        ~eRawFile();
-       
        int open(const char *filename, int cached = 0);
        void setfd(int fd);
        off_t lseek(off_t offset, int whence);
        int close();
        ssize_t read(void *buf, size_t count); /* NOTE: you must be able to handle short reads! */
        off_t length();
+       off_t position();
        int valid();
+       eSingleLock &getLock();
+       bool is_shared() { return ref.count > 1; }
 private:
        int m_fd;     /* for uncached */
        FILE *m_file; /* for cached */
-       
        int m_cached;
        std::string m_basename;
        off_t m_splitsize, m_totallength, m_current_offset, m_base_offset, m_last_offset;