Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
storage.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 // $Id: storage.hpp,v 1.20.2.3 2012/02/25 14:43:44 edrusb Rel $
22 //
23 /*********************************************************************/
24 
28 
29 #include "../my_config.h"
30 #include "erreurs.hpp"
31 #include "integers.hpp"
32 #include "special_alloc.hpp"
33 
34 #ifdef LIBDAR_MODE
35 #include "infinint.hpp"
36 #endif
37 
38  // it is necessary to not protect the previous inclusion inside
39  // the STORAGE_HPP protection to avoid cyclic dependancies.
40 
41 #ifndef STORAGE_HPP
42 #define STORAGE_HPP
43 
44 #ifndef LIBDAR_MODE
45 namespace libdar
46 {
47  class infinint;
48 }
49 #endif
50 
51 namespace libdar
52 {
53  class generic_file;
54 
56 
59 
60  class storage
61  {
62  private:
63  struct cellule
64  {
65  cellule() : next(NULL), prev(NULL), data(NULL), size(0) {};
66  struct cellule *next, *prev;
67  unsigned char *data;
68  U_32 size;
69  };
70 
71  public:
72  storage(U_32 size)
73  { make_alloc(size, first, last); };
74  storage(const infinint & size);
75  storage(const storage & ref)
76  { copy_from(ref); };
77  storage(generic_file & f, const infinint &size);
78  ~storage()
79  { detruit(first); };
80 
81  const storage & operator = (const storage & val)
82  { detruit(first); copy_from(val); return *this; };
83 
84  bool operator < (const storage & ref) const
85  { return difference(ref) < 0; }; // true if arg uses more space than this
86  bool operator == (const storage & ref) const
87  { return difference(ref) == 0; }; //true if arg have same space than this
88  bool operator > (const storage & ref) const
89  { return difference(ref) > 0; };
90  bool operator <= (const storage & ref) const
91  { return difference(ref) <= 0; };
92  bool operator >= (const storage & ref) const
93  { return difference(ref) >= 0; };
94  bool operator != (const storage & ref) const
95  { return difference(ref) != 0; };
96  unsigned char & operator [](infinint position);
97  unsigned char operator [](const infinint & position) const;
98  infinint size() const;
99  void clear(unsigned char val = 0);
100  void dump(generic_file & f) const;
101 
102  class iterator
103  {
104  public :
105  iterator() : ref(NULL), cell(NULL), offset(0) {};
106  // default constructor by reference is OK
107  // default destructor is OK
108  // default operator = is OK
109 
110  iterator operator ++ (S_I x)
111  { iterator ret = *this; skip_plus_one(); return ret; };
112  iterator operator -- (S_I x)
113  { iterator ret = *this; skip_less_one(); return ret; };
114  iterator & operator ++ ()
115  { skip_plus_one(); return *this; };
116  iterator & operator -- ()
117  { skip_less_one(); return *this; };
118  iterator operator + (U_32 s) const
119  { iterator ret = *this; ret += s; return ret; };
120  iterator operator - (U_32 s) const
121  { iterator ret = *this; ret -= s; return ret; };
122  iterator & operator += (U_32 s);
123  iterator & operator -= (U_32 s);
124  unsigned char &operator *() const;
125 
126  void skip_to(const storage & st, infinint val); // absolute position in st
127  infinint get_position() const;
128 
129  bool operator == (const iterator & cmp) const
130  { return ref == cmp.ref && cell == cmp.cell && offset == cmp.offset; };
131  bool operator != (const iterator & cmp) const
132  { return ! (*this == cmp); };
133 
134  private:
135  static const U_32 OFF_BEGIN = 1;
136  static const U_32 OFF_END = 2;
137 
138  const storage *ref;
139  struct cellule *cell;
140  U_32 offset;
141 
142  void relative_skip_to(S_32 val);
143  bool points_on_data() const
144  { return ref != NULL && cell != NULL && offset < cell->size; };
145 
146  inline void skip_plus_one();
147  inline void skip_less_one();
148 
149  friend class storage;
150  };
151 
152  // public storage methode using iterator
153 
154  iterator begin() const
155  { iterator ret; ret.cell = first; if(ret.cell != NULL) ret.offset = 0; else ret.offset = iterator::OFF_END; ret.ref = this; return ret; };
156  iterator end() const
157  { iterator ret; ret.cell = NULL; ret.offset = iterator::OFF_END; ret.ref = this; return ret; };
158 
159  // WARNING for the two following methods :
160  // there is no "reverse_iterator" type, unlike the standart lib,
161  // thus when going from rbegin() to rend(), you must use the -- operator
162  // unlike the stdlib, that uses the ++ operator. this is the only difference in use with stdlib.
163  iterator rbegin() const
164  { iterator ret; ret.cell = last; ret.offset = last != NULL ? last->size-1 : 0; ret.ref = this; return ret; };
165  iterator rend() const
166  { iterator ret; ret.cell = NULL, ret.offset = iterator::OFF_BEGIN; ret.ref = this; return ret; };
167 
169 
173  U_I write(iterator & it, unsigned char *a, U_I size);
174  U_I read(iterator & it, unsigned char *a, U_I size) const;
175  bool write(iterator & it, unsigned char a)
176  { return write(it, &a, 1) == 1; };
177  bool read(iterator & it, unsigned char &a) const
178  { return read(it, &a, 1) == 1; };
179 
180  // after one of these 3 calls, the iterator given in argument are undefined (they may point nowhere)
181  void insert_null_bytes_at_iterator(iterator it, U_I size);
182  void insert_const_bytes_at_iterator(iterator it, unsigned char a, U_I size);
183  void insert_bytes_at_iterator(iterator it, unsigned char *a, U_I size);
184  void insert_as_much_as_necessary_const_byte_to_be_as_wider_as(const storage & ref, const iterator & it, unsigned char value);
185  void remove_bytes_at_iterator(iterator it, U_I number);
186  void remove_bytes_at_iterator(iterator it, infinint number);
187 
188 #ifdef LIBDAR_SPECIAL_ALLOC
189  USE_SPECIAL_ALLOC(storage);
190 #endif
191  private:
192  struct cellule *first, *last;
193 
194  void copy_from(const storage & ref);
195  S_32 difference(const storage & ref) const;
196  void reduce(); // heuristic that tries to free some memory;
197  void insert_bytes_at_iterator_cmn(iterator it, bool constant, unsigned char *a, U_I size);
198  void fusionne(struct cellule *a_first, struct cellule *a_last, struct cellule *b_first, struct cellule *b_last,
199  struct cellule *&res_first, struct cellule * & res_last);
200 
202  // STATIC statments :
203  //
204 
205  static void detruit(struct cellule *c);
206  static void make_alloc(U_32 size, struct cellule * & begin, struct cellule * & end);
207  static void make_alloc(infinint size, cellule * & begin, struct cellule * & end);
208 
209  friend class storage::iterator;
210  };
211 
212  inline void storage::iterator::skip_plus_one()
213  {
214  if(cell != NULL)
215  if(++offset >= cell->size)
216  {
217  cell = cell->next;
218  if(cell != NULL)
219  offset = 0;
220  else
221  offset = OFF_END;
222  }
223  }
224 
225  inline void storage::iterator::skip_less_one()
226  {
227  if(cell != NULL)
228  {
229  if(offset > 0)
230  --offset;
231  else
232  {
233  cell = cell->prev;
234  if(cell != NULL)
235  offset = cell->size - 1;
236  else
237  offset = OFF_BEGIN;
238  }
239  }
240  }
241 
242 } // end of namespace
243 
244 #endif