foobar2000 SDK  2015-08-03
stream_buffer_helper.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
4 {
5  m_buffer.set_size_in_range(pfc::min_t<size_t>(1024, p_buffer), p_buffer);
7 }
8 
9 t_size stream_reader_buffered::read(void * p_buffer,t_size p_bytes,abort_callback & p_abort) {
10  if (p_bytes <= m_bufferRemaining) {
11  memcpy( p_buffer, m_bufferPtr, p_bytes );
12  m_bufferRemaining -= p_bytes;
13  m_bufferPtr += p_bytes;
14  return p_bytes;
15  }
16 
17  p_abort.check();
18  char * output = (char*) p_buffer;
19  t_size output_ptr = 0;
20 
21  while(output_ptr < p_bytes) {
22  {
23  t_size delta = pfc::min_t(p_bytes - output_ptr, m_bufferRemaining);
24  if (delta > 0)
25  {
26  memcpy(output + output_ptr, m_bufferPtr, delta);
27  output_ptr += delta;
28  m_bufferPtr += delta;
29  m_bufferRemaining -= delta;
30  }
31  }
32 
33  if (m_bufferRemaining == 0)
34  {
35  t_size bytes_read;
36  bytes_read = m_base->read(m_buffer.get_ptr(), m_buffer.get_size(), p_abort);
38  m_bufferRemaining = bytes_read;
39 
40  if (m_bufferRemaining == 0) break;
41  }
42 
43  }
44 
45  return output_ptr;
46 }
47 
49  : m_base(p_base)
50 {
51  m_buffer.set_size_in_range(pfc::min_t<size_t>(1024, p_buffer), p_buffer);
52  m_buffer_ptr = 0;
53 }
54 
55 void stream_writer_buffered::write(const void * p_buffer,t_size p_bytes,abort_callback & p_abort) {
56  p_abort.check_e();
57  const char * source = (const char*)p_buffer;
58  t_size source_remaining = p_bytes;
59  const t_size buffer_size = m_buffer.get_size();
60  if (source_remaining >= buffer_size)
61  {
62  flush(p_abort);
63  m_base->write_object(source,source_remaining,p_abort);
64  return;
65  }
66 
67  if (m_buffer_ptr + source_remaining >= buffer_size)
68  {
69  t_size delta = buffer_size - m_buffer_ptr;
70  memcpy(m_buffer.get_ptr() + m_buffer_ptr, source,delta);
71  source += delta;
72  source_remaining -= delta;
73  m_buffer_ptr += delta;
74  flush(p_abort);
75  }
76 
77  memcpy(m_buffer.get_ptr() + m_buffer_ptr, source,source_remaining);
78  m_buffer_ptr += source_remaining;
79 }
80 
81 
83  if (m_buffer_ptr > 0) {
84  m_base->write_object(m_buffer.get_ptr(),m_buffer_ptr,p_abort);
85  m_buffer_ptr = 0;
86  }
87 }
void set_size_in_range(size_t minSize, size_t maxSize)
Definition: array.h:113
const t_item * get_ptr() const
Definition: array.h:213
T min_t(const T &item1, const T &item2)
Definition: primitives.h:556
pfc::array_t< char > m_buffer
stream_writer_buffered(stream_writer *p_base, t_size p_buffer)
void write(const void *p_buffer, t_size p_bytes, abort_callback &p_abort)
t_size read(void *p_buffer, t_size p_bytes, abort_callback &p_abort)
pfc::array_t< char > m_buffer
size_t t_size
Definition: int_types.h:48
void flush(abort_callback &p_abort)
stream_reader_buffered(stream_reader *p_base, t_size p_buffer)
t_size get_size() const
Definition: array.h:130
Definition: output.h:95