xref: /openbmc/bmcweb/redfish-core/include/registries.hpp (revision 80f595e7c00dc5b82e1aba8828c3a8205f9a4332)
170304cb5SJason M. Bills /*
270304cb5SJason M. Bills // Copyright (c) 2019 Intel Corporation
370304cb5SJason M. Bills //
470304cb5SJason M. Bills // Licensed under the Apache License, Version 2.0 (the "License");
570304cb5SJason M. Bills // you may not use this file except in compliance with the License.
670304cb5SJason M. Bills // You may obtain a copy of the License at
770304cb5SJason M. Bills //
870304cb5SJason M. Bills //      http://www.apache.org/licenses/LICENSE-2.0
970304cb5SJason M. Bills //
1070304cb5SJason M. Bills // Unless required by applicable law or agreed to in writing, software
1170304cb5SJason M. Bills // distributed under the License is distributed on an "AS IS" BASIS,
1270304cb5SJason M. Bills // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1370304cb5SJason M. Bills // See the License for the specific language governing permissions and
1470304cb5SJason M. Bills // limitations under the License.
1570304cb5SJason M. Bills */
1670304cb5SJason M. Bills #pragma once
17c5ba4c27SEd Tanous 
18d5c80ad9SNan Zhou #include <array>
19*80f595e7SEd Tanous #include <charconv>
20d5c80ad9SNan Zhou #include <cstddef>
21*80f595e7SEd Tanous #include <iostream>
22*80f595e7SEd Tanous #include <numeric>
23c5ba4c27SEd Tanous #include <span>
24c5ba4c27SEd Tanous #include <string>
25c5ba4c27SEd Tanous #include <string_view>
26d5c80ad9SNan Zhou #include <utility>
27d5c80ad9SNan Zhou 
28d5c80ad9SNan Zhou // IWYU pragma: no_include <stddef.h>
29c5ba4c27SEd Tanous 
30fffb8c1fSEd Tanous namespace redfish::registries
3170304cb5SJason M. Bills {
32351d3063SJason M. Bills struct Header
33351d3063SJason M. Bills {
34351d3063SJason M. Bills     const char* copyright;
35351d3063SJason M. Bills     const char* type;
36351d3063SJason M. Bills     const char* id;
37351d3063SJason M. Bills     const char* name;
38351d3063SJason M. Bills     const char* language;
39351d3063SJason M. Bills     const char* description;
40351d3063SJason M. Bills     const char* registryPrefix;
41351d3063SJason M. Bills     const char* registryVersion;
42351d3063SJason M. Bills     const char* owningEntity;
43351d3063SJason M. Bills };
4470304cb5SJason M. Bills 
4570304cb5SJason M. Bills struct Message
4670304cb5SJason M. Bills {
4770304cb5SJason M. Bills     const char* description;
4870304cb5SJason M. Bills     const char* message;
49e7808c93SGunnar Mills     const char* messageSeverity;
50271584abSEd Tanous     const size_t numberOfArgs;
5170304cb5SJason M. Bills     std::array<const char*, 5> paramTypes;
5270304cb5SJason M. Bills     const char* resolution;
5370304cb5SJason M. Bills };
5470304cb5SJason M. Bills using MessageEntry = std::pair<const char*, const Message>;
55c5ba4c27SEd Tanous 
56*80f595e7SEd Tanous inline std::string
57*80f595e7SEd Tanous     fillMessageArgs(const std::span<const std::string_view> messageArgs,
58*80f595e7SEd Tanous                     std::string_view msg)
59c5ba4c27SEd Tanous {
60*80f595e7SEd Tanous     std::string ret;
61*80f595e7SEd Tanous     size_t reserve = msg.size();
62*80f595e7SEd Tanous     for (const std::string_view& arg : messageArgs)
63c5ba4c27SEd Tanous     {
64*80f595e7SEd Tanous         reserve += arg.size();
65c5ba4c27SEd Tanous     }
66*80f595e7SEd Tanous     ret.reserve(reserve);
67*80f595e7SEd Tanous 
68*80f595e7SEd Tanous     for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos;
69*80f595e7SEd Tanous          stringIndex = msg.find('%'))
70*80f595e7SEd Tanous     {
71*80f595e7SEd Tanous         ret += msg.substr(0, stringIndex);
72*80f595e7SEd Tanous         msg.remove_prefix(stringIndex + 1);
73*80f595e7SEd Tanous         size_t number = 0;
74*80f595e7SEd Tanous         auto it = std::from_chars(msg.data(), &*msg.end(), number);
75*80f595e7SEd Tanous         if (it.ec != std::errc())
76*80f595e7SEd Tanous         {
77*80f595e7SEd Tanous             return "";
78c5ba4c27SEd Tanous         }
79*80f595e7SEd Tanous         msg.remove_prefix(1);
80*80f595e7SEd Tanous         // Redfish message args are 1 indexed.
81*80f595e7SEd Tanous         number--;
82*80f595e7SEd Tanous         if (number >= messageArgs.size())
83*80f595e7SEd Tanous         {
84*80f595e7SEd Tanous             return "";
85*80f595e7SEd Tanous         }
86*80f595e7SEd Tanous         ret += messageArgs[number];
87*80f595e7SEd Tanous     }
88*80f595e7SEd Tanous     ret += msg;
89*80f595e7SEd Tanous     return ret;
90c5ba4c27SEd Tanous }
91c5ba4c27SEd Tanous 
92fffb8c1fSEd Tanous } // namespace redfish::registries
93