ctkPluginManifest.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. namespace ctk {
  21. PluginManifest::PluginManifest()
  22. {
  23. }
  24. PluginManifest::PluginManifest(const QByteArray& in)
  25. {
  26. read(in);
  27. }
  28. void PluginManifest::read(const QByteArray& in)
  29. {
  30. mainAttributes.clear();
  31. sections.clear();
  32. QString key;
  33. QString value;
  34. QString currSection;
  35. QList<QByteArray> lines = in.split('\n');
  36. foreach (const QString& line, lines)
  37. {
  38. // skip empty lines and comments
  39. if (line.trimmed().isEmpty() | line.startsWith('#')) continue;
  40. // a valid new key/value pair starts with no white-space and contains
  41. // either a ':' or a '='
  42. if (!(line.startsWith(' ') || line.startsWith('\t')) && (line.contains(':') || line.contains('=')))
  43. {
  44. // see if we found a new section header
  45. if (line.startsWith('['))
  46. {
  47. currSection = line.mid(1, line.indexOf(']')-1);
  48. }
  49. else
  50. {
  51. // we found a new key/value pair, save the old one
  52. if (!key.isEmpty())
  53. {
  54. if (currSection.isEmpty())
  55. {
  56. mainAttributes.insert(key, value);
  57. }
  58. else
  59. {
  60. sections[currSection].insert(key, value);
  61. }
  62. }
  63. int colonIndex = line.indexOf(':');
  64. int equalIndex = line.indexOf('=');
  65. int sepIndex = -1;
  66. if (colonIndex < 0) sepIndex = equalIndex;
  67. else if (equalIndex < 0) sepIndex = colonIndex;
  68. else sepIndex = colonIndex < equalIndex ? colonIndex : equalIndex;
  69. key = line.left(sepIndex);
  70. value = line.right(line.size()-(sepIndex+1)).trimmed();
  71. }
  72. }
  73. else
  74. {
  75. // add the line to the value
  76. value += line;
  77. }
  78. }
  79. // save the last key/value pair
  80. if (!key.isEmpty())
  81. {
  82. if (currSection.isEmpty())
  83. {
  84. mainAttributes.insert(key, value);
  85. }
  86. else
  87. {
  88. sections[currSection].insert(key, value);
  89. }
  90. }
  91. }
  92. PluginManifest::Attributes PluginManifest::getMainAttributes() const
  93. {
  94. return mainAttributes;
  95. }
  96. QString PluginManifest::getAttribute(const QString& key) const
  97. {
  98. if (!mainAttributes.contains(key)) return QString();
  99. return mainAttributes[key];
  100. }
  101. PluginManifest::Attributes PluginManifest::getAttributes(const QString& section) const
  102. {
  103. if (!sections.contains(section))
  104. {
  105. throw std::invalid_argument(std::string("Manifest section invalid: ") + qPrintable(section));
  106. }
  107. return sections[section];
  108. }
  109. QStringList PluginManifest::getSections() const
  110. {
  111. return sections.keys();
  112. }
  113. }