ctkPluginManifest.cpp 3.9 KB

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