source: trunk/lib/Configuration.h @ 1378

Last change on this file since 1378 was 1378, checked in by Peter Johansson, 10 years ago

include configuration string in svncopyright cache.

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