Changeset View
Changeset View
Standalone View
Standalone View
src/kconfig_compiler/KConfigCodeGenerator.cpp
- This file was added.
1 | #include "KConfigCodeGenerator.h" | ||||
---|---|---|---|---|---|
2 | #include "KConfigXTParameters.h" | ||||
3 | #include "KConfigCommonStructs.h" | ||||
4 | | ||||
5 | #include <QTextStream> | ||||
6 | #include <QLatin1Char> | ||||
7 | #include <QFileInfo> | ||||
8 | | ||||
9 | #include <ostream> | ||||
10 | | ||||
11 | using std::endl; | ||||
12 | | ||||
13 | namespace | ||||
14 | { | ||||
15 | QTextStream cout(stdout); | ||||
16 | QTextStream cerr(stderr); | ||||
17 | } | ||||
18 | | ||||
19 | KConfigCodeGenerator::KConfigCodeGenerator( | ||||
20 | const QString& inputFile, | ||||
21 | const QString& baseDir, | ||||
22 | const QString& fileName, | ||||
23 | const KConfigXTParameters ¶meters, | ||||
24 | ParseResult &parseResult) | ||||
25 | : inputFile(inputFile), baseDir(baseDir), fileName(fileName), cfg(parameters), parseResult(parseResult) | ||||
26 | { | ||||
27 | file.setFileName(baseDir + fileName); | ||||
28 | if (!file.open(QIODevice::WriteOnly)) { | ||||
29 | cerr << "Can not open '" << baseDir << fileName << "for writing." << endl; | ||||
30 | exit(1); | ||||
31 | } | ||||
32 | stream.setDevice(&file); | ||||
33 | stream.setCodec("utf-8"); | ||||
34 | | ||||
35 | if (cfg.staticAccessors) { | ||||
36 | This = QStringLiteral("self()->"); | ||||
37 | } else { | ||||
38 | Const = QStringLiteral(" const"); | ||||
39 | } | ||||
40 | } | ||||
41 | | ||||
42 | KConfigCodeGenerator::~KConfigCodeGenerator() | ||||
43 | { | ||||
44 | file.close(); | ||||
45 | } | ||||
46 | | ||||
47 | void KConfigCodeGenerator::indent() | ||||
48 | { | ||||
49 | indentLevel += 4; | ||||
50 | } | ||||
51 | | ||||
52 | void KConfigCodeGenerator::unindent() | ||||
53 | { | ||||
54 | indentLevel -= 4; | ||||
55 | } | ||||
56 | | ||||
57 | QString KConfigCodeGenerator::whitespace() | ||||
58 | { | ||||
59 | QString spaces; | ||||
60 | for (int i = 0; i < indentLevel; i++) { | ||||
61 | spaces.append(QLatin1Char(' ')); | ||||
62 | } | ||||
63 | return spaces; | ||||
64 | } | ||||
65 | | ||||
66 | void KConfigCodeGenerator::startScope() | ||||
67 | { | ||||
68 | stream << QLatin1Char('{'); | ||||
69 | stream << endl; | ||||
70 | indent(); | ||||
71 | } | ||||
72 | | ||||
73 | void KConfigCodeGenerator::endScope() | ||||
74 | { | ||||
75 | unindent(); | ||||
76 | stream << whitespace() << QLatin1Char('}'); | ||||
77 | stream << endl; | ||||
78 | } | ||||
79 | | ||||
80 | void KConfigCodeGenerator::start() | ||||
81 | { | ||||
82 | const QString fileName = QFileInfo(inputFile).fileName(); | ||||
83 | stream << "// This file is generated by kconfig_compiler_kf5 from " << fileName << "." << endl; | ||||
84 | stream << "// All changes you do to this file will be lost." << endl; | ||||
85 | stream << endl; | ||||
86 | } | ||||
87 | | ||||
88 | void KConfigCodeGenerator::addHeaders(const QStringList& headerList) | ||||
89 | { | ||||
90 | for (auto include : headerList) { | ||||
91 | if (include.startsWith(QLatin1Char('"'))) { | ||||
92 | stream << "#include " << include << endl; | ||||
93 | } else { | ||||
94 | stream << "#include <" << include << ">" << endl; | ||||
95 | } | ||||
96 | } | ||||
97 | | ||||
98 | if (!headerList.isEmpty()) { | ||||
99 | stream << endl; | ||||
100 | } | ||||
101 | } | ||||
102 | | ||||
103 | // adds as many 'namespace foo {' lines to p_out as | ||||
104 | // there are namespaces in p_ns | ||||
105 | void KConfigCodeGenerator::beginNamespaces() | ||||
106 | { | ||||
107 | if (!cfg.nameSpace.isEmpty()) { | ||||
108 | for (const QString &ns : cfg.nameSpace.split(QStringLiteral("::"))) { | ||||
109 | stream << "namespace " << ns << " {" << endl; | ||||
110 | } | ||||
111 | stream << endl; | ||||
112 | } | ||||
113 | } | ||||
114 | | ||||
115 | // adds as many '}' lines to p_out as | ||||
116 | // there are namespaces in p_ns | ||||
117 | void KConfigCodeGenerator::endNamespaces() | ||||
118 | { | ||||
119 | if (!cfg.nameSpace.isEmpty()) { | ||||
120 | const int namespaceCount = cfg.nameSpace.count(QStringLiteral("::")) + 1; | ||||
121 | for (int i = 0; i < namespaceCount; ++i) { | ||||
122 | stream << "}" << endl; | ||||
123 | } | ||||
124 | stream << endl; | ||||
125 | } | ||||
126 | } |