summaryrefslogtreecommitdiffstats
path: root/extensions/openpower-pels/json_utils.hpp
blob: e122f47ebd28a217a85a1860b0a079d784ddac25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once

#include <ctype.h>
#include <stdio.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

namespace openpower
{
namespace pels
{
const uint8_t indentLevel = 4;
const uint8_t colAlign = 32;
/**
 * @brief escape json - use it for PEL hex dumps.
 * @param[in] std::string - the unescaped JSON as a string literal
 * @return std::string - escaped JSON string literal
 */
std::string escapeJSON(const std::string& input);

/**
 * @brief get hex dump for PEL section in json format.
 * @param[in] const void* - Raw PEL data
 * @param[i] size_t - size of Raw PEL
 * @return char * - the Hex dump
 */
char* dumpHex(const void* data, size_t size);

/**
 * @brief Inserts key-value into a JSON string
 *
 * @param[in] jsonStr - The JSON string
 * @param[in] fieldName - The JSON key to insert
 * @param[in] fieldValue - The JSON value to insert
 * @param[in] indentCount - Indent count for the line
 */
void jsonInsert(std::string& jsonStr, const std::string& fieldName,
                std::string fieldValue, uint8_t indentCount);

/**
 * @brief Inserts key-value array into a JSON string
 *
 * @param[in] jsonStr - The JSON string
 * @param[in] fieldName - The JSON key to insert
 * @param[in] values - The JSON array to insert
 * @param[in] indentCount - Indent count for the line
 */
void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
                     std::vector<std::string>& values, uint8_t indentCount);

/**
 * @brief Converts an integer to a formatted string
 * @param[in] format - the format of output string
 * @param[in] number - the integer to convert
 * @return std::string - the formatted string
 */
template <typename T>
std::string getNumberString(const char* format, T number)
{
    char* value = nullptr;
    std::string numString;

    static_assert(std::is_integral<T>::value, "Integral required.");

    int len = asprintf(&value, format, number);
    if (len)
    {
        numString = value;
    }
    free(value);

    return numString;
}

/**
 * @brief helper function to trim trailing whitespaces
 * @return std::string - trimmed string
 * @param[in] std::string - string to trim
 */
std::string trimEnd(std::string s);

} // namespace pels
} // namespace openpower
OpenPOWER on IntegriCloud