bb39dc6e82a0363797fa4b0a156145cf7268887b
[vuplus_dvbapp] / lib / base / rawfile.h
1 #ifndef __lib_base_rawfile_h
2 #define __lib_base_rawfile_h
3
4 #include <string>
5 #include <lib/base/object.h>
6
7 class iDataSource: public iObject
8 {
9 public:
10         virtual off_t lseek(off_t offset, int whence)=0;
11         virtual ssize_t read(void *buf, size_t count)=0; /* NOTE: you must be able to handle short reads! */
12         virtual off_t length()=0;
13         virtual off_t position()=0;
14         virtual int valid()=0;
15         virtual eSingleLock &getLock()=0;
16         virtual bool is_shared()=0;
17 };
18
19 class iDataSourcePositionRestorer
20 {
21         ePtr<iDataSource> &m_source;
22         off_t m_position;
23 public:
24         iDataSourcePositionRestorer(ePtr<iDataSource> &source)
25                 :m_source(source)
26         {
27                 if (m_source->is_shared())
28                         m_position = m_source->position();
29         }
30         ~iDataSourcePositionRestorer()
31         {
32                 if (m_source->is_shared())
33                         m_source->lseek(m_position, SEEK_SET);
34         }
35 };
36
37 class eRawFile: public iDataSource
38 {
39         DECLARE_REF(eRawFile);
40         eSingleLock m_lock;
41 public:
42         eRawFile();
43         ~eRawFile();
44         int open(const char *filename, int cached = 0);
45         void setfd(int fd);
46         off_t lseek(off_t offset, int whence);
47         int close();
48         ssize_t read(void *buf, size_t count); /* NOTE: you must be able to handle short reads! */
49         off_t length();
50         off_t position();
51         int valid();
52         eSingleLock &getLock();
53         bool is_shared() { return ref.count > 1; }
54 private:
55         int m_fd;     /* for uncached */
56         FILE *m_file; /* for cached */
57         int m_cached;
58         std::string m_basename;
59         off_t m_splitsize, m_totallength, m_current_offset, m_base_offset, m_last_offset;
60         int m_nrfiles;
61         void scan();
62         int m_current_file;
63         int switchOffset(off_t off);
64         FILE *openFileCached(int nr);
65         int openFileUncached(int nr);
66 };
67
68 #endif