Bayeux  3.4.1
Core Foundation library for SuperNEMO
smart_filename.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date : 2011-09-12
4  * Last modified : 2011-09-12
5  *
6  * Copyright (C) 2011 Francois Mauger <mauger@lpccaen.in2p3.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  * Description:
24  *
25  * Automated incremental filenames
26  *
27  */
28 #ifndef DATATOOLS_SMART_FILENAME_H
29 #define DATATOOLS_SMART_FILENAME_H
30 
31 // Standard Library:
32 #include <iostream>
33 #include <string>
34 #include <vector>
35 
36 // Third Party:
37 // - Boost:
38 #include <boost/cstdint.hpp>
39 
40 // This Project:
41 #include <datatools/properties.h>
42 #include <datatools/logger.h>
43 
44 namespace datatools {
45 
53  {
54  public:
55  static const int MODE_INCREMENTAL_UNRANGED = -1;
56  static const int MODE_INCREMENTAL_DEFAULT_START = 0;
57  static const int MODE_INCREMENTAL_DEFAULT_INCREMENT = 1;
58 
59  enum mode_t {
61  MODE_SINGLE = 0x1,
62  MODE_LIST = 0x2,
64  };
65 
66  struct labels {
67  static const std::string & mode_single();
68  static const std::string & mode_list();
69  static const std::string & mode_incremental();
70  };
71 
72  typedef std::vector<std::string> list_type;
73  typedef list_type::const_iterator const_iterator;
74 
77 
79  virtual ~smart_filename();
80 
83 
86 
88  int get_mode() const;
89 
91  bool is_initialized() const;
92 
94  bool is_single() const;
95 
97  bool is_list() const;
98 
100  bool is_incremental() const;
101 
103  bool is_valid() const;
104 
106  bool is_ranged() const;
107 
109  bool is_expand_path() const;
110 
112  size_t size() const;
113 
115  size_t current_size() const;
116 
118  const_iterator begin() const;
119 
121  const_iterator end() const;
122 
124  const std::string & operator[](int index_) const;
125 
127  const std::string & get_filename(int index_) const;
128 
130  bool has_filename(const std::string & name_, bool expand_ = true) const;
131 
133  void set(const std::string & filename_);
134 
136  void set_single(const std::string & filename_);
137 
139  void add(const std::string & filename_);
140 
142  void add_to_list(const std::string & filename_);
143 
145  void reset();
146 
148  void build_incremental_filename(int increment_index_,
149  std::string & filename_) const;
150 
152  void dump(std::ostream & out_ = std::clog) const;
153 
155  void initialize(const properties & config_);
156 
158  void print_list_of_filenames(std::ostream & out_ = std::clog) const;
159 
161  void store_list_of_filenames(const std::string & list_filename_,
162  bool append_ = true) const;
163 
165  static void make_single(smart_filename & smart_filename_,
166  const std::string & filename_,
167  bool expand_path_ = true);
168 
170  static void make_list(smart_filename & smart_filename_,
171  bool allow_duplication_ = false,
172  bool expand_path_ = true);
173 
175  static void make_list(smart_filename & smart_filename_,
176  const std::string & list_file_,
177  bool allow_duplication_ = false,
178  bool expand_path_ = true);
179 
181  static void make_incremental(smart_filename & smart_filename_,
182  const std::string & path_,
183  const std::string & prefix_,
184  const std::string & extension_,
185  int stopping_index_,
186  int starting_index_ = 0,
187  int increment_index_ = 1,
188  const std::string & suffix_ = "",
189  int incremental_index_ndigit_ = 0,
190  bool expand_path_ = true);
191 
193  static void make_unranged_incremental(smart_filename & smart_filename_,
194  const std::string & path_,
195  const std::string & prefix_,
196  const std::string & extension_,
197  int starting_index_ = 0,
198  int increment_index_ = 1,
199  const std::string & suffix_ = "",
200  int incremental_index_ndigit_ = 0,
201  bool expand_path_ = true);
202 
203  protected:
204 
206  void set_mode(int);
207 
209  void add_list(const std::string & filename_);
210 
212  void set_list_allow_duplication(bool);
213 
215  void set_current_index(int);
216 
217  private:
218 
219  datatools::logger::priority _logging_;
220  uint32_t _mode_;
221  bool _expand_path_;
222  list_type _list_;
223  bool _list_allow_duplication_;
224  bool _ranged_;
225  std::string _incremental_path_;
226  std::string _incremental_prefix_;
227  std::string _incremental_suffix_;
228  std::string _incremental_extension_;
229  int32_t _incremental_starting_index_;
230  int32_t _incremental_stopping_index_;
231  int32_t _incremental_increment_;
232  uint32_t _incremental_index_ndigit_;
233  };
234 
235 } // end of namespace datatools
236 
237 /***************
238  * OCD support *
239  ***************/
240 #include <datatools/ocd_macros.h>
242 
243 #endif // DATATOOLS_SMART_FILENAME_H
244 
245 // Local Variables: --
246 // mode: c++ --
247 // c-file-style: "gnu" --
248 // tab-width: 2 --
249 // End: --
smart_filename()
Default constructor:
static const int MODE_INCREMENTAL_DEFAULT_INCREMENT
Definition: smart_filename.h:57
const_iterator begin() const
Return the first const iterator from the embedded list of filenames.
void dump(std::ostream &out_=std::clog) const
Raw print.
static const std::string & mode_list()
const_iterator end() const
Return the fpast-the-end const iterator from the embedded list of filenames.
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
Definition: smart_filename.h:63
void add(const std::string &filename_)
Add a filename in list mode.
void set_mode(int)
Set the mode.
Definition: smart_filename.h:66
void print_list_of_filenames(std::ostream &out_=std::clog) const
Print the list of filenames.
const std::string & operator[](int index_) const
Access to a filename by index.
bool is_ranged() const
Check is the list of filenames is ranged.
static const int MODE_INCREMENTAL_DEFAULT_START
Definition: smart_filename.h:56
static const std::string & mode_incremental()
static const int MODE_INCREMENTAL_UNRANGED
Definition: smart_filename.h:55
void set_logging_priority(datatools::logger::priority p_)
Set logging priority.
Definition: smart_filename.h:62
void add_list(const std::string &filename_)
Add a file name in the embedded list of filenames.
static void make_single(smart_filename &smart_filename_, const std::string &filename_, bool expand_path_=true)
Make a smart_filename object in single mode.
DOCD_CLASS_DECLARATION(my::algo)
Declaration of the OCD support for the my::algo class.
static void make_unranged_incremental(smart_filename &smart_filename_, const std::string &path_, const std::string &prefix_, const std::string &extension_, int starting_index_=0, int increment_index_=1, const std::string &suffix_="", int incremental_index_ndigit_=0, bool expand_path_=true)
Make a smart_filename object in unranged incremental mode.
void set_single(const std::string &filename_)
Set the single mode and associated filename.
bool is_valid() const
Check if the object is valid.
void reset()
Reset the object.
void set(const std::string &filename_)
Set the filename in single mode.
void initialize(const properties &config_)
Initialize from a list of configuration parameters.
Definition: smart_filename.h:60
datatools::logger::priority get_logging_priority() const
Returns logging priority.
void store_list_of_filenames(const std::string &list_filename_, bool append_=true) const
Store the list of filenames in a file.
bool has_filename(const std::string &name_, bool expand_=true) const
Check if a given filename exists from the embedded list of filenames.
void build_incremental_filename(int increment_index_, std::string &filename_) const
Build the filename from the increment index in incremental mode.
static void make_incremental(smart_filename &smart_filename_, const std::string &path_, const std::string &prefix_, const std::string &extension_, int stopping_index_, int starting_index_=0, int increment_index_=1, const std::string &suffix_="", int incremental_index_ndigit_=0, bool expand_path_=true)
Make a smart_filename object in incremental mode.
Definition: smart_filename.h:61
const std::string & get_filename(int index_) const
Access to a filename by index.
bool is_list() const
Check the list mode.
size_t current_size() const
Return the current number of filenames.
A class that builds an arbitrary list of filenames on user request. Supported modes are:
Definition: smart_filename.h:52
void add_to_list(const std::string &filename_)
Add a filename in list mode.
size_t size() const
Return the number of filenames.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
Utilities for logging information.
bool is_single() const
Check the single mode.
void set_list_allow_duplication(bool)
Set the flag to allow duplicated filenames.
list_type::const_iterator const_iterator
Definition: smart_filename.h:73
std::vector< std::string > list_type
Definition: smart_filename.h:72
bool is_initialized() const
Check the initialization flag.
void set_current_index(int)
Set the current in incremental/list mode.
virtual ~smart_filename()
Destructor.
static void make_list(smart_filename &smart_filename_, bool allow_duplication_=false, bool expand_path_=true)
Make a smart_filename object in list mode.
bool is_incremental() const
Check the incremental mode.
static const std::string & mode_single()
bool is_expand_path() const
Check the expand path flag.
int get_mode() const
Return the mode.
mode_t
Definition: smart_filename.h:59
A dictionary of arbitrary properties.
Definition: properties.h:125