blob: e122f47ebd28a217a85a1860b0a079d784ddac25 [file] [log] [blame]
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08001#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
12namespace openpower
13{
14namespace pels
15{
16const uint8_t indentLevel = 4;
17const 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 */
23std::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 */
31char* 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 */
41void jsonInsert(std::string& jsonStr, const std::string& fieldName,
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080042 std::string fieldValue, uint8_t indentCount);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080043
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 */
52void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
53 std::vector<std::string>& values, uint8_t indentCount);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080054
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 */
61template <typename T>
62std::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
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080079/**
80 * @brief helper function to trim trailing whitespaces
81 * @return std::string - trimmed string
82 * @param[in] std::string - string to trim
83 */
84std::string trimEnd(std::string s);
85
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080086} // namespace pels
87} // namespace openpower