1 #include "properties.h"
2 
3 #include <QTextStream>
4 #include <QStringList>
5 #include <QFile>
6 
7 /**
8  * @brief Creates properties
9  * @param fileName
10  * @param group
11  */
Properties(const QString & fileName,const QString & group)12 Properties::Properties(const QString &fileName, const QString &group) {
13   if (!fileName.isEmpty()) {
14     load(fileName, group);
15   }
16 }
17 //---------------------------------------------------------------------------
18 
19 /**
20  * @brief Creates properties
21  * @param other properies
22  */
Properties(const Properties & other)23 Properties::Properties(const Properties &other) {
24   this->data = other.data;
25 }
26 //---------------------------------------------------------------------------
27 
28 
29 /**
30  * @brief Loads property file
31  * @param fileName
32  * @param group
33  * @return true if load was successful
34  */
load(const QString & fileName,const QString & group)35 bool Properties::load(const QString &fileName, const QString &group) {
36 
37   // NOTE: This class is used for reading of property files instead of QSettings
38   // class, which considers separator ';' as comment
39 
40   // Try open file
41   QFile file(fileName);
42   if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
43     return false;
44   }
45 
46   // Clear old data
47   data.clear();
48 
49   // Indicator whether group was found or not, if name of group was not
50   // specified, groupFound is always true
51   bool groupFound = group.isEmpty();
52 
53   // Read propeties
54   QTextStream in(&file);
55   while (!in.atEnd()) {
56 
57     // Read new line
58     QString line = in.readLine();
59 
60     // Skip empty line or line with invalid format
61     if (line.trimmed().isEmpty()) {
62       continue;
63     }
64 
65     // Read group
66     // NOTE: symbols '[' and ']' can be found not only in group names, but
67     // only group can start with '['
68     if (!group.isEmpty() && line.trimmed().startsWith("[")) {
69       QString tmp = line.trimmed().replace("[", "").replace("]", "");
70       groupFound = group.trimmed().compare(tmp) == 0;
71     }
72 
73     // If we are in correct group and line contains assignment then read data
74     if (groupFound && line.contains("=")) {
75         int index = line.indexOf("=");
76         QString key = line.mid(0, index);
77         QString val = line.mid(index+1);
78         data.insert(key, val);
79     }
80   }
81   file.close();
82   return true;
83 }
84 //---------------------------------------------------------------------------
85 
86 /**
87  * @brief Saves properties to file
88  * @param fileName
89  * @param group
90  * @return true if success
91  */
save(const QString & fileName,const QString & group)92 bool Properties::save(const QString &fileName, const QString &group) {
93 
94   // Try open file
95   QFile file(fileName);
96   if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
97     return false;
98   }
99 
100   // Write group
101   QTextStream out(&file);
102   if (!group.isEmpty()) {
103     out << "[" + group + "]\n";
104   }
105 
106   // Write data
107   foreach (QString key, data.keys()) {
108     out << key << "=" << data.value(key).toString() << "\n";
109   }
110 
111   // Exit
112   file.close();
113   return true;
114 }
115 //---------------------------------------------------------------------------
116 
117 /**
118  * @brief Returns true if property with given key is present in properties
119  * @param key
120  * @return true if property with given key is present in properties
121  */
contains(const QString & key) const122 bool Properties::contains(const QString &key) const {
123   return data.contains(key);
124 }
125 //---------------------------------------------------------------------------
126 
127 /**
128  * @brief Returns value
129  * @param key
130  * @param defaultValue
131  * @return value
132  */
value(const QString & key,const QVariant & defaultValue)133 QVariant Properties::value(const QString &key, const QVariant &defaultValue) {
134   return data.value(key, defaultValue);
135 }
136 //---------------------------------------------------------------------------
137 
138 /**
139  * @brief Sets value to properties
140  * @param key
141  * @param value
142  */
set(const QString & key,const QVariant & value)143 void Properties::set(const QString &key, const QVariant &value) {
144   if (data.contains(key)) {
145     data.take(key);
146   }
147   data.insert(key, value);
148 }
149 //---------------------------------------------------------------------------
150 
151 /**
152  * @brief Returns keys (names of properties)
153  * @param key
154  * @param value
155  */
getKeys() const156 QStringList Properties::getKeys() const {
157   return data.keys();
158 }
159 //---------------------------------------------------------------------------
160