source: trunk/lib/Configuration.h

Last change on this file was 1515, checked in by Peter Johansson, 9 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1#ifndef _theplu_svndigest_configuration_
2#define _theplu_svndigest_configuration_
3
4// $Id: Configuration.h 1515 2012-09-26 00:35:10Z peter $
5
6/*
7  Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010, 2011, 2012 Peter Johansson
9
10  This file is part of svndigest, http://dev.thep.lu.se/svndigest
11
12  svndigest is free software; you can redistribute it and/or modify it
13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 3 of the License, or
15  (at your option) any later version.
16
17  svndigest is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "Alias.h"
27#include "utility.h"
28
29#include <iostream>
30#include <map>
31#include <stdexcept>
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace theplu{
37namespace svndigest{
38
39  ///
40  /// Configuration class takes care of all setting defined in the
41  /// configuration file.
42  ///
43  class Configuration
44  {
45  public:
46    static Configuration& instance(void);
47
48    /**
49       \return Hexadecimal color code (e.g. 5aee4a) that is used in
50       blame output and as line colors in plots. If no color is
51       configured for \a author, an empty string is returned.
52     */
53    std::string author_str_color(const std::string& author) const;
54
55    /**
56       The map to lookup the author-color mapping set in the
57       configuration file. The color code is a six digit hexadecimal
58       number rrggbb.
59
60       \return The author-color map
61    */
62    const std::map<std::string, std::string>& author_colors(void) const;
63
64    /**
65       \return vector of parse codons for the given \a file_name
66     */
67    const std::vector<std::pair<std::string, std::string> >*
68    codon(std::string file_name) const;
69
70    /**
71       A string describing the configuration relevant for file named
72       \a path
73     */
74    std::string code(const std::string& path) const;
75
76    ///
77    /// @brief Aliases for Copyright
78    ///
79    const std::map<std::string, Alias>& copyright_alias(void) const;
80
81    /**
82       @return copyright string e.g. 'Copyright (C)' (default)
83    */
84    const std::string& copyright_string(void) const;
85
86    /**
87       \return pdf, png, none, or svg
88     */
89    const std::string& image_anchor_format(void) const;
90
91    /**
92       \brief set image_anchor_format
93     */
94    void image_anchor_format(const std::string&);
95
96    /**
97       \return png, none, svg, or svgz
98     */
99    const std::string& image_format(void) const;
100
101    /**
102       \brief set image_format
103     */
104    void image_format(const std::string&);
105
106    ///
107    /// throw if stream is not a valid config
108    ///
109    /// @brief load configuration from stream
110    ///
111    void load(std::istream&);
112
113    ///
114    /// @return true if we should warn about missing copyright statement
115    ///
116    bool  missing_copyright_warning(void) const;
117
118    /**
119       \return true (default) if we wanna output blame information
120     */
121    bool output_blame_information(void) const;
122
123    /**
124       \return true (default) if we wanna output stats for file
125     */
126    bool output_file(void) const;
127
128    /**
129       svn properties set in config file
130
131       This is supposed to be used only in SVNproperty class
132
133       \return NULL if there is no prop for \a filename in config
134     */
135    const std::map<std::string, std::string>&
136    svn_properties(const std::string& filename) const;
137
138    /**
139       \return size of a tab, i.e., how many spaces are ued to display
140       a space (in blame output)
141     */
142    size_t tab_size(void) const;
143
144    ///
145    /// @return root for the trac envrionment, e.g.,
146    /// http://dev.thep.lu.se/svndigest/
147    ///
148    std::string trac_root(void) const;
149
150  private:
151    ///
152    /// Creates a Config object with default settings.
153    ///
154    /// @brief Default Constructor
155    ///
156    Configuration(void);
157    // Copy Constructor not implemented
158    Configuration(const Configuration&);
159    // assignment not implemented because assignment is always self-assignment
160    Configuration& operator=(const Configuration&);
161    // destructor not implemented
162    ~Configuration(void);
163
164    friend std::ostream& operator<<(std::ostream&, const Configuration&);
165
166    void add_codon(std::string, std::string, std::string);
167
168    void clear(void);
169    const std::pair<std::string,std::string>* dictionary(std::string lhs) const;
170    bool equal_false(std::string) const;
171    bool equal_true(std::string) const;
172
173    /**
174       find first element in range, [first, last) for which
175       element->first matches filename.
176
177       Iterator->first must return string, i.e., [first, last) is
178       often a range of pair<string, T>
179
180       If no match is found, last is returned.
181     */
182    template<typename Iterator>
183    Iterator find_fn(Iterator first, Iterator last,
184                     const std::string& filename) const;
185
186    ///
187    /// @brief load deafult configuration
188    ///
189    void load(void);
190
191    void set_default(void);
192    /**
193       Translate string \a str using dictionary \a dict
194
195       \note \a str must be equal to d.first (see regexp(5)),
196       or behavior is unspecified.
197
198       \throw if a '$' character is not followed by a positive integer
199       that is not larger than number of wildcards in dictionary \a d.
200     */
201    std::string translate(const std::string& str,
202                          const std::pair<std::string, std::string>& d) const;
203
204    void validate_dictionary(void) const;
205
206    static Configuration* instance_;
207
208    std::map<std::string, std::string> author_color_;
209    std::map<std::string, Alias> copyright_alias_;
210    std::string copyright_string_;
211
212    bool missing_copyright_warning_;
213
214    typedef std::vector<std::pair<std::string, std::string> > VectorPair;
215    typedef std::vector<std::pair<std::string, VectorPair> > String2Codons;
216    String2Codons string2codons_;
217
218    VectorPair dictionary_;
219    std::string image_anchor_format_;
220    std::string image_format_;
221    bool output_blame_information_;
222    bool output_file_;
223    std::string trac_root_;
224    size_t tab_size_;
225
226    typedef std::map<std::string, std::string> str_map;
227    typedef std::pair<std::string, str_map> props;
228    std::vector<props> svn_props_;
229    const str_map empty_str_map_;
230  };
231
232  ///
233  /// @brief Output operator
234  ///
235  std::ostream& operator<<(std::ostream&, const Configuration&);
236
237  /**
238     If first character is '\n' replace it with "<NEWLINE>"
239   */
240  std::string trans_end_code(std::string);
241
242  /**
243     If last character is '\n' replace it with "<NEWLINE>"
244   */
245  std::string trans_beg_code(std::string);
246
247  /**
248     Trim \a c from beginning and end of string \a str;
249
250     \return resulting string
251
252     \throw if first or last character of \a str is NOT character \a c
253  */
254  std::string trim(std::string str, char c);
255
256  /**
257     \brief Class for errors when reading config file.
258   */
259  class Config_error : public std::runtime_error
260  {
261  public:
262    Config_error(const std::string& line, const std::string& message);
263  };
264
265  // template implementation
266
267  template<typename Iterator>
268  Iterator Configuration::find_fn(Iterator first, Iterator last,
269                                  const std::string& filename) const
270  {
271    for( ; first!=last; ++first) {
272      if (fnmatch(first->first, filename))
273        return first;
274    }
275    return last;
276  }
277
278
279}} // end of namespace svndigest and namespace theplu
280
281#endif
Note: See TracBrowser for help on using the repository browser.