iTsSource: Remove lseek()
authorMike Looijmans <milo-software@users.sourceforge.net>
Sat, 27 Oct 2012 09:23:07 +0000 (11:23 +0200)
committerMike Looijmans <milo-software@users.sourceforge.net>
Sat, 27 Oct 2012 09:26:31 +0000 (11:26 +0200)
lseek was only used to get the length, and by only one object ever.
Change the interface, use length() to return the length and make
the length function have no side-effects.

lib/base/httpstream.cpp
lib/base/httpstream.h
lib/base/itssource.h
lib/base/rawfile.cpp
lib/base/rawfile.h
lib/dvb/tstools.cpp
lib/service/servicem2ts.cpp

index c658fef..8674e75 100644 (file)
@@ -178,11 +178,6 @@ int eHttpStream::open(const char *url)
        return -1;
 }
 
-off_t eHttpStream::lseek(off_t offset, int whence)
-{
-       return (off_t)-1;
-}
-
 int eHttpStream::close()
 {
        int retval = -1;
index 3a819c7..4993e82 100644 (file)
@@ -16,7 +16,6 @@ class eHttpStream: public iTsSource, public eSocketBase, public Object
        int openUrl(const std::string &url, std::string &newurl);
 
        /* iTsSource */
-       off_t lseek(off_t offset, int whence);
        ssize_t read(off_t offset, void *buf, size_t count);
        off_t length();
        off_t offset();
index a372238..79e9673 100644 (file)
@@ -10,12 +10,11 @@ protected:
 
 public:
        iTsSource(int packetsize = 188) : packetSize(packetsize) {}
-        /* 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!! */
 
+       /* Fetch the length, without side-effects like seeking */
        virtual off_t length()=0;
        virtual int valid()=0;
        virtual off_t offset() = 0;
index bcccefa..a3bd204 100644 (file)
@@ -53,43 +53,24 @@ int eRawFile::openCached(const char *filename)
        return 0;
 }
 
-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)
+off_t eRawFile::lseek_internal(off_t offset)
 {
 //     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)
        {
                if (!m_cached)
-                       return ::lseek(m_fd, offset, whence);
+               {
+                       return ::lseek(m_fd, offset, SEEK_SET);
+               }
                else
                {
-                       if (::fseeko(m_file, offset, whence) < 0)
+                       if (::fseeko(m_file, offset, SEEK_SET) < 0)
                                perror("fseeko");
                        return ::ftello(m_file);
                }
        }
-       switch (whence)
-       {
-       case SEEK_SET:
-               m_current_offset = offset;
-               break;
-       case SEEK_CUR:
-               m_current_offset += offset;
-               break;
-       case SEEK_END:
-               m_current_offset = m_totallength + offset;
-               break;
-       }
-
-       if (m_current_offset < 0)
-               m_current_offset = 0;
+       m_current_offset = offset;
        return m_current_offset;
 }
 
@@ -124,7 +105,7 @@ ssize_t eRawFile::read(off_t offset, void *buf, size_t count)
 
        if (offset != m_current_offset)
        {
-               m_current_offset = lseek_internal(offset, SEEK_SET);
+               m_current_offset = lseek_internal(offset);
                if (m_current_offset < 0)
                        return m_current_offset;
        }
@@ -267,7 +248,18 @@ int eRawFile::openFileUncached(int nr)
 
 off_t eRawFile::length()
 {
-       return m_totallength;
+       if (m_nrfiles >= 2)
+       {
+               return m_totallength;
+       }
+       else
+       {
+               struct stat st;
+               int fd = m_cached ? fileno(m_file) : m_fd;
+               if (::fstat(fd, &st) < 0)
+                       return -1;
+               return st.st_size;
+       }
 }
 
 off_t eRawFile::offset()
index 8b38131..7d5bb78 100644 (file)
@@ -15,7 +15,6 @@ public:
        int openCached(const char *filename);
 
        // iTsSource
-       off_t lseek(off_t offset, int whence);
        ssize_t read(off_t offset, void *buf, size_t count);
        off_t length();
        off_t offset();
@@ -33,7 +32,7 @@ private:
        int close();
        void scan();
        int switchOffset(off_t off);
-       off_t lseek_internal(off_t offset, int whence);
+       off_t lseek_internal(off_t offset);
        FILE *openFileCached(int nr);
        int openFileUncached(int nr);
 };
index acc4418..73ecbc4 100644 (file)
@@ -450,7 +450,7 @@ void eDVBTSTools::calcEnd()
        // If there's a structure file, the calculation is much smarter, so we can try more often
        off_t threshold = m_streaminfo.hasStructure() ? 100*1024 : 1024*1024;
 
-       off_t end = m_source->lseek(0, SEEK_END);
+       off_t end = m_source->length();
        if (llabs(end - m_last_filelength) > threshold)
        {
                m_last_filelength = end;
index 1a8824f..6b8667c 100644 (file)
@@ -16,7 +16,6 @@ public:
        ~eM2TSFile();
 
        // iTsSource
-       off_t lseek(off_t offset, int whence);
        ssize_t read(off_t offset, void *buf, size_t count);
        off_t length();
        off_t offset();
@@ -186,18 +185,6 @@ eM2TSFile::~eM2TSFile()
                ::close(m_fd);
 }
 
-off_t eM2TSFile::lseek(off_t offset, int whence)
-{
-       eSingleLocker l(m_lock);
-
-       offset = (offset % 188) + (offset * 192) / 188;
-
-       if (offset != m_current_offset)
-               m_current_offset = lseek_internal(offset, whence);
-
-       return m_current_offset;
-}
-
 off_t eM2TSFile::lseek_internal(off_t offset, int whence)
 {
        off_t ret;