1 /* Copyright 2013-2019 MultiMC Contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "settings/INIFile.h"
17 #include <FileSystem.h>
18 
19 #include <QFile>
20 #include <QTextStream>
21 #include <QStringList>
22 #include <QSaveFile>
23 #include <QDebug>
24 
INIFile()25 INIFile::INIFile()
26 {
27 }
28 
unescape(QString orig)29 QString INIFile::unescape(QString orig)
30 {
31     QString out;
32     QChar prev = 0;
33     for(auto c: orig)
34     {
35         if(prev == '\\')
36         {
37             if(c == 'n')
38                 out += '\n';
39             else if(c == 't')
40                 out += '\t';
41             else if(c == '#')
42                 out += '#';
43             else
44                 out += c;
45             prev = 0;
46         }
47         else
48         {
49             if(c == '\\')
50             {
51                 prev = c;
52                 continue;
53             }
54             out += c;
55             prev = 0;
56         }
57     }
58     return out;
59 }
60 
escape(QString orig)61 QString INIFile::escape(QString orig)
62 {
63     QString out;
64     for(auto c: orig)
65     {
66         if(c == '\n')
67             out += "\\n";
68         else if (c == '\t')
69             out += "\\t";
70         else if(c == '\\')
71             out += "\\\\";
72         else if(c == '#')
73             out += "\\#";
74         else
75             out += c;
76     }
77     return out;
78 }
79 
saveFile(QString fileName)80 bool INIFile::saveFile(QString fileName)
81 {
82     QByteArray outArray;
83     for (Iterator iter = begin(); iter != end(); iter++)
84     {
85         QString value = iter.value().toString();
86         value = escape(value);
87         outArray.append(iter.key().toUtf8());
88         outArray.append('=');
89         outArray.append(value.toUtf8());
90         outArray.append('\n');
91     }
92 
93     try
94     {
95         FS::write(fileName, outArray);
96     }
97     catch (const Exception &e)
98     {
99         qCritical() << e.what();
100         return false;
101     }
102 
103     return true;
104 }
105 
106 
loadFile(QString fileName)107 bool INIFile::loadFile(QString fileName)
108 {
109     QFile file(fileName);
110     if (!file.open(QIODevice::ReadOnly))
111         return false;
112     bool success = loadFile(file.readAll());
113     file.close();
114     return success;
115 }
116 
loadFile(QByteArray file)117 bool INIFile::loadFile(QByteArray file)
118 {
119     QTextStream in(file);
120     in.setCodec("UTF-8");
121 
122     QStringList lines = in.readAll().split('\n');
123     for (int i = 0; i < lines.count(); i++)
124     {
125         QString &lineRaw = lines[i];
126         // Ignore comments.
127         int commentIndex = 0;
128         QString line = lineRaw;
129         // Search for comments until no more escaped # are available
130         while((commentIndex = line.indexOf('#', commentIndex + 1)) != -1) {
131             if(commentIndex > 0 && line.at(commentIndex - 1) == '\\') {
132                 continue;
133             }
134             line = line.left(lineRaw.indexOf('#')).trimmed();
135         }
136 
137         int eqPos = line.indexOf('=');
138         if (eqPos == -1)
139             continue;
140         QString key = line.left(eqPos).trimmed();
141         QString valueStr = line.right(line.length() - eqPos - 1).trimmed();
142 
143         valueStr = unescape(valueStr);
144 
145         QVariant value(valueStr);
146         this->operator[](key) = value;
147     }
148 
149     return true;
150 }
151 
get(QString key,QVariant def) const152 QVariant INIFile::get(QString key, QVariant def) const
153 {
154     if (!this->contains(key))
155         return def;
156     else
157         return this->operator[](key);
158 }
159 
set(QString key,QVariant val)160 void INIFile::set(QString key, QVariant val)
161 {
162     this->operator[](key) = val;
163 }
164