Disk ARchive  2.5.15
Full featured and portable backup and archiving tool
pile.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 
25 
26 
27 #ifndef PILE_HPP
28 #define PILE_HPP
29 
30 #include "../my_config.h"
31 
32 #include <vector>
33 #include <list>
34 #include "generic_file.hpp"
35 
36 namespace libdar
37 {
38 
41 
42  class pile : public generic_file
43  {
44  public:
49 
50  pile() : generic_file(gf_read_only) { stack.clear(); };
51  pile(const pile & ref) : generic_file(ref) { copy_from(ref); };
52  const pile & operator = (const pile & ref) { detruit(); copy_from(ref); return *this; };
53  ~pile() { detruit(); };
54 
65  void push(generic_file *f, const std::string & label = "", bool extend_mode = false);
66 
71  generic_file *pop();
72 
76  template <class T> bool pop_and_close_if_type_is(T *ptr);
77 
79  generic_file *top() { if(stack.empty()) return nullptr; else return stack.back().ptr; };
80 
82  generic_file *bottom() { if(stack.empty()) return nullptr; else return stack[0].ptr; };
83 
85  U_I size() const { return stack.size(); };
86 
88  bool is_empty() const { return stack.empty(); };
89 
91  void clear() { detruit(); };
92 
96  template<class T> void find_first_from_top(T * & ref);
97 
99  template<class T> void find_first_from_bottom(T * & ref);
100 
101 
103  generic_file *get_below(const generic_file *ref);
104 
106  generic_file *get_above(const generic_file *ref);
107 
108 
113  generic_file *get_by_label(const std::string & label);
114 
115 
116 
121  void clear_label(const std::string & label);
122 
123 
129  void add_label(const std::string & label);
130 
131 
133  void sync_write_above(generic_file *ptr);
134 
136  void flush_read_above(generic_file *ptr);
137 
138  // inherited methods from generic_file
139  // they all apply to the top generic_file object, they fail by Erange() exception if the stack is empty
140 
141  bool skippable(skippability direction, const infinint & amount);
142  bool skip(const infinint & pos);
143  bool skip_to_eof();
144  bool skip_relative(S_I x);
145  infinint get_position() const;
146 
147  void copy_to(generic_file & ref);
148  void copy_to(generic_file & ref, const infinint & crc_size, crc * & value);
149 
150  protected:
151  void inherited_read_ahead(const infinint & amount);
152  U_I inherited_read(char *a, U_I size);
153  void inherited_write(const char *a, U_I size);
154  void inherited_sync_write();
155  void inherited_flush_read();
156  void inherited_terminate();
157 
158  private:
159  struct face
160  {
161  generic_file * ptr;
162  std::list<std::string> labels;
163  }; // ok, had not much idea to find a name for that struct, "face" was the first idea found to be associated with "pile", which means stack
164  // in French but also is the name of the face of a coin where its value is written. The opposite face of a coin is called "face" in French
165  // because often a face is design there and the expression "tirer `a pile ou face" (meaning "to toss up") is very common.
166 
167  std::vector<face> stack;
168 
169  void copy_from(const pile & ref)
170  {
171  throw SRC_BUG; // it is not possible to copy an object to its another of the exact same type when only a pure virtual pointer pointing on it is available, or when no virtual "clone'-like method is available from the root pure virtual class (generic_file here).
172  };
173  void detruit();
174  std::vector<face>::iterator look_for_label(const std::string & label);
175  };
176 
177 
178  template <class T> bool pile::pop_and_close_if_type_is(T *ptr)
179  {
180  generic_file *top = nullptr;
181 
182  if(!stack.empty())
183  {
184  top = stack.back().ptr;
185  ptr = dynamic_cast<T *>(top);
186  if(ptr != nullptr)
187  {
188  stack.pop_back();
189  delete top;
190  return true;
191  }
192  else
193  return false;
194  }
195  else
196  return false;
197  }
198 
199  template <class T> void pile::find_first_from_top(T * & ref)
200  {
201  ref = nullptr;
202  for(std::vector<face>::reverse_iterator it = stack.rbegin(); it != stack.rend() && ref == nullptr; ++it)
203  ref = dynamic_cast<T *>(it->ptr);
204  }
205 
206 
207  template <class T> void pile::find_first_from_bottom(T * & ref)
208  {
209  ref = nullptr;
210  for(std::vector<face>::iterator it = stack.begin(); it != stack.end() && ref == nullptr; ++it)
211  ref = dynamic_cast<T *>(it->ptr);
212  }
213 
215 
216 } // end of namespace
217 
218 #endif
U_32 copy_to(generic_file &ref, U_32 size)
small copy (up to 4GB) with CRC calculation
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
generic_file(gf_mode m)
main constructor
this is the interface class from which all other data transfer classes inherit
read only access
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47