source: trunk/lib/Configuration.h @ 522

Last change on this file since 522 was 522, checked in by Peter Johansson, 14 years ago

improved error message when config file is invalid

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1#ifndef _theplu_svndigest_configuration_
2#define _theplu_svndigest_configuration_
3
4// $Id: Configuration.h 522 2007-12-25 00:51:27Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of svndigest, http://trac.thep.lu.se/svndigest
10
11  svndigest is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 2 of the License, or
14  (at your option) any later version.
15
16  svndigest is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "Alias.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 vector of parse codons for the given \a file_name
50     */
51    const std::vector<std::pair<std::string, std::string> >* 
52    codon(std::string file_name) const;
53
54    ///
55    /// @brief Aliases for Copyright
56    ///
57    const std::map<std::string, Alias>& copyright_alias(void) const;
58
59    ///
60    /// @brief load deafult configuration
61    ///
62    void load(void);
63
64    ///
65    /// throw if stream is not a valid config
66    ///
67    /// @brief load configuration from stream
68    ///
69    void load(std::istream&);
70
71    ///
72    /// @return true if we should warn about missing copyright statement
73    ///
74    bool  missing_copyright_warning(void) const;
75
76    ///
77    /// @return root for the trac envrionment, e.g.,
78    /// http://trac.thep.lu.se/svndigest/
79    ///
80    std::string trac_root(void) const;
81
82  private:
83    ///
84    /// Creates a Config object with default settings.
85    ///
86    /// @brief Default Constructor
87    ///
88    Configuration(void);
89    // Copy Constructor not implemented
90    Configuration(const Configuration&);
91    // assignment not implemented because assignment is always self-assignment
92    Configuration& operator=(const Configuration&);
93
94    friend std::ostream& operator<<(std::ostream&, const Configuration&);
95
96    void add_codon(std::string, std::string, std::string);
97
98    void clear(void);
99    const std::pair<std::string,std::string>* dictionary(std::string lhs) const;
100    bool equal_false(const std::string&) const;
101    bool equal_true(const std::string&) const;
102    void set_default(void);
103    /**
104       Translate string \a str using dictionary \a dict
105
106       \note \a str must be equal to d.first, or behavior is unspecified.
107
108       \throw if a '$' character is not followed by a positive integer
109       that is not larger than number of wildcards in dictionary \a d.
110     */
111    std::string translate(const std::string& str,
112                          const std::pair<std::string, std::string>& d) const;
113   
114    static Configuration* instance_;
115
116    std::map<std::string, Alias> copyright_alias_;
117
118    bool missing_copyright_warning_;
119   
120    typedef std::vector<std::pair<std::string, std::string> > VectorPair;
121    typedef std::vector<std::pair<std::string, VectorPair> > String2Codons; 
122    String2Codons string2codons_;
123
124    VectorPair dictionary_;
125
126    std::string trac_root_;
127  };
128
129  ///
130  /// @brief Output operator
131  ///
132  std::ostream& operator<<(std::ostream&, const Configuration&);
133
134  /**
135     If first character is '\n' replace it with "<NEWLINE>"
136   */
137  std::string trans_end_code(std::string);
138
139  /**
140     If last character is '\n' replace it with "<NEWLINE>"
141   */
142  std::string trans_beg_code(std::string);
143
144  /**
145     Trim \a c from beginning and end of string \a str;
146     
147     \return resulting string
148
149     \throw if first or last character of \a str is NOT character \a c
150  */
151  std::string trim(std::string str, char c);
152
153  /**
154     \brief Class for errors when reading config file.
155   */
156  class Config_error : public std::runtime_error
157  {
158  public:
159    Config_error(const std::string& line, const std::string& message);
160  };
161
162}} // end of namespace svndigest and namespace theplu
163
164#endif
165
166
Note: See TracBrowser for help on using the repository browser.