replace openFile() by two functions openFileCached() and openFileUncached(), to avoid...
[enigma2.git] / lib / base / rawfile.cpp
index d55f925..e444ba9 100644 (file)
@@ -59,7 +59,10 @@ off_t eRawFile::lseek(off_t offset, int whence)
                if (!m_cached)
                        return ::lseek(m_fd, offset, whence);
                else
-                       return ::fseeko(m_file, offset, whence);
+               {
+                       ::fseeko(m_file, offset, whence);
+                       return ::ftello(m_file);
+               }
        }
        switch (whence)
        {
@@ -123,7 +126,10 @@ ssize_t eRawFile::read(void *buf, size_t count)
 
 int eRawFile::valid()
 {
-       return m_fd != -1;
+       if (!m_cached)
+               return m_fd != -1;
+       else
+               return !!m_file;
 }
 
 void eRawFile::scan()
@@ -134,7 +140,7 @@ void eRawFile::scan()
        {
                if (!m_cached)
                {
-                       int f = openFile(m_nrfiles);
+                       int f = openFileUncached(m_nrfiles);
                        if (f < 0)
                                break;
                        if (!m_nrfiles)
@@ -143,12 +149,13 @@ void eRawFile::scan()
                        ::close(f);
                } else
                {
-                       FILE *f = (FILE*)openFile(m_nrfiles);
+                       FILE *f = openFileCached(m_nrfiles);
                        if (!f)
                                break;
+                       ::fseeko(f, 0, SEEK_END);
                        if (!m_nrfiles)
-                               m_splitsize = ::fseeko(f, 0, SEEK_END);
-                       m_totallength += ::fseeko(f, 0, SEEK_END);
+                               m_splitsize = ::ftello(f);
+                       m_totallength += ::ftello(f);
                        ::fclose(f);
                }
                
@@ -169,9 +176,9 @@ int eRawFile::switchOffset(off_t off)
 //                     eDebug("-> %d", filenr);
                        close();
                        if (!m_cached)
-                               m_fd = openFile(filenr);
+                               m_fd = openFileUncached(filenr);
                        else
-                               m_file = (FILE*)openFile(filenr);
+                               m_file = openFileCached(filenr);
                        m_last_offset = m_base_offset = m_splitsize * filenr;
                        m_current_file = filenr;
                }
@@ -183,7 +190,10 @@ int eRawFile::switchOffset(off_t off)
                if (!m_cached)
                        m_last_offset = ::lseek(m_fd, off - m_base_offset, SEEK_SET) + m_base_offset;
                else
-                       m_last_offset = ::fseeko(m_file, off - m_base_offset, SEEK_SET) + m_base_offset;
+               {
+                       ::fseeko(m_file, off - m_base_offset, SEEK_SET);
+                       m_last_offset = ::ftello(m_file) + m_base_offset;
+               }
                return m_last_offset;
        } else
        {
@@ -191,7 +201,8 @@ int eRawFile::switchOffset(off_t off)
        }
 }
 
-int eRawFile::openFile(int nr)
+/* m_cached */
+FILE *eRawFile::openFileCached(int nr)
 {
        std::string filename = m_basename;
        if (nr)
@@ -200,8 +211,18 @@ int eRawFile::openFile(int nr)
                snprintf(suffix, 5, ".%03d", nr);
                filename += suffix;
        }
-       if (!m_cached)
-               return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
-       else
-               return (int)::fopen64(filename.c_str(), "rb");
+       return ::fopen64(filename.c_str(), "rb");
+}
+
+/* !m_cached */
+int eRawFile::openFileUncached(int nr)
+{
+       std::string filename = m_basename;
+       if (nr)
+       {
+               char suffix[5];
+               snprintf(suffix, 5, ".%03d", nr);
+               filename += suffix;
+       }
+       return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
 }