blob: 4247aa866abfb71ccfa9e495f76848ae85a8ddf3 [file] [log] [blame]
Jason M. Bills70304cb2019-03-27 12:03:59 -07001/*
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 Tanousc5ba4c22022-02-07 09:59:55 -080017
Nan Zhoud5c80ad2022-07-11 01:16:31 +000018#include <array>
Ed Tanous80f595e2022-02-14 09:32:05 -080019#include <charconv>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000020#include <cstddef>
Ed Tanous80f595e2022-02-14 09:32:05 -080021#include <iostream>
22#include <numeric>
Ed Tanousc5ba4c22022-02-07 09:59:55 -080023#include <span>
24#include <string>
25#include <string_view>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000026#include <utility>
27
28// IWYU pragma: no_include <stddef.h>
Ed Tanousc5ba4c22022-02-07 09:59:55 -080029
Ed Tanousfffb8c12022-02-07 23:53:03 -080030namespace redfish::registries
Jason M. Bills70304cb2019-03-27 12:03:59 -070031{
Jason M. Bills351d3062019-03-27 12:58:21 -070032struct 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. Bills70304cb2019-03-27 12:03:59 -070044
45struct Message
46{
47 const char* description;
48 const char* message;
Gunnar Millse7808c92020-07-08 21:17:44 -050049 const char* messageSeverity;
Ed Tanous271584a2019-07-09 16:24:22 -070050 const size_t numberOfArgs;
Jason M. Bills70304cb2019-03-27 12:03:59 -070051 std::array<const char*, 5> paramTypes;
52 const char* resolution;
53};
54using MessageEntry = std::pair<const char*, const Message>;
Ed Tanousc5ba4c22022-02-07 09:59:55 -080055
Ed Tanous80f595e2022-02-14 09:32:05 -080056inline std::string
57 fillMessageArgs(const std::span<const std::string_view> messageArgs,
58 std::string_view msg)
Ed Tanousc5ba4c22022-02-07 09:59:55 -080059{
Ed Tanous80f595e2022-02-14 09:32:05 -080060 std::string ret;
61 size_t reserve = msg.size();
62 for (const std::string_view& arg : messageArgs)
Ed Tanousc5ba4c22022-02-07 09:59:55 -080063 {
Ed Tanous80f595e2022-02-14 09:32:05 -080064 reserve += arg.size();
Ed Tanousc5ba4c22022-02-07 09:59:55 -080065 }
Ed Tanous80f595e2022-02-14 09:32:05 -080066 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 Tanousc5ba4c22022-02-07 09:59:55 -080090}
91
Ed Tanousfffb8c12022-02-07 23:53:03 -080092} // namespace redfish::registries