comparison src/Input/console/abstract_file.h @ 0:13389e613d67 trunk

[svn] - initial import of audacious-plugins tree (lots to do)
author nenolod
date Mon, 18 Sep 2006 01:11:49 -0700
parents
children 088092a52fea
comparison
equal deleted inserted replaced
-1:000000000000 0:13389e613d67
1
2 // Abstract file access interfaces
3
4 #ifndef ABSTRACT_FILE_H
5 #define ABSTRACT_FILE_H
6
7 #include <stdio.h>
8 #include "libaudacious/vfs.h"
9
10 // Supports reading and finding out how many bytes are remaining
11 class Data_Reader {
12 public:
13 Data_Reader() { }
14 virtual ~Data_Reader() { }
15
16 // NULL on success, otherwise error string
17 typedef const char* error_t;
18
19 // Read at most 'n' bytes. Return number of bytes read, zero or negative
20 // if error.
21 virtual long read_avail( void*, long n ) = 0;
22
23 // Read exactly 'n' bytes (error if fewer are available).
24 virtual error_t read( void*, long );
25
26 // Number of bytes remaining
27 virtual long remain() const = 0;
28
29 // Skip forwards by 'n' bytes.
30 virtual error_t skip( long n );
31
32 // to do: bytes remaining = LONG_MAX when unknown?
33
34 private:
35 // noncopyable
36 Data_Reader( const Data_Reader& );
37 Data_Reader& operator = ( const Data_Reader& );
38 };
39
40 // Adds seeking operations
41 class File_Reader : public Data_Reader {
42 public:
43 // Size of file
44 virtual long size() const = 0;
45
46 // Current position in file
47 virtual long tell() const = 0;
48
49 // Change position in file
50 virtual error_t seek( long ) = 0;
51
52 virtual long remain() const;
53
54 error_t skip( long n );
55 };
56
57 // Limit access to a subset of data
58 class Subset_Reader : public Data_Reader {
59 Data_Reader* in;
60 long remain_;
61 public:
62 Subset_Reader( Data_Reader*, long size );
63 long remain() const;
64 long read_avail( void*, long );
65 };
66
67 // Treat range of memory as a file
68 class Mem_File_Reader : public File_Reader {
69 const char* const begin;
70 long pos;
71 const long size_;
72 public:
73 Mem_File_Reader( const void*, long size );
74
75 long size() const;
76 long read_avail( void*, long );
77
78 long tell() const;
79 error_t seek( long );
80 };
81
82 // File reader based on C VFSFile*
83 class Std_File_Reader : public File_Reader {
84 VFSFile* file_;
85 protected:
86 void reset( VFSFile* f ) { file_ = f; }
87 //VFSFile* owned_file;
88 public:
89 Std_File_Reader();
90 ~Std_File_Reader();
91
92 error_t open( const char* );
93
94 VFSFile* file() const { return file_; }
95
96 // Forward read requests to file. Caller must close file later.
97 //void forward( VFSFile* );
98
99 long size() const;
100 long read_avail( void*, long );
101
102 long tell() const;
103 error_t seek( long );
104
105 void close();
106 };
107
108 // Supports writing
109 class Data_Writer {
110 public:
111 Data_Writer() { }
112 virtual ~Data_Writer() { }
113
114 typedef const char* error_t;
115
116 // Write 'n' bytes. NULL on success, otherwise error string.
117 virtual error_t write( const void*, long n ) = 0;
118
119 void satisfy_lame_linker_();
120 private:
121 // noncopyable
122 Data_Writer( const Data_Writer& );
123 Data_Writer& operator = ( const Data_Writer& );
124 };
125
126 class Std_File_Writer : public Data_Writer {
127 VFSFile* file_;
128 protected:
129 void reset( VFSFile* f ) { file_ = f; }
130 public:
131 Std_File_Writer();
132 ~Std_File_Writer();
133
134 error_t open( const char* );
135
136 VFSFile* file() const { return file_; }
137
138 // Forward writes to file. Caller must close file later.
139 //void forward( VFSFile* );
140
141 error_t write( const void*, long );
142
143 void close();
144 };
145
146 // Write data to memory
147 class Mem_Writer : public Data_Writer {
148 char* data_;
149 long size_;
150 long allocated;
151 enum { expanding, fixed, ignore_excess } mode;
152 public:
153 // Keep all written data in expanding block of memory
154 Mem_Writer();
155
156 // Write to fixed-size block of memory. If ignore_excess is false, returns
157 // error if more than 'size' data is written, otherwise ignores any excess.
158 Mem_Writer( void*, long size, int ignore_excess = 0 );
159
160 error_t write( const void*, long );
161
162 // Pointer to beginning of written data
163 char* data() { return data_; }
164
165 // Number of bytes written
166 long size() const { return size_; }
167
168 ~Mem_Writer();
169 };
170
171 // Written data is ignored
172 class Null_Writer : public Data_Writer {
173 public:
174 error_t write( const void*, long );
175 };
176
177 #endif
178