PothosUtilDocParse.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2014-2016 Josh Blum
  2. // SPDX-License-Identifier: BSL-1.0
  3. #include "PothosUtil.hpp"
  4. #include <Pothos/Util/BlockDescription.hpp>
  5. #include <Pothos/Plugin.hpp>
  6. #include <Poco/Format.h>
  7. #include <Poco/NumberFormatter.h>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <vector>
  11. #include <string>
  12. /***********************************************************************
  13. * generate a cpp source that adds the json string to the registry
  14. **********************************************************************/
  15. static void jsonArrayToCppStaticBlock(const Pothos::Util::BlockDescriptionParser &parser, std::ostream &os, const std::string &target)
  16. {
  17. os << "#include <Pothos/Framework.hpp>\n";
  18. os << "#include <Pothos/Plugin.hpp>\n";
  19. os << Poco::format("pothos_static_block(registerPothosBlockDocs%s)\n", target);
  20. os << "{\n";
  21. for (const auto &factory : parser.listFactories())
  22. {
  23. //create escaped string of json
  24. std::string escaped;
  25. for (const auto &ch : parser.getJSONObject(factory))
  26. {
  27. escaped += "\\x" + Poco::NumberFormatter::formatHex(int(ch), 2/*width*/, false/*no 0x*/);
  28. }
  29. //register the block description at the specified path
  30. const auto pluginPath = Pothos::PluginPath("/blocks/docs", factory);
  31. os << Poco::format(" Pothos::PluginRegistry::add(\"%s\", std::string(\"%s\"));\n", pluginPath.toString(), escaped);
  32. }
  33. os << "}\n";
  34. }
  35. void PothosUtilBase::docParse(const std::vector<std::string> &inputFilePaths)
  36. {
  37. Pothos::Util::BlockDescriptionParser parser;
  38. //feed all input files into the parser
  39. for (const auto &inputFilePath : inputFilePaths)
  40. {
  41. parser.feedFilePath(inputFilePath);
  42. }
  43. //write to output (file if specified, otherwise stdout)
  44. const auto outputFilePath = this->config().getString("outputFile", "");
  45. const size_t indentSpaces = 4;
  46. if (outputFilePath.empty())
  47. {
  48. std::cout << std::endl;
  49. std::cout << parser.getJSONArray(indentSpaces);
  50. std::cout << std::endl;
  51. }
  52. else
  53. {
  54. const auto outputFileName = Poco::Path(outputFilePath).getBaseName();
  55. const auto outputFileExt = Poco::Path(outputFilePath).getExtension();
  56. std::ofstream outputFile(outputFilePath.c_str());
  57. if (outputFileExt == "json") outputFile << parser.getJSONArray(indentSpaces);
  58. else if (outputFileExt == "cpp") jsonArrayToCppStaticBlock(parser, outputFile, outputFileName);
  59. else throw Pothos::Exception("PothosUtilBase::docParse()", "unsupported file extension: " + outputFilePath);
  60. outputFile << std::endl;
  61. outputFile.close();
  62. }
  63. }