ctkPluginManifest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkPluginManifest_p.h"
  16. #include <QStringList>
  17. #include <QIODevice>
  18. #include <QDebug>
  19. #include <stdexcept>
  20. ctkPluginManifest::ctkPluginManifest()
  21. {
  22. }
  23. ctkPluginManifest::ctkPluginManifest(const QByteArray& in)
  24. {
  25. read(in);
  26. }
  27. void ctkPluginManifest::read(const QByteArray& in)
  28. {
  29. mainAttributes.clear();
  30. sections.clear();
  31. QString key;
  32. QString value;
  33. QString currSection;
  34. QList<QByteArray> lines = in.split('\n');
  35. foreach (const QString& line, lines)
  36. {
  37. // skip empty lines and comments
  38. if (line.trimmed().isEmpty() | line.startsWith('#')) continue;
  39. // a valid new key/value pair starts with no white-space and contains
  40. // either a ':' or a '='
  41. if (!(line.startsWith(' ') || line.startsWith('\t')) && (line.contains(':') || line.contains('=')))
  42. {
  43. // see if we found a new section header
  44. if (line.startsWith('['))
  45. {
  46. currSection = line.mid(1, line.indexOf(']')-1);
  47. }
  48. else
  49. {
  50. // we found a new key/value pair, save the old one
  51. if (!key.isEmpty())
  52. {
  53. if (currSection.isEmpty())
  54. {
  55. mainAttributes.insert(key, value);
  56. }
  57. else
  58. {
  59. sections[currSection].insert(key, value);
  60. }
  61. }
  62. int colonIndex = line.indexOf(':');
  63. int equalIndex = line.indexOf('=');
  64. int sepIndex = -1;
  65. if (colonIndex < 0) sepIndex = equalIndex;
  66. else if (equalIndex < 0) sepIndex = colonIndex;
  67. else sepIndex = colonIndex < equalIndex ? colonIndex : equalIndex;
  68. key = line.left(sepIndex);
  69. value = line.right(line.size()-(sepIndex+1)).trimmed();
  70. }
  71. }
  72. else
  73. {
  74. // add the line to the value
  75. value += line;
  76. }
  77. }
  78. // save the last key/value pair
  79. if (!key.isEmpty())
  80. {
  81. if (currSection.isEmpty())
  82. {
  83. mainAttributes.insert(key, value);
  84. }
  85. else
  86. {
  87. sections[currSection].insert(key, value);
  88. }
  89. }
  90. }
  91. ctkPluginManifest::Attributes ctkPluginManifest::getMainAttributes() const
  92. {
  93. return mainAttributes;
  94. }
  95. QString ctkPluginManifest::getAttribute(const QString& key) const
  96. {
  97. if (!mainAttributes.contains(key)) return QString();
  98. return mainAttributes[key];
  99. }
  100. ctkPluginManifest::Attributes ctkPluginManifest::getAttributes(const QString& section) const
  101. {
  102. if (!sections.contains(section))
  103. {
  104. throw std::invalid_argument(std::string("Manifest section invalid: ") + qPrintable(section));
  105. }
  106. return sections[section];
  107. }
  108. QStringList ctkPluginManifest::getSections() const
  109. {
  110. return sections.keys();
  111. }