1 #pragma once 2 3 #include <ctype.h> 4 #include <stdio.h> 5 #include <string.h> 6 7 #include <cstdint> 8 #include <fstream> 9 #include <iomanip> 10 #include <iostream> 11 #include <memory> 12 #include <string> 13 #include <vector> 14 15 namespace openpower 16 { 17 namespace pels 18 { 19 const uint8_t indentLevel = 4; 20 const uint8_t colAlign = 32; 21 /** 22 * @brief escape json - use it for PEL hex dumps. 23 * @param[in] std::string - the unescaped JSON as a string literal 24 * @return std::string - escaped JSON string literal 25 */ 26 std::string escapeJSON(const std::string& input); 27 28 /** 29 * @brief get hex dump for PEL section in json format. 30 * @param[in] const void* data - Raw PEL data 31 * @param[i] size_t size - size of Raw PEL 32 * @param[in] size_t indentCount - The number of indent levels to indent 33 * @param[in] bool toJson - if true, output lines as JSON array, else print 34 * output as plain text 35 * @return std::unique_ptr<char[]> - the Hex dump 36 */ 37 std::unique_ptr<char[]> dumpHex(const void* data, size_t size, 38 size_t indentCount, bool toJson = true); 39 40 /** 41 * @brief Inserts key-value into a JSON string 42 * 43 * @param[in] jsonStr - The JSON string 44 * @param[in] fieldName - The JSON key to insert 45 * @param[in] fieldValue - The JSON value to insert 46 * @param[in] indentCount - Indent count for the line 47 */ 48 void jsonInsert(std::string& jsonStr, const std::string& fieldName, 49 const std::string& fieldValue, uint8_t indentCount); 50 51 /** 52 * @brief Inserts key-value array into a JSON string 53 * 54 * @param[in] jsonStr - The JSON string 55 * @param[in] fieldName - The JSON key to insert 56 * @param[in] values - The JSON array to insert 57 * @param[in] indentCount - Indent count for the line 58 */ 59 void jsonInsertArray(std::string& jsonStr, const std::string& fieldName, 60 const std::vector<std::string>& values, 61 uint8_t indentCount); 62 63 /** 64 * @brief Converts an integer to a formatted string 65 * @param[in] format - the format of output string 66 * @param[in] number - the integer to convert 67 * @return std::string - the formatted string 68 */ 69 template <typename T> 70 std::string getNumberString(const char* format, T number) 71 { 72 constexpr size_t valueSize = 100; 73 char value[valueSize]; 74 std::string numString; 75 76 static_assert(std::is_integral<T>::value, "Integral required."); 77 78 int len = snprintf(value, valueSize, format, number); 79 if (len >= 0) 80 { 81 numString = value; 82 } 83 else 84 { 85 throw std::invalid_argument( 86 std::string("getNumberString: invalid format string: ") + format); 87 } 88 89 return numString; 90 } 91 92 /** 93 * @brief helper function to trim trailing whitespaces 94 * @return std::string - trimmed string 95 * @param[in] std::string - string to trim 96 */ 97 std::string trimEnd(std::string s); 98 99 /** 100 * @brief Returns the component name for the component ID. 101 * 102 * It will try to look up the name to use in JSON files based on 103 * the creator ID. If PHYP, will convert the component ID to 104 * two characters. 105 * 106 * If nothing else, it will just return the name as a string like 107 * "0x1234". 108 * 109 * @return std::string - The component name 110 */ 111 std::string getComponentName(uint16_t compID, uint8_t creatorID); 112 113 } // namespace pels 114 } // namespace openpower 115