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