fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <boost/property_tree/ptree.hpp>
  5. #include <boost/property_tree/xml_parser.hpp>
  6. #include <boost/foreach.hpp>
  7.  
  8. #include <boost/property_tree/ptree.hpp>
  9. #include <map>
  10. #include <string>
  11.  
  12. // Assumes all arrays are flat except for the special case in 1687_if_RAIL, which is ignored for counting
  13. std::map<std::string, int> countFabricBlocks(const boost::property_tree::ptree* pt) {
  14. std::map<std::string, int> result;
  15. if (!pt) return result;
  16.  
  17. auto pathsOpt = pt->get_child_optional("PATHS");
  18. if (!pathsOpt) return result;
  19.  
  20. const auto& paths = pathsOpt.get();
  21.  
  22. for (const auto& kv : paths) {
  23. const auto& arr = kv.second;
  24. // Check if the first value is "SERIAL"
  25. auto it = arr.begin();
  26. if (it == arr.end()) continue;
  27. if (it->second.get_value<std::string>() != "SERIAL") continue;
  28.  
  29. // Check if the second value exists and is a string
  30. ++it;
  31. if (it == arr.end()) continue;
  32. std::string fabricName;
  33. try {
  34. fabricName = it->second.get_value<std::string>();
  35. } catch (...) {
  36. continue; // Not a string, skip
  37. }
  38. result[fabricName]++;
  39. }
  40. return result;
  41. }
  42.  
  43. #include <boost/property_tree/json_parser.hpp>
  44. #include <iostream>
  45.  
  46. int main() {
  47. std::string json_str = R"({
  48. "PATHS": {
  49. "SF_MAIN_RAIL": [
  50. "SERIAL",
  51. "rail_in_if",
  52. "cpu_C7T1_fabric1_inst",
  53. "down_if"
  54. ],
  55. "cpu_C7T1_fabric1_inst_to_codec_cpu_C7T1_comp1_inst_P12": [
  56. "SERIAL",
  57. "cpu_C7T1_fabric1_inst",
  58. "cpu_C7T1_comp1_inst_P6",
  59. "cpu_C7T1_comp1_inst_P0",
  60. "cpu_C7T1_comp1_inst_P3",
  61. "cpu_C7T1_comp1_inst_P10",
  62. "cpu_C7T1_comp1_inst_P12"
  63. ],
  64. "cpu_C7T1_fabric1_inst_to_codec_cpu_C7T1_comp1_inst_P11": [
  65. "SERIAL",
  66. "cpu_C7T1_fabric1_inst",
  67. "cpu_C7T1_comp1_inst_P7",
  68. "cpu_C7T1_comp1_inst_P1",
  69. "cpu_C7T1_comp1_inst_P4",
  70. "cpu_C7T1_comp1_inst_P11"
  71. ],
  72. "cpu_C7T1_fabric1_inst_to_codec_cpu_C7T1_comp1_inst_P5": [
  73. "SERIAL",
  74. "cpu_C7T1_fabric1_inst",
  75. "cpu_C7T1_comp1_inst_P8",
  76. "cpu_C7T1_comp1_inst_P2",
  77. "cpu_C7T1_comp1_inst_P5"
  78. ],
  79. "cpu_C7T1_fabric1_inst_to_codec_cpu_C7T1_comp1_inst_P9": [
  80. "SERIAL",
  81. "cpu_C7T1_fabric1_inst",
  82. "cpu_C7T1_comp1_inst_P9"
  83. ],
  84. "cpu_C7T1_fabric1_inst_to_codec_cpu_C7T1_comp1_inst_P5_2": [
  85. "SERIAL",
  86. "cpu_C7T1_fabric2_inst",
  87. "cpu_C7T1_comp1_inst_P8",
  88. "cpu_C7T1_comp1_inst_P2",
  89. "cpu_C7T1_comp1_inst_P5"
  90. ],
  91. "cpu_C7T1_fabric1_inst_to_codec_cpu_C7T1_comp1_inst_P9_2": [
  92. "SERIAL",
  93. "cpu_C7T1_fabric2_inst",
  94. "cpu_C7T1_comp1_inst_P9"
  95. ]
  96. }
  97. })";
  98.  
  99. std::stringstream ss(json_str);
  100. boost::property_tree::ptree pt;
  101. boost::property_tree::read_json(ss, pt);
  102.  
  103. auto result = countFabricBlocks(&pt);
  104.  
  105. for (const auto& kv : result) {
  106. std::cout << "key: " << kv.first << ", value: " << kv.second << std::endl;
  107. }
  108. return 0;
  109. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
key: cpu_C7T1_fabric1_inst, value: 4
key: cpu_C7T1_fabric2_inst, value: 2
key: rail_in_if, value: 1