move iDataSource to own header file,
authorghost <andreas.monzner@multimedia-labs.de>
Wed, 10 Nov 2010 16:11:47 +0000 (17:11 +0100)
committerghost <andreas.monzner@multimedia-labs.de>
Wed, 10 Nov 2010 16:11:47 +0000 (17:11 +0100)
change read function for easier thread locking (seek must called with a offset/position every time)

lib/base/idatasource.h [new file with mode: 0644]
lib/base/rawfile.cpp
lib/base/rawfile.h

diff --git a/lib/base/idatasource.h b/lib/base/idatasource.h
new file mode 100644 (file)
index 0000000..0daa526
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef __lib_base_idatasource_h
+#define __lib_base_idatasource_h
+
+#include <lib/base/object.h>
+
+class iDataSource: public iObject
+{
+public:
+        /* NOTE: should only be used to get current position or filelength */
+       virtual off_t lseek(off_t offset, int whence)=0;
+       
+       /* NOTE: you must be able to handle short reads! */
+       virtual ssize_t read(off_t offset, void *buf, size_t count)=0; /* NOTE: this is what you in normal case have to use!! */
+
+       virtual off_t length()=0;
+       virtual int valid()=0;
+};
+
+#endif
index 1552203..d4c16d7 100644 (file)
@@ -56,6 +56,13 @@ void eRawFile::setfd(int fd)
 
 off_t eRawFile::lseek(off_t offset, int whence)
 {
 
 off_t eRawFile::lseek(off_t offset, int whence)
 {
+       eSingleLocker l(m_lock);
+       m_current_offset = lseek_internal(offset, whence);
+       return m_current_offset;
+}
+
+off_t eRawFile::lseek_internal(off_t offset, int whence)
+{
 //     eDebug("lseek: %lld, %d", offset, whence);
                /* if there is only one file, use the native lseek - the file could be growing! */
        if (m_nrfiles < 2)
 //     eDebug("lseek: %lld, %d", offset, whence);
                /* if there is only one file, use the native lseek - the file could be growing! */
        if (m_nrfiles < 2)
@@ -64,7 +71,8 @@ off_t eRawFile::lseek(off_t offset, int whence)
                        return ::lseek(m_fd, offset, whence);
                else
                {
                        return ::lseek(m_fd, offset, whence);
                else
                {
-                       ::fseeko(m_file, offset, whence);
+                       if (::fseeko(m_file, offset, whence) < 0)
+                               perror("fseeko");
                        return ::ftello(m_file);
                }
        }
                        return ::ftello(m_file);
                }
        }
@@ -103,11 +111,19 @@ int eRawFile::close()
        }
 }
 
        }
 }
 
-ssize_t eRawFile::read(void *buf, size_t count)
+ssize_t eRawFile::read(off_t offset, void *buf, size_t count)
 {
 {
-//     eDebug("read: %p, %d", buf, count);
+       eSingleLocker l(m_lock);
+
+       if (offset != m_current_offset)
+       {
+               m_current_offset = lseek_internal(offset, SEEK_SET);
+               if (m_current_offset < 0)
+                       return m_current_offset;
+       }
+
        switchOffset(m_current_offset);
        switchOffset(m_current_offset);
-       
+
        if (m_nrfiles >= 2)
        {
                if (m_current_offset + count > m_totallength)
        if (m_nrfiles >= 2)
        {
                if (m_current_offset + count > m_totallength)
@@ -235,20 +251,3 @@ off_t eRawFile::length()
 {
        return m_totallength;
 }
 {
        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 bb39dc6..1720d58 100644 (file)
@@ -2,37 +2,7 @@
 #define __lib_base_rawfile_h
 
 #include <string>
 #define __lib_base_rawfile_h
 
 #include <string>
-#include <lib/base/object.h>
-
-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);
-       }
-};
+#include <lib/base/idatasource.h>
 
 class eRawFile: public iDataSource
 {
 
 class eRawFile: public iDataSource
 {
@@ -43,14 +13,13 @@ public:
        ~eRawFile();
        int open(const char *filename, int cached = 0);
        void setfd(int fd);
        ~eRawFile();
        int open(const char *filename, int cached = 0);
        void setfd(int fd);
-       off_t lseek(off_t offset, int whence);
        int close();
        int close();
-       ssize_t read(void *buf, size_t count); /* NOTE: you must be able to handle short reads! */
+
+       // iDataSource
+       off_t lseek(off_t offset, int whence);
+       ssize_t read(off_t offset, void *buf, size_t count);
        off_t length();
        off_t length();
-       off_t position();
        int valid();
        int valid();
-       eSingleLock &getLock();
-       bool is_shared() { return ref.count > 1; }
 private:
        int m_fd;     /* for uncached */
        FILE *m_file; /* for cached */
 private:
        int m_fd;     /* for uncached */
        FILE *m_file; /* for cached */
@@ -61,6 +30,8 @@ private:
        void scan();
        int m_current_file;
        int switchOffset(off_t off);
        void scan();
        int m_current_file;
        int switchOffset(off_t off);
+
+       off_t lseek_internal(off_t offset, int whence);
        FILE *openFileCached(int nr);
        int openFileUncached(int nr);
 };
        FILE *openFileCached(int nr);
        int openFileUncached(int nr);
 };