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 | /* |
| 3 | * Copyright (c) 2014-2018, 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 | |
| 31 | namespace nfd { |
| 32 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 33 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 34 | |
| 35 | namespace xml { |
| 36 | |
| 37 | void |
| 38 | printHeader(std::ostream& os); |
| 39 | |
| 40 | void |
| 41 | printFooter(std::ostream& os); |
| 42 | |
| 43 | struct Text |
| 44 | { |
| 45 | const std::string& s; |
| 46 | }; |
| 47 | |
| 48 | /** \brief print XML text with special character represented as predefined entities |
| 49 | */ |
| 50 | std::ostream& |
| 51 | operator<<(std::ostream& os, const Text& text); |
| 52 | |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 53 | /** \brief print true as an empty element and false as nothing |
| 54 | */ |
| 55 | struct Flag |
| 56 | { |
| 57 | const char* elementName; |
| 58 | bool flag; |
| 59 | }; |
| 60 | |
| 61 | std::ostream& |
| 62 | operator<<(std::ostream& os, Flag v); |
| 63 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 64 | /** \return duration in XML duration format |
| 65 | * |
| 66 | * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#duration |
| 67 | */ |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 68 | std::string |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 69 | formatDuration(time::nanoseconds d); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 70 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 71 | /** \return timestamp in XML dateTime format |
| 72 | * |
| 73 | * Definition of this format: https://www.w3.org/TR/xmlschema11-2/#dateTime |
| 74 | */ |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 75 | std::string |
| 76 | formatTimestamp(time::system_clock::TimePoint t); |
| 77 | |
| 78 | } // namespace xml |
| 79 | |
| 80 | namespace text { |
| 81 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 82 | /** \brief print a number of whitespaces |
| 83 | */ |
| 84 | struct Spaces |
| 85 | { |
| 86 | int nSpaces; ///< number of spaces; print nothing if negative |
| 87 | }; |
| 88 | |
| 89 | std::ostream& |
| 90 | operator<<(std::ostream& os, const Spaces& spaces); |
| 91 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 92 | /** \brief print different string on first and subsequent usage |
| 93 | * |
| 94 | * \code |
| 95 | * Separator sep(","); |
| 96 | * for (int i = 1; i <= 3; ++i) { |
| 97 | * os << sep << i; |
| 98 | * } |
| 99 | * // prints: 1,2,3 |
| 100 | * \endcode |
| 101 | */ |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 102 | class Separator : noncopyable |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 103 | { |
| 104 | public: |
| 105 | Separator(const std::string& first, const std::string& subsequent); |
| 106 | |
| 107 | explicit |
| 108 | Separator(const std::string& subsequent); |
| 109 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 110 | int |
| 111 | getCount() const |
| 112 | { |
| 113 | return m_count; |
| 114 | } |
| 115 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 116 | private: |
| 117 | std::string m_first; |
| 118 | std::string m_subsequent; |
| 119 | int m_count; |
| 120 | |
| 121 | friend std::ostream& operator<<(std::ostream& os, Separator& sep); |
| 122 | }; |
| 123 | |
| 124 | std::ostream& |
| 125 | operator<<(std::ostream& os, Separator& sep); |
| 126 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 127 | /** \brief print attributes of an item |
| 128 | * |
| 129 | * \code |
| 130 | * ItemAttributes ia(wantMultiLine, 3); |
| 131 | * os << ia("id") << 500 |
| 132 | * << ia("uri") << "udp4://192.0.2.1:6363" |
| 133 | * << ia.end(); |
| 134 | * |
| 135 | * // prints in single-line style (wantMultiLine==false): |
| 136 | * // id=500 uri=udp4://192.0.2.1:6363 [no-newline] |
| 137 | * |
| 138 | * // prints in multi-line style (wantMultiLine==true): |
| 139 | * // id=500 |
| 140 | * // uri=udp4://192.0.2.1:6363 [newline] |
| 141 | * \endcode |
| 142 | */ |
| 143 | class ItemAttributes : noncopyable |
| 144 | { |
| 145 | public: |
| 146 | /** \brief constructor |
| 147 | * \param wantMultiLine true to select multi-line style, false to use single-line style |
| 148 | * \param maxAttributeWidth maximum width of attribute names, for alignment in multi-line style |
| 149 | */ |
| 150 | explicit |
| 151 | ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0); |
| 152 | |
| 153 | struct Attribute |
| 154 | { |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 155 | ItemAttributes& ia; |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 156 | std::string attribute; |
| 157 | }; |
| 158 | |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 159 | /** \note Caller must ensure ItemAttributes object is alive until after all Attribute objects are |
| 160 | * destructed. |
| 161 | */ |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 162 | Attribute |
| 163 | operator()(const std::string& attribute); |
| 164 | |
| 165 | std::string |
| 166 | end() const; |
| 167 | |
| 168 | private: |
| 169 | bool m_wantMultiLine; |
| 170 | int m_maxAttributeWidth; |
| 171 | int m_count; |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 172 | |
| 173 | friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | std::ostream& |
| 177 | operator<<(std::ostream& os, const ItemAttributes::Attribute& attr); |
| 178 | |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame] | 179 | /** \brief print boolean as 'on' or 'off' |
| 180 | */ |
| 181 | struct OnOff |
| 182 | { |
| 183 | bool flag; |
| 184 | }; |
| 185 | |
| 186 | std::ostream& |
| 187 | operator<<(std::ostream& os, OnOff v); |
| 188 | |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame^] | 189 | /** \brief print boolean as 'yes' or 'no' |
| 190 | */ |
| 191 | struct YesNo |
| 192 | { |
| 193 | bool flag; |
| 194 | }; |
| 195 | |
| 196 | std::ostream& |
| 197 | operator<<(std::ostream& os, YesNo v); |
| 198 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 199 | namespace detail { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 200 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 201 | template<typename DurationT> |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 202 | std::string |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 203 | getTimeUnit(bool isLong); |
| 204 | |
| 205 | template<> |
| 206 | inline std::string |
| 207 | getTimeUnit<time::nanoseconds>(bool isLong) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 208 | { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 209 | return isLong ? "nanoseconds" : "ns"; |
| 210 | } |
| 211 | |
| 212 | template<> |
| 213 | inline std::string |
| 214 | getTimeUnit<time::microseconds>(bool isLong) |
| 215 | { |
| 216 | return isLong ? "microseconds" : "us"; |
| 217 | } |
| 218 | |
| 219 | template<> |
| 220 | inline std::string |
| 221 | getTimeUnit<time::milliseconds>(bool isLong) |
| 222 | { |
| 223 | return isLong ? "milliseconds" : "ms"; |
| 224 | } |
| 225 | |
| 226 | template<> |
| 227 | inline std::string |
| 228 | getTimeUnit<time::seconds>(bool isLong) |
| 229 | { |
| 230 | return isLong ? "seconds" : "s"; |
| 231 | } |
| 232 | |
| 233 | template<> |
| 234 | inline std::string |
| 235 | getTimeUnit<time::minutes>(bool isLong) |
| 236 | { |
| 237 | return isLong ? "minutes" : "m"; |
| 238 | } |
| 239 | |
| 240 | template<> |
| 241 | inline std::string |
| 242 | getTimeUnit<time::hours>(bool isLong) |
| 243 | { |
| 244 | return isLong ? "hours" : "h"; |
| 245 | } |
| 246 | |
| 247 | template<> |
| 248 | inline std::string |
| 249 | getTimeUnit<time::days>(bool isLong) |
| 250 | { |
| 251 | return isLong ? "days" : "d"; |
| 252 | } |
| 253 | |
| 254 | } // namespace detail |
| 255 | |
| 256 | template<typename OutputPrecision> |
| 257 | std::string |
| 258 | formatDuration(time::nanoseconds d, bool isLong = false) |
| 259 | { |
| 260 | return to_string(time::duration_cast<OutputPrecision>(d).count()) + |
| 261 | (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | std::string |
| 265 | formatTimestamp(time::system_clock::TimePoint t); |
| 266 | |
| 267 | } // namespace text |
| 268 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 269 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 270 | } // namespace tools |
| 271 | } // namespace nfd |
| 272 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 273 | #endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP |