foobar2000 SDK  2015-08-03
Public Member Functions | Static Public Member Functions | Private Attributes
seekabilizer

#include <seekabilizer.h>

+ Inheritance diagram for seekabilizer:

Public Member Functions

bool can_seek ()
 
bool get_content_type (pfc::string_base &p_out)
 
t_filesize get_position (abort_callback &p_abort)
 
t_filesize get_size (abort_callback &p_abort)
 
t_filetimestamp get_timestamp (abort_callback &p_abort)
 
void initialize (service_ptr_t< file > p_base, t_size p_buffer_size, abort_callback &p_abort)
 
bool is_in_memory ()
 
bool is_remote ()
 
void on_idle (abort_callback &p_abort)
 
t_size read (void *p_buffer, t_size p_bytes, abort_callback &p_abort)
 
void reopen (abort_callback &p_abort)
 
void seek (t_filesize p_position, abort_callback &p_abort)
 

Static Public Member Functions

static void g_seekabilize (service_ptr_t< file > &p_reader, t_size p_buffer_size, abort_callback &p_abort)
 

Private Attributes

seekabilizer_backbuffer m_buffer
 
service_ptr_t< filem_file
 
t_filesize m_position
 
t_filesize m_position_base
 
t_filesize m_size
 

Detailed Description

Definition at line 15 of file seekabilizer.h.

Member Function Documentation

bool seekabilizer::can_seek ( )

Definition at line 190 of file seekabilizer.cpp.

191 {
192  return true;
193 }
void seekabilizer::g_seekabilize ( service_ptr_t< file > &  p_reader,
t_size  p_buffer_size,
abort_callback p_abort 
)
static

Definition at line 87 of file seekabilizer.cpp.

87  {
88  if (p_reader.is_valid() && p_reader->is_remote() && p_buffer_size > 0) {
90  instance->initialize(p_reader,p_buffer_size,p_abort);
91  p_reader = instance.get_ptr();
92  }
93 }
Template implementing reference-counting features of service_base. Intended for dynamic instantiation...
Definition: service_impl.h:4
T * get_ptr() const
Definition: service.h:117
bool is_valid() const
Definition: service.h:119
Autopointer class to be used with all services. Manages reference counter calls behind-the-scenes.
Definition: service.h:55
bool seekabilizer::get_content_type ( pfc::string_base p_out)

Definition at line 195 of file seekabilizer.cpp.

195 {return m_file->get_content_type(p_out);}
service_ptr_t< file > m_file
Definition: seekabilizer.h:33
t_filesize seekabilizer::get_position ( abort_callback p_abort)

Definition at line 156 of file seekabilizer.cpp.

156  {
157  p_abort.check_e();
158  return m_position;
159 }
t_filesize m_position
Definition: seekabilizer.h:35
t_filesize seekabilizer::get_size ( abort_callback p_abort)

Definition at line 151 of file seekabilizer.cpp.

151  {
152  p_abort.check_e();
153  return m_size;
154 }
t_filesize m_size
Definition: seekabilizer.h:35
t_filetimestamp seekabilizer::get_timestamp ( abort_callback p_abort)

Definition at line 201 of file seekabilizer.cpp.

201  {
202  p_abort.check_e();
203  return m_file->get_timestamp(p_abort);
204 }
service_ptr_t< file > m_file
Definition: seekabilizer.h:33
void seekabilizer::initialize ( service_ptr_t< file p_base,
t_size  p_buffer_size,
abort_callback p_abort 
)

Definition at line 80 of file seekabilizer.cpp.

80  {
81  m_buffer.initialize(p_buffer_size);
82  m_file = p_base;
84  m_size = m_file->get_size(p_abort);
85 }
t_filesize m_position_base
Definition: seekabilizer.h:35
t_filesize m_position
Definition: seekabilizer.h:35
service_ptr_t< file > m_file
Definition: seekabilizer.h:33
seekabilizer_backbuffer m_buffer
Definition: seekabilizer.h:34
void initialize(t_size p_size)
Definition: seekabilizer.cpp:5
t_filesize m_size
Definition: seekabilizer.h:35
bool seekabilizer::is_in_memory ( )

Definition at line 197 of file seekabilizer.cpp.

197 {return false;}
bool seekabilizer::is_remote ( )

Definition at line 216 of file seekabilizer.cpp.

217 {
218  return m_file->is_remote();
219 }
service_ptr_t< file > m_file
Definition: seekabilizer.h:33
void seekabilizer::on_idle ( abort_callback p_abort)

Definition at line 199 of file seekabilizer.cpp.

199 {return m_file->on_idle(p_abort);}
service_ptr_t< file > m_file
Definition: seekabilizer.h:33
t_size seekabilizer::read ( void *  p_buffer,
t_size  p_bytes,
abort_callback p_abort 
)

Definition at line 95 of file seekabilizer.cpp.

95  {
96  p_abort.check_e();
97 
98  if (m_position > m_position_base + pfc::max_t<t_size>(m_buffer.get_max_depth(),backread_on_seek) && m_file->can_seek()) {
99  m_buffer.reset();
100  t_filesize target = m_position;
101  if (target < backread_on_seek) target = 0;
102  else target -= backread_on_seek;
103  m_file->seek(target,p_abort);
104  m_position_base = target;
105  }
106 
107  //seek ahead
108  while(m_position > m_position_base) {
109  enum {tempsize = 1024};
110  t_uint8 temp[tempsize];
111  t_size delta = (t_size) pfc::min_t<t_filesize>(tempsize,m_position - m_position_base);
112  t_size bytes_read = 0;
113  bytes_read = m_file->read(temp,delta,p_abort);
114  m_buffer.write(temp,bytes_read);
115  m_position_base += bytes_read;
116 
117  if (bytes_read < delta) {
118  return 0;
119  }
120  }
121 
122  t_size done = 0;
123  t_uint8 * targetptr = (t_uint8*) p_buffer;
124 
125  //try to read backbuffer
126  if (m_position < m_position_base) {
127  if (m_position_base - m_position > (t_filesize)m_buffer.get_depth()) throw exception_io_seek_out_of_range();
128  t_size backread_depth = (t_size) (m_position_base - m_position);
129  t_size delta = pfc::min_t<t_size>(backread_depth,p_bytes-done);
130  m_buffer.read(backread_depth,targetptr,delta);
131  done += delta;
132  m_position += delta;
133  }
134 
135  //regular read
136  if (done < p_bytes)
137  {
138  t_size bytes_read;
139  bytes_read = m_file->read(targetptr+done,p_bytes-done,p_abort);
140 
141  m_buffer.write(targetptr+done,bytes_read);
142 
143  done += bytes_read;
144  m_position += bytes_read;
145  m_position_base += bytes_read;
146  }
147 
148  return done;
149 }
uint8_t t_uint8
Definition: int_types.h:9
t_size get_max_depth() const
t_filesize m_position_base
Definition: seekabilizer.h:35
void write(const void *p_buffer, t_size p_bytes)
t_filesize m_position
Definition: seekabilizer.h:35
service_ptr_t< file > m_file
Definition: seekabilizer.h:33
seekabilizer_backbuffer m_buffer
Definition: seekabilizer.h:34
size_t t_size
Definition: int_types.h:48
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
void read(t_size p_backlogdepth, void *p_buffer, t_size p_bytes) const
t_size get_depth() const
void seekabilizer::reopen ( abort_callback p_abort)

Definition at line 206 of file seekabilizer.cpp.

206  {
207  if (m_position_base - m_buffer.get_depth() == 0) {
208  seek(0,p_abort);
209  } else {
211  m_buffer.reset();
212  m_file->reopen(p_abort);
213  }
214 }
t_filesize m_position_base
Definition: seekabilizer.h:35
t_filesize m_position
Definition: seekabilizer.h:35
service_ptr_t< file > m_file
Definition: seekabilizer.h:33
seekabilizer_backbuffer m_buffer
Definition: seekabilizer.h:34
void seek(t_filesize p_position, abort_callback &p_abort)
t_size get_depth() const
void seekabilizer::seek ( t_filesize  p_position,
abort_callback p_abort 
)

Definition at line 161 of file seekabilizer.cpp.

161  {
162  assert(m_position_base >= m_buffer.get_depth());
163  p_abort.check_e();
164 
165  if (m_size != filesize_invalid && p_position > m_size) throw exception_io_seek_out_of_range();
166 
168 
169  if (p_position < lowest) {
170  if (m_file->can_seek()) {
171  m_buffer.reset();
172  t_filesize target = p_position;
173  t_size delta = m_buffer.get_max_depth();
174  if (delta > backread_on_seek) delta = backread_on_seek;
175  if (target > delta) target -= delta;
176  else target = 0;
177  m_file->seek(target,p_abort);
178  m_position_base = target;
179  }
180  else {
181  m_buffer.reset();
182  m_file->reopen(p_abort);
183  m_position_base = 0;
184  }
185  }
186 
187  m_position = p_position;
188 }
t_size get_max_depth() const
t_filesize m_position_base
Definition: seekabilizer.h:35
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
t_filesize m_position
Definition: seekabilizer.h:35
service_ptr_t< file > m_file
Definition: seekabilizer.h:33
seekabilizer_backbuffer m_buffer
Definition: seekabilizer.h:34
size_t t_size
Definition: int_types.h:48
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
t_size get_depth() const
t_filesize m_size
Definition: seekabilizer.h:35

Field Documentation

seekabilizer_backbuffer seekabilizer::m_buffer
private

Definition at line 34 of file seekabilizer.h.

service_ptr_t<file> seekabilizer::m_file
private

Definition at line 33 of file seekabilizer.h.

t_filesize seekabilizer::m_position
private

Definition at line 35 of file seekabilizer.h.

t_filesize seekabilizer::m_position_base
private

Definition at line 35 of file seekabilizer.h.

t_filesize seekabilizer::m_size
private

Definition at line 35 of file seekabilizer.h.


The documentation for this class was generated from the following files: