Path.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2013-2016 Josh Blum
  2. // SPDX-License-Identifier: BSL-1.0
  3. #include <Pothos/Plugin/Path.hpp>
  4. #include <Pothos/Plugin/Exception.hpp>
  5. #include <Poco/StringTokenizer.h>
  6. static void validatePathString(const std::string &path)
  7. {
  8. if (path == "/") return; //root path is ok, but may be funny for tokenizer since it ends in /
  9. bool first = true;
  10. for (const auto &name : Poco::StringTokenizer(path, "/"))
  11. {
  12. if (first and not name.empty())
  13. {
  14. throw Pothos::PluginPathError("Pothos::PluginPath("+path+")", "must start with root slash");
  15. }
  16. if (not first and name.empty())
  17. {
  18. throw Pothos::PluginPathError("Pothos::PluginPath("+path+")", "contains an empty name");
  19. }
  20. for (size_t i = 0; i < name.size(); i++)
  21. {
  22. if (name[i] >= 'A' and name[i] <= 'Z') continue;
  23. if (name[i] >= 'a' and name[i] <= 'z') continue;
  24. if (name[i] >= '0' and name[i] <= '9') continue;
  25. if (name[i] == '_' or name[i] == '-') continue;
  26. throw Pothos::PluginPathError("Pothos::PluginPath("+path+")", "contains non-alphanumeric-slashy-dashy name");
  27. }
  28. first = false;
  29. }
  30. }
  31. Pothos::PluginPath::PluginPath(void):
  32. _path("/")
  33. {
  34. validatePathString(_path);
  35. }
  36. Pothos::PluginPath::PluginPath(const std::string &path):
  37. _path(path)
  38. {
  39. validatePathString(_path);
  40. }
  41. Pothos::PluginPath::PluginPath(const PluginPath &path0, const PluginPath &path1):
  42. _path(path0._path + path1._path) //should always be valid because of individual PluginPath rules
  43. {
  44. validatePathString(_path);
  45. }
  46. Pothos::PluginPath::PluginPath(const char *path):
  47. _path(path)
  48. {
  49. validatePathString(_path);
  50. }
  51. Pothos::PluginPath::PluginPath(const PluginPath &path):
  52. _path(path._path)
  53. {
  54. return;
  55. }
  56. Pothos::PluginPath::~PluginPath(void)
  57. {
  58. return;
  59. }
  60. Pothos::PluginPath &Pothos::PluginPath::operator=(const PluginPath &path)
  61. {
  62. _path = path._path;
  63. return *this;
  64. }
  65. Pothos::PluginPath Pothos::PluginPath::join(const std::string &subPath) const
  66. {
  67. const std::string joiner = (_path == "/")? "": "/"; //don't join with only root /
  68. return PluginPath(this->toString() + joiner + subPath);
  69. }
  70. std::vector<std::string> Pothos::PluginPath::listNodes(void) const
  71. {
  72. std::vector<std::string> pathNames;
  73. Poco::StringTokenizer tok(_path, "/", Poco::StringTokenizer::TOK_IGNORE_EMPTY);
  74. for (Poco::StringTokenizer::Iterator it = tok.begin(); it != tok.end(); it++)
  75. {
  76. pathNames.push_back(*it);
  77. }
  78. return pathNames;
  79. }
  80. std::string Pothos::PluginPath::toString(void) const
  81. {
  82. return _path;
  83. }
  84. bool Pothos::operator==(const PluginPath &lhs, const PluginPath &rhs)
  85. {
  86. return lhs.toString() == rhs.toString();
  87. }
  88. #include <Pothos/Managed.hpp>
  89. static auto managedPluginPath = Pothos::ManagedClass()
  90. .registerConstructor<Pothos::PluginPath>()
  91. .registerConstructor<Pothos::PluginPath, const std::string &>()
  92. .registerConstructor<Pothos::PluginPath, const Pothos::PluginPath &, const Pothos::PluginPath &>()
  93. .registerMethod(POTHOS_FCN_TUPLE(Pothos::PluginPath, join))
  94. .registerMethod(POTHOS_FCN_TUPLE(Pothos::PluginPath, listNodes))
  95. .registerMethod(POTHOS_FCN_TUPLE(Pothos::PluginPath, toString))
  96. .commit("Pothos/PluginPath");