1 #pragma once
2 
3 #include <ctype.h>
4 #include <stdio.h>
5 
6 #include <fstream>
7 #include <iomanip>
8 #include <iostream>
9 #include <string>
10 #include <vector>
11 
12 namespace openpower
13 {
14 namespace pels
15 {
16 const uint8_t indentLevel = 4;
17 const uint8_t colAlign = 32;
18 /**
19  * @brief escape json - use it for PEL hex dumps.
20  * @param[in] std::string - the unescaped JSON as a string literal
21  * @return std::string - escaped JSON string literal
22  */
23 std::string escapeJSON(const std::string& input);
24 
25 /**
26  * @brief get hex dump for PEL section in json format.
27  * @param[in] const void* - Raw PEL data
28  * @param[i] size_t - size of Raw PEL
29  * @return char * - the Hex dump
30  */
31 char* dumpHex(const void* data, size_t size);
32 
33 /**
34  * @brief Inserts key-value into a JSON string
35  *
36  * @param[in] jsonStr - The JSON string
37  * @param[in] fieldName - The JSON key to insert
38  * @param[in] fieldValue - The JSON value to insert
39  * @param[in] indentCount - Indent count for the line
40  */
41 void jsonInsert(std::string& jsonStr, const std::string& fieldName,
42                 std::string fieldValue, uint8_t indentCount);
43 
44 /**
45  * @brief Inserts key-value array into a JSON string
46  *
47  * @param[in] jsonStr - The JSON string
48  * @param[in] fieldName - The JSON key to insert
49  * @param[in] values - The JSON array to insert
50  * @param[in] indentCount - Indent count for the line
51  */
52 void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
53                      std::vector<std::string>& values, uint8_t indentCount);
54 
55 /**
56  * @brief Converts an integer to a formatted string
57  * @param[in] format - the format of output string
58  * @param[in] number - the integer to convert
59  * @return std::string - the formatted string
60  */
61 template <typename T>
62 std::string getNumberString(const char* format, T number)
63 {
64     char* value = nullptr;
65     std::string numString;
66 
67     static_assert(std::is_integral<T>::value, "Integral required.");
68 
69     int len = asprintf(&value, format, number);
70     if (len)
71     {
72         numString = value;
73     }
74     free(value);
75 
76     return numString;
77 }
78 
79 /**
80  * @brief helper function to trim trailing whitespaces
81  * @return std::string - trimmed string
82  * @param[in] std::string - string to trim
83  */
84 std::string trimEnd(std::string s);
85 
86 } // namespace pels
87 } // namespace openpower
88