Loading...
Searching...
No Matches
generic_file.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2025 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author, see the AUTHOR file
/*********************************************************************/
#ifndef GENERIC_FILE_HPP
#define GENERIC_FILE_HPP
#include "../my_config.h"
extern "C"
{
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
} // end extern "C"
#include "proto_generic_file.hpp"
#include "integers.hpp"
#include "crc.hpp"
#include "infinint.hpp"
#include "gf_mode.hpp"
#include <string>
namespace libdar
{
class generic_file: public proto_generic_file
{
public :
generic_file(gf_mode m) { rw = m; terminated = no_read_ahead = false; enable_crc(false); checksum = nullptr; };
generic_file(const generic_file &ref) { copy_from(ref); };
generic_file(generic_file && ref) noexcept { nullifyptr(); move_from(std::move(ref)); };
generic_file & operator = (const generic_file & ref) { destroy(); copy_from(ref); return *this; };
generic_file & operator = (generic_file && ref) noexcept { move_from(std::move(ref)); return *this; };
~generic_file() noexcept(false) { destroy(); };
void terminate();
bool operator == (generic_file & ref);
bool operator != (generic_file & ref) { return ! (*this == ref); };
gf_mode get_mode() const { return rw; };
void read_ahead(const infinint & amount);
void ignore_read_ahead(bool mode) { no_read_ahead = mode; };
virtual U_I read(char *a, U_I size) override;
virtual void write(const char *a, U_I size) override;
void write(const std::string & arg);
S_I read_back(char &a);
S_I read_forward(char &a) { if(terminated) throw SRC_BUG; return read(&a, 1); };
enum skippability { skip_backward, skip_forward };
virtual bool skippable(skippability direction, const infinint & amount) = 0;
virtual bool skip(const infinint & pos) = 0;
virtual bool skip_to_eof() = 0;
virtual bool skip_relative(S_I x) = 0;
virtual bool truncatable(const infinint & pos) const = 0;
virtual void truncate(const infinint & pos);
virtual infinint get_position() const = 0;
virtual void copy_to(generic_file &ref);
virtual void copy_to(generic_file &ref, const infinint & crc_size, crc * & value);
U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied
infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied
bool diff(generic_file & f,
const infinint & me_read_ahead,
const infinint & you_read_ahead,
const infinint & crc_size,
crc * & value);
bool diff(generic_file & f,
const infinint & me_read_ahead,
const infinint & you_read_ahead,
const infinint & crc_size,
crc * & value,
infinint & err_offset);
void reset_crc(const infinint & width);
bool crc_status() const { return active_read == &generic_file::read_crc; };
crc *get_crc();
void sync_write();
void flush_read();
protected :
void set_mode(gf_mode x) { rw = x; };
virtual void inherited_read_ahead(const infinint & amount) = 0;
virtual U_I inherited_read(char *a, U_I size) = 0;
virtual void inherited_write(const char *a, U_I size) = 0;
virtual void inherited_truncate(const infinint & pos) = 0;
virtual void inherited_sync_write() = 0;
virtual void inherited_flush_read() = 0;
virtual void inherited_terminate() = 0;
bool is_terminated() const { return terminated; };
private :
gf_mode rw;
crc *checksum;
bool terminated;
bool no_read_ahead;
U_I (generic_file::* active_read)(char *a, U_I size);
void (generic_file::* active_write)(const char *a, U_I size);
void enable_crc(bool mode);
U_I read_crc(char *a, U_I size);
void write_crc(const char *a, U_I size);
void destroy();
void nullifyptr() noexcept { checksum = nullptr; };
void copy_from(const generic_file & ref);
void move_from(generic_file && ref) noexcept;
};
} // end of namespace
#endif
libdar::crc
pure virtual class defining interface of a CRC object
Definition crc.hpp:47
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition generic_file.hpp:77
libdar::generic_file::write
virtual void write(const char *a, U_I size) override
write data to the generic_file inherited from proto_generic_file
libdar::generic_file::copy_to
infinint copy_to(generic_file &ref, infinint size)
copy the given amount to the object in argument
libdar::generic_file::truncatable
virtual bool truncatable(const infinint &pos) const =0
whether the implementation is able to truncate to the given position
libdar::generic_file::terminate
void terminate()
destructor-like call, except that it is allowed to throw exceptions
libdar::generic_file::inherited_truncate
virtual void inherited_truncate(const infinint &pos)=0
truncate file at the give offset
libdar::generic_file::inherited_sync_write
virtual void inherited_sync_write()=0
write down any pending data
libdar::generic_file::~generic_file
~generic_file() noexcept(false)
virtual destructor,
Definition generic_file.hpp:97
libdar::generic_file::skip
virtual bool skip(const infinint &pos)=0
skip at the absolute position
libdar::generic_file::generic_file
generic_file(const generic_file &ref)
copy constructor
Definition generic_file.hpp:83
libdar::generic_file::truncate
virtual void truncate(const infinint &pos)
truncate file at the given offset
libdar::generic_file::generic_file
generic_file(gf_mode m)
main constructor
Definition generic_file.hpp:80
libdar::generic_file::operator=
generic_file & operator=(const generic_file &ref)
assignment operator
Definition generic_file.hpp:89
libdar::generic_file::copy_to
virtual void copy_to(generic_file &ref)
copy all data from current position to the object in argument
libdar::generic_file::skippable
virtual bool skippable(skippability direction, const infinint &amount)=0
whether the implementation is able to skip
libdar::generic_file::copy_to
virtual void copy_to(generic_file &ref, const infinint &crc_size, crc *&value)
copy all data from the current position to the object in argument and computes a CRC value of the tra...
libdar::generic_file::crc_status
bool crc_status() const
to known whether CRC calculation is activated or not
Definition generic_file.hpp:248
libdar::generic_file::inherited_read_ahead
virtual void inherited_read_ahead(const infinint &amount)=0
tells the object that several calls to read() will follow to probably obtain at least the given amoun...
libdar::generic_file::read_back
S_I read_back(char &a)
skip back one char, read on char and skip back one char
libdar::generic_file::read_ahead
void read_ahead(const infinint &amount)
libdar::generic_file::read_forward
S_I read_forward(char &a)
read one char
Definition generic_file.hpp:140
libdar::generic_file::get_position
virtual infinint get_position() const =0
get the current read/write position
libdar::generic_file::flush_read
void flush_read()
be ready to read at current position, reseting all pending data for reading, cached and in compressio...
libdar::generic_file::skip_relative
virtual bool skip_relative(S_I x)=0
skip relatively to the current position
libdar::generic_file::operator==
bool operator==(generic_file &ref)
libdar::generic_file::skip_to_eof
virtual bool skip_to_eof()=0
skip to the end of file
libdar::generic_file::inherited_write
virtual void inherited_write(const char *a, U_I size)=0
implementation of the write() operation
libdar::generic_file::inherited_read
virtual U_I inherited_read(char *a, U_I size)=0
implementation of read() operation
libdar::generic_file::diff
bool diff(generic_file &f, const infinint &me_read_ahead, const infinint &you_read_ahead, const infinint &crc_size, crc *&value, infinint &err_offset)
libdar::generic_file::diff
bool diff(generic_file &f, const infinint &me_read_ahead, const infinint &you_read_ahead, const infinint &crc_size, crc *&value)
compares the contents with the object in argument
libdar::generic_file::generic_file
generic_file(generic_file &&ref) noexcept
move constructor
Definition generic_file.hpp:86
libdar::generic_file::reset_crc
void reset_crc(const infinint &width)
reset CRC on read or writen data
libdar::generic_file::ignore_read_ahead
void ignore_read_ahead(bool mode)
Definition generic_file.hpp:123
libdar::generic_file::is_terminated
bool is_terminated() const
Definition generic_file.hpp:327
libdar::generic_file::get_mode
gf_mode get_mode() const
retreive the openning mode for this object
Definition generic_file.hpp:109
libdar::generic_file::inherited_terminate
virtual void inherited_terminate()=0
destructor-like call, except that it is allowed to throw exceptions
libdar::generic_file::write
void write(const std::string &arg)
write a string to the generic_file
libdar::generic_file::get_crc
crc * get_crc()
get CRC of the transfered date since last reset
libdar::generic_file::sync_write
void sync_write()
write any pending data
libdar::generic_file::read
virtual U_I read(char *a, U_I size) override
read data from the generic_file inherited from proto_generic_file
libdar::generic_file::copy_to
U_32 copy_to(generic_file &ref, U_32 size)
small copy (up to 4GB) with CRC calculation
libdar::generic_file::inherited_flush_read
virtual void inherited_flush_read()=0
reset internal engine, flush caches in order to read the data at current position
libdar::infinint
the arbitrary large positive integer class
Definition real_infinint.hpp:62
libdar::proto_generic_file
ancestor class of generic_file
Definition proto_generic_file.hpp:55
crc.hpp
class crc definition, used to handle Cyclic Redundancy Checks
gf_mode.hpp
generic modes to open file
libdar::gf_mode
gf_mode
generic_file openning modes
Definition gf_mode.hpp:44
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
integers.hpp
are defined here basic integer types that tend to be portable
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47
proto_generic_file.hpp
precursor class of generic_file used to avoid cyclic dependencies with storage and infinint