split_string.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * split_string.cpp - node to split string into string list
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <boost/algorithm/string.hpp>
  19. #include <core/node_info.h>
  20. #include <core/node/node.h>
  21. #include <core/node/property.h>
  22. namespace rainynite::core {
  23. namespace nodes {
  24. class SplitString : public Node<vector<string>> {
  25. public:
  26. SplitString() {
  27. init<string>(source, "");
  28. init<string>(split, "\n");
  29. }
  30. public:
  31. vector<NodeInContext> get_list_links(shared_ptr<Context> ctx) const override {
  32. vector<NodeInContext> result;
  33. try {
  34. auto list = get(ctx);
  35. std::transform(
  36. std::begin(list),
  37. std::end(list),
  38. std::back_inserter(result),
  39. [ctx](auto&& s) -> NodeInContext {
  40. return { make_value<string>(s), ctx };
  41. }
  42. );
  43. } catch (...) {
  44. }
  45. return result;
  46. }
  47. vector<string> get(shared_ptr<Context> ctx) const override {
  48. try {
  49. vector<string> result;
  50. auto s = get_source()->get(ctx);
  51. auto split = get_split()->get(ctx);
  52. boost::split(result, s, boost::is_any_of(split));
  53. return result;
  54. } catch (...) {
  55. return {};
  56. }
  57. }
  58. private:
  59. NODE_PROPERTY(source, string);
  60. NODE_PROPERTY(split, string);
  61. };
  62. REGISTER_NODE(SplitString);
  63. } // namespace nodes
  64. } // namespace rainynite::core