/usr/include/dar/storage.hpp is in libdar-dev 2.4.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | /*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2052 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// to contact the author : http://dar.linux.free.fr/email.html
/*********************************************************************/
// $Id: storage.hpp,v 1.20 2011/01/22 22:39:10 edrusb Rel $
//
/*********************************************************************/
/// \file storage.hpp
/// \brief contains a class that permits arbitrary large data storage
/// \ingroup Private
#include "/usr/include/dar/libdar_my_config.h"
#include "/usr/include/dar/erreurs.hpp"
#include "/usr/include/dar/integers.hpp"
#ifdef LIBDAR_MODE
#include "/usr/include/dar/infinint.hpp"
#endif
// it is necessary to not protect the previous inclusion inside
// the STORAGE_HPP protection to avoid cyclic dependancies.
#ifndef STORAGE_HPP
#define STORAGE_HPP
#ifndef LIBDAR_MODE
namespace libdar
{
class infinint;
}
#endif
namespace libdar
{
class generic_file;
/// arbitrary large storage structure
/// used to store infinint
/// \ingroup Private
class storage
{
private:
struct cellule
{
cellule() : next(NULL), prev(NULL), data(NULL), size(0) {};
struct cellule *next, *prev;
unsigned char *data;
U_32 size;
};
public:
storage(U_32 size)
{ E_BEGIN; make_alloc(size, first, last); E_END("storage::storage","U_32"); };
storage(const infinint & size);
storage(const storage & ref)
{ E_BEGIN; copy_from(ref); E_END("storage::storage", "storage &"); };
storage(generic_file & f, const infinint &size);
~storage()
{ E_BEGIN; detruit(first); E_END("storage::~storage", ""); };
const storage & operator = (const storage & val)
{ E_BEGIN; detruit(first); copy_from(val); return *this; E_END("storage::operator=",""); };
bool operator < (const storage & ref) const
{ E_BEGIN; return difference(ref) < 0; E_END("storage::operator <",""); }; // true if arg uses more space than this
bool operator == (const storage & ref) const
{ E_BEGIN; return difference(ref) == 0; E_END("storage::operator ==",""); }; //true if arg have same space than this
bool operator > (const storage & ref) const
{ E_BEGIN; return difference(ref) > 0; E_END("storage::operator >", ""); };
bool operator <= (const storage & ref) const
{ E_BEGIN; return difference(ref) <= 0; E_END("storage::operator <=", ""); };
bool operator >= (const storage & ref) const
{ E_BEGIN; return difference(ref) >= 0; E_END("storage::operator >=", ""); };
bool operator != (const storage & ref) const
{ E_BEGIN; return difference(ref) != 0; E_END("storage::operator !=", ""); };
unsigned char & operator [](infinint position);
unsigned char operator [](const infinint & position) const;
infinint size() const;
void clear(unsigned char val = 0);
void dump(generic_file & f) const;
class iterator
{
public :
iterator() : ref(NULL), cell(NULL), offset(0) {};
// default constructor by reference is OK
// default destructor is OK
// default operator = is OK
iterator operator ++ (S_I x)
{ E_BEGIN; iterator ret = *this; skip_plus_one(); return ret; E_END("storage::iterator::operator++", "(S_I)"); };
iterator operator -- (S_I x)
{ E_BEGIN; iterator ret = *this; skip_less_one(); return ret; E_END("storage::iterator::operator--", "(S_I)");};
iterator & operator ++ ()
{ E_BEGIN; skip_plus_one(); return *this; E_END("storage::iterator::operator++", "()"); };
iterator & operator -- ()
{ E_BEGIN; skip_less_one(); return *this; E_END("storage::iterator::operator--", "()"); };
iterator operator + (U_32 s) const
{ E_BEGIN; iterator ret = *this; ret += s; return ret; E_END("storage::iterator::operator +", ""); };
iterator operator - (U_32 s) const
{ E_BEGIN; iterator ret = *this; ret -= s; return ret; E_END("storage::iterator::operator -", ""); };
iterator & operator += (U_32 s);
iterator & operator -= (U_32 s);
unsigned char &operator *() const;
void skip_to(const storage & st, infinint val); // absolute position in st
infinint get_position() const;
bool operator == (const iterator & cmp) const
{ E_BEGIN; return ref == cmp.ref && cell == cmp.cell && offset == cmp.offset; E_END("storage::iterator::operator ==", ""); };
bool operator != (const iterator & cmp) const
{ E_BEGIN; return ! (*this == cmp); E_END("storage::iterator::operator !=", ""); };
private:
static const U_32 OFF_BEGIN = 1;
static const U_32 OFF_END = 2;
const storage *ref;
struct cellule *cell;
U_32 offset;
void relative_skip_to(S_32 val);
bool points_on_data() const
{ E_BEGIN; return ref != NULL && cell != NULL && offset < cell->size; E_END("storage::iterator::point_on_data", "");};
inline void skip_plus_one();
inline void skip_less_one();
friend class storage;
};
// public storage methode using iterator
iterator begin() const
{ E_BEGIN; iterator ret; ret.cell = first; if(ret.cell != NULL) ret.offset = 0; else ret.offset = iterator::OFF_END; ret.ref = this; return ret; E_END("storage::begin", ""); };
iterator end() const
{ E_BEGIN; iterator ret; ret.cell = NULL; ret.offset = iterator::OFF_END; ret.ref = this; return ret; E_END("storage::end", ""); };
// WARNING for the two following methods :
// there is no "reverse_iterator" type, unlike the standart lib,
// thus when going from rbegin() to rend(), you must use the -- operator
// unlike the stdlib, that uses the ++ operator. this is the only difference in use with stdlib.
iterator rbegin() const
{ E_BEGIN; iterator ret; ret.cell = last; ret.offset = last != NULL ? last->size-1 : 0; ret.ref = this; return ret; E_END("storage::rbegin", ""); };
iterator rend() const
{ E_BEGIN; iterator ret; ret.cell = NULL, ret.offset = iterator::OFF_BEGIN; ret.ref = this; return ret; E_END("storage::rend", ""); };
/// write data to the storage at the location pointed to by it
/// \param[in,out] it where to write data to, at the end this iterator points just after the data that has been wrote
/// \param[in] a gives to the address where is located the data to write to the storage object
/// \param[in] size how much bytes to write to the storage
U_I write(iterator & it, unsigned char *a, U_I size);
U_I read(iterator & it, unsigned char *a, U_I size) const;
bool write(iterator & it, unsigned char a)
{ E_BEGIN; return write(it, &a, 1) == 1; E_END("storage::write", "unsigned char"); };
bool read(iterator & it, unsigned char &a) const
{ E_BEGIN; return read(it, &a, 1) == 1; E_END("storage::read", "unsigned char"); };
// after one of these 3 calls, the iterator given in argument are undefined (they may point nowhere)
void insert_null_bytes_at_iterator(iterator it, U_I size);
void insert_const_bytes_at_iterator(iterator it, unsigned char a, U_I size);
void insert_bytes_at_iterator(iterator it, unsigned char *a, U_I size);
void insert_as_much_as_necessary_const_byte_to_be_as_wider_as(const storage & ref, const iterator & it, unsigned char value);
void remove_bytes_at_iterator(iterator it, U_I number);
void remove_bytes_at_iterator(iterator it, infinint number);
private:
struct cellule *first, *last;
void copy_from(const storage & ref);
S_32 difference(const storage & ref) const;
void reduce(); // heuristic that tries to free some memory;
void insert_bytes_at_iterator_cmn(iterator it, bool constant, unsigned char *a, U_I size);
void fusionne(struct cellule *a_first, struct cellule *a_last, struct cellule *b_first, struct cellule *b_last,
struct cellule *&res_first, struct cellule * & res_last);
///////////////////////////////
// STATIC statments :
//
static void detruit(struct cellule *c);
static void make_alloc(U_32 size, struct cellule * & begin, struct cellule * & end);
static void make_alloc(infinint size, cellule * & begin, struct cellule * & end);
friend class storage::iterator;
};
inline void storage::iterator::skip_plus_one()
{
E_BEGIN;
if(cell != NULL)
if(++offset >= cell->size)
{
cell = cell->next;
if(cell != NULL)
offset = 0;
else
offset = OFF_END;
}
E_END("storage::iterator::slik_plus_one", "");
}
inline void storage::iterator::skip_less_one()
{
E_BEGIN;
if(cell != NULL)
{
if(offset > 0)
--offset;
else
{
cell = cell->prev;
if(cell != NULL)
offset = cell->size - 1;
else
offset = OFF_BEGIN;
}
}
E_END("storage::iterator::slik_plus_one", "");
}
} // end of namespace
#endif
|