1 /*
2   Copyright (c) 2006 - 2021
3   CLST  - Radboud University
4   ILK   - Tilburg University
5 
6   This file is part of ticcutils
7 
8   ticcutils 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
11   (at your option) any later version.
12 
13   ticcutils is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU 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, see <http://www.gnu.org/licenses/>.
20 
21   For questions and suggestions, see:
22       https://github.com/LanguageMachines/ticcutils/issues
23   or send mail to:
24       lamasoftware (at ) science.ru.nl
25 */
26 
27 #ifndef TICC_CONFIGURATION_H
28 #define TICC_CONFIGURATION_H
29 
30 #include <string>
31 #include <map>
32 #include <set>
33 #include <iostream>
34 
35 namespace TiCC {
36 
37   /// \brief a class to store configurations
38   ///
39   /// This class stores configuration information as key-value pairs.
40   /// The Configuration is devided in sections. A key must be unique \e per
41   /// \e section.
42   ///
43   /// We provide functions to read a Configuration from a file, or to set
44   /// an attribute-value in a certain section.
45   ///
46   /// There is an implicit \b global section when the section is unnamed.
47   class Configuration {
48   public:
49     typedef std::map<std::string,std::string> ssMap;
50     typedef std::map<std::string, ssMap> sssMap;
51     Configuration();
52     void merge( const Configuration&, bool = false );
53     bool fill( const std::string& );
54     bool fill( const std::string&, const std::string& );
55     bool hasSection( const std::string& ) const;
56     std::string lookUp( const std::string& v,
57 			const std::string& s = "" ) const {
58       /// alternative name for getatt() function
59       return getatt( v, s );
60     }
61     ssMap lookUpAll( const std::string& ) const;
62     std::set<std::string> lookUpSections() const;
63     std::string getatt( const std::string&,
64 			const std::string& = "" ) const;
65     std::string setatt( const std::string&,
66 			const std::string&,
67 			const std::string& = "" );
68     std::string clearatt( const std::string&,
69 			  const std::string& = "" );
70     void erasesection( const std::string& );
71     void dump( std::ostream& ) const;
72     void create_configfile( const std::string& ) const;
configDir()73     std::string configDir() const {
74       /// give the value of the configDir attribute. (set when parsing a file)
75       return lookUp( "configDir" );
76     };
77   private:
78     sssMap myMap;
79     bool get_att_val( const std::string&, const std::string& );
80   };
81 
82   inline std::ostream& operator<<( std::ostream& os, const Configuration& c ){
83     /// output a Configuration to a stream
84     c.dump(os);
85     return os;
86   }
87 
88   inline std::ostream& operator<<( std::ostream& os, const Configuration* c ){
89     /// output a Configuration to a stream
90     if ( c ){
91       c->dump(os);
92     }
93     else {
94       os << "empty config";
95     }
96     return os;
97   }
98 
99 }
100 #endif
101