Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 26 | #ifndef NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP |
| 27 | #define NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 28 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 29 | #include "core/common.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 30 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::tools::nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 32 | |
| 33 | namespace xml { |
| 34 | |
| 35 | void |
| 36 | printHeader(std::ostream& os); |
| 37 | |
| 38 | void |
| 39 | printFooter(std::ostream& os); |
| 40 | |
| 41 | struct Text |
| 42 | { |
| 43 | const std::string& s; |
| 44 | }; |
| 45 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 46 | /** \brief Print XML text with special character represented as predefined entities. |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 47 | */ |
| 48 | std::ostream& |
| 49 | operator<<(std::ostream& os, const Text& text); |
| 50 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 51 | /** \brief Print true as an empty element and false as nothing. |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 52 | */ |
| 53 | struct Flag |
| 54 | { |
| 55 | const char* elementName; |
| 56 | bool flag; |
| 57 | }; |
| 58 | |
| 59 | std::ostream& |
| 60 | operator<<(std::ostream& os, Flag v); |
| 61 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 62 | /** \return duration in XML duration format |
| 63 | * |
| 64 | * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#duration |
| 65 | */ |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 66 | std::string |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 67 | formatDuration(time::nanoseconds d); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 68 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 69 | /** \return timestamp in XML dateTime format |
| 70 | * |
| 71 | * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#dateTime |
| 72 | */ |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 73 | std::string |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 74 | formatTimestamp(time::system_clock::time_point t); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 75 | |
| 76 | } // namespace xml |
| 77 | |
| 78 | namespace text { |
| 79 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 80 | /** \brief Print a number of whitespaces. |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 81 | */ |
| 82 | struct Spaces |
| 83 | { |
| 84 | int nSpaces; ///< number of spaces; print nothing if negative |
| 85 | }; |
| 86 | |
| 87 | std::ostream& |
| 88 | operator<<(std::ostream& os, const Spaces& spaces); |
| 89 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 90 | /** \brief Print different string on first and subsequent usage. |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 91 | * |
| 92 | * \code |
| 93 | * Separator sep(","); |
| 94 | * for (int i = 1; i <= 3; ++i) { |
| 95 | * os << sep << i; |
| 96 | * } |
| 97 | * // prints: 1,2,3 |
| 98 | * \endcode |
| 99 | */ |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 100 | class Separator : noncopyable |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 101 | { |
| 102 | public: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 103 | Separator(std::string_view first, std::string_view subsequent); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 104 | |
| 105 | explicit |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 106 | Separator(std::string_view subsequent); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 107 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 108 | int |
| 109 | getCount() const |
| 110 | { |
| 111 | return m_count; |
| 112 | } |
| 113 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 114 | private: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 115 | const std::string m_first; |
| 116 | const std::string m_subsequent; |
| 117 | int m_count = 0; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 118 | |
| 119 | friend std::ostream& operator<<(std::ostream& os, Separator& sep); |
| 120 | }; |
| 121 | |
| 122 | std::ostream& |
| 123 | operator<<(std::ostream& os, Separator& sep); |
| 124 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 125 | /** \brief Print attributes of an item. |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 126 | * |
| 127 | * \code |
| 128 | * ItemAttributes ia(wantMultiLine, 3); |
| 129 | * os << ia("id") << 500 |
| 130 | * << ia("uri") << "udp4://192.0.2.1:6363" |
| 131 | * << ia.end(); |
| 132 | * |
| 133 | * // prints in single-line style (wantMultiLine==false): |
| 134 | * // id=500 uri=udp4://192.0.2.1:6363 [no-newline] |
| 135 | * |
| 136 | * // prints in multi-line style (wantMultiLine==true): |
| 137 | * // id=500 |
| 138 | * // uri=udp4://192.0.2.1:6363 [newline] |
| 139 | * \endcode |
| 140 | */ |
| 141 | class ItemAttributes : noncopyable |
| 142 | { |
| 143 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 144 | /** \brief Constructor. |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 145 | * \param wantMultiLine true to select multi-line style, false to use single-line style |
| 146 | * \param maxAttributeWidth maximum width of attribute names, for alignment in multi-line style |
| 147 | */ |
| 148 | explicit |
| 149 | ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0); |
| 150 | |
| 151 | struct Attribute |
| 152 | { |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 153 | ItemAttributes& ia; |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 154 | std::string attribute; |
| 155 | }; |
| 156 | |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 157 | /** \note Caller must ensure ItemAttributes object is alive until after all Attribute objects are |
| 158 | * destructed. |
| 159 | */ |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 160 | Attribute |
| 161 | operator()(const std::string& attribute); |
| 162 | |
| 163 | std::string |
| 164 | end() const; |
| 165 | |
| 166 | private: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 167 | const bool m_wantMultiLine; |
| 168 | const int m_maxAttributeWidth; |
| 169 | int m_count = 0; |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 170 | |
| 171 | friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | std::ostream& |
| 175 | operator<<(std::ostream& os, const ItemAttributes::Attribute& attr); |
| 176 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 177 | /** \brief Print boolean as 'on' or 'off'. |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame] | 178 | */ |
| 179 | struct OnOff |
| 180 | { |
| 181 | bool flag; |
| 182 | }; |
| 183 | |
| 184 | std::ostream& |
| 185 | operator<<(std::ostream& os, OnOff v); |
| 186 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 187 | /** \brief Print boolean as 'yes' or 'no'. |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 188 | */ |
| 189 | struct YesNo |
| 190 | { |
| 191 | bool flag; |
| 192 | }; |
| 193 | |
| 194 | std::ostream& |
| 195 | operator<<(std::ostream& os, YesNo v); |
| 196 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 197 | namespace detail { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 198 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 199 | template<typename DurationT> |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 200 | std::string |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 201 | getTimeUnit(bool isLong); |
| 202 | |
| 203 | template<> |
| 204 | inline std::string |
| 205 | getTimeUnit<time::nanoseconds>(bool isLong) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 206 | { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 207 | return isLong ? "nanoseconds" : "ns"; |
| 208 | } |
| 209 | |
| 210 | template<> |
| 211 | inline std::string |
| 212 | getTimeUnit<time::microseconds>(bool isLong) |
| 213 | { |
| 214 | return isLong ? "microseconds" : "us"; |
| 215 | } |
| 216 | |
| 217 | template<> |
| 218 | inline std::string |
| 219 | getTimeUnit<time::milliseconds>(bool isLong) |
| 220 | { |
| 221 | return isLong ? "milliseconds" : "ms"; |
| 222 | } |
| 223 | |
| 224 | template<> |
| 225 | inline std::string |
| 226 | getTimeUnit<time::seconds>(bool isLong) |
| 227 | { |
| 228 | return isLong ? "seconds" : "s"; |
| 229 | } |
| 230 | |
| 231 | template<> |
| 232 | inline std::string |
| 233 | getTimeUnit<time::minutes>(bool isLong) |
| 234 | { |
| 235 | return isLong ? "minutes" : "m"; |
| 236 | } |
| 237 | |
| 238 | template<> |
| 239 | inline std::string |
| 240 | getTimeUnit<time::hours>(bool isLong) |
| 241 | { |
| 242 | return isLong ? "hours" : "h"; |
| 243 | } |
| 244 | |
| 245 | template<> |
| 246 | inline std::string |
| 247 | getTimeUnit<time::days>(bool isLong) |
| 248 | { |
| 249 | return isLong ? "days" : "d"; |
| 250 | } |
| 251 | |
| 252 | } // namespace detail |
| 253 | |
| 254 | template<typename OutputPrecision> |
| 255 | std::string |
| 256 | formatDuration(time::nanoseconds d, bool isLong = false) |
| 257 | { |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 258 | return std::to_string(time::duration_cast<OutputPrecision>(d).count()) + |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 259 | (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | std::string |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 263 | formatTimestamp(time::system_clock::time_point t); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 264 | |
| 265 | } // namespace text |
| 266 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 267 | } // namespace nfd::tools::nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 268 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 269 | #endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP |