blob: 71a85e3bde9ff6fbd5e341af10e4a7ba82af8405 [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.
Matt Spinler4220a152020-03-26 10:18:09 -050027 * @param[in] const void* data - Raw PEL data
28 * @param[i] size_t size - size of Raw PEL
29 * @param[in] size_t indentCount - The number of indent levels to indent
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080030 * @return char * - the Hex dump
31 */
Matt Spinler4220a152020-03-26 10:18:09 -050032char* dumpHex(const void* data, size_t size, size_t indentCount);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080033
34/**
35 * @brief Inserts key-value into a JSON string
36 *
37 * @param[in] jsonStr - The JSON string
38 * @param[in] fieldName - The JSON key to insert
39 * @param[in] fieldValue - The JSON value to insert
40 * @param[in] indentCount - Indent count for the line
41 */
42void jsonInsert(std::string& jsonStr, const std::string& fieldName,
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080043 std::string fieldValue, uint8_t indentCount);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080044
45/**
46 * @brief Inserts key-value array into a JSON string
47 *
48 * @param[in] jsonStr - The JSON string
49 * @param[in] fieldName - The JSON key to insert
50 * @param[in] values - The JSON array to insert
51 * @param[in] indentCount - Indent count for the line
52 */
53void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
54 std::vector<std::string>& values, uint8_t indentCount);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080055
56/**
57 * @brief Converts an integer to a formatted string
58 * @param[in] format - the format of output string
59 * @param[in] number - the integer to convert
60 * @return std::string - the formatted string
61 */
62template <typename T>
63std::string getNumberString(const char* format, T number)
64{
65 char* value = nullptr;
66 std::string numString;
67
68 static_assert(std::is_integral<T>::value, "Integral required.");
69
70 int len = asprintf(&value, format, number);
71 if (len)
72 {
73 numString = value;
74 }
75 free(value);
76
77 return numString;
78}
79
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080080/**
81 * @brief helper function to trim trailing whitespaces
82 * @return std::string - trimmed string
83 * @param[in] std::string - string to trim
84 */
85std::string trimEnd(std::string s);
86
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080087} // namespace pels
88} // namespace openpower