foobar2000 SDK  2015-01-14
Public Member Functions | Protected Member Functions
foobar2000_io::stream_readerabstract

#include <filesystem.h>

+ Inheritance diagram for foobar2000_io::stream_reader:

Public Member Functions

virtual t_size read (void *p_buffer, t_size p_bytes, abort_callback &p_abort)=0
 
template<typename T >
void read_bendian_t (T &p_object, abort_callback &p_abort)
 
template<typename T >
void read_lendian_t (T &p_object, abort_callback &p_abort)
 
virtual void read_object (void *p_buffer, t_size p_bytes, abort_callback &p_abort)
 
template<typename T >
void read_object_t (T &p_object, abort_callback &p_abort)
 
void read_string (pfc::string_base &p_out, abort_callback &p_abort)
 
pfc::string read_string (abort_callback &p_abort)
 
void read_string_ex (pfc::string_base &p_out, t_size p_bytes, abort_callback &p_abort)
 
pfc::string read_string_ex (t_size p_len, abort_callback &p_abort)
 
void read_string_nullterm (pfc::string_base &out, abort_callback &abort)
 
void read_string_raw (pfc::string_base &p_out, abort_callback &p_abort)
 
template<typename t_outArray >
void read_till_eof (t_outArray &out, abort_callback &abort)
 
virtual t_filesize skip (t_filesize p_bytes, abort_callback &p_abort)
 
virtual void skip_object (t_filesize p_bytes, abort_callback &p_abort)
 
t_filesize skip_till_eof (abort_callback &abort)
 

Protected Member Functions

 stream_reader ()
 
 ~stream_reader ()
 

Detailed Description

Generic interface to read data from a nonseekable stream. Also see: stream_writer, file.
Error handling: all methods may throw exception_io or one of derivatives on failure; exception_aborted when abort_callback is signaled.

Definition at line 87 of file filesystem.h.

Constructor & Destructor Documentation

foobar2000_io::stream_reader::stream_reader ( )
inlineprotected

Definition at line 154 of file filesystem.h.

154 {}
foobar2000_io::stream_reader::~stream_reader ( )
inlineprotected

Definition at line 155 of file filesystem.h.

155 {}

Member Function Documentation

virtual t_size foobar2000_io::stream_reader::read ( void *  p_buffer,
t_size  p_bytes,
abort_callback p_abort 
)
pure virtual

Attempts to reads specified number of bytes from the stream.

Parameters
p_bufferReceives data being read. Must have at least p_bytes bytes of space allocated.
p_bytesNumber of bytes to read.
p_abortabort_callback object signaling user aborting the operation.
Returns
Number of bytes actually read. May be less than requested when EOF was reached.
template<typename T >
void foobar2000_io::stream_reader::read_bendian_t ( T p_object,
abort_callback p_abort 
)
inline

Helper template built around read_object. Reads single raw object from the stream; corrects byte order assuming stream uses big endian order.

Parameters
p_objectReceives object read from the stream on success.
p_abortabort_callback object signaling user aborting the operation.

Definition at line 121 of file filesystem.h.

121 {read_object_t(p_object,p_abort); byte_order::order_be_to_native_t(p_object);}
void order_be_to_native_t(T &param)
void read_object_t(T &p_object, abort_callback &p_abort)
Helper template built around read_object. Reads single raw object from the stream.
Definition: filesystem.h:113
template<typename T >
void foobar2000_io::stream_reader::read_lendian_t ( T p_object,
abort_callback p_abort 
)
inline

Helper template built around read_object. Reads single raw object from the stream; corrects byte order assuming stream uses little endian order.

Parameters
p_objectReceives object read from the stream on success.
p_abortabort_callback object signaling user aborting the operation.

Definition at line 117 of file filesystem.h.

117 {read_object_t(p_object,p_abort); byte_order::order_le_to_native_t(p_object);}
void order_le_to_native_t(T &param)
void read_object_t(T &p_object, abort_callback &p_abort)
Helper template built around read_object. Reads single raw object from the stream.
Definition: filesystem.h:113
void stream_reader::read_object ( void *  p_buffer,
t_size  p_bytes,
abort_callback p_abort 
)
virtual

Reads specified number of bytes from the stream. If requested amount of bytes can't be read (e.g. EOF), throws exception_io_data_truncation.

Parameters
p_bufferReceives data being read. Must have at least p_bytes bytes of space allocated.
p_bytesNumber of bytes to read.
p_abortabort_callback object signaling user aborting the operation.

Definition at line 590 of file filesystem.cpp.

590  {
591  if (read(p_buffer,p_bytes,p_abort) != p_bytes) throw exception_io_data_truncation();
592 }
virtual t_size read(void *p_buffer, t_size p_bytes, abort_callback &p_abort)=0
Attempts to reads specified number of bytes from the stream.
template<typename T >
void foobar2000_io::stream_reader::read_object_t ( T p_object,
abort_callback p_abort 
)
inline

Helper template built around read_object. Reads single raw object from the stream.

Parameters
p_objectReceives object read from the stream on success.
p_abortabort_callback object signaling user aborting the operation.

Definition at line 113 of file filesystem.h.

113 {pfc::assert_raw_type<T>(); read_object(&p_object,sizeof(p_object),p_abort);}
virtual void read_object(void *p_buffer, t_size p_bytes, abort_callback &p_abort)
Reads specified number of bytes from the stream. If requested amount of bytes can't be read (e...
Definition: filesystem.cpp:590
void stream_reader::read_string ( pfc::string_base p_out,
abort_callback p_abort 
)

Helper function; reads a string (with a 32-bit header indicating length in bytes followed by UTF-8 encoded data without a null terminator).

Definition at line 709 of file filesystem.cpp.

710 {
711  t_uint32 length;
712  read_lendian_t(length,p_abort);
713  read_string_ex(p_out,length,p_abort);
714 }
void read_string_ex(pfc::string_base &p_out, t_size p_bytes, abort_callback &p_abort)
Helper function; reads a string of specified length from the stream.
Definition: filesystem.cpp:689
void read_lendian_t(T &p_object, abort_callback &p_abort)
Helper template built around read_object. Reads single raw object from the stream; corrects byte orde...
Definition: filesystem.h:117
uint32_t t_uint32
Definition: int_types.h:5
pfc::string stream_reader::read_string ( abort_callback p_abort)

Helper function; reads a string (with a 32-bit header indicating length in bytes followed by UTF-8 encoded data without a null terminator).

Definition at line 965 of file filesystem.cpp.

965  {
966  t_uint32 len;
967  read_lendian_t(len,p_abort);
968  return read_string_ex(len,p_abort);
969 }
void read_string_ex(pfc::string_base &p_out, t_size p_bytes, abort_callback &p_abort)
Helper function; reads a string of specified length from the stream.
Definition: filesystem.cpp:689
void read_lendian_t(T &p_object, abort_callback &p_abort)
Helper template built around read_object. Reads single raw object from the stream; corrects byte orde...
Definition: filesystem.h:117
uint32_t t_uint32
Definition: int_types.h:5
void stream_reader::read_string_ex ( pfc::string_base p_out,
t_size  p_bytes,
abort_callback p_abort 
)

Helper function; reads a string of specified length from the stream.

Definition at line 689 of file filesystem.cpp.

689  {
690  const t_size expBase = 64*1024;
691  if (p_bytes > expBase) {
692  pfc::array_t<char> temp;
693  t_size allocWalk = expBase;
694  t_size done = 0;
695  for(;;) {
696  const t_size target = pfc::min_t(allocWalk, p_bytes);
697  temp.set_size(target);
698  read_object(temp.get_ptr() + done, target - done, p_abort);
699  if (target == p_bytes) break;
700  done = target;
701  allocWalk <<= 1;
702  }
703  p_out.set_string(temp.get_ptr(), p_bytes);
704  } else {
705  pfc::string_buffer buf(p_out, p_bytes);
706  read_object(buf.get_ptr(),p_bytes,p_abort);
707  }
708 }
const t_item * get_ptr() const
Definition: array.h:213
T min_t(const T &item1, const T &item2)
Definition: primitives.h:556
virtual void read_object(void *p_buffer, t_size p_bytes, abort_callback &p_abort)
Reads specified number of bytes from the stream. If requested amount of bytes can't be read (e...
Definition: filesystem.cpp:590
size_t t_size
Definition: int_types.h:48
void set_size(t_size p_size)
Definition: array.h:104
virtual void set_string(const char *p_string, t_size p_length=~0)
Definition: string_base.h:197
pfc::string stream_reader::read_string_ex ( t_size  p_len,
abort_callback p_abort 
)

Helper function; reads a string of specified length from the stream.

Definition at line 970 of file filesystem.cpp.

970  {
971  pfc::rcptr_t<pfc::string8> temp; temp.new_t();
972  read_object(temp->lock_buffer(p_len),p_len,p_abort);
973  temp->unlock_buffer();
974  return pfc::string::t_data(temp);
975 }
void new_t()
Definition: rcptr.h:101
void unlock_buffer()
Definition: string_base.h:416
rcptr_t< string_base const > t_data
Definition: stringNew.h:21
virtual void read_object(void *p_buffer, t_size p_bytes, abort_callback &p_abort)
Reads specified number of bytes from the stream. If requested amount of bytes can't be read (e...
Definition: filesystem.cpp:590
char * lock_buffer(t_size n)
Definition: string_base.h:408
void stream_reader::read_string_nullterm ( pfc::string_base out,
abort_callback abort 
)

Definition at line 1028 of file filesystem.cpp.

1028  {
1029  enum { bufCount = 256 };
1030  char buffer[bufCount];
1031  out.reset();
1032  size_t w = 0;
1033  for(;;) {
1034  char & c = buffer[w];
1035  this->read_object( &c, 1, abort );
1036  if (c == 0) {
1037  out.add_string( buffer, w ); break;
1038  }
1039  if (++w == bufCount ) {
1040  out.add_string( buffer, bufCount ); w = 0;
1041  }
1042  }
1043 }
virtual void add_string(const char *p_string, t_size p_length=~0)=0
virtual void read_object(void *p_buffer, t_size p_bytes, abort_callback &p_abort)
Reads specified number of bytes from the stream. If requested amount of bytes can't be read (e...
Definition: filesystem.cpp:590
void stream_reader::read_string_raw ( pfc::string_base p_out,
abort_callback p_abort 
)

Helper function; alternate way of storing strings; assumes string takes space up to end of stream.

Definition at line 716 of file filesystem.cpp.

716  {
717  enum {delta = 256};
718  char buffer[delta];
719  p_out.reset();
720  for(;;) {
721  t_size delta_done;
722  delta_done = read(buffer,delta,p_abort);
723  p_out.add_string(buffer,delta_done);
724  if (delta_done < delta) break;
725  }
726 }
virtual void add_string(const char *p_string, t_size p_length=~0)=0
virtual t_size read(void *p_buffer, t_size p_bytes, abort_callback &p_abort)=0
Attempts to reads specified number of bytes from the stream.
size_t t_size
Definition: int_types.h:48
template<typename t_outArray >
void foobar2000_io::stream_reader::read_till_eof ( t_outArray &  out,
abort_callback abort 
)
inline

Definition at line 140 of file filesystem.h.

140  {
141  pfc::assert_raw_type<typename t_outArray::t_item>();
142  const t_size itemWidth = sizeof(typename t_outArray::t_item);
143  out.set_size(pfc::max_t<t_size>(1,256 / itemWidth)); t_size done = 0;
144  for(;;) {
145  t_size delta = out.get_size() - done;
146  t_size delta2 = read(out.get_ptr() + done, delta * itemWidth, abort ) / itemWidth;
147  done += delta2;
148  if (delta2 != delta) break;
149  out.set_size(out.get_size() << 1);
150  }
151  out.set_size(done);
152  }
virtual t_size read(void *p_buffer, t_size p_bytes, abort_callback &p_abort)=0
Attempts to reads specified number of bytes from the stream.
size_t t_size
Definition: int_types.h:48
t_filesize stream_reader::skip ( t_filesize  p_bytes,
abort_callback p_abort 
)
virtual

Attempts to skip specified number of bytes in the stream.

Parameters
p_bytesNumber of bytes to skip.
p_abortabort_callback object signaling user aborting the operation.
Returns
Number of bytes actually skipped, May be less than requested when EOF was reached.

Reimplemented in foobar2000_io::file.

Definition at line 602 of file filesystem.cpp.

603 {
604  t_uint8 temp[256];
605  t_filesize todo = p_bytes, done = 0;
606  while(todo > 0) {
607  t_size delta,deltadone;
608  delta = sizeof(temp);
609  if (delta > todo) delta = (t_size) todo;
610  deltadone = read(temp,delta,p_abort);
611  done += deltadone;
612  todo -= deltadone;
613  if (deltadone < delta) break;
614  }
615  return done;
616 }
uint8_t t_uint8
Definition: int_types.h:9
virtual t_size read(void *p_buffer, t_size p_bytes, abort_callback &p_abort)=0
Attempts to reads specified number of bytes from the stream.
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 stream_reader::skip_object ( t_filesize  p_bytes,
abort_callback p_abort 
)
virtual

Skips specified number of bytes in the stream. If requested amount of bytes can't be skipped (e.g. EOF), throws exception_io_data_truncation.

Parameters
p_bytesNumber of bytes to skip.
p_abortabort_callback object signaling user aborting the operation.

Definition at line 618 of file filesystem.cpp.

618  {
619  if (skip(p_bytes,p_abort) != p_bytes) throw exception_io_data_truncation();
620 }
virtual t_filesize skip(t_filesize p_bytes, abort_callback &p_abort)
Attempts to skip specified number of bytes in the stream.
Definition: filesystem.cpp:602
t_filesize stream_reader::skip_till_eof ( abort_callback abort)

Definition at line 1045 of file filesystem.cpp.

1045  {
1046  t_filesize atOnce = 1024 * 1024;
1047  t_filesize done = 0;
1048  for (;; ) {
1049  abort.check();
1050  t_filesize did = this->skip(atOnce, abort);
1051  done += did;
1052  if (did != atOnce) break;
1053  }
1054  return done;
1055 }
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
virtual t_filesize skip(t_filesize p_bytes, abort_callback &p_abort)
Attempts to skip specified number of bytes in the stream.
Definition: filesystem.cpp:602

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