Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 17 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 18 | #include <array> |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame^] | 19 | #include <charconv> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 20 | #include <cstddef> |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame^] | 21 | #include <iostream> |
| 22 | #include <numeric> |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 23 | #include <span> |
| 24 | #include <string> |
| 25 | #include <string_view> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 26 | #include <utility> |
| 27 | |
| 28 | // IWYU pragma: no_include <stddef.h> |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 29 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 30 | namespace redfish::registries |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 31 | { |
Jason M. Bills | 351d306 | 2019-03-27 12:58:21 -0700 | [diff] [blame] | 32 | struct Header |
| 33 | { |
| 34 | const char* copyright; |
| 35 | const char* type; |
| 36 | const char* id; |
| 37 | const char* name; |
| 38 | const char* language; |
| 39 | const char* description; |
| 40 | const char* registryPrefix; |
| 41 | const char* registryVersion; |
| 42 | const char* owningEntity; |
| 43 | }; |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 44 | |
| 45 | struct Message |
| 46 | { |
| 47 | const char* description; |
| 48 | const char* message; |
Gunnar Mills | e7808c9 | 2020-07-08 21:17:44 -0500 | [diff] [blame] | 49 | const char* messageSeverity; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 50 | const size_t numberOfArgs; |
Jason M. Bills | 70304cb | 2019-03-27 12:03:59 -0700 | [diff] [blame] | 51 | std::array<const char*, 5> paramTypes; |
| 52 | const char* resolution; |
| 53 | }; |
| 54 | using MessageEntry = std::pair<const char*, const Message>; |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 55 | |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame^] | 56 | inline std::string |
| 57 | fillMessageArgs(const std::span<const std::string_view> messageArgs, |
| 58 | std::string_view msg) |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 59 | { |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame^] | 60 | std::string ret; |
| 61 | size_t reserve = msg.size(); |
| 62 | for (const std::string_view& arg : messageArgs) |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 63 | { |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame^] | 64 | reserve += arg.size(); |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 65 | } |
Ed Tanous | 80f595e | 2022-02-14 09:32:05 -0800 | [diff] [blame^] | 66 | ret.reserve(reserve); |
| 67 | |
| 68 | for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos; |
| 69 | stringIndex = msg.find('%')) |
| 70 | { |
| 71 | ret += msg.substr(0, stringIndex); |
| 72 | msg.remove_prefix(stringIndex + 1); |
| 73 | size_t number = 0; |
| 74 | auto it = std::from_chars(msg.data(), &*msg.end(), number); |
| 75 | if (it.ec != std::errc()) |
| 76 | { |
| 77 | return ""; |
| 78 | } |
| 79 | msg.remove_prefix(1); |
| 80 | // Redfish message args are 1 indexed. |
| 81 | number--; |
| 82 | if (number >= messageArgs.size()) |
| 83 | { |
| 84 | return ""; |
| 85 | } |
| 86 | ret += messageArgs[number]; |
| 87 | } |
| 88 | ret += msg; |
| 89 | return ret; |
Ed Tanous | c5ba4c2 | 2022-02-07 09:59:55 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 92 | } // namespace redfish::registries |