OpenASIP  2.0
ConfigurationFile.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ConfigurationFile.hh
26  *
27  * Declaration of ConfigurationFile class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2007 (pjaaskel-no.spam-cs.tut.fi)
31  * @note rating: red
32  */
33 
34 #ifndef TTA_CONFIGURATION_FILE_HH
35 #define TTA_CONFIGURATION_FILE_HH
36 
37 #include <map>
38 #include <string>
39 #include <vector>
40 
41 #include "Exception.hh"
42 #include "TCEString.hh"
43 
44 /**
45  * Class that is able to read files with key-value-pairs, such as the processor
46  * configuration file.
47  *
48  * Can do simple sanity and type checking of the file while loading it.
49  */
51 public:
52  ConfigurationFile(bool doChecking = false);
53  virtual ~ConfigurationFile();
54 
55  void load(std::istream& inputStream);
56 
57  bool hasKey(const std::string& key);
58  std::string value(const std::string& key, int index = 0);
59 
60  int itemCount(const std::string& key);
61 
62  int intValue(const std::string& key, int index = 0);
63  float floatValue(const std::string& key, int index = 0);
64  std::string stringValue(const std::string& key, int index = 0);
65  bool booleanValue(const std::string& key, int index = 0);
66 
67  unsigned int timeStampValue(const std::string& key);
68 
69 protected:
70  /**
71  * Different value types.
72  */
74  VT_INTEGER, ///< Integer value.
75  VT_FLOAT, ///< Float value.
76  VT_STRING, ///< String value.
77  VT_BOOLEAN, ///< Boolean value.
78  VT_READABLE_TIME, ///< Time in readable format.
79  VT_UNIX_TIMESTAMP ///< Time as seconds since starting of 1970.
80  };
81 
82  /**
83  * Error types.
84  */
86  FE_SYNTAX, ///< Syntax error.
87  FE_ILLEGAL_TYPE, ///< Illegal type error.
88  FE_UNKNOWN_KEY, ///< Unknown key error.
89  FE_MISSING_VALUE ///< Missing value error.
90  };
91 
92  void addSupportedKey(
93  const std::string& key,
95  bool caseSensitive = false);
96 
97  virtual bool handleError(
98  int lineNumber,
100  const std::string& line);
101 
102 private:
103  struct Key;
104  typedef std::map<TCEString, std::vector<TCEString> > ValueContainer;
105 
106  // potential indeterminism?
107  typedef std::map<Key*, ConfigurationValueType> KeyContainer;
108 
109  void parse(std::istream& inputStream);
110  bool checkSemantics(
111  const std::string& key,
112  const std::string& value,
113  int lineNumber,
114  const std::string& line);
115  bool isComment(const std::string& line);
116  bool legalTime(const std::string& line);
117  std::string valueOfKey(const std::string& key, int index);
118 
119  /**
120  * Key.
121  */
122  struct Key {
123  /**
124  * Constructor.
125  */
127  /// Name of the key.
128  std::string name_;
129  /// True if name is case sensitive.
131  };
132 
133  /// True if semantics of the configuration file is checked.
134  bool check_;
135  /// Contains all the values of configuration file.
137  /// Contains all the legal keys of the configuration file.
139 };
140 
141 #endif
ConfigurationFile::ConfigurationValueType
ConfigurationValueType
Definition: ConfigurationFile.hh:73
ConfigurationFile::Key::caseSensitive_
bool caseSensitive_
True if name is case sensitive.
Definition: ConfigurationFile.hh:130
ConfigurationFile::ConfigurationFile
ConfigurationFile(bool doChecking=false)
Definition: ConfigurationFile.cc:72
ConfigurationFile::booleanValue
bool booleanValue(const std::string &key, int index=0)
Definition: ConfigurationFile.cc:202
ConfigurationFile::itemCount
int itemCount(const std::string &key)
Definition: ConfigurationFile.cc:126
ConfigurationFile::handleError
virtual bool handleError(int lineNumber, ConfigurationFileError error, const std::string &line)
Definition: ConfigurationFile.cc:310
ConfigurationFile::VT_STRING
@ VT_STRING
String value.
Definition: ConfigurationFile.hh:76
ConfigurationFile::parse
void parse(std::istream &inputStream)
Definition: ConfigurationFile.cc:324
ConfigurationFile::floatValue
float floatValue(const std::string &key, int index=0)
Definition: ConfigurationFile.cc:167
Exception.hh
ConfigurationFile
Definition: ConfigurationFile.hh:50
ConfigurationFile::VT_UNIX_TIMESTAMP
@ VT_UNIX_TIMESTAMP
Time as seconds since starting of 1970.
Definition: ConfigurationFile.hh:79
ConfigurationFile::VT_INTEGER
@ VT_INTEGER
Integer value.
Definition: ConfigurationFile.hh:74
ConfigurationFile::values_
ValueContainer values_
Contains all the values of configuration file.
Definition: ConfigurationFile.hh:136
ConfigurationFile::check_
bool check_
True if semantics of the configuration file is checked.
Definition: ConfigurationFile.hh:134
ConfigurationFile::FE_UNKNOWN_KEY
@ FE_UNKNOWN_KEY
Unknown key error.
Definition: ConfigurationFile.hh:88
ConfigurationFile::isComment
bool isComment(const std::string &line)
Definition: ConfigurationFile.cc:388
ConfigurationFile::VT_FLOAT
@ VT_FLOAT
Float value.
Definition: ConfigurationFile.hh:75
ConfigurationFile::value
std::string value(const std::string &key, int index=0)
Definition: ConfigurationFile.cc:114
ConfigurationFile::Key::name_
std::string name_
Name of the key.
Definition: ConfigurationFile.hh:128
TCEString.hh
ConfigurationFile::legalTime
bool legalTime(const std::string &line)
Definition: ConfigurationFile.cc:483
ConfigurationFile::VT_BOOLEAN
@ VT_BOOLEAN
Boolean value.
Definition: ConfigurationFile.hh:77
ConfigurationFile::~ConfigurationFile
virtual ~ConfigurationFile()
Definition: ConfigurationFile.cc:78
ConfigurationFile::ValueContainer
std::map< TCEString, std::vector< TCEString > > ValueContainer
Definition: ConfigurationFile.hh:103
ConfigurationFile::FE_SYNTAX
@ FE_SYNTAX
Syntax error.
Definition: ConfigurationFile.hh:86
ConfigurationFile::ConfigurationFileError
ConfigurationFileError
Definition: ConfigurationFile.hh:85
ConfigurationFile::keys_
KeyContainer keys_
Contains all the legal keys of the configuration file.
Definition: ConfigurationFile.hh:138
ConfigurationFile::checkSemantics
bool checkSemantics(const std::string &key, const std::string &value, int lineNumber, const std::string &line)
Definition: ConfigurationFile.cc:406
ConfigurationFile::FE_ILLEGAL_TYPE
@ FE_ILLEGAL_TYPE
Illegal type error.
Definition: ConfigurationFile.hh:87
ConfigurationFile::load
void load(std::istream &inputStream)
Definition: ConfigurationFile.cc:88
ConfigurationFile::Key::Key
Key()
Definition: ConfigurationFile.hh:126
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
ConfigurationFile::intValue
int intValue(const std::string &key, int index=0)
Definition: ConfigurationFile.cc:146
ConfigurationFile::timeStampValue
unsigned int timeStampValue(const std::string &key)
Definition: ConfigurationFile.cc:228
ConfigurationFile::hasKey
bool hasKey(const std::string &key)
Definition: ConfigurationFile.cc:99
ConfigurationFile::addSupportedKey
void addSupportedKey(const std::string &key, ConfigurationValueType type, bool caseSensitive=false)
Definition: ConfigurationFile.cc:292
ConfigurationFile::stringValue
std::string stringValue(const std::string &key, int index=0)
Definition: ConfigurationFile.cc:187
ConfigurationFile::FE_MISSING_VALUE
@ FE_MISSING_VALUE
Missing value error.
Definition: ConfigurationFile.hh:89
ConfigurationFile::valueOfKey
std::string valueOfKey(const std::string &key, int index)
Definition: ConfigurationFile.cc:505
ConfigurationFile::KeyContainer
std::map< Key *, ConfigurationValueType > KeyContainer
Definition: ConfigurationFile.hh:107
ConfigurationFile::VT_READABLE_TIME
@ VT_READABLE_TIME
Time in readable format.
Definition: ConfigurationFile.hh:78
ConfigurationFile::Key
Definition: ConfigurationFile.hh:122