foobar2000 SDK  2015-08-03
Public Types | Public Member Functions | Static Public Member Functions
foobar2000_io::fileabstract

#include <filesystem.h>

+ Inheritance diagram for foobar2000_io::file:

Public Types

enum  t_seek_mode { seek_from_beginning = 0, seek_from_current = 1, seek_from_eof = 2 }
 
- Public Types inherited from service_base
typedef service_base t_interface
 

Public Member Functions

virtual bool can_seek ()=0
 
void ensure_local ()
 
void ensure_seekable ()
 
 FB2K_MAKE_SERVICE_INTERFACE (file, service_base)
 
virtual bool get_content_type (pfc::string_base &p_out)=0
 
virtual t_filesize get_position (abort_callback &p_abort)=0
 
t_filesize get_remaining (abort_callback &p_abort)
 
virtual t_filesize get_size (abort_callback &p_abort)=0
 
t_filesize get_size_ex (abort_callback &p_abort)
 
t_filestats get_stats (abort_callback &p_abort)
 
virtual t_filetimestamp get_timestamp (abort_callback &p_abort)
 
bool is_eof (abort_callback &p_abort)
 
virtual bool is_in_memory ()
 
virtual bool is_remote ()=0
 
virtual void on_idle (abort_callback &p_abort)
 
void probe_remaining (t_filesize bytes, abort_callback &p_abort)
 
virtual void reopen (abort_callback &p_abort)=0
 
virtual void resize (t_filesize p_size, abort_callback &p_abort)=0
 
virtual void seek (t_filesize p_position, abort_callback &p_abort)=0
 
virtual void seek_ex (t_sfilesize p_position, t_seek_mode p_mode, abort_callback &p_abort)
 
void seek_probe (t_filesize p_position, abort_callback &p_abort)
 
void set_eof (abort_callback &p_abort)
 
t_filesize skip (t_filesize p_bytes, abort_callback &p_abort)
 
t_filesize skip_seek (t_filesize p_bytes, abort_callback &p_abort)
 
void truncate (t_filesize p_position, abort_callback &p_abort)
 
- Public Member Functions inherited from service_base
template<typename outPtr_t >
bool cast (outPtr_t &outPtr)
 
virtual int service_add_ref ()=0 throw ()
 
virtual bool service_query (service_ptr_t< service_base > &p_out, const GUID &p_guid)
 
template<class T >
bool service_query_t (service_ptr_t< T > &p_out)
 
virtual int service_release ()=0 throw ()
 
- Public Member Functions inherited from foobar2000_io::stream_reader
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 void skip_object (t_filesize p_bytes, abort_callback &p_abort)
 
t_filesize skip_till_eof (abort_callback &abort)
 
- Public Member Functions inherited from foobar2000_io::stream_writer
virtual void write (const void *p_buffer, t_size p_bytes, abort_callback &p_abort)=0
 
template<typename T >
void write_bendian_t (const T &p_object, abort_callback &p_abort)
 
template<typename T >
void write_lendian_t (const T &p_object, abort_callback &p_abort)
 
void write_object (const void *p_buffer, t_size p_bytes, abort_callback &p_abort)
 
template<typename T >
void write_object_t (const T &p_object, abort_callback &p_abort)
 
void write_string (const char *p_string, abort_callback &p_abort)
 
void write_string (const char *p_string, t_size p_len, abort_callback &p_abort)
 
template<typename T >
void write_string (const T &val, abort_callback &p_abort)
 
void write_string_nullterm (const char *p_string, abort_callback &p_abort)
 
void write_string_raw (const char *p_string, abort_callback &p_abort)
 

Static Public Member Functions

static t_filesize g_transfer (stream_reader *src, stream_writer *dst, t_filesize bytes, abort_callback &p_abort)
 
static t_filesize g_transfer (service_ptr_t< file > p_src, service_ptr_t< file > p_dst, t_filesize p_bytes, abort_callback &p_abort)
 
static void g_transfer_file (const service_ptr_t< file > &p_from, const service_ptr_t< file > &p_to, abort_callback &p_abort)
 
static void g_transfer_object (stream_reader *src, stream_writer *dst, t_filesize bytes, abort_callback &p_abort)
 
static void g_transfer_object (service_ptr_t< file > p_src, service_ptr_t< file > p_dst, t_filesize p_bytes, abort_callback &p_abort)
 

Additional Inherited Members

- Protected Member Functions inherited from service_base
 service_base ()
 
 ~service_base ()
 
- Protected Member Functions inherited from foobar2000_io::stream_reader
 stream_reader ()
 
 ~stream_reader ()
 
- Protected Member Functions inherited from foobar2000_io::stream_writer
 stream_writer ()
 
 ~stream_writer ()
 

Detailed Description

A class providing abstraction for an open file object, with reading/writing/seeking methods. See also: stream_reader, stream_writer (which it inherits read/write methods from).
Error handling: all methods may throw exception_io or one of derivatives on failure; exception_aborted when abort_callback is signaled.

Definition at line 208 of file filesystem.h.

Member Enumeration Documentation

Seeking mode constants. Note: these are purposedly defined to same values as standard C SEEK_* constants.

Enumerator
seek_from_beginning 

Seek relative to beginning of file (same as seeking to absolute offset).

seek_from_current 

Seek relative to current position.

seek_from_eof 

Seek relative to end of file.

Definition at line 212 of file filesystem.h.

212  {
216  seek_from_current = 1,
218  seek_from_eof = 2,
219  };
Seek relative to current position.
Definition: filesystem.h:216
Seek relative to beginning of file (same as seeking to absolute offset).
Definition: filesystem.h:214
Seek relative to end of file.
Definition: filesystem.h:218

Member Function Documentation

virtual bool foobar2000_io::file::can_seek ( )
pure virtual

Returns whether the file is seekable or not. If can_seek() returns false, all seek() or seek_ex() calls will fail; reopen() is still usable on nonseekable streams.

void file::ensure_local ( )

Helper; throws exception_io_object_is_remote if the file is remote.

Definition at line 839 of file filesystem.cpp.

839  {
840  if (is_remote()) throw exception_io_object_is_remote();
841 }
virtual bool is_remote()=0
Indicates whether the file is a remote resource and non-sequential access may be slowed down by lag...
void file::ensure_seekable ( )

Helper; throws exception_io_object_not_seekable if file is not seekable.

Definition at line 843 of file filesystem.cpp.

843  {
844  if (!can_seek()) throw exception_io_object_not_seekable();
845 }
virtual bool can_seek()=0
Returns whether the file is seekable or not. If can_seek() returns false, all seek() or seek_ex() cal...
foobar2000_io::file::FB2K_MAKE_SERVICE_INTERFACE ( file  ,
service_base   
)
static t_filesize foobar2000_io::file::g_transfer ( stream_reader src,
stream_writer dst,
t_filesize  bytes,
abort_callback p_abort 
)
static

Helper; transfers specified number of bytes between streams.

Returns
number of bytes actually transferred. May be less than requested if e.g. EOF is reached.
t_filesize file::g_transfer ( service_ptr_t< file p_src,
service_ptr_t< file p_dst,
t_filesize  p_bytes,
abort_callback p_abort 
)
static

Helper; improved performance over g_transfer on streams (avoids disk fragmentation when transferring large blocks).

Definition at line 868 of file filesystem.cpp.

868  {
869  return g_transfer(pfc::implicit_cast<stream_reader*>(p_src.get_ptr()),pfc::implicit_cast<stream_writer*>(p_dst.get_ptr()),p_bytes,p_abort);
870 }
static t_filesize g_transfer(stream_reader *src, stream_writer *dst, t_filesize bytes, abort_callback &p_abort)
Helper; transfers specified number of bytes between streams.
T * get_ptr() const
Definition: service.h:117
void file::g_transfer_file ( const service_ptr_t< file > &  p_from,
const service_ptr_t< file > &  p_to,
abort_callback p_abort 
)
static

Helper; transfers entire file content from one file to another, erasing previous content.

Definition at line 625 of file filesystem.cpp.

625  {
626  t_filesize length = p_from->get_size(p_abort);
627  p_from->reopen( p_abort );
628 // p_from->seek(0,p_abort);
629  p_to->seek(0,p_abort);
630  p_to->set_eof(p_abort);
631  if (length == filesize_invalid) {
632  g_transfer(p_from, p_to, ~0, p_abort);
633  } else if (length > 0) {
634  g_transfer_object(p_from,p_to,length,p_abort);
635  }
636 }
static t_filesize g_transfer(stream_reader *src, stream_writer *dst, t_filesize bytes, abort_callback &p_abort)
Helper; transfers specified number of bytes between streams.
static void g_transfer_object(stream_reader *src, stream_writer *dst, t_filesize bytes, abort_callback &p_abort)
Helper; transfers specified number of bytes between streams. Throws exception if requested number of ...
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
static void foobar2000_io::file::g_transfer_object ( stream_reader src,
stream_writer dst,
t_filesize  bytes,
abort_callback p_abort 
)
static

Helper; transfers specified number of bytes between streams. Throws exception if requested number of bytes could not be read (EOF).

void file::g_transfer_object ( service_ptr_t< file p_src,
service_ptr_t< file p_dst,
t_filesize  p_bytes,
abort_callback p_abort 
)
static

Helper; improved performance over g_transfer_file on streams (avoids disk fragmentation when transferring large blocks).

Definition at line 872 of file filesystem.cpp.

872  {
873  if (p_bytes > 1024) /* don't bother on small objects */
874  {
875  t_filesize srcFileSize = p_src->get_size(p_abort); // detect truncation
876  if (srcFileSize != ~0) {
877  t_filesize remaining = srcFileSize - p_src->get_position(p_abort);
878  if (p_bytes > remaining) throw exception_io_data_truncation();
879  }
880 
881  t_filesize oldsize = p_dst->get_size(p_abort); // pre-resize the target file
882  if (oldsize != filesize_invalid) {
883  t_filesize newpos = p_dst->get_position(p_abort) + p_bytes;
884  if (newpos > oldsize) p_dst->resize(newpos ,p_abort);
885  }
886 
887  }
888  g_transfer_object(pfc::implicit_cast<stream_reader*>(p_src.get_ptr()),pfc::implicit_cast<stream_writer*>(p_dst.get_ptr()),p_bytes,p_abort);
889 }
static void g_transfer_object(stream_reader *src, stream_writer *dst, t_filesize bytes, abort_callback &p_abort)
Helper; transfers specified number of bytes between streams. Throws exception if requested number of ...
T * get_ptr() const
Definition: service.h:117
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
virtual bool foobar2000_io::file::get_content_type ( pfc::string_base p_out)
pure virtual

Retrieves mime type of the file.

Parameters
p_outReceives content type string on success.
virtual t_filesize foobar2000_io::file::get_position ( abort_callback p_abort)
pure virtual

Retrieves read/write cursor position in the file. In case of non-seekable stream, this should return number of bytes read so far since open/reopen call.

Parameters
p_abortabort_callback object signaling user aborting the operation.
Returns
Read/write cursor position
t_filesize file::get_remaining ( abort_callback p_abort)

Helper; retrieves amount of bytes between read/write cursor position and end of file. Fails when length can't be determined.

Definition at line 852 of file filesystem.cpp.

852  {
853  t_filesize length = get_size_ex(p_abort);
854  t_filesize position = get_position(p_abort);
855  pfc::dynamic_assert(position <= length);
856  return length - position;
857 }
t_filesize get_size_ex(abort_callback &p_abort)
Helper; retrieves size of the file. If size is not available (get_size() returns filesize_invalid), throws exception_io_no_length.
Definition: filesystem.cpp:833
void dynamic_assert(bool p_condition, const char *p_msg)
Definition: primitives.h:239
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
virtual t_filesize get_position(abort_callback &p_abort)=0
Retrieves read/write cursor position in the file. In case of non-seekable stream, this should return ...
virtual t_filesize foobar2000_io::file::get_size ( abort_callback p_abort)
pure virtual

Retrieves size of the file.

Parameters
p_abortabort_callback object signaling user aborting the operation.
Returns
File size on success; filesize_invalid if unknown (nonseekable stream etc).
t_filesize file::get_size_ex ( abort_callback p_abort)

Helper; retrieves size of the file. If size is not available (get_size() returns filesize_invalid), throws exception_io_no_length.

Definition at line 833 of file filesystem.cpp.

833  {
834  t_filesize temp = get_size(p_abort);
835  if (temp == filesize_invalid) throw exception_io_no_length();
836  return temp;
837 }
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
virtual t_filesize get_size(abort_callback &p_abort)=0
Retrieves size of the file.
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
t_filestats file::get_stats ( abort_callback p_abort)

Retrieves file stats structure. Uses get_size() and get_timestamp().

Definition at line 594 of file filesystem.cpp.

595 {
596  t_filestats temp;
597  temp.m_size = get_size(p_abort);
598  temp.m_timestamp = get_timestamp(p_abort);
599  return temp;
600 }
virtual t_filesize get_size(abort_callback &p_abort)=0
Retrieves size of the file.
virtual t_filetimestamp get_timestamp(abort_callback &p_abort)
Retrieves last modification time of the file.
Definition: filesystem.h:267
virtual t_filetimestamp foobar2000_io::file::get_timestamp ( abort_callback p_abort)
inlinevirtual

Retrieves last modification time of the file.

Parameters
p_abortabort_callback object signaling user aborting the operation.
Returns
Last modification time o fthe file; filetimestamp_invalid if N/A.

Definition at line 267 of file filesystem.h.

267 {return filetimestamp_invalid;}
const t_filetimestamp filetimestamp_invalid
Invalid/unknown file timestamp constant. Also see: t_filetimestamp.
Definition: filesystem.h:14
bool file::is_eof ( abort_callback p_abort)

Returns whether read/write cursor position is at the end of file.

Definition at line 675 of file filesystem.cpp.

675  {
676  t_filesize position,size;
677  position = get_position(p_abort);
678  size = get_size(p_abort);
679  if (size == filesize_invalid) return false;
680  return position >= size;
681 }
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
virtual t_filesize get_size(abort_callback &p_abort)=0
Retrieves size of the file.
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
virtual t_filesize get_position(abort_callback &p_abort)=0
Retrieves read/write cursor position in the file. In case of non-seekable stream, this should return ...
virtual bool foobar2000_io::file::is_in_memory ( )
inlinevirtual

Hint, returns whether the file is already fully buffered into memory.

Definition at line 258 of file filesystem.h.

258 {return false;}
virtual bool foobar2000_io::file::is_remote ( )
pure virtual

Indicates whether the file is a remote resource and non-sequential access may be slowed down by lag. This is typically returns to true on non-seekable sources but may also return true on seekable sources indicating that seeking is supported but will be relatively slow.

virtual void foobar2000_io::file::on_idle ( abort_callback p_abort)
inlinevirtual

Optional, called by owner thread before sleeping.

Parameters
p_abortabort_callback object signaling user aborting the operation.

Definition at line 262 of file filesystem.h.

262 {}
void file::probe_remaining ( t_filesize  bytes,
abort_callback p_abort 
)

Security helper; fails early with exception_io_data_truncation if it is not possible to read this amount of bytes from this file at this position.

Definition at line 859 of file filesystem.cpp.

859  {
860  t_filesize length = get_size(p_abort);
861  if (length != ~0) {
862  t_filesize remaining = length - get_position(p_abort);
863  if (remaining < bytes) throw exception_io_data_truncation();
864  }
865 }
virtual t_filesize get_size(abort_callback &p_abort)=0
Retrieves size of the file.
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
virtual t_filesize get_position(abort_callback &p_abort)=0
Retrieves read/write cursor position in the file. In case of non-seekable stream, this should return ...
virtual void foobar2000_io::file::reopen ( abort_callback p_abort)
pure virtual

Resets non-seekable stream, or seeks to zero on seekable file.

Parameters
p_abortabort_callback object signaling user aborting the operation.
virtual void foobar2000_io::file::resize ( t_filesize  p_size,
abort_callback p_abort 
)
pure virtual

Resizes file to the specified size in bytes.

Parameters
p_abortabort_callback object signaling user aborting the operation.
virtual void foobar2000_io::file::seek ( t_filesize  p_position,
abort_callback p_abort 
)
pure virtual

Sets read/write cursor position to the specified offset. Throws exception_io_seek_out_of_range if the specified offset is outside the valid range.

Parameters
p_positionposition to seek to.
p_abortabort_callback object signaling user aborting the operation.
void file::seek_ex ( t_sfilesize  p_position,
file::t_seek_mode  p_mode,
abort_callback p_abort 
)
virtual

Sets read/write cursor position to the specified offset; extended form allowing seeking relative to current position or to end of file.

Parameters
p_positionPosition to seek to; interpretation of this value depends on p_mode parameter.
p_modeSeeking mode; see t_seek_mode enum values for further description.
p_abortabort_callback object signaling user aborting the operation.

Definition at line 24 of file filesystem.cpp.

24  {
25  switch(p_mode) {
27  seek(p_position,p_abort);
28  break;
29  case seek_from_current:
30  seek(p_position + get_position(p_abort),p_abort);
31  break;
32  case seek_from_eof:
33  seek(p_position + get_size_ex(p_abort),p_abort);
34  break;
35  default:
36  throw exception_io_data();
37  }
38 }
t_filesize get_size_ex(abort_callback &p_abort)
Helper; retrieves size of the file. If size is not available (get_size() returns filesize_invalid), throws exception_io_no_length.
Definition: filesystem.cpp:833
virtual void seek(t_filesize p_position, abort_callback &p_abort)=0
Sets read/write cursor position to the specified offset. Throws exception_io_seek_out_of_range if the...
Seek relative to current position.
Definition: filesystem.h:216
Seek relative to beginning of file (same as seeking to absolute offset).
Definition: filesystem.h:214
Seek relative to end of file.
Definition: filesystem.h:218
virtual t_filesize get_position(abort_callback &p_abort)=0
Retrieves read/write cursor position in the file. In case of non-seekable stream, this should return ...
void file::seek_probe ( t_filesize  p_position,
abort_callback p_abort 
)

Same as seek() but throws exception_io_data instead of exception_io_seek_out_of_range.

Definition at line 20 of file filesystem.cpp.

20  {
21  try { seek(p_position, p_abort); } catch(exception_io_seek_out_of_range) {throw exception_io_data();}
22 }
virtual void seek(t_filesize p_position, abort_callback &p_abort)=0
Sets read/write cursor position to the specified offset. Throws exception_io_seek_out_of_range if the...
void foobar2000_io::file::set_eof ( abort_callback p_abort)
inline

Truncates the file at current read/write cursor position.

Definition at line 286 of file filesystem.h.

286 {resize(get_position(p_abort),p_abort);}
virtual void resize(t_filesize p_size, abort_callback &p_abort)=0
Resizes file to the specified size in bytes.
virtual t_filesize get_position(abort_callback &p_abort)=0
Retrieves read/write cursor position in the file. In case of non-seekable stream, this should return ...
t_filesize file::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 from foobar2000_io::stream_reader.

Definition at line 925 of file filesystem.cpp.

925  {
926  if (p_bytes > 1024 && can_seek()) {
927  const t_filesize size = get_size(p_abort);
928  if (size != filesize_invalid) {
929  const t_filesize position = get_position(p_abort);
930  const t_filesize toskip = pfc::min_t( p_bytes, size - position );
931  seek(position + toskip,p_abort);
932  return toskip;
933  }
934  }
935  return stream_reader::skip(p_bytes,p_abort);
936 }
T min_t(const T &item1, const T &item2)
Definition: primitives.h:556
virtual void seek(t_filesize p_position, abort_callback &p_abort)=0
Sets read/write cursor position to the specified offset. Throws exception_io_seek_out_of_range if the...
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
virtual t_filesize get_size(abort_callback &p_abort)=0
Retrieves size of the file.
virtual bool can_seek()=0
Returns whether the file is seekable or not. If can_seek() returns false, all seek() or seek_ex() cal...
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
virtual t_filesize get_position(abort_callback &p_abort)=0
Retrieves read/write cursor position in the file. In case of non-seekable stream, this should return ...
t_filesize file::skip_seek ( t_filesize  p_bytes,
abort_callback p_abort 
)

Definition at line 912 of file filesystem.cpp.

912  {
913  const t_filesize size = get_size(p_abort);
914  if (size != filesize_invalid) {
915  const t_filesize position = get_position(p_abort);
916  const t_filesize toskip = pfc::min_t( p_bytes, size - position );
917  seek(position + toskip,p_abort);
918  return toskip;
919  } else {
920  this->seek_ex( p_bytes, seek_from_current, p_abort );
921  return p_bytes;
922  }
923 }
T min_t(const T &item1, const T &item2)
Definition: primitives.h:556
virtual void seek(t_filesize p_position, abort_callback &p_abort)=0
Sets read/write cursor position to the specified offset. Throws exception_io_seek_out_of_range if the...
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
virtual t_filesize get_size(abort_callback &p_abort)=0
Retrieves size of the file.
Seek relative to current position.
Definition: filesystem.h:216
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
virtual t_filesize get_position(abort_callback &p_abort)=0
Retrieves read/write cursor position in the file. In case of non-seekable stream, this should return ...
virtual void seek_ex(t_sfilesize p_position, t_seek_mode p_mode, abort_callback &p_abort)
Sets read/write cursor position to the specified offset; extended form allowing seeking relative to c...
Definition: filesystem.cpp:24
void file::truncate ( t_filesize  p_position,
abort_callback p_abort 
)

Truncates file to specified size (while preserving read/write cursor position if possible); uses set_eof().

Definition at line 741 of file filesystem.cpp.

741  {
742  if (p_position < get_size(p_abort)) resize(p_position,p_abort);
743 }
virtual void resize(t_filesize p_size, abort_callback &p_abort)=0
Resizes file to the specified size in bytes.
virtual t_filesize get_size(abort_callback &p_abort)=0
Retrieves size of the file.

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