Debian Codegraph @

FaCT++.Python/lib/Kernel/configure.h

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef CONFIGURE_H
21 #define CONFIGURE_H
22
23 const unsigned MaxConfLineLen = 1024;
24
25 #include <cstring>
26 #include <cstdlib>
27 #include <iosfwd>
28
29 #include <string>
30 #include <vector>
31
32 /// configure element in the form Name = Value
33 class ConfElem
34 {
35 public: // members
36 /// name of the field
37 const std::string Name;
38 /// value of the field
39 std::string Value;
40
41 public: // interface
42 /// empty c'tor
43 ConfElem() = default;
44 /// init c'tor
45 ConfElem ( const std::string& n, const std::string& v ) : Name (n), Value (v) {}
46
47 /// save element to target stream
48 void Save ( std::ostream& o ) const;
49
50 // interface
51 long GetLong ( void ) const { return std::stol(Value); }
52 double GetDouble ( void ) const { return atof ( Value.c_str() ); }
53 const char* GetString ( void ) const { return Value.c_str(); }
54 }; // ConfElem
55
56 /// class for storing configure section with all the keys
57 class ConfSection
58 {
59 protected:
60 const std::string Name;
61
62 typedef std::vector <ConfElem*> ConfBase;
63 ConfBase Base;
64
65 public:
66 explicit ConfSection ( const std::string& pc ) : Name ( pc ) {}
67 ~ConfSection();
68
69 bool operator == ( const std::string& pc ) const { return ( Name == pc ); }
70 bool operator != ( const std::string& pc ) const { return ( Name != pc ); }
71
72 void addEntry ( const std::string& Name, const std::string& Value );
73
74 // find element; return NULL if not found
75 ConfElem* FindByName ( const std::string& name ) const;
76
77 void Save ( std::ostream& o ) const;
78 }; // ConfSection
79
80 /// class for reading general configuration file
81 class Configuration
82 {
83 protected: // parsing part
84 std::string fileName; // fileName
85 char Line [MaxConfLineLen+1]; // \0
86 bool isLoaded = false; // flags
87 bool isSaved = false;
88
89 // parser methods
90 void loadString ( std::istream& );
91 bool isComment ( void ) const;
92 bool isSection ( void ) const
93 { return ( Line [0] == '[' && Line [strlen(Line)-1] == ']' ); }
94 void loadSection ( void );
95 // splits line 'name=value' on 2 ASCIIZ parts
96 int SplitLine ( char*& pName, char*& pValue );
97
98 protected: // logic part
99 typedef std::vector <ConfSection*> ConfSectBase;
100 ConfSectBase Base;
101 ConfSection* Section = nullptr;
102 ConfElem* Element = nullptr;
103
104 // navigation methods
105 ConfSection* FindSection ( const std::string& pc ) const;
106
107 public: // interface
108 /// init c'tor
109 Configuration() = default;
110 /// no copy c'tor
111 Configuration ( const Configuration& ) = delete;
112 /// no assignment
113 Configuration& operator = ( const Configuration& ) = delete;
114 /// d'tor
115 ~Configuration();
116
117 // load config from file; @return true if cannot
118 bool Load ( const char* Filename );
119 // save config to file; @return true if cannot
120 bool Save ( const char* Filename );
121 // save config to the loaded file; @return true if cannot
122 bool Save ( void ) { return isLoaded ? Save(fileName.c_str()) : true; }
123
124 // status methods
125 bool Loaded ( void ) const { return isLoaded; }
126 bool Saved ( void ) const { return isSaved; }
127 /// add section if necessary; set Current to new pointer
128 void createSection ( const std::string& name );
129 /// check if Sect.Field exists;
130 bool checkValue ( const std::string& Section, const std::string& Element );
131 /// check if Field exists if Current is set;
132 bool checkValue ( const std::string& Element );
133 /// add Field.value to current Section; sets Trying to new p.
134 bool setValue ( const std::string& Element, const std::string& Value );
135
136 // get checked value
137 std::string getValue ( void ) const { return Element->Value; }
138 long getLong ( void ) const { return Element->GetLong(); }
139 double getDouble ( void ) const { return Element->GetDouble(); }
140 const char* getString ( void ) const { return Element->GetString(); }
141 // set Sect as a default
142 bool useSection ( const std::string& name )
143 { Section = FindSection(name); return !Section; }
144 }; // Configuration
145
146 #endif