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 17 18 #include <array> 19 #include <charconv> 20 #include <cstddef> 21 #include <iostream> 22 #include <numeric> 23 #include <span> 24 #include <string> 25 #include <string_view> 26 #include <utility> 27 28 // IWYU pragma: no_include <stddef.h> 29 30 namespace redfish::registries 31 { 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 }; 44 45 struct Message 46 { 47 const char* description; 48 const char* message; 49 const char* messageSeverity; 50 const size_t numberOfArgs; 51 std::array<const char*, 5> paramTypes; 52 const char* resolution; 53 }; 54 using MessageEntry = std::pair<const char*, const Message>; 55 56 inline std::string 57 fillMessageArgs(const std::span<const std::string_view> messageArgs, 58 std::string_view msg) 59 { 60 std::string ret; 61 size_t reserve = msg.size(); 62 for (const std::string_view& arg : messageArgs) 63 { 64 reserve += arg.size(); 65 } 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; 90 } 91 92 } // namespace redfish::registries 93