Officially at GitHub
[sloppygui.git] / tools / tagtrimmer.cpp
blobe8ca0bc6924bb2257d905afdbd968b40d1ca62f5
1 /*
2 A command-line tool for removing unneeded data from a Doxygen tagfile.
3 The tool's main purpose is to trim the Qt library's tagfile to a manageable
4 size, but it may work with other tagfiles as well.
5 */
7 #include <QtCore>
8 #include <QtXml>
9 #include <QtDebug>
12 /*!
13 * Remove the Doxygen-generated "class", "namespace", or whatever prefix
14 * from a file name, and make it lowercase.
16 static void fixFilename(QDomElement& node, const QString& compoundKind)
18 QDomNode child = node.firstChild();
19 QString text = child.nodeValue();
20 if (text.startsWith(compoundKind))
22 int len = compoundKind.length();
23 text = text.mid(len).toLower();
24 child.setNodeValue(text);
28 /*! Remove duplicate members from the compound. */
29 static void trimMembers(QDomElement& compound)
31 QDomElement elem;
32 for (elem = compound.firstChildElement("member");
33 !elem.isNull();
34 elem = elem.nextSiblingElement("member"))
36 QDomElement nameElem1 = elem.firstChildElement("name");
38 QDomElement elem2 = elem.nextSiblingElement("member");
39 while (!elem2.isNull())
41 // If two members have a "name" tag with the same
42 // value, the second one of them gets removed.
43 QDomElement nameElem2 = elem2.firstChildElement("name");
44 if (nameElem2.text() == nameElem1.text())
46 QDomElement old = elem2;
47 elem2 = elem2.nextSiblingElement("member");
48 compound.removeChild(old);
49 continue;
51 elem2 = elem2.nextSiblingElement("member");
56 /*! Remove unneeded nodes from a compound. */
57 static void trimCompound(QDomElement& compound)
59 QDomElement elem = compound.firstChildElement();
60 while (!elem.isNull())
62 QString name = elem.tagName();
63 if (name == "filename")
64 fixFilename(elem, compound.attribute("kind"));
65 else if (name == "member"
66 && elem.attribute("kind") == "enumeration")
68 QDomElement tmp(elem.firstChildElement("anchorfile"));
69 if (!tmp.isNull())
70 fixFilename(tmp, compound.attribute("kind"));
72 // The "arglist" tag is never used in class or
73 // namespace compounds.
74 tmp = elem.firstChildElement("arglist");
75 if (!tmp.isNull())
76 elem.removeChild(tmp);
78 else if (name != "name")
80 QDomElement old = elem;
81 elem = elem.nextSiblingElement();
82 compound.removeChild(old);
83 continue;
85 elem = elem.nextSiblingElement();
88 trimMembers(compound);
91 /*! Trim out useless weight from the tagfile. */
92 static bool trimDoc(QDomDocument& doc)
94 QDomElement root = doc.documentElement();
95 if (root.tagName() != "tagfile")
96 return false;
98 QDomElement compound = root.firstChildElement();
99 while (!compound.isNull())
101 QString compoundKind = compound.attribute("kind");
102 if (compound.tagName() != "compound"
103 || (compoundKind != "class" && compoundKind != "namespace"))
105 QDomElement old = compound;
106 compound = compound.nextSiblingElement();
107 root.removeChild(old);
108 continue;
111 trimCompound(compound);
112 compound = compound.nextSiblingElement();
115 return true;
118 int main(int argc, char* argv[])
120 QCoreApplication app(argc, argv);
121 QStringList args = app.arguments();
122 if (args.size() <= 2)
124 qDebug() << "Usage: tagtrimmer SOURCE_FILE DEST_FILE";
125 return 1;
128 QFile file(args[1]);
129 if (!file.open(QIODevice::ReadOnly))
131 qWarning() << "Can't open file" << args[1];
132 return 1;
135 QDomDocument doc;
136 QString errorMsg;
137 int line, col;
138 if (!doc.setContent(&file, &errorMsg, &line, &col))
140 qWarning() << "Error in line" << line << "column" << col;
141 qWarning() << errorMsg;
142 return 1;
144 file.close();
146 if (!trimDoc(doc))
148 qWarning() << "Couldn't trim the Doxytag file" << args[1];
149 return 1;
152 QFile file2(args[2]);
153 if (!file2.open(QIODevice::WriteOnly))
155 qWarning() << "Can't open file" << args[2];
156 return 1;
158 QTextStream out(&file2);
159 doc.save(out, 2);
161 return 0;