Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 53 | std::string |
| 54 | formatSeconds(time::seconds d); |
| 55 | |
| 56 | template<typename DURATION> |
| 57 | std::string |
| 58 | formatDuration(DURATION d) |
| 59 | { |
| 60 | return formatSeconds(time::duration_cast<time::seconds>(d)); |
| 61 | } |
| 62 | |
| 63 | std::string |
| 64 | formatTimestamp(time::system_clock::TimePoint t); |
| 65 | |
| 66 | } // namespace xml |
| 67 | |
| 68 | namespace text { |
| 69 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 70 | /** \brief print a number of whitespaces |
| 71 | */ |
| 72 | struct Spaces |
| 73 | { |
| 74 | int nSpaces; ///< number of spaces; print nothing if negative |
| 75 | }; |
| 76 | |
| 77 | std::ostream& |
| 78 | operator<<(std::ostream& os, const Spaces& spaces); |
| 79 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 80 | /** \brief print different string on first and subsequent usage |
| 81 | * |
| 82 | * \code |
| 83 | * Separator sep(","); |
| 84 | * for (int i = 1; i <= 3; ++i) { |
| 85 | * os << sep << i; |
| 86 | * } |
| 87 | * // prints: 1,2,3 |
| 88 | * \endcode |
| 89 | */ |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 90 | class Separator : noncopyable |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 91 | { |
| 92 | public: |
| 93 | Separator(const std::string& first, const std::string& subsequent); |
| 94 | |
| 95 | explicit |
| 96 | Separator(const std::string& subsequent); |
| 97 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 98 | int |
| 99 | getCount() const |
| 100 | { |
| 101 | return m_count; |
| 102 | } |
| 103 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 104 | private: |
| 105 | std::string m_first; |
| 106 | std::string m_subsequent; |
| 107 | int m_count; |
| 108 | |
| 109 | friend std::ostream& operator<<(std::ostream& os, Separator& sep); |
| 110 | }; |
| 111 | |
| 112 | std::ostream& |
| 113 | operator<<(std::ostream& os, Separator& sep); |
| 114 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 115 | /** \brief print attributes of an item |
| 116 | * |
| 117 | * \code |
| 118 | * ItemAttributes ia(wantMultiLine, 3); |
| 119 | * os << ia("id") << 500 |
| 120 | * << ia("uri") << "udp4://192.0.2.1:6363" |
| 121 | * << ia.end(); |
| 122 | * |
| 123 | * // prints in single-line style (wantMultiLine==false): |
| 124 | * // id=500 uri=udp4://192.0.2.1:6363 [no-newline] |
| 125 | * |
| 126 | * // prints in multi-line style (wantMultiLine==true): |
| 127 | * // id=500 |
| 128 | * // uri=udp4://192.0.2.1:6363 [newline] |
| 129 | * \endcode |
| 130 | */ |
| 131 | class ItemAttributes : noncopyable |
| 132 | { |
| 133 | public: |
| 134 | /** \brief constructor |
| 135 | * \param wantMultiLine true to select multi-line style, false to use single-line style |
| 136 | * \param maxAttributeWidth maximum width of attribute names, for alignment in multi-line style |
| 137 | */ |
| 138 | explicit |
| 139 | ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0); |
| 140 | |
| 141 | struct Attribute |
| 142 | { |
| 143 | bool wantNewline; |
| 144 | Spaces spaces; |
| 145 | std::string attribute; |
| 146 | }; |
| 147 | |
| 148 | Attribute |
| 149 | operator()(const std::string& attribute); |
| 150 | |
| 151 | std::string |
| 152 | end() const; |
| 153 | |
| 154 | private: |
| 155 | bool m_wantMultiLine; |
| 156 | int m_maxAttributeWidth; |
| 157 | int m_count; |
| 158 | }; |
| 159 | |
| 160 | std::ostream& |
| 161 | operator<<(std::ostream& os, const ItemAttributes::Attribute& attr); |
| 162 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 163 | std::string |
| 164 | formatSeconds(time::seconds d, bool isLong = false); |
| 165 | |
| 166 | template<typename DURATION> |
| 167 | std::string |
| 168 | formatDuration(DURATION d, bool isLong = false) |
| 169 | { |
| 170 | return formatSeconds(time::duration_cast<time::seconds>(d), isLong); |
| 171 | } |
| 172 | |
| 173 | std::string |
| 174 | formatTimestamp(time::system_clock::TimePoint t); |
| 175 | |
| 176 | } // namespace text |
| 177 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 178 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 179 | } // namespace tools |
| 180 | } // namespace nfd |
| 181 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 182 | #endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP |