Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtCore/QCoreApplication>
- #include <QString>
- #include <QtCore/QStringList>
- #include <QDebug>
- #include <QFile>
- void print_copyright()
- {
- qDebug() << "Licensed under GPLv3";
- qDebug() << "Copyright (C) 2012 Mohammad Abu-Garbeyyeh";
- }
- void print_insufficient_arguments(QString forThis)
- {
- qDebug() << "Insufficient arguemnts for" << forThis;
- }
- void print_version()
- {
- qDebug() << QCoreApplication::applicationName().remove("\"") << "version" << QCoreApplication::applicationVersion().remove("\"");
- }
- void print_help()
- {
- qDebug() << "";
- print_version();
- qDebug() << "Helps get and change value for mce.ini";
- qDebug() << QString("Usage: %1 <options>").arg(QCoreApplication::arguments().first());
- qDebug() << "Options:\n";
- qDebug() << "\t-g --get-value <key> - get the value defined in mce.ini for key";
- qDebug() << "\t-s --set-value <key> <value> - set the value of key to value";
- qDebug() << "\t-f --mce-file <path> - use this instead of /etc/mce/mce.ini";
- qDebug() << "\t-v --version - show version number and exit";
- qDebug() << "\t-h --help - show this help screen.";
- qDebug() << "";
- print_copyright();
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication application(argc, argv);
- application.setApplicationName("mce-config-editor");
- application.setApplicationVersion("0.1");
- QStringList argumentsList = application.arguments();
- argumentsList.removeAt(0);
- if (argumentsList.count() == 0) {
- print_help();
- return 1;
- }
- QString mceFilePath = "/etc/mce/mce.ini";
- QString firstArgument = argumentsList.at(0);
- if (argumentsList.contains("--mce-file") || argumentsList.contains("-f")) {
- int argPos = argumentsList.indexOf("--mce-file");
- if (argPos == -1)
- argPos = argumentsList.indexOf("-f");
- if (argumentsList.count() < argPos+1) {
- print_insufficient_arguments("mce-file");
- return 1;
- } else {
- mceFilePath = argumentsList.at(argPos+1);
- QFile file(mceFilePath);
- if (!file.exists()) {
- qDebug() << QString("File %1 does not exist!").arg(mceFilePath);
- return 1;
- }
- }
- }
- if (firstArgument == "-h" || firstArgument == "--help") {
- print_help();
- return 0;
- } else if (firstArgument == "-v" || firstArgument == "--version") {
- print_version();
- print_copyright();
- return 0;
- } else if (firstArgument == "-g" || firstArgument == "--get-value") {
- if (argumentsList.count() < 2) {
- print_insufficient_arguments("get-value");
- return 1;
- } else {
- QFile file(mceFilePath);
- int argPos = argumentsList.indexOf("--get-value");
- if (argPos == -1)
- argPos = argumentsList.indexOf("-g");
- if (argumentsList.count() < argPos+1) {
- print_insufficient_arguments("get-value");
- return 1;
- } else {
- QString key = argumentsList.at(argPos+1);
- if (file.open(QIODevice::ReadOnly)) {
- QStringList lines = QString(file.readAll()).split("\n");
- file.close();
- bool found = false;
- foreach (QString line, lines) {
- if (line.contains(key) && !line.startsWith("#")) {
- line.remove(key);
- line.remove(0, line.indexOf("=") +1);
- qDebug(line.toUtf8());
- found = true;
- return 0;
- }
- }
- if (!found) {
- qDebug() << "Couldn't find" << key << "in" << mceFilePath;
- return 1;
- }
- } else {
- qDebug() << QString("Failed to open file: ").append(mceFilePath);
- return 1;
- }
- }
- }
- }
- return application.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement