Advertisement
Guest User

Untitled

a guest
Dec 17th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QtCore/QCoreApplication>
  2. #include <QString>
  3. #include <QtCore/QStringList>
  4. #include <QDebug>
  5. #include <QFile>
  6.  
  7. void print_copyright()
  8. {
  9.     qDebug() << "Licensed under GPLv3";
  10.     qDebug() << "Copyright (C) 2012 Mohammad Abu-Garbeyyeh";
  11. }
  12.  
  13. void print_insufficient_arguments(QString forThis)
  14. {
  15.     qDebug() << "Insufficient arguemnts for" << forThis;
  16. }
  17.  
  18. void print_version()
  19. {
  20.     qDebug() << QCoreApplication::applicationName().remove("\"") << "version" << QCoreApplication::applicationVersion().remove("\"");
  21. }
  22.  
  23. void print_help()
  24. {
  25.     qDebug() << "";
  26.     print_version();
  27.     qDebug() << "Helps get and change value for mce.ini";
  28.     qDebug() << QString("Usage: %1 <options>").arg(QCoreApplication::arguments().first());
  29.     qDebug() << "Options:\n";
  30.     qDebug() << "\t-g --get-value <key> - get the value defined in mce.ini for key";
  31.     qDebug() << "\t-s --set-value <key> <value> - set the value of key to value";
  32.     qDebug() << "\t-f --mce-file <path> - use this instead of /etc/mce/mce.ini";
  33.     qDebug() << "\t-v --version - show version number and exit";
  34.     qDebug() << "\t-h --help - show this help screen.";
  35.     qDebug() << "";
  36.     print_copyright();
  37. }
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.     QCoreApplication application(argc, argv);
  42.     application.setApplicationName("mce-config-editor");
  43.     application.setApplicationVersion("0.1");
  44.  
  45.     QStringList argumentsList = application.arguments();
  46.     argumentsList.removeAt(0);
  47.  
  48.     if (argumentsList.count() == 0) {
  49.         print_help();
  50.         return 1;
  51.     }
  52.  
  53.     QString mceFilePath = "/etc/mce/mce.ini";
  54.  
  55.     QString firstArgument = argumentsList.at(0);
  56.  
  57.     if (argumentsList.contains("--mce-file") || argumentsList.contains("-f")) {
  58.         int argPos = argumentsList.indexOf("--mce-file");
  59.         if (argPos == -1)
  60.             argPos = argumentsList.indexOf("-f");
  61.  
  62.         if (argumentsList.count() < argPos+1) {
  63.             print_insufficient_arguments("mce-file");
  64.             return 1;
  65.         } else {
  66.             mceFilePath = argumentsList.at(argPos+1);
  67.             QFile file(mceFilePath);
  68.             if (!file.exists()) {
  69.                 qDebug() << QString("File %1 does not exist!").arg(mceFilePath);
  70.                 return 1;
  71.             }
  72.         }
  73.     }
  74.  
  75.     if (firstArgument == "-h" || firstArgument == "--help") {
  76.         print_help();
  77.         return 0;
  78.     } else if (firstArgument == "-v" || firstArgument == "--version") {
  79.         print_version();
  80.         print_copyright();
  81.         return 0;
  82.     } else if (firstArgument == "-g" || firstArgument == "--get-value") {
  83.         if (argumentsList.count() < 2) {
  84.             print_insufficient_arguments("get-value");
  85.             return 1;
  86.         } else {
  87.             QFile file(mceFilePath);
  88.             int argPos = argumentsList.indexOf("--get-value");
  89.             if (argPos == -1)
  90.                 argPos = argumentsList.indexOf("-g");
  91.  
  92.             if (argumentsList.count() < argPos+1) {
  93.                 print_insufficient_arguments("get-value");
  94.                 return 1;
  95.             } else {
  96.                 QString key = argumentsList.at(argPos+1);
  97.                 if (file.open(QIODevice::ReadOnly)) {
  98.                     QStringList lines = QString(file.readAll()).split("\n");
  99.                     file.close();
  100.                     bool found = false;
  101.                     foreach (QString line, lines) {
  102.                         if (line.contains(key) && !line.startsWith("#")) {
  103.                             line.remove(key);
  104.                             line.remove(0, line.indexOf("=") +1);
  105.                             qDebug(line.toUtf8());
  106.                             found = true;
  107.                             return 0;
  108.                         }
  109.                     }
  110.                     if (!found) {
  111.                         qDebug() << "Couldn't find" << key << "in" << mceFilePath;
  112.                         return 1;
  113.                     }
  114.                 } else {
  115.                     qDebug() << QString("Failed to open file: ").append(mceFilePath);
  116.                     return 1;
  117.                 }
  118.             }
  119.         }
  120.     }
  121.  
  122.     return application.exec();
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement