aboutsummaryrefslogtreecommitdiffstats
path: root/tools/version/version.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/version/version.cpp')
-rw-r--r--tools/version/version.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/tools/version/version.cpp b/tools/version/version.cpp
new file mode 100644
index 0000000..d59d110
--- /dev/null
+++ b/tools/version/version.cpp
@@ -0,0 +1,106 @@
+#include <iostream>
+#include <QProcess>
+#include <QStringList>
+#include <QFile>
+#include <QTextStream>
+#include <QDate>
+#include <QTime>
+#include <QFileInfo>
+#include <QTemporaryFile>
+#include <cstdlib>
+
+using namespace std;
+
+static int getBuildNumber()
+{
+ const QDate today(QDate::currentDate());
+ return ((today.year() - 1994) * 1000) + today.dayOfYear();
+}
+
+static int getSubversionRevision()
+{
+ int revision = 0;
+ QProcess process;
+ process.start("svnversion", QStringList() << "." << "--no-newline");
+ if (process.waitForStarted() && process.waitForReadyRead())
+ {
+ const QString str(process.readAll().constData());
+ const int pos = str.indexOf(':');
+ if (pos != -1)
+ {
+ revision = atoi(str.mid(pos + 1).toAscii().constData());
+ }
+ else
+ {
+ revision = atoi(str.toAscii().constData());
+ }
+ process.waitForFinished();
+ }
+ return revision;
+}
+
+static QByteArray readFile(const QString& fileName)
+{
+ QFile file(fileName);
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ return QByteArray();
+ }
+ return file.readAll();
+}
+
+static int writeFile(const QString& fileName, const int major, const int minor, const int revision, const int build)
+{
+ // Create a temp file containing the version info and
+ // only replace the existing one if they are different
+ QTemporaryFile tempFile;
+ if (tempFile.open())
+ {
+ QTextStream out(&tempFile);
+ out << "#ifndef VERSION_H\r\n";
+ out << "#define VERSION_H\r\n\r\n";
+ out << "namespace Version\r\n";
+ out << "{\r\n";
+ out << "\tstatic const int MAJOR = " << major << ";\r\n";
+ out << "\tstatic const int MINOR = " << minor << ";\r\n";
+ out << "\tstatic const int REVISION = " << revision << ";\r\n";
+ out << "\tstatic const int BUILD = " << build << ";\r\n";
+ out << "}\r\n\r\n";
+ out << "#endif // VERSION_H\r\n";
+
+ const QString tempFileName = tempFile.fileName();
+ tempFile.close();
+
+ if (!QFile::exists(fileName) || readFile(fileName) != readFile(tempFileName))
+ {
+ QFile::remove(fileName);
+ QFile::copy(tempFileName, fileName);
+ }
+
+ return 0;
+ }
+ else
+ {
+ cout << "Error creating temporary file!" << endl;
+ return 1;
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc != 4)
+ {
+ cout << "Usage: version major minor filename" << endl;
+ return 1;
+ }
+
+ const int major = atoi(argv[1]);
+ const int minor = atoi(argv[2]);
+ const int revision = getSubversionRevision();
+ const int build = getBuildNumber();
+
+ cout << major << '.' << minor << '.' << revision << '.' << build << endl;
+
+ return writeFile(argv[3], major, minor, revision, build);
+}
+