source: branches/0.9-stable/lib/Configuration.h @ 1501

Last change on this file since 1501 was 1501, checked in by Jari Häkkinen, 10 years ago

Addresses #516. clang++ requires include utility.h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1#ifndef _theplu_svndigest_configuration_
2#define _theplu_svndigest_configuration_
3
4// $Id: Configuration.h 1501 2012-08-28 10:20:12Z jari $
5
6/*
7  Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010 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    /// @brief Aliases for Copyright
72    ///
73    const std::map<std::string, Alias>& copyright_alias(void) const;
74
75    /**
76       @return copyright string e.g. 'Copyright (C)' (default)
77    */
78    const std::string& copyright_string(void) const;
79
80    /**
81       \return pdf, png, none, or svg
82     */
83    const std::string& image_anchor_format(void) const;
84
85    /**
86       \brief set image_anchor_format
87     */
88    void image_anchor_format(const std::string&);
89
90    /**
91       \return png, none, svg, or svgz
92     */
93    const std::string& image_format(void) const;
94
95    /**
96       \brief set image_format
97     */
98    void image_format(const std::string&);
99
100    ///
101    /// throw if stream is not a valid config
102    ///
103    /// @brief load configuration from stream
104    ///
105    void load(std::istream&);
106
107    ///
108    /// @return true if we should warn about missing copyright statement
109    ///
110    bool  missing_copyright_warning(void) const;
111
112    /**
113       \return true (default) if we wanna output blame information
114     */
115    bool output_blame_information(void) const;
116
117    /**
118       \return true (default) if we wanna output stats for file
119     */
120    bool output_file(void) const;
121
122    /**
123       svn properties set in config file
124
125       This is supposed to be used only in SVNproperty class
126
127       \return NULL if there is no prop for \a filename in config
128     */
129    const std::map<std::string, std::string>& 
130    svn_properties(const std::string& filename) const;
131
132    /**
133       \return size of a tab, i.e., how many spaces are ued to display
134       a space (in blame output)
135     */
136    size_t tab_size(void) const;
137
138    ///
139    /// @return root for the trac envrionment, e.g.,
140    /// http://dev.thep.lu.se/svndigest/
141    ///
142    std::string trac_root(void) const;
143
144  private:
145    ///
146    /// Creates a Config object with default settings.
147    ///
148    /// @brief Default Constructor
149    ///
150    Configuration(void);
151    // Copy Constructor not implemented
152    Configuration(const Configuration&);
153    // assignment not implemented because assignment is always self-assignment
154    Configuration& operator=(const Configuration&);
155    // destructor not implemented
156    ~Configuration(void);
157
158    friend std::ostream& operator<<(std::ostream&, const Configuration&);
159
160    void add_codon(std::string, std::string, std::string);
161
162    void clear(void);
163    const std::pair<std::string,std::string>* dictionary(std::string lhs) const;
164    bool equal_false(std::string) const;
165    bool equal_true(std::string) const;
166
167    /**
168       find first element in range, [first, last) for which
169       element->first matches filename.
170
171       Iterator->first must return string, i.e., [first, last) is
172       often a range of pair<string, T>
173
174       If no match is found, last is returned.
175     */
176    template<typename Iterator>
177    Iterator find_fn(Iterator first, Iterator last,
178                     const std::string& filename) const;
179
180    ///
181    /// @brief load deafult configuration
182    ///
183    void load(void);
184
185    void set_default(void);
186    /**
187       Translate string \a str using dictionary \a dict
188
189       \note \a str must be equal to d.first (see regexp(5)),
190       or behavior is unspecified.
191
192       \throw if a '$' character is not followed by a positive integer
193       that is not larger than number of wildcards in dictionary \a d.
194     */
195    std::string translate(const std::string& str,
196                          const std::pair<std::string, std::string>& d) const;
197   
198    void validate_dictionary(void) const;
199
200    static Configuration* instance_;
201
202    std::map<std::string, std::string> author_color_;
203    std::map<std::string, Alias> copyright_alias_;
204    std::string copyright_string_;
205
206    bool missing_copyright_warning_;
207   
208    typedef std::vector<std::pair<std::string, std::string> > VectorPair;
209    typedef std::vector<std::pair<std::string, VectorPair> > String2Codons; 
210    String2Codons string2codons_;
211
212    VectorPair dictionary_;
213    std::string image_anchor_format_;
214    std::string image_format_;
215    bool output_blame_information_;
216    bool output_file_;
217    std::string trac_root_;
218    size_t tab_size_;
219
220    typedef std::map<std::string, std::string> str_map;
221    typedef std::pair<std::string, str_map> props;
222    std::vector<props> svn_props_;
223    const str_map empty_str_map_;
224  };
225
226  ///
227  /// @brief Output operator
228  ///
229  std::ostream& operator<<(std::ostream&, const Configuration&);
230
231  /**
232     If first character is '\n' replace it with "<NEWLINE>"
233   */
234  std::string trans_end_code(std::string);
235
236  /**
237     If last character is '\n' replace it with "<NEWLINE>"
238   */
239  std::string trans_beg_code(std::string);
240
241  /**
242     Trim \a c from beginning and end of string \a str;
243     
244     \return resulting string
245
246     \throw if first or last character of \a str is NOT character \a c
247  */
248  std::string trim(std::string str, char c);
249
250  /**
251     \brief Class for errors when reading config file.
252   */
253  class Config_error : public std::runtime_error
254  {
255  public:
256    Config_error(const std::string& line, const std::string& message);
257  };
258
259  // template implementation
260
261  template<typename Iterator>
262  Iterator Configuration::find_fn(Iterator first, Iterator last,
263                                  const std::string& filename) const
264  {
265    for( ; first!=last; ++first) {
266      if (fnmatch(first->first.c_str(), filename.c_str()))
267        return first;
268    }
269    return last;
270  }
271
272
273}} // end of namespace svndigest and namespace theplu
274
275#endif
276
277
Note: See TracBrowser for help on using the repository browser.