utils.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
  3. * Copyright (C) 2011 University of Szeged
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "utils.h"
  29. bool takeOptionFlag(QStringList* arguments, const QString& name)
  30. {
  31. int index = arguments->indexOf(name);
  32. if (index != -1)
  33. arguments->removeAt(index);
  34. return index != -1;
  35. }
  36. QString takeOptionValue(QStringList* arguments, const QString& name)
  37. {
  38. QString result;
  39. int index = arguments->indexOf(name);
  40. if (index != -1) {
  41. arguments->removeAt(index);
  42. if (index < arguments->count() && !arguments->at(index).startsWith("-"))
  43. result = arguments->takeAt(index);
  44. }
  45. return result;
  46. }
  47. QString formatKeys(QList<QString> keys)
  48. {
  49. QString result;
  50. for (int i = 0; i < keys.count() - 1; i++)
  51. result.append(keys.at(i) + "|");
  52. result.append(keys.last());
  53. return result;
  54. }
  55. QList<QString> enumToKeys(const QMetaObject o, const QString& name, const QString& strip)
  56. {
  57. QList<QString> list;
  58. int enumIndex = o.indexOfEnumerator(name.toLatin1().data());
  59. QMetaEnum enumerator = o.enumerator(enumIndex);
  60. if (enumerator.isValid()) {
  61. for (int i = 0; i < enumerator.keyCount(); i++) {
  62. QString key(enumerator.valueToKey(i));
  63. list.append(key.remove(strip));
  64. }
  65. }
  66. return list;
  67. }
  68. void appQuit(int exitCode, const QString& msg)
  69. {
  70. if (!msg.isEmpty()) {
  71. if (exitCode > 0)
  72. qDebug("ERROR: %s", msg.toLatin1().data());
  73. else
  74. qDebug() << msg;
  75. }
  76. exit(exitCode);
  77. }
  78. QUrl Utils::urlFromUserInput(const QString& string)
  79. {
  80. QString input(string);
  81. QFileInfo fi(input);
  82. if (fi.exists() && fi.isRelative())
  83. input = fi.absoluteFilePath();
  84. return QUrl::fromUserInput(input);
  85. }