Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 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. |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 27 | * @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 Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 30 | * @param[in] bool toJson - if true, output lines as JSON array, else print |
| 31 | * output as plain text |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 32 | * @return char * - the Hex dump |
| 33 | */ |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 34 | char* dumpHex(const void* data, size_t size, size_t indentCount, |
| 35 | bool toJson = true); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * @brief Inserts key-value into a JSON string |
| 39 | * |
| 40 | * @param[in] jsonStr - The JSON string |
| 41 | * @param[in] fieldName - The JSON key to insert |
| 42 | * @param[in] fieldValue - The JSON value to insert |
| 43 | * @param[in] indentCount - Indent count for the line |
| 44 | */ |
| 45 | void jsonInsert(std::string& jsonStr, const std::string& fieldName, |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 46 | std::string fieldValue, uint8_t indentCount); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * @brief Inserts key-value array into a JSON string |
| 50 | * |
| 51 | * @param[in] jsonStr - The JSON string |
| 52 | * @param[in] fieldName - The JSON key to insert |
| 53 | * @param[in] values - The JSON array to insert |
| 54 | * @param[in] indentCount - Indent count for the line |
| 55 | */ |
| 56 | void jsonInsertArray(std::string& jsonStr, const std::string& fieldName, |
| 57 | std::vector<std::string>& values, uint8_t indentCount); |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * @brief Converts an integer to a formatted string |
| 61 | * @param[in] format - the format of output string |
| 62 | * @param[in] number - the integer to convert |
| 63 | * @return std::string - the formatted string |
| 64 | */ |
| 65 | template <typename T> |
| 66 | std::string getNumberString(const char* format, T number) |
| 67 | { |
| 68 | char* value = nullptr; |
| 69 | std::string numString; |
| 70 | |
| 71 | static_assert(std::is_integral<T>::value, "Integral required."); |
| 72 | |
| 73 | int len = asprintf(&value, format, number); |
| 74 | if (len) |
| 75 | { |
| 76 | numString = value; |
| 77 | } |
| 78 | free(value); |
| 79 | |
| 80 | return numString; |
| 81 | } |
| 82 | |
Harisuddin Mohamed Isa | e2d1bf3 | 2020-02-06 17:32:38 +0800 | [diff] [blame] | 83 | /** |
| 84 | * @brief helper function to trim trailing whitespaces |
| 85 | * @return std::string - trimmed string |
| 86 | * @param[in] std::string - string to trim |
| 87 | */ |
| 88 | std::string trimEnd(std::string s); |
| 89 | |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 90 | } // namespace pels |
| 91 | } // namespace openpower |